Created
June 19, 2021 15:49
-
-
Save miyaokamarina/a9378b6b0434ecc921c878c043ddae76 to your computer and use it in GitHub Desktop.
String length in Unicode grapheme clusters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const segmenter = new Intl.Segmenter('en-us', { | |
granularity: 'grapheme', | |
}); | |
const length = x => { | |
x = x.normalize('NFC'); | |
const graphemes = segmenter.segment(x); | |
return Array.from(graphemes).length; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment