Skip to content

Commit

Permalink
Merge aca6575 into 299fb32
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Nov 27, 2019
2 parents 299fb32 + aca6575 commit 8ab99e5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
27 changes: 24 additions & 3 deletions spec/es6tests.js
Expand Up @@ -138,7 +138,18 @@ module.exports = [
value1: undefined,
value2: new Map,
equal: false
}
},
{
description: 'map and a pseudo map are not equal',
value1: map({}),
value2: {
constructor: Map,
size: 0,
has: () => true,
get: () => 1,
},
equal: false
},
]
},

Expand Down Expand Up @@ -228,7 +239,17 @@ module.exports = [
value1: set([undefined]),
value2: set([]),
equal: false
}
},
{
description: 'set and pseudo set are not equal',
value1: new Set,
value2: {
constructor: Set,
size: 0,
has: () => true,
},
equal: false
},
]
},

Expand Down Expand Up @@ -287,7 +308,7 @@ module.exports = [
},
{
description: 'pseudo array and equivalent typed array are not equal',
value1: {'0': 1, '1': 2, length: 2},
value1: {'0': 1, '1': 2, length: 2, constructor: Int32Array},
value2: new Int32Array([1, 2]),
equal: false
}
Expand Down
3 changes: 3 additions & 0 deletions spec/index.spec.js
Expand Up @@ -16,6 +16,9 @@ function testCases(equalFunc, suiteName, suiteTests) {
(test.skip ? it.skip : it)(test.description, function() {
assert.strictEqual(equalFunc(test.value1, test.value2), test.equal);
});
(test.skip ? it.skip : it)(test.description + ' (reverse arguments)', function() {
assert.strictEqual(equalFunc(test.value2, test.value1), test.equal);
});
});
});
});
Expand Down
26 changes: 6 additions & 20 deletions src/index.jst
Expand Up @@ -22,37 +22,23 @@ module.exports = function equal(a, b) {
}

{{? it.es6 }}
if (a instanceof Map) {
if ((a instanceof Map) && (b instanceof Map)) {
if (a.size !== b.size) return false;
for(i of a.entries())
for (i of a.entries())
if (!b.has(i[0])) return false;

for(i of a.entries())
for (i of a.entries())
if (!equal(i[1], b.get(i[0]))) return false;

return true;
}

if (a instanceof Set) {
if ((a instanceof Set) && (b instanceof Set)) {
if (a.size !== b.size) return false;
for(i of a.entries())
for (i of a.entries())
if (!b.has(i[0])) return false;

return true;
}

if (a.constructor.BYTES_PER_ELEMENT && (
a instanceof Int8Array ||
a instanceof Uint8Array ||
a instanceof Uint8ClampedArray ||
a instanceof Int16Array ||
a instanceof Uint16Array ||
a instanceof Int32Array ||
a instanceof Uint32Array ||
a instanceof Float32Array ||
a instanceof Float64Array ||
(envHasBigInt64Array && (a instanceof BigInt64Array || a instanceof BigUint64Array))
)) {
if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
length = a.length;
if (length != b.length) return false;
for (i = length; i-- !== 0;)
Expand Down

0 comments on commit 8ab99e5

Please sign in to comment.