Skip to content

Commit

Permalink
make collections .from method non-generic
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Dec 26, 2023
1 parent 6e650fb commit a090286
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 127 deletions.
37 changes: 14 additions & 23 deletions packages/core-js/internals/collection-from.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
'use strict';
// https://tc39.github.io/proposal-setmap-offrom/
var bind = require('../internals/function-bind-context');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var aConstructor = require('../internals/a-constructor');
var isNullOrUndefined = require('../internals/is-null-or-undefined');
var anObject = require('../internals/an-object');
var iterate = require('../internals/iterate');

var push = [].push;

module.exports = function from(source /* , mapFn, thisArg */) {
var length = arguments.length;
var mapFn = length > 1 ? arguments[1] : undefined;
var mapping, array, n, boundFunction;
aConstructor(this);
mapping = mapFn !== undefined;
if (mapping) aCallable(mapFn);
if (isNullOrUndefined(source)) return new this();
array = [];
if (mapping) {
n = 0;
boundFunction = bind(mapFn, length > 2 ? arguments[2] : undefined);
module.exports = function (C, adder, ENTRY) {
return function from(source /* , mapFn, thisArg */) {
var length = arguments.length;
var mapFn = length > 1 ? arguments[1] : undefined;
var mapping = mapFn !== undefined;
var boundFunction = mapping ? bind(mapFn, length > 2 ? arguments[2] : undefined) : undefined;
var result = new C();
var n = 0;
iterate(source, function (nextItem) {
call(push, array, boundFunction(nextItem, n++));
var entry = mapping ? boundFunction(nextItem, n++) : nextItem;
if (ENTRY) adder(result, anObject(entry)[0], entry[1]);
else adder(result, entry);
});
} else {
iterate(source, push, { that: array });
}
return new this(array);
return result;
};
};
5 changes: 3 additions & 2 deletions packages/core-js/modules/esnext.map.from.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';
var $ = require('../internals/export');
var from = require('../internals/collection-from');
var MapHelpers = require('../internals/map-helpers');
var createCollectionFrom = require('../internals/collection-from');

// `Map.from` method
// https://tc39.github.io/proposal-setmap-offrom/#sec-map.from
$({ target: 'Map', stat: true, forced: true }, {
from: from
from: createCollectionFrom(MapHelpers.Map, MapHelpers.set, true)
});
5 changes: 3 additions & 2 deletions packages/core-js/modules/esnext.set.from.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';
var $ = require('../internals/export');
var from = require('../internals/collection-from');
var SetHelpers = require('../internals/set-helpers');
var createCollectionFrom = require('../internals/collection-from');

// `Set.from` method
// https://tc39.github.io/proposal-setmap-offrom/#sec-set.from
$({ target: 'Set', stat: true, forced: true }, {
from: from
from: createCollectionFrom(SetHelpers.Set, SetHelpers.add, false)
});
5 changes: 3 additions & 2 deletions packages/core-js/modules/esnext.weak-map.from.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';
var $ = require('../internals/export');
var from = require('../internals/collection-from');
var WeakMapHelpers = require('../internals/weak-map-helpers');
var createCollectionFrom = require('../internals/collection-from');

// `WeakMap.from` method
// https://tc39.github.io/proposal-setmap-offrom/#sec-weakmap.from
$({ target: 'WeakMap', stat: true, forced: true }, {
from: from
from: createCollectionFrom(WeakMapHelpers.WeakMap, WeakMapHelpers.set, true)
});
5 changes: 3 additions & 2 deletions packages/core-js/modules/esnext.weak-set.from.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';
var $ = require('../internals/export');
var from = require('../internals/collection-from');
var WeakSetHelpers = require('../internals/weak-set-helpers');
var createCollectionFrom = require('../internals/collection-from');

// `WeakSet.from` method
// https://tc39.github.io/proposal-setmap-offrom/#sec-weakset.from
$({ target: 'WeakSet', stat: true, forced: true }, {
from: from
from: createCollectionFrom(WeakSetHelpers.WeakSet, WeakSetHelpers.add, false)
});
19 changes: 6 additions & 13 deletions tests/unit-global/esnext.map.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,17 @@ QUnit.test('Map.from', assert => {
assert.name(from, 'from');
assert.looksNative(from);
assert.nonEnumerable(Map, 'from');
assert.true(Map.from() instanceof Map);
assert.deepEqual(toArray(Map.from([])), []);
assert.deepEqual(toArray(Map.from([[1, 2]])), [[1, 2]]);
assert.deepEqual(toArray(Map.from([[1, 2], [2, 3], [1, 4]])), [[1, 4], [2, 3]]);
assert.deepEqual(toArray(Map.from(createIterable([[1, 2], [2, 3], [1, 4]]))), [[1, 4], [2, 3]]);
assert.true(from([]) instanceof Map);
assert.deepEqual(toArray(from([])), []);
assert.deepEqual(toArray(from([[1, 2]])), [[1, 2]]);
assert.deepEqual(toArray(from([[1, 2], [2, 3], [1, 4]])), [[1, 4], [2, 3]]);
assert.deepEqual(toArray(from(createIterable([[1, 2], [2, 3], [1, 4]]))), [[1, 4], [2, 3]]);
const pair = [1, 2];
const context = {};
Map.from([pair], function (element, index) {
from([pair], function (element, index) {
assert.same(element, pair);
assert.same(index, 0);
assert.same(this, context);
return element;
}, context);
assert.throws(() => from([1, 2]));
let arg = null;
function F(it) {
return arg = it;
}
from.call(F, createIterable([1, 2, 3]), it => it ** 2);
assert.deepEqual(arg, [1, 4, 9]);
});
19 changes: 6 additions & 13 deletions tests/unit-global/esnext.set.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@ QUnit.test('Set.from', assert => {
assert.name(from, 'from');
assert.looksNative(from);
assert.nonEnumerable(Set, 'from');
assert.true(Set.from() instanceof Set);
assert.deepEqual(toArray(Set.from([])), []);
assert.deepEqual(toArray(Set.from([1])), [1]);
assert.deepEqual(toArray(Set.from([1, 2, 3, 2, 1])), [1, 2, 3]);
assert.deepEqual(toArray(Set.from(createIterable([1, 2, 3, 2, 1]))), [1, 2, 3]);
assert.true(from([]) instanceof Set);
assert.deepEqual(toArray(from([])), []);
assert.deepEqual(toArray(from([1])), [1]);
assert.deepEqual(toArray(from([1, 2, 3, 2, 1])), [1, 2, 3]);
assert.deepEqual(toArray(from(createIterable([1, 2, 3, 2, 1]))), [1, 2, 3]);
const context = {};
Set.from([1], function (element, index) {
from([1], function (element, index) {
assert.same(element, 1);
assert.same(index, 0);
assert.same(this, context);
return element;
}, context);
assert.throws(() => from(1));
let arg = null;
function F(it) {
return arg = it;
}
from.call(F, createIterable([1, 2, 3]), it => it ** 2);
assert.deepEqual(arg, [1, 4, 9]);
});
15 changes: 4 additions & 11 deletions tests/unit-global/esnext.weak-map.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,16 @@ QUnit.test('WeakMap.from', assert => {
assert.name(from, 'from');
assert.looksNative(from);
assert.nonEnumerable(WeakMap, 'from');
assert.true(WeakMap.from() instanceof WeakMap);
assert.true(from([]) instanceof WeakMap);
const array = [];
assert.same(WeakMap.from([[array, 2]]).get(array), 2);
assert.same(WeakMap.from(createIterable([[array, 2]])).get(array), 2);
assert.same(from([[array, 2]]).get(array), 2);
assert.same(from(createIterable([[array, 2]])).get(array), 2);
const pair = [{}, 1];
const context = {};
WeakMap.from([pair], function (element, index) {
from([pair], function (element, index) {
assert.same(element, pair);
assert.same(index, 0);
assert.same(this, context);
return element;
}, context);
assert.throws(() => from([{}, 1]));
let arg = null;
function F(it) {
return arg = it;
}
from.call(F, createIterable([1, 2, 3]), it => it ** 2);
assert.deepEqual(arg, [1, 4, 9]);
});
15 changes: 4 additions & 11 deletions tests/unit-global/esnext.weak-set.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,16 @@ QUnit.test('WeakSet.from', assert => {
assert.name(from, 'from');
assert.looksNative(from);
assert.nonEnumerable(WeakSet, 'from');
assert.true(WeakSet.from() instanceof WeakSet);
assert.true(from([]) instanceof WeakSet);
const array = [];
assert.true(WeakSet.from([array]).has(array));
assert.true(WeakSet.from(createIterable([array])).has(array));
assert.true(from([array]).has(array));
assert.true(from(createIterable([array])).has(array));
const object = {};
const context = {};
WeakSet.from([object], function (element, index) {
from([object], function (element, index) {
assert.same(element, object);
assert.same(index, 0);
assert.same(this, context);
return element;
}, context);
assert.throws(() => from({}));
let arg = null;
function F(it) {
return arg = it;
}
from.call(F, createIterable([1, 2, 3]), it => it ** 2);
assert.deepEqual(arg, [1, 4, 9]);
});
19 changes: 6 additions & 13 deletions tests/unit-pure/esnext.map.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,17 @@ QUnit.test('Map.from', assert => {
const { from } = Map;
assert.isFunction(from);
assert.arity(from, 1);
assert.true(Map.from() instanceof Map);
assert.deepEqual(toArray(Map.from([])), []);
assert.deepEqual(toArray(Map.from([[1, 2]])), [[1, 2]]);
assert.deepEqual(toArray(Map.from([[1, 2], [2, 3], [1, 4]])), [[1, 4], [2, 3]]);
assert.deepEqual(toArray(Map.from(createIterable([[1, 2], [2, 3], [1, 4]]))), [[1, 4], [2, 3]]);
assert.true(from([]) instanceof Map);
assert.deepEqual(toArray(from([])), []);
assert.deepEqual(toArray(from([[1, 2]])), [[1, 2]]);
assert.deepEqual(toArray(from([[1, 2], [2, 3], [1, 4]])), [[1, 4], [2, 3]]);
assert.deepEqual(toArray(from(createIterable([[1, 2], [2, 3], [1, 4]]))), [[1, 4], [2, 3]]);
const pair = [1, 2];
const context = {};
Map.from([pair], function (element, index) {
from([pair], function (element, index) {
assert.same(element, pair);
assert.same(index, 0);
assert.same(this, context);
return element;
}, context);
assert.throws(() => from([1, 2]));
let arg = null;
function F(it) {
return arg = it;
}
from.call(F, createIterable([1, 2, 3]), it => it ** 2);
assert.deepEqual(arg, [1, 4, 9]);
});
19 changes: 6 additions & 13 deletions tests/unit-pure/esnext.set.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,16 @@ QUnit.test('Set.from', assert => {
const { from } = Set;
assert.isFunction(from);
assert.arity(from, 1);
assert.true(Set.from() instanceof Set);
assert.deepEqual(toArray(Set.from([])), []);
assert.deepEqual(toArray(Set.from([1])), [1]);
assert.deepEqual(toArray(Set.from([1, 2, 3, 2, 1])), [1, 2, 3]);
assert.deepEqual(toArray(Set.from(createIterable([1, 2, 3, 2, 1]))), [1, 2, 3]);
assert.true(from([]) instanceof Set);
assert.deepEqual(toArray(from([])), []);
assert.deepEqual(toArray(from([1])), [1]);
assert.deepEqual(toArray(from([1, 2, 3, 2, 1])), [1, 2, 3]);
assert.deepEqual(toArray(from(createIterable([1, 2, 3, 2, 1]))), [1, 2, 3]);
const context = {};
Set.from([1], function (element, index) {
from([1], function (element, index) {
assert.same(element, 1);
assert.same(index, 0);
assert.same(this, context);
return element;
}, context);
assert.throws(() => from(1));
let arg = null;
function F(it) {
return arg = it;
}
from.call(F, createIterable([1, 2, 3]), it => it ** 2);
assert.deepEqual(arg, [1, 4, 9]);
});
15 changes: 4 additions & 11 deletions tests/unit-pure/esnext.weak-map.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@ QUnit.test('WeakMap.from', assert => {
const { from } = WeakMap;
assert.isFunction(from);
assert.arity(from, 1);
assert.true(WeakMap.from() instanceof WeakMap);
assert.true(from([]) instanceof WeakMap);
const array = [];
assert.same(WeakMap.from([[array, 2]]).get(array), 2);
assert.same(WeakMap.from(createIterable([[array, 2]])).get(array), 2);
assert.same(from([[array, 2]]).get(array), 2);
assert.same(from(createIterable([[array, 2]])).get(array), 2);
const pair = [{}, 1];
const context = {};
WeakMap.from([pair], function (element, index) {
from([pair], function (element, index) {
assert.same(element, pair);
assert.same(index, 0);
assert.same(this, context);
return element;
}, context);
assert.throws(() => from([{}, 1]));
let arg = null;
function F(it) {
return arg = it;
}
from.call(F, createIterable([1, 2, 3]), it => it ** 2);
assert.deepEqual(arg, [1, 4, 9]);
});
15 changes: 4 additions & 11 deletions tests/unit-pure/esnext.weak-set.from.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,16 @@ QUnit.test('WeakSet.from', assert => {
const { from } = WeakSet;
assert.isFunction(from);
assert.arity(from, 1);
assert.true(WeakSet.from() instanceof WeakSet);
assert.true(from([]) instanceof WeakSet);
const array = [];
assert.true(WeakSet.from([array]).has(array));
assert.true(WeakSet.from(createIterable([array])).has(array));
assert.true(from([array]).has(array));
assert.true(from(createIterable([array])).has(array));
const object = {};
const context = {};
WeakSet.from([object], function (element, index) {
from([object], function (element, index) {
assert.same(element, object);
assert.same(index, 0);
assert.same(this, context);
return element;
}, context);
assert.throws(() => from({}));
let arg = null;
function F(it) {
return arg = it;
}
from.call(F, createIterable([1, 2, 3]), it => it ** 2);
assert.deepEqual(arg, [1, 4, 9]);
});

0 comments on commit a090286

Please sign in to comment.