Skip to content

Commit d4a0766

Browse files
committed
Don't rely on entries or values iterators to create arrays
We don't have them in IE
1 parent ad955fa commit d4a0766

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

internal/util/comparisons.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,19 @@
55

66
const regexFlagsSupported = /a/g.flags !== undefined;
77

8-
const arrayFrom = require('array-from');
8+
const arrayFromSet = set => {
9+
const array = [];
10+
set.forEach(value => array.push(value));
11+
12+
return array;
13+
};
14+
15+
const arrayFromMap = map => {
16+
const array = [];
17+
map.forEach((value, key) => array.push([key, value]));
18+
19+
return array;
20+
};
921

1022
const objectIs = Object.is ? Object.is : require('object-is');
1123
const objectGetOwnPropertySymbols = Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols : () => [];
@@ -347,7 +359,7 @@ function keyCheck(val1, val2, strict, memos, iterationType, aKeys) {
347359

348360
function setHasEqualElement(set, val1, strict, memo) {
349361
// Go looking.
350-
const setValues = arrayFrom(set.values());
362+
const setValues = arrayFromSet(set);
351363
for (let i = 0; i < setValues.length; i++) {
352364
const val2 = setValues[i];
353365
if (innerDeepEqual(val1, val2, strict, memo)) {
@@ -410,7 +422,7 @@ function setEquiv(a, b, strict, memo) {
410422
// This is a lazily initiated Set of entries which have to be compared
411423
// pairwise.
412424
let set = null;
413-
const aValues = arrayFrom(a.values());
425+
const aValues = arrayFromSet(a);
414426
for (let i = 0; i < aValues.length; i++) {
415427
const val = aValues[i];
416428
// Note: Checking for the objects first improves the performance for object
@@ -442,7 +454,7 @@ function setEquiv(a, b, strict, memo) {
442454
}
443455

444456
if (set !== null) {
445-
const bValues = arrayFrom(b.values());
457+
const bValues = arrayFromSet(b);
446458
for (let i = 0; i < bValues.length; i++) {
447459
const val = bValues[i];
448460
// We have to check if a primitive value is already
@@ -466,7 +478,7 @@ function mapHasEqualEntry(set, map, key1, item1, strict, memo) {
466478
// To be able to handle cases like:
467479
// Map([[{}, 'a'], [{}, 'b']]) vs Map([[{}, 'b'], [{}, 'a']])
468480
// ... we need to consider *all* matching keys, not just the first we find.
469-
const setValues = arrayFrom(set.values());
481+
const setValues = arrayFromSet(set);
470482
for (let i = 0; i < setValues.length; i++) {
471483
const key2 = setValues[i];
472484
if (innerDeepEqual(key1, key2, strict, memo) &&
@@ -482,7 +494,7 @@ function mapHasEqualEntry(set, map, key1, item1, strict, memo) {
482494
function mapEquiv(a, b, strict, memo) {
483495
let set = null;
484496

485-
const aEntries = arrayFrom(a.entries());
497+
const aEntries = arrayFromMap(a);
486498
for (let i = 0; i < aEntries.length; i++) {
487499
const [key, item1] = aEntries[i];
488500
if (typeof key === 'object' && key !== null) {
@@ -511,7 +523,7 @@ function mapEquiv(a, b, strict, memo) {
511523
}
512524

513525
if (set !== null) {
514-
const bEntries = arrayFrom(b.entries());
526+
const bEntries = arrayFromMap(b);
515527
for (let i = 0; i < bEntries.length; i++) {
516528
const [key, item] = bEntries[i];
517529
if (typeof key === 'object' && key !== null) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
"array-fill": "^1.2.0",
2828
"core-js": "^3.0.1",
2929
"cross-env": "^5.2.0",
30+
"object.entries": "^1.1.0",
3031
"object.getownpropertydescriptors": "^2.0.3",
3132
"tape": "^4.10.1"
3233
},
3334
"dependencies": {
34-
"array-from": "^2.1.1",
3535
"buffer": "^5.2.1",
3636
"es6-object-assign": "^1.1.0",
3737
"is-nan": "^1.2.1",

test/common/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ const getOwnPropertyDescriptors = Object.getOwnPropertyDescriptors
44
? Object.getOwnPropertyDescriptors
55
: require('object.getownpropertydescriptors');
66

7+
const objectEntries = Object.entries
8+
? Object.entries
9+
: require('object.entries');
10+
711
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
812
function repeat(str, count) {
913
if (str == null)
@@ -139,7 +143,7 @@ function _expectWarning(name, expected, code) {
139143
if (typeof expected === 'string') {
140144
expected = [[expected, code]];
141145
} else if (!Array.isArray(expected)) {
142-
expected = Object.entries(expected).map(([a, b]) => [b, a]);
146+
expected = objectEntries(expected).map(([a, b]) => [b, a]);
143147
} else if (!(Array.isArray(expected[0]))) {
144148
expected = [[expected[0], expected[1]]];
145149
}

test/parallel/test-assert-deep.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ const defaultMsgStart = 'Expected values to be strictly deep-equal:\n';
1111
const defaultMsgStartFull = `${defaultMsgStart}+ actual - expected`;
1212

1313
const objectEntries = require('object.entries');
14-
const arrayFrom = require('array-from');
14+
15+
const arrayFromSet = set => {
16+
const array = [];
17+
set.forEach(value => array.push(value));
18+
19+
return array;
20+
};
1521

1622
// Disable colored output to prevent color codes from breaking assertion
1723
// message comparisons. This should only be an issue when process.stdout
@@ -38,7 +44,7 @@ const createMap = (values = []) => {
3844
// for assert.throws()
3945
function re(literals, ...values) {
4046
let result = 'Expected values to be loosely deep-equal:\n\n';
41-
arrayFrom(values.entries()).forEach(([i, value]) => {
47+
values.forEach((value, i) => {
4248
const str = util.inspect(value, {
4349
compact: false,
4450
depth: 1000,
@@ -192,8 +198,8 @@ assert.throws(
192198
(function() { return arguments; })(1)
193199
]);
194200

195-
arrayFrom(similar).forEach(a => {
196-
arrayFrom(similar).forEach(b => {
201+
arrayFromSet(similar).forEach(a => {
202+
arrayFromSet(similar).forEach(b => {
197203
if (a !== b) {
198204
assert.notDeepEqual(a, b);
199205
assert.throws(

0 commit comments

Comments
 (0)