underscore icon indicating copy to clipboard operation
underscore copied to clipboard

_.size to return symbol count of strings.

Open jdalton opened this issue 10 years ago β€’ 4 comments

Continuing the discussion, at the moment:

_.size('πŸ’•')
// => 2

Since str.length is trivial I think it --may-- be handy if _.size returned the number of symbols in a string instead.

_.size('πŸ’•')
// => 1

jdalton avatar Sep 16 '15 17:09 jdalton

I'm not sure what you're looking to count with this function. I recently read about combining characters and character width, and a few examples are pretty convincing. It's impossible to count the number of symbols, summing character widths is pointless, number of code points depends on the unicode normalisation form, byte count is derived from length, other properties depend on the version of Unicode the browser is using, ...

michaelficarra avatar Sep 18 '15 05:09 michaelficarra

I'm not sure what you're looking to count with this function.

Based on my example, I imagine:

_.size(string) === _.toArray(string).length;

It's impossible to count the number of symbols, summing character widths is pointless, number of code points depends on the unicode normalisation form, byte count is derived from length, other properties depend on the version of Unicode the browser is using, ...

Unless you want to truncate, or produce some slice/substring.

jdalton avatar Sep 18 '15 05:09 jdalton

@michaelficarra on a related note following up on your link I added

added support for regional indicator symbols

[..."πŸ‡ΊπŸ‡Έ"] // => ["πŸ‡Ί", "πŸ‡Έ"]
_.toArray("πŸ‡ΊπŸ‡Έ") // => ["πŸ‡ΊπŸ‡Έ"]

added support for zero-width-joiners

and added support for variation selector characters:

and added support for unicode modifiers to lodash methods:

jdalton avatar Sep 19 '15 01:09 jdalton

:+1:

mkxml avatar Sep 23 '15 12:09 mkxml