Skip to content

Commit

Permalink
[Fix] ES2016: IterableToArrayLike: add proper fallback for string…
Browse files Browse the repository at this point in the history
…s, pre-Symbols
  • Loading branch information
ljharb committed Sep 8, 2019
1 parent 069a2c8 commit a6b5b30
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
18 changes: 17 additions & 1 deletion es2016.js
Expand Up @@ -11,6 +11,8 @@ var assign = require('./helpers/assign');
var callBind = require('./helpers/callBind');

var $arrayPush = callBind($Array.prototype.push);
var $arraySlice = callBind($Array.prototype.slice);
var $arrayJoin = callBind($Array.prototype.join);

var ES2016 = assign(assign({}, ES2015), {
// https://www.ecma-international.org/ecma-262/7.0/#sec-samevaluenonnumber
Expand Down Expand Up @@ -41,7 +43,21 @@ var ES2016 = assign(assign({}, ES2015), {
};
};
} else if (this.Type(items) === 'String') {
// fallback for strings
var ES = this;
usingIterator = function () {
var i = 0;
return {
next: function () {
var nextIndex = ES.AdvanceStringIndex(items, i, true);
var value = $arrayJoin($arraySlice(items, i, nextIndex), '');
i = nextIndex;
return {
done: nextIndex > items.length,
value: value
};
}
};
};
}
if (typeof usingIterator !== 'undefined') {
var iterator = this.GetIterator(items, usingIterator);
Expand Down
3 changes: 2 additions & 1 deletion test/tests.js
Expand Up @@ -3008,7 +3008,8 @@ var es2016 = function ES2016(ES, ops, expectedMissing, skips) {
});

t.deepEqual(ES.IterableToArrayLike('abc'), ['a', 'b', 'c'], 'a string of code units spreads');
t.deepEqual(ES.IterableToArrayLike('☃'), ['☃'], 'a string of code points spreads');
t.deepEqual(ES.IterableToArrayLike('💩'), ['💩'], 'a string of code points spreads');
t.deepEqual(ES.IterableToArrayLike('a💩c'), ['a', '💩', 'c'], 'a string of code points and units spreads');

var arr = [1, 2, 3];
t.deepEqual(ES.IterableToArrayLike(arr), arr, 'an array becomes a similar array');
Expand Down

0 comments on commit a6b5b30

Please sign in to comment.