回复: Proposal: `String.prototype.codePointCount`

fanerge fanerge at qq.com
Thu Aug 8 09:07:21 UTC 2019


Thank you for your reply,I know that there are ways to get the right results, but I still think that there should be such a method on the prototype chain of String, rather than by other means (mostly by converting to an array to find the length). I hope that members of ECMA can consider it.




Javascript itself supports such a method is definitely better than the developer alone.

 

There are many such requirements in a real development scenario, such as how many characters are allowed to be entered by the user, which is something we should consider not in Unicode for Basic Multilingual Plane. Unify the awareness of users and developers.

```

eg.

let str = '𠮷𠮷𠮷𠮷';

[...str].length; // 4

or

Array.from(str).length // 4
 
```














------------------ 原始邮件 ------------------
发件人: "Claude Pache"<claude.pache at gmail.com>;
发送时间: 2019年8月8日(星期四) 下午4:45
收件人: "fanerge"<fanerge at qq.com>;
抄送: "es-discuss"<es-discuss at mozilla.org>;
主题: Re: Proposal: `String.prototype.codePointCount`



Le 8 août 2019 à 04:37, fanerge <fanerge at qq.com> a écrit :

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.



Note however that “the number of code points” is not the same thing as “the actual number of characters” for what a human usually perceives as “character”. For example:


```js
Object.defineProperty(String.prototype, "codePointCount", {
    get() { return [...this].length }
})
"🇨🇦".codePointCount // 2
"n̈".codePointCount // 2
"é".codePointCount // 1
"é".normalize("NFD").codePointCount // 2
```







I believe that most developers need such a method and property to get the number of codePoints in a string.




For what purposes do “most developers” need the number of code points?


—Claude
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190808/ca19f832/attachment.html>


More information about the es-discuss mailing list