From cc9e0619330bd159d2afefa34d84e887e3938941 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Mon, 10 Feb 2020 12:08:35 +0100 Subject: [PATCH 1/4] test: failing tests for same-value --- src/arrays.test.js | 12 ++++++++++++ src/objects.test.js | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/arrays.test.js b/src/arrays.test.js index fdcf789..830151b 100644 --- a/src/arrays.test.js +++ b/src/arrays.test.js @@ -48,6 +48,18 @@ var tests = [ arrA: [obj1, obj2, obj3], arrB: [obj1, obj2, obj3], result: true + }, + { + should: 'return true if all corresponding elements pass Object.is', + arrA: [Number.NaN], + arrB: [0/0], + result: true + }, + { + should: 'return false if all corresponding elements don\'t pass Object.is', + arrA: [-0], + arrB: [+0], + result: false } ]; diff --git a/src/objects.test.js b/src/objects.test.js index 6f54a8e..33d2c5b 100644 --- a/src/objects.test.js +++ b/src/objects.test.js @@ -52,6 +52,18 @@ var tests = [ objA: { first: undefined }, objB: { second: 'green' }, result: false + }, + { + should: 'return true when all values pass Object.is', + objA: { first: Number.NaN }, + objB: { first: 0/0 }, + result: true + }, + { + should: 'return false when all values don\'t pass Object.is', + objA: { first: 0 }, + objB: { first: -0 }, + result: false } ]; From ae21f26189a3a94a98b5f324648f7fd5185b2f72 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Mon, 10 Feb 2020 12:16:28 +0100 Subject: [PATCH 2/4] fix: shallow-equal not using same-value --- package-lock.json | 5 +++++ package.json | 5 ++++- src/arrays.js | 4 +++- src/objects.js | 4 +++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index bcaef1e..5c14fe4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -397,6 +397,11 @@ "safe-buffer": "~5.1.1" } }, + "core-js-pure": { + "version": "3.6.4", + "resolved": "https://registry.npmjs.org/core-js-pure/-/core-js-pure-3.6.4.tgz", + "integrity": "sha512-epIhRLkXdgv32xIUFaaAry2wdxZYBi6bgM7cB136dzzXXa+dFyRLTZeLUJxnd8ShrmyVXBub63n2NHo2JAt8Cw==" + }, "cp-file": { "version": "6.2.0", "resolved": "https://registry.npmjs.org/cp-file/-/cp-file-6.2.0.tgz", diff --git a/package.json b/package.json index e6d97b3..f9de3a5 100644 --- a/package.json +++ b/package.json @@ -73,5 +73,8 @@ ], "check-coverage": true }, - "license": "MIT" + "license": "MIT", + "dependencies": { + "core-js-pure": "^3.6.4" + } } diff --git a/src/arrays.js b/src/arrays.js index 72fa593..e795ecf 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -1,3 +1,5 @@ +import is from 'core-js-pure/es/object/is'; + export default function shallowEqualArrays(arrA, arrB) { if (arrA === arrB) { return true; @@ -14,7 +16,7 @@ export default function shallowEqualArrays(arrA, arrB) { } for (var i = 0; i < len; i++) { - if (arrA[i] !== arrB[i]) { + if (is(arrA[i], arrB[i]) === false) { return false; } } diff --git a/src/objects.js b/src/objects.js index 079c439..a3a1352 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,3 +1,5 @@ +import is from 'core-js-pure/es/object/is'; + export default function shallowEqualObjects(objA, objB) { if (objA === objB) { return true; @@ -18,7 +20,7 @@ export default function shallowEqualObjects(objA, objB) { for (var i = 0; i < len; i++) { var key = aKeys[i]; - if (objA[key] !== objB[key] || !Object.prototype.hasOwnProperty.call(objB, key)) { + if (is(objA[key], objB[key]) === false || !Object.prototype.hasOwnProperty.call(objB, key)) { return false; } } From 6be705c6b8db60f0f7a13395a76f0dd114619eb1 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Tue, 11 Feb 2020 08:42:49 +0100 Subject: [PATCH 3/4] chore: switch from core-js-pure to object-is --- package-lock.json | 10 +++++----- package.json | 8 ++++---- src/arrays.js | 4 ++-- src/objects.js | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5c14fe4..bd61c52 100644 --- a/package-lock.json +++ b/package-lock.json @@ -397,11 +397,6 @@ "safe-buffer": "~5.1.1" } }, - "core-js-pure": { - "version": "3.6.4", - "resolved": "https://registry.npmjs.org/core-js-pure/-/core-js-pure-3.6.4.tgz", - "integrity": "sha512-epIhRLkXdgv32xIUFaaAry2wdxZYBi6bgM7cB136dzzXXa+dFyRLTZeLUJxnd8ShrmyVXBub63n2NHo2JAt8Cw==" - }, "cp-file": { "version": "6.2.0", "resolved": "https://registry.npmjs.org/cp-file/-/cp-file-6.2.0.tgz", @@ -1535,6 +1530,11 @@ "integrity": "sha512-a7pEHdh1xKIAgTySUGgLMx/xwDZskN1Ud6egYYN3EdRW4ZMPNEDUTF+hwy2LUC+Bl+SyLXANnwz/jyh/qutKUw==", "dev": true }, + "object-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/object-is/-/object-is-1.0.2.tgz", + "integrity": "sha512-Epah+btZd5wrrfjkJZq1AOB9O6OxUQto45hzFd7lXGrpHPGE0W1k+426yrZV+k6NJOzLNNW/nVsmZdIWsAqoOQ==" + }, "object-keys": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", diff --git a/package.json b/package.json index f9de3a5..9e0a121 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,9 @@ "author": "Misha Moroshko ", "main": "dist/index.cjs.js", "module": "dist/index.esm.js", + "dependencies": { + "object-is": "^1.0.2" + }, "scripts": { "lint": "eslint src", "test": "nyc --require esm mocha 'src/*.test.js'", @@ -73,8 +76,5 @@ ], "check-coverage": true }, - "license": "MIT", - "dependencies": { - "core-js-pure": "^3.6.4" - } + "license": "MIT" } diff --git a/src/arrays.js b/src/arrays.js index e795ecf..633ee29 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -1,4 +1,4 @@ -import is from 'core-js-pure/es/object/is'; +import objectIs from 'object-is'; export default function shallowEqualArrays(arrA, arrB) { if (arrA === arrB) { @@ -16,7 +16,7 @@ export default function shallowEqualArrays(arrA, arrB) { } for (var i = 0; i < len; i++) { - if (is(arrA[i], arrB[i]) === false) { + if (objectIs(arrA[i], arrB[i]) === false) { return false; } } diff --git a/src/objects.js b/src/objects.js index a3a1352..4b5ea14 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,4 +1,4 @@ -import is from 'core-js-pure/es/object/is'; +import objectIs from 'object-is'; export default function shallowEqualObjects(objA, objB) { if (objA === objB) { @@ -20,7 +20,7 @@ export default function shallowEqualObjects(objA, objB) { for (var i = 0; i < len; i++) { var key = aKeys[i]; - if (is(objA[key], objB[key]) === false || !Object.prototype.hasOwnProperty.call(objB, key)) { + if (objectIs(objA[key], objB[key]) === false || !Object.prototype.hasOwnProperty.call(objB, key)) { return false; } } From 1d5f1b1cf3b5ff294b36b7de2aeec043c4aa9e3a Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Wed, 12 Feb 2020 06:48:23 +0100 Subject: [PATCH 4/4] inline object-is --- package-lock.json | 5 ----- package.json | 3 --- src/arrays.js | 2 +- src/objects.js | 2 +- src/util.js | 12 ++++++++++++ 5 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 src/util.js diff --git a/package-lock.json b/package-lock.json index bd61c52..bcaef1e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1530,11 +1530,6 @@ "integrity": "sha512-a7pEHdh1xKIAgTySUGgLMx/xwDZskN1Ud6egYYN3EdRW4ZMPNEDUTF+hwy2LUC+Bl+SyLXANnwz/jyh/qutKUw==", "dev": true }, - "object-is": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/object-is/-/object-is-1.0.2.tgz", - "integrity": "sha512-Epah+btZd5wrrfjkJZq1AOB9O6OxUQto45hzFd7lXGrpHPGE0W1k+426yrZV+k6NJOzLNNW/nVsmZdIWsAqoOQ==" - }, "object-keys": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.1.1.tgz", diff --git a/package.json b/package.json index 9e0a121..e6d97b3 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,6 @@ "author": "Misha Moroshko ", "main": "dist/index.cjs.js", "module": "dist/index.esm.js", - "dependencies": { - "object-is": "^1.0.2" - }, "scripts": { "lint": "eslint src", "test": "nyc --require esm mocha 'src/*.test.js'", diff --git a/src/arrays.js b/src/arrays.js index 633ee29..f0e3ee7 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -1,4 +1,4 @@ -import objectIs from 'object-is'; +import { objectIs } from './util'; export default function shallowEqualArrays(arrA, arrB) { if (arrA === arrB) { diff --git a/src/objects.js b/src/objects.js index 4b5ea14..e3e6676 100644 --- a/src/objects.js +++ b/src/objects.js @@ -1,4 +1,4 @@ -import objectIs from 'object-is'; +import { objectIs } from './util'; export default function shallowEqualObjects(objA, objB) { if (objA === objB) { diff --git a/src/util.js b/src/util.js new file mode 100644 index 0000000..40f8a02 --- /dev/null +++ b/src/util.js @@ -0,0 +1,12 @@ +export function objectIs(a, b) { + if (a === 0 && b === 0) { + return 1 / a === 1 / b; + } + if (a === b) { + return true; + } + if (a !== a && b !== b) { + return true; + } + return false; +}