Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expect to have a property or method to get the number of characters in the string #1630

Closed
fanerge opened this issue Jul 15, 2019 · 3 comments
Labels
feature suggestion Please see https://github.com/tc39/ecma262/blob/HEAD/CONTRIBUTING.md#creating-a-new-proposal

Comments

@fanerge
Copy link

fanerge commented Jul 15, 2019

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.
MDN-String.prototype.length

let num1 = '𠮷𠮷𠮷𠮷'.length; // 8
let num2 = '𠮷𠮷𠮷𠮷'.charCount; // 4 (Method similar to the number of statistical characters)

It is expected that there is a way to return the format of a character in a string instead of the number of UTF-16 coding units.

@ljharb
Copy link
Member

ljharb commented Jul 15, 2019

Despite your expectations, there isn’t a direct way - you can use [...str].length though as a workaround. (A string’s length is the number of code units, not the number of code points)

If you’d like to suggest a new feature, please see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md#creating-a-new-proposal

@claudepache
Copy link
Contributor

What is a “character”? Given the complexity of Unicode, that need first to define precisely what you want .

@ljharb You gave a way to count the number of code points. However, a grapheme cluster is much closer of what is informally called a “character”... although it is not clear whether the OP actually knew that either.

@fanerge
Copy link
Author

fanerge commented Jul 15, 2019

Despite your expectations, there isn’t a direct way - you can use [...str].length though as a workaround. (A string’s length is the number of code units, not the number of code points)

If you’d like to suggest a new feature, please see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md#creating-a-new-proposal

thanks!
I think String's prototype chain should provide a method or property that counts the number of characters(code points) in a string. It shouldn't be converted to an array, using the length property of the array.
I will apply for this feature as you said.

@ljharb ljharb closed this as completed Jul 15, 2019
@ljharb ljharb added the feature suggestion Please see https://github.com/tc39/ecma262/blob/HEAD/CONTRIBUTING.md#creating-a-new-proposal label Jul 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature suggestion Please see https://github.com/tc39/ecma262/blob/HEAD/CONTRIBUTING.md#creating-a-new-proposal
Projects
None yet
Development

No branches or pull requests

3 participants