make collections .of method non-generic

This commit is contained in:
Denis Pushkarev
2023-12-27 04:49:41 +07:00
parent 34a6051350
commit bafea057f0
13 changed files with 43 additions and 87 deletions
+11 -3
View File
@@ -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;
};
};
+3 -2
View File
@@ -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)
});
+3 -2
View File
@@ -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)
});
@@ -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)
});
@@ -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)
});
+3 -10
View File
@@ -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]]);
});
+3 -10
View File
@@ -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]);
});
+2 -9
View File
@@ -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);
});
+2 -9
View File
@@ -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));
});
+3 -10
View File
@@ -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]]);
});
+3 -10
View File
@@ -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]);
});
+2 -9
View File
@@ -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);
});
+2 -9
View File
@@ -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));
});