Proposal: `String.prototype.codePointCount`

kdex kdex at kdex.de
Thu Aug 8 07:23:11 UTC 2019


So what's wrong with `Array.from(str).length`?

On Thursday, August 8, 2019 4:37:07 AM CEST fanerge wrote:
> I expect to be able to add an attribute to String.prototype that returns the
> number of codePoints of the string to reflect the actual number of
> characters instead of the code unit.
 
> 
> 
> 
>  
> 
> Definition of String.prototype.length
> 
>  
> 
> This property returns the number of code units in the string. UTF-16, the
> string format used by JavaScript, uses a single 16-bit code unit to
> represent the most common characters, but needs to use two code units for
> less commonly-used characters, so it's possible for the value returned by
> length to not match the actual number of characters in the string.
 
>  
> We refer to the String class in Java
>  
> The String class in the Java JVM uses UTF-16 encoding. 
> String.length(): The method returns the number of characters in char in the
> string; 
 String.codePointCount(): The method returns the number of
> codewords in the string. 
> 
> 
> 
>  
> 
> I want the ECMA organization to be able to add a property or method to
> String.prototype that returns the value of the codePoint of the string. For
> example: String.prototype.codePointCount can return the actual number of
> codePoints instead of code unit.
 
> ```
> 
> const str1 = ‘1111’;
> 
> str1.length; // 4
> 
> str1.codePointCount; // 4
> 
> // ‘1’.codePointAt(0) // 49
> 
> 
> 
> 
> const str2 = '𠮷𠮷𠮷𠮷’;
> 
> str2.length; // 8 
> 
> str2.codePointCount; // 4 
> 
> // '𠮷'.codePointAt(0); // 134071
> 
> 
> 
> 
> const str3 = ‘😯😯😯😯’;
> 
> str3.length; // 8
> 
> str3.codePointCount; // 4
> 
>  
> 
> // '😯'.codePointAt(0); // 128559
> 
> ```
> 
>  
> 
> I believe that most developers need such a method and property to get the
> number of codePoints in a string. I sincerely hope that you can accept my
> proposal, thanks.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190808/ff87f314/attachment.sig>


More information about the es-discuss mailing list