Skip to content

Commit

Permalink
[Refactor] move Map/Set forEach method detection to a separate …
Browse files Browse the repository at this point in the history
…module.
  • Loading branch information
ljharb committed Dec 16, 2015
1 parent 8546880 commit 815dfbe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
6 changes: 6 additions & 0 deletions getCollectionsForEach.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function () {
var mapForEach = typeof Map === 'function' ? Map.prototype.forEach : null;
var setForEach = typeof Set === 'function' ? Set.prototype.forEach : null;

return { Map: mapForEach, Set: setForEach };
};
9 changes: 4 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ var functionsHaveNames = foo.name === 'foo';
var symbolValue = typeof Symbol === 'function' ? Symbol.prototype.valueOf : null;
var symbolIterator = require('./getSymbolIterator')();

var mapForEach = typeof Map === 'function' ? Map.prototype.forEach : null;
var setForEach = typeof Set === 'function' ? Set.prototype.forEach : null;
var collectionsForEach = require('./getCollectionsForEach')();

var getPrototypeOf = Object.getPrototypeOf;
if (!getPrototypeOf) {
Expand Down Expand Up @@ -62,12 +61,12 @@ var normalizeFnWhitespace = function normalizeFnWhitespace(fnStr) {
var tryMapSetEntries = function tryMapSetEntries(collection) {
var foundEntries = [];
try {
mapForEach.call(collection, function (key, value) {
collectionsForEach.Map.call(collection, function (key, value) {
foundEntries.push([key, value]);
});
} catch (notMap) {
try {
setForEach.call(collection, function (value) {
collectionsForEach.Set.call(collection, function (value) {
foundEntries.push([value]);
});
} catch (notSet) {
Expand Down Expand Up @@ -182,7 +181,7 @@ module.exports = function isEqual(value, other) {
} while (!valueNext.done && !otherNext.done);
return valueNext.done === otherNext.done;
}
} else if (mapForEach || setForEach) {
} else if (collectionsForEach.Map || collectionsForEach.Set) {
var valueEntries = tryMapSetEntries(value);
var otherEntries = tryMapSetEntries(other);
if (isArray(valueEntries) !== isArray(otherEntries)) {
Expand Down
6 changes: 4 additions & 2 deletions test/native.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var hasArrowFunctionSupport = arrowFunctions.length > 0;
var objectEntries = require('object.entries');
var forEach = require('foreach');

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

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

Expand Down Expand Up @@ -251,7 +253,7 @@ var genericIterator = function (obj) {
};

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

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

0 comments on commit 815dfbe

Please sign in to comment.