Open issuefiler opened 2 years ago
"He slices the 🦄 in half".slice(0, 15)
// "🦄" === "\uD83E\uDD84"
"He slices the \uD83E"
Note that slice
also breaks a Unicode grapheme cluster (e.g. combined family emojis). While disassembled Unicode grapheme clusters are still valid, breaking a surrogate pair, which represents a single code point, renders the string invalid in UTF-8 and UTF-16.
We can use Intl.Segmenter
to solve this now that this package targets Node.js 16.
Handle the possible surrogate pair (a pair of two UTF-16 code points) at the end properly when you truncate the filename.
Like how the package “
truncate-utf8-bytes
” does; but note that this package truncates a string to a specific number of bytes, not UTF-16 code points, unlikeString.prototype.slice
.And by the way, truncating to a specific number of bytes instead of UTF-16 code points might be more suitable for filenames.