Skip to content

Commit

Permalink
[Fix] getSymbolIterator: make sure it can handle core-js also
Browse files Browse the repository at this point in the history
 - [Tests] improve the tests for older engines without Symbols
  • Loading branch information
ljharb committed Nov 27, 2019
1 parent 35e5e37 commit d9d9198
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
12 changes: 7 additions & 5 deletions getSymbolIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ module.exports = function getSymbolIterator() {
var symbolIterator = typeof Symbol === 'function' && isSymbol(Symbol.iterator) ? Symbol.iterator : null;

if (typeof Object.getOwnPropertyNames === 'function' && typeof Map === 'function' && typeof Map.prototype.entries === 'function') {
Object.getOwnPropertyNames(Map.prototype).forEach(function (name) {
if (name !== 'entries' && name !== 'size' && Map.prototype[name] === Map.prototype.entries) {
symbolIterator = name;
}
});
Object.getOwnPropertyNames(Map.prototype)
.concat(Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(Map.prototype) : [])
.forEach(function (name) {
if (name !== 'entries' && name !== 'size' && Map.prototype[name] === Map.prototype.entries) {
symbolIterator = name;
}
});
}

return symbolIterator;
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
"es6-shim": "^0.35.5",
"eslint": "^6.7.1",
"foreach": "^2.0.5",
"functions-have-names": "^1.2.0",
"has-bigints": "^1.0.0",
"has-symbols": "^1.0.1",
"make-arrow-function": "^1.1.0",
"make-generator-function": "^1.1.0",
Expand Down
19 changes: 8 additions & 11 deletions test/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

var test = require('tape');
var isEqual = require('../');
var hasSymbols = require('has-symbols');
var hasSymbols = require('has-symbols')();
var hasSymbolShams = require('has-symbols/shams')();
var hasBigInts = require('has-bigints')();
var genFn = require('make-generator-function');
var hasGeneratorSupport = typeof genFn === 'function';
var arrowFunctions = require('make-arrow-function').list();
var hasArrowFunctionSupport = arrowFunctions.length > 0;
var objectEntries = require('object.entries');
var forEach = require('foreach');
var functionsHaveNames = require('functions-have-names')();

var collectionsForEach = require('../getCollectionsForEach')();

var fooFn = function fooFn() {};
var functionsHaveNames = fooFn.name === 'fooFn';

var symbolIterator = require('../getSymbolIterator')();
var symbolIterator = (hasSymbols || hasSymbolShams) && Symbol.iterator;

var copyFunction = function (fn) {
/* eslint-disable no-new-func */
Expand Down Expand Up @@ -250,7 +248,7 @@ test('functions', function (t) {
t.end();
});

test('symbols', { skip: !hasSymbols() }, function (t) {
test('symbols', { skip: !hasSymbols }, function (t) {
var foo = 'foo';
var fooSym = Symbol(foo);
var objectFooSym = Object(fooSym);
Expand All @@ -276,7 +274,6 @@ test('symbols', { skip: !hasSymbols() }, function (t) {
t.end();
});

var hasBigInts = typeof BigInt === 'function';
test('bigints', { skip: !hasBigInts }, function (t) {
var bigInt = BigInt(42);
var objectBigInt = Object(bigInt);
Expand Down Expand Up @@ -321,7 +318,7 @@ var genericIterator = function (obj) {
};

test('iterables', function (t) {
t.test('Maps', { skip: !collectionsForEach.Map }, function (mt) {
t.test('Maps', { skip: typeof Map !== 'function' }, function (mt) {
var a = new Map();
a.set('a', 'b');
a.set('c', 'd');
Expand All @@ -341,7 +338,7 @@ test('iterables', function (t) {
mt.end();
});

t.test('Sets', { skip: !collectionsForEach.Set }, function (st) {
t.test('Sets', { skip: typeof Set !== 'function' }, function (st) {
var a = new Set();
a.add('a');
a.add('b');
Expand Down
35 changes: 12 additions & 23 deletions test/why.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@

var test = require('tape');
var isEqualWhy = require('../why');
var hasSymbols = require('has-symbols');
var hasSymbols = require('has-symbols')();
var hasSymbolShams = require('has-symbols/shams')();
var hasBigInts = require('has-bigints')();
var genFn = require('make-generator-function');
var hasGeneratorSupport = typeof genFn === 'function';
var arrowFunctions = require('make-arrow-function').list();
var hasArrowFunctionSupport = arrowFunctions.length > 0;
var objectEntries = require('object.entries');
var forEach = require('foreach');
var functionsHaveNames = require('functions-have-names')();

var collectionsForEach = require('../getCollectionsForEach')();

var fooFn = function fooFn() {};
var functionsHaveNames = fooFn.name === 'fooFn';

var symbolIterator = require('../getSymbolIterator')();
var symbolIterator = (hasSymbols || hasSymbolShams) && Symbol.iterator;

var copyFunction = function (fn) {
/* eslint-disable no-new-func */
Expand Down Expand Up @@ -391,7 +389,7 @@ test('functions', function (t) {
t.end();
});

test('symbols', { skip: !hasSymbols() }, function (t) {
test('symbols', { skip: !hasSymbols }, function (t) {
var foo = 'foo';
var fooSym = Symbol(foo);
var objectFooSym = Object(fooSym);
Expand Down Expand Up @@ -427,7 +425,6 @@ test('symbols', { skip: !hasSymbols() }, function (t) {
t.end();
});

var hasBigInts = typeof BigInt === 'function';
test('bigints', { skip: !hasBigInts }, function (t) {
var bigInt = BigInt(42);
var objectBigInt = Object(bigInt);
Expand Down Expand Up @@ -480,7 +477,7 @@ var genericIterator = function (obj) {
};

test('iterables', function (t) {
t.test('Maps', { skip: !collectionsForEach.Map }, function (mt) {
t.test('Maps', { skip: typeof Map !== 'function' }, function (mt) {
var a = new Map();
a.set('a', 'b');
a.set('c', 'd');
Expand All @@ -502,37 +499,29 @@ test('iterables', function (t) {
);
mt.equal(
isEqualWhy(a, c),
symbolIterator
? 'second argument finished iterating before first'
: 'Collection entries differ: arrays have different length: 2 !== 1',
'second argument finished iterating before first',
'unequal Maps (a, c) are not equal'
);
mt.equal(
isEqualWhy(b, c),
symbolIterator
? 'second argument finished iterating before first'
: 'Collection entries differ: arrays have different length: 2 !== 1',
'second argument finished iterating before first',
'unequal Maps (b, c) are not equal'
);
mt.equal(
isEqualWhy(c, a),
symbolIterator
? 'first argument finished iterating before second'
: 'Collection entries differ: arrays have different length: 1 !== 2',
'first argument finished iterating before second',
'unequal Maps (c, a) are not equal'
);
mt.equal(
isEqualWhy(c, b),
symbolIterator
? 'first argument finished iterating before second'
: 'Collection entries differ: arrays have different length: 1 !== 2',
'first argument finished iterating before second',
'unequal Maps (c, b) are not equal'
);

mt.end();
});

t.test('Sets', { skip: !collectionsForEach.Set }, function (st) {
t.test('Sets', { skip: typeof Set !== 'function' }, function (st) {
var a = new Set();
a.add('a');
a.add('b');
Expand Down

0 comments on commit d9d9198

Please sign in to comment.