Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use same-value over strict-equality #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"author": "Misha Moroshko <[email protected]>",
"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'",
Expand Down
4 changes: 3 additions & 1 deletion src/arrays.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import objectIs from 'object-is';

export default function shallowEqualArrays(arrA, arrB) {
if (arrA === arrB) {
return true;
Expand All @@ -14,7 +16,7 @@ export default function shallowEqualArrays(arrA, arrB) {
}

for (var i = 0; i < len; i++) {
if (arrA[i] !== arrB[i]) {
if (objectIs(arrA[i], arrB[i]) === false) {
return false;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/arrays.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
];

Expand Down
4 changes: 3 additions & 1 deletion src/objects.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import objectIs from 'object-is';

export default function shallowEqualObjects(objA, objB) {
if (objA === objB) {
return true;
Expand All @@ -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 (objectIs(objA[key], objB[key]) === false || !Object.prototype.hasOwnProperty.call(objB, key)) {
return false;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/objects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
];

Expand Down