feat: enhance toCodePoints to prevent potential unicode 14 errors (#615)
## Description `Array.from` may fail when handling certain characters newly added in Unicode 14. Where possible, it seems better to use `Intl.Segmenter` for more reliable processing. 
This commit is contained in:
@@ -34,6 +34,10 @@ function clamp(v: number, min: number, max: number): number {
|
|||||||
* ---------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------- */
|
||||||
|
|
||||||
function toCodePoints(str: string): Array<string> {
|
function toCodePoints(str: string): Array<string> {
|
||||||
|
if (typeof Intl !== "undefined" && "Segmenter" in Intl) {
|
||||||
|
const seg = new Intl.Segmenter();
|
||||||
|
return [...seg.segment(str)].map((seg) => seg.segment);
|
||||||
|
}
|
||||||
// [...str] or Array.from both iterate by UTF‑32 code point, handling
|
// [...str] or Array.from both iterate by UTF‑32 code point, handling
|
||||||
// surrogate pairs correctly.
|
// surrogate pairs correctly.
|
||||||
return Array.from(str);
|
return Array.from(str);
|
||||||
|
|||||||
Reference in New Issue
Block a user