Skip to content

Commit

Permalink
improve equal
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioricali committed Jul 7, 2017
1 parent 5728e7c commit 2f2c9aa
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/asserts/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,36 @@ Mixed.semVer = (value) => {
*
* @function
* @name equal
* @param value {Number|String|Boolean|RegExp} first
* @param other {Number|String|Boolean|RegExp} second
* @param value {Number|String|Boolean|RegExp|Array|Object} first
* @param other {Number|String|Boolean|RegExp|Array|Object} second
* @returns {boolean}
* @example
* be.equal('hello', 'hello') // true
* be.equal('hello', 'hellow') // false
* be.equal(true, 'true') // false
* be.equal([1,2,3], [1,1,1]) // false
* be.equal({a:1}, {a:1}) // true
*/
Mixed.equal = (value, other) => {
console.log('sss',Types.all.array(value, other));
if(Types.all.number(value, other))
return value === other && 1 / value === 1 / other;
else if((Types.all.string(value, other)) || (Types.all.regexp(value, other)))
return value + '' === '' + other;
else if(Types.all.string(value, other) || Types.all.regexp(value, other))
return value.toString() === other.toString();
else if(Types.all.boolean(value, other))
return value === other;
else
return false;
else if(Types.all.object(value, other) || Types.all.array(value, other)) {
if (Object.keys(value).length !== Object.keys(other).length)
return false;
for (let prop in value) {
if (value.hasOwnProperty(prop) && other.hasOwnProperty(prop)){
if (!Mixed.equal(value[prop], other[prop]))
return false;
}else
return false;
}
return true;
} else return false;
};

Mixed.equal.multiple = false;
Expand Down
20 changes: 20 additions & 0 deletions test/mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,26 @@ describe('equal', function () {
console.log(result);
assert.equal(result, true);
});
it('object, should be return true', function () {
var result = be.equal({a:1}, {a:1});
console.log(result);
assert.equal(result, true);
});
it('object different, should be return false', function () {
var result = be.equal({a:1}, {a:2});
console.log(result);
assert.equal(result, false);
});
it('array, should be return true', function () {
var result = be.equal([1,2,3], [1,2,3]);
console.log(result);
assert.equal(result, true);
});
it('array different, should be return false', function () {
var result = be.equal([1,2,3], [1,1,1]);
console.log(result);
assert.equal(result, false);
});
});

describe('creditCard', function () {
Expand Down

0 comments on commit 2f2c9aa

Please sign in to comment.