`String.prototype.symbolAt()` (improved `String.prototype.charAt()`)

Allen Wirfs-Brock allen at wirfs-brock.com
Sat Oct 19 09:46:37 PDT 2013


On Oct 18, 2013, at 10:53 PM, Domenic Denicola wrote:

> On 19 Oct 2013, at 01:12, "Mathias Bynens" <mathias at qiwi.be> wrote:
>> `String.prototype.codePointAt` or `String.prototype.at` come in handy in case you only need to get the first code point or symbol in a string, for example.
> 
> Are they useful for anything else, though? For example, if I wanted to get the second symbol in a string, how would I do that?

We discussed the utility of 'codePointAt' in the context of Norbert's full Unicode support proposal.  At that time we concluded that it was something we needed.  I don't see any new evidence  that suggests that we need to reopen that decision at this point in the process.

The utility of a hypothetical 'at' method is presumably exactly that of 'codePointAt'. 

   str.at(p)
would just be a convenience  for expressing
   String.fromCodePoint(str.codePointAt(p))

So the real question is probably, how common is that  use case.
    
It's relatively easy using 'at'  do a for loop over the characters of a string using 'at'. Something like:

let c = '';
for (let p=0; p<str.length; p+=c.length) {
   c = str.at(p);
   ...
}

although, a for-of would be better in most cases:
   for (let c of str)

The use case that we don't support well is any sort of back wards iteration of the characters of a string. We don't current have an iterator specified to do it, nor do we have a one stop way to test whether we at looking at the trailing surrogate of a surrogate pair.

Allen




More information about the es-discuss mailing list