Skip to content

Commit

Permalink
fix isEqaul bug for ""==[null] #3
Browse files Browse the repository at this point in the history
  • Loading branch information
mithriljs-cn committed Aug 7, 2017
1 parent 535a159 commit 6263c1b
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 45 deletions.
16 changes: 9 additions & 7 deletions dist/objutil.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function remove(x, y, force) {
function pick(obj, props, force) {
var o = {};
if(is(props, 'Array')){
forEach(props, key=>{
forEach(props, function(key) {
if(key in obj) o[key] = obj[key];
});
return o
Expand All @@ -229,16 +229,18 @@ function pick(obj, props, force) {

function isEqual(x, y, isStrict) {
var equal = true;
// if b===null, then don't iterate, so here compare first
if (isPrimitive(x) || isPrimitive(y)) return isStrict ? x === y : x == y
var compare = function (a, b, key) {
if ((isPrimitive(a[key]) || isPrimitive(b[key]))
&& (isStrict ? b[key] !== a[key] : b[key] != a[key])) {
var isPrimitiveA = isPrimitive(a[key]);
var isPrimitiveB = isPrimitive(b[key]);
if (isPrimitiveA || isPrimitiveB) {
if(isStrict
? b[key] !== a[key]
: isPrimitiveA!==isPrimitiveB || b[key] != a[key]) return (equal = false)
} else if(_keys(a[key]).length !== _keys(b[key]).length) {
return (equal = false)
}
};
deepIt(x, y, compare);
if (equal) deepIt(y, x, compare);
deepIt([x], [y], compare);
return equal
}

Expand Down
16 changes: 9 additions & 7 deletions dist/objutil.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function remove(x, y, force) {
function pick(obj, props, force) {
var o = {};
if(is(props, 'Array')){
forEach(props, key=>{
forEach(props, function(key) {
if(key in obj) o[key] = obj[key];
});
return o
Expand All @@ -229,16 +229,18 @@ function pick(obj, props, force) {

function isEqual(x, y, isStrict) {
var equal = true;
// if b===null, then don't iterate, so here compare first
if (isPrimitive(x) || isPrimitive(y)) return isStrict ? x === y : x == y
var compare = function (a, b, key) {
if ((isPrimitive(a[key]) || isPrimitive(b[key]))
&& (isStrict ? b[key] !== a[key] : b[key] != a[key])) {
var isPrimitiveA = isPrimitive(a[key]);
var isPrimitiveB = isPrimitive(b[key]);
if (isPrimitiveA || isPrimitiveB) {
if(isStrict
? b[key] !== a[key]
: isPrimitiveA!==isPrimitiveB || b[key] != a[key]) return (equal = false)
} else if(_keys(a[key]).length !== _keys(b[key]).length) {
return (equal = false)
}
};
deepIt(x, y, compare);
if (equal) deepIt(y, x, compare);
deepIt([x], [y], compare);
return equal
}

Expand Down
16 changes: 9 additions & 7 deletions dist/objutil.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function remove(x, y, force) {
function pick(obj, props, force) {
var o = {};
if(is(props, 'Array')){
forEach(props, key=>{
forEach(props, function(key) {
if(key in obj) o[key] = obj[key];
});
return o
Expand All @@ -227,16 +227,18 @@ function pick(obj, props, force) {

function isEqual(x, y, isStrict) {
var equal = true;
// if b===null, then don't iterate, so here compare first
if (isPrimitive(x) || isPrimitive(y)) return isStrict ? x === y : x == y
var compare = function (a, b, key) {
if ((isPrimitive(a[key]) || isPrimitive(b[key]))
&& (isStrict ? b[key] !== a[key] : b[key] != a[key])) {
var isPrimitiveA = isPrimitive(a[key]);
var isPrimitiveB = isPrimitive(b[key]);
if (isPrimitiveA || isPrimitiveB) {
if(isStrict
? b[key] !== a[key]
: isPrimitiveA!==isPrimitiveB || b[key] != a[key]) return (equal = false)
} else if(_keys(a[key]).length !== _keys(b[key]).length) {
return (equal = false)
}
};
deepIt(x, y, compare);
if (equal) deepIt(y, x, compare);
deepIt([x], [y], compare);
return equal
}

Expand Down
16 changes: 9 additions & 7 deletions dist/objutil.iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ function remove(x, y, force) {
function pick(obj, props, force) {
var o = {};
if(is(props, 'Array')){
forEach(props, key=>{
forEach(props, function(key) {
if(key in obj) o[key] = obj[key];
});
return o
Expand All @@ -230,16 +230,18 @@ function pick(obj, props, force) {

function isEqual(x, y, isStrict) {
var equal = true;
// if b===null, then don't iterate, so here compare first
if (isPrimitive(x) || isPrimitive(y)) return isStrict ? x === y : x == y
var compare = function (a, b, key) {
if ((isPrimitive(a[key]) || isPrimitive(b[key]))
&& (isStrict ? b[key] !== a[key] : b[key] != a[key])) {
var isPrimitiveA = isPrimitive(a[key]);
var isPrimitiveB = isPrimitive(b[key]);
if (isPrimitiveA || isPrimitiveB) {
if(isStrict
? b[key] !== a[key]
: isPrimitiveA!==isPrimitiveB || b[key] != a[key]) return (equal = false)
} else if(_keys(a[key]).length !== _keys(b[key]).length) {
return (equal = false)
}
};
deepIt(x, y, compare);
if (equal) deepIt(y, x, compare);
deepIt([x], [y], compare);
return equal
}

Expand Down
2 changes: 1 addition & 1 deletion dist/objutil.min.js

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

16 changes: 9 additions & 7 deletions dist/objutil.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function remove(x, y, force) {
function pick(obj, props, force) {
var o = {};
if(is(props, 'Array')){
forEach(props, key=>{
forEach(props, function(key) {
if(key in obj) o[key] = obj[key];
});
return o
Expand All @@ -233,16 +233,18 @@ function pick(obj, props, force) {

function isEqual(x, y, isStrict) {
var equal = true;
// if b===null, then don't iterate, so here compare first
if (isPrimitive(x) || isPrimitive(y)) return isStrict ? x === y : x == y
var compare = function (a, b, key) {
if ((isPrimitive(a[key]) || isPrimitive(b[key]))
&& (isStrict ? b[key] !== a[key] : b[key] != a[key])) {
var isPrimitiveA = isPrimitive(a[key]);
var isPrimitiveB = isPrimitive(b[key]);
if (isPrimitiveA || isPrimitiveB) {
if(isStrict
? b[key] !== a[key]
: isPrimitiveA!==isPrimitiveB || b[key] != a[key]) return (equal = false)
} else if(_keys(a[key]).length !== _keys(b[key]).length) {
return (equal = false)
}
};
deepIt(x, y, compare);
if (equal) deepIt(y, x, compare);
deepIt([x], [y], compare);
return equal
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "objutil",
"version": "2.15.0",
"version": "2.15.1",
"description": "Javascript Object util with deep traverse, support ES6 tree shaking methods: get/set/unset/remove object path, visit, assign(extend), merge, remove, defaults, pick, filter, forEach, map, some, every, isEqual. Customize the APIs into one file.",
"main": "dist/objutil.cjs.js",
"browser": "dist/objutil.umd.js",
Expand Down
17 changes: 9 additions & 8 deletions src/objutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function remove(x, y, force) {
function pick(obj, props, force) {
var o = {}
if(is(props, 'Array')){
forEach(props, key=>{
forEach(props, function(key) {
if(key in obj) o[key] = obj[key]
})
return o
Expand All @@ -229,19 +229,20 @@ function pick(obj, props, force) {

function isEqual(x, y, isStrict) {
var equal = true
// if b===null, then don't iterate, so here compare first
if (isPrimitive(x) || isPrimitive(y)) return isStrict ? x === y : x == y
var compare = function (a, b, key) {
if ((isPrimitive(a[key]) || isPrimitive(b[key]))
&& (isStrict ? b[key] !== a[key] : b[key] != a[key])) {
var isPrimitiveA = isPrimitive(a[key])
var isPrimitiveB = isPrimitive(b[key])
if (isPrimitiveA || isPrimitiveB) {
if(isStrict
? b[key] !== a[key]
: isPrimitiveA!==isPrimitiveB || b[key] != a[key]) return (equal = false)
} else if(_keys(a[key]).length !== _keys(b[key]).length) {
return (equal = false)
}
}
deepIt(x, y, compare)
if (equal) deepIt(y, x, compare)
deepIt([x], [y], compare)
return equal
}

// below line will generate from rollup dynamically, see 'rollup.config.js' file
// export { methods... }

11 changes: 11 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,17 @@ describe('Test a/b with lib', function () {
expect(lib.isEqual(null, { a: 1, b: { d: 3 } })).not.ok
expect(lib.isEqual({ a: 1, b: { d: 3 } }, null)).not.ok
expect(lib.isEqual(null, null)).ok
expect(lib.isEqual(null, undefined)).ok
expect(lib.isEqual([null], [undefined])).ok
expect(lib.isEqual(null, '')).not.ok

// fix #3 isEqual bug: '' == [null]
expect(lib.isEqual('', [null])).not.ok
expect(lib.isEqual(null, [null])).not.ok
expect(lib.isEqual([''], [null])).not.ok
expect(lib.isEqual([], [null])).not.ok
expect(lib.isEqual([null], [])).not.ok
expect(lib.isEqual([undefined], [null])).ok

// isStrict
expect(lib.isEqual({ a: 1, b: { d: '3' } }, { a: 1, b: { d: 3 } }, true)).not.ok
Expand Down

0 comments on commit 6263c1b

Please sign in to comment.