Proposal: `String.prototype.codePointCount`

fanerge fanerge at qq.com
Thu Aug 8 02:37:07 UTC 2019


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 --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190808/fa291e10/attachment.html>


More information about the es-discuss mailing list