Skip to content

Commit

Permalink
make collections .of method non-generic
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Dec 26, 2023
1 parent 87540ce commit 6e650fb
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 87 deletions.
14 changes: 11 additions & 3 deletions packages/core-js/internals/collection-of.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
'use strict';
var arraySlice = require('../internals/array-slice');
var anObject = require('../internals/an-object');

// https://tc39.github.io/proposal-setmap-offrom/
module.exports = function of() {
return new this(arraySlice(arguments));
module.exports = function (C, adder, ENTRY) {
return function of() {
var result = new C();
var length = arguments.length;
for (var index = 0; index < length; index++) {
var entry = arguments[index];
if (ENTRY) adder(result, anObject(entry)[0], entry[1]);
else adder(result, entry);
} return result;
};
};
5 changes: 3 additions & 2 deletions packages/core-js/modules/esnext.map.of.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';
var $ = require('../internals/export');
var of = require('../internals/collection-of');
var MapHelpers = require('../internals/map-helpers');
var createCollectionOf = require('../internals/collection-of');

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

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

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

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

0 comments on commit 6e650fb

Please sign in to comment.