'Get grapheme character count in javascript strings?

I'm trying to get the length of a javascript string in user-visible graphemes, ie ignoring combining characters (and surrogate pairs?). Is this possible, and if so, how would I go about it?

We're using the dojo toolkit on our project, but any general javascript solution would be great.



Solution 1:[1]

Here is a pure JavaScript library that does just that:

https://github.com/orling/grapheme-splitter

It implements the Unicode UAX-29 standard in all its edge cases that you're likely to miss in a home-brew solution, like non-Latin diacritics, Hangul (Korean) jamo characters, emoji, multiple combining marks, etc.

Solution 2:[2]

This open-source CoffeeScript implementation seems to work decently enough: https://github.com/devongovett/grapheme-breaker (if only it wasn't CS ?)

Solution 3:[3]

Split string to array

Then count

let arr = [..."???????"] // ["?", "?", "?", "?", "?", "?", "?"]
let len = arr.lenght

Credit to downGoat

Note that this solution won't work in some special cases, such as commented below were one smiley is composed by four: [..."???????"] -> ['?', '?', '?', '?', '?', '?', '?']

Though I posted it here for Google searches as for most cases it works, and it is much easier then all other alternatives.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Orlin Georgiev
Solution 2 TooTallNate
Solution 3