AFAIK that's also what Allen said didn't want to implement in core. An expensive operation per each invocation due stateless loop over arbitrary indexes.
Although, strings are immutable in JS so I'd implement that logic creating a snapshot once and use that as if it was an Array ... something like the following: ```javascript !function(dict){ function getOrCreate(str) { if (!(str in dict)) { dict[str] = { i: 0, l: 0, v: (Array.from || function(){ // miserable callback return str.split('') })(str) // or the for/of loop }; } // times it's used dict[str].i++; return dict[str].v; } setInterval(function () { var key, value; for(key in dict) { value = dict[key]; value.l = value.i - value.l; // used only once or never used again if (value.l < 2) { // free all the RAM delete dict[key]; } } }, 5000); // 5 seconds should be enough ? // incremental works better with // slower timeout though // 500 might be good too Object.defineProperties( String.prototype, { at: { configurable: true, writable: true, value: function at(i) { return getOrCreate(this)[i]; } }, // or any meaningful name size: { configurable: true, get: function () { return getOrCreate(this).length; } } } ); }(Object.create(null)); // @example var str = 'abc'; alert([ str.size, // 3 str.at(1) // b ]); ``` Regards On Sat, Oct 19, 2013 at 10:54 AM, Domenic Denicola < dome...@domenicdenicola.com> wrote: > From: Mathias Bynens [mailto:math...@qiwi.be] > > > > This brings us back to the earlier discussion of whether something like > `String.prototype.codePoints` should be added: > http://esdiscuss.org/topic/how-to-count-the-number-of-symbols-in-a-stringIt > could be a getter or a generator… Or does `for…of` iteration handle this > use case adequately? > > It sounds like you are proposing a second name for > `String.prototype[Symbol.iterator]`, which does not sound very useful. > > A property for the string's "real length" does seem somewhat useful, as > does a method that does random-access on "real characters." Certainly more > useful than the proposed symbolAt/at. But I suppose we can pave whatever > cowpaths arise. > > My proposed cowpaths: > > ```js > Object.mixin(String.prototype, { > realCharacterAt(i) { > let index = 0; > for (var c of this) { > if (index++ === i) { > return c; > } > } > } > get realLength() { > let counter = 0; > for (var c of this) { > ++counter; > } > return counter; > } > }); > ``` > > This would allow you to e.g. find the character in the "real" middle of a > string with code like > > ```js > var middleIndex = Math.floor(theString.realLength / 2); > var middleRealCharacter = theString.realCharacterAt(middleIndex); > ``` > > _______________________________________________ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss >
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss