While manipulating strings, I often need to loop though the character code instead of their characters. Current we can do Array.from(str, c => c.codePointAt(0))
or [...str].map(c => c.codePointAt(0))
, but (1) it’s verbose, (2) unintuitive and (3) I am using TypeScript, so I will need a non-null assertion c.charCodeAt(0)!
in strict mode, but without a “no non-null assertion” rule, ESLint starts complaining as using non-null assertion is considered a bad practice.
So I am here to propose the String.prototype.codePoints()
(and possibly also the String.prototype.charCodes()
) method.