Skip to content
This repository was archived by the owner on Nov 19, 2020. It is now read-only.
This repository was archived by the owner on Nov 19, 2020. It is now read-only.

Getting to work with a character-aware "length" #7

@brettz9

Description

@brettz9

While iteration such as with for...of may avoid the need for a lot of length-based for-loop iteration, I wonder whether at (and for that matter codePointAt) might be more appealing if there were a more Unicode-friendly replacement for length that it could hook into.

For example,

const icons = '🐎👱❤'; // code units respectively: \ud83d\udc0e , \ud83d\udc71 , \u2764
console.log('Character length', icons.charLength);
for (let i = 0; i < icons.charLength; i++) {
    console.log(icons.charLengthAwareAt(i));
    console.log(icons.charLengthAwareCodePointAt(i));
}
Character length 3
🐎
128014
👱
128113
❤
10084

While there are still other problematic methods as you have pointed out elsewhere (e.g., reverse), it would seem to me that this fundamental one, length, really ought to be addressed, to allow developers (e.g., at least those using eslint rules against non-Unicode aware methods) to avoid needing to care about how characters are composed at the code unit level.

It is not complicated to polyfill:

Object.defineProperty(String.prototype, 'charLength', {
  get () {
    return [...this].length;
  }
});

The fact of it being easy to polyfill though doesn't mean it wouldn't be good to promote its use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions