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

Joshua Bell jsbell at google.com
Fri Oct 18 15:51:03 PDT 2013

Given that you can only use the proposed String.prototype.at() properly for
indexes > 0 if you know the index of a non-BMP character or lead surrogate
by some other means, or if you will test the return value for a trailing
surrogate, is it really an advantage over using codePointAt / fromCodePoint?

The name "at" is so tempting I'm imagining naive scripts of the form for (i
= 0; i < s.length; ++i) { r += s.at(i); } which will work fine until they
get a non-BMP input at which point they're suddenly duplicating the
trailing surrogates.

Pushing people towards for-of iteration and even Allen's Array.from(
'𝌆𝌆𝌆'))[1] seems safer; users who need more subtle things have have
/ fromCodePoint available and hopefully the knowledge to use them.

On Fri, Oct 18, 2013 at 1:30 PM, Mathias Bynens <mathias at qiwi.be> wrote:

> Please ignore my previous email; it has been answered already. (It was a
> draft I wrote up this morning before I lost my internet connection.)
> On 18 Oct 2013, at 11:57, Allen Wirfs-Brock <allen at wirfs-brock.com> wrote:
> > Given that we have charAt, charCodeAt and codePointAt,  I think the most
> appropiate name for such a method would be 'at':
> >      '𝌆'.at(0)
> Love it!
> > The issue when this sort of method has been discussed in the past has
> been what to do when you index at a trailing surrogate possition:
> >
> >     '𝌆'.at(1)
> >
> > do you still get '𝌆' or do you get the equivalent of
> String.fromCharCode('𝌆'[1]) ?
> In my proposal it would return the equivalent of
> `String.fromCharCode('𝌆'[1])`. I think that’s the most sane behavior in
> that case. This also mimics the way `String.codePointAt` works in such a
> case.
> Here’s a prollyfill for `String.prototype.at` based on my earlier
> proposal: https://github.com/mathiasbynens/String.prototype.at Tests:
> https://github.com/mathiasbynens/String.prototype.at/blob/master/tests/tests.js
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20131018/7415e23e/attachment.html>

More information about the es-discuss mailing list