Skip to content

Commit

Permalink
Add egality test to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
philipnrmn committed Feb 10, 2016
1 parent 46d59fc commit 42d0eda
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/js/helpers/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ var Util = {
a != null &&
b != null &&
a.length === b.length &&
a.every((v, i) => v === b[i]);
a.every((v, i) => Util.isEgal(v, b[i]));
},
compareProperties: function (a, b, ...keys) {
return keys.every((key) => {
Expand All @@ -184,8 +184,25 @@ var Util = {
return this.compareArrays(Object.keys(aVal), Object.keys(bVal))
&& this.compareArrays(Object.values(aVal), Object.values(bVal));
}
return aVal === bVal;
return Util.isEgal(aVal, bVal);
});
},
isEgal: function (a, b) {
if (a === b) {
return true;
}
if (Util.isArray(a) && Util.isArray(b)) {
return Util.compareArrays(a, b);
}
if (Util.isObject(a) && Util.isObject(b)) {
var aKeys = Object.keys(a);
var bKeys = Object.keys(b);
if (aKeys.length !== bKeys.length) {
return false;
}
return aKeys.every((key) => Util.isEgal(a[key], b[key]));
}
return Number.isNaN(a) && Number.isNaN(b);
}
};

Expand Down
80 changes: 80 additions & 0 deletions src/test/units/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,86 @@ describe("Util", function () {
});
});

describe("isEgal", function () {

it("compares primitives strictly", function () {
expect(Util.isEgal(0, 0)).to.equal(true);
expect(Util.isEgal(1, 1)).to.equal(true);
expect(Util.isEgal(10, -10)).to.equal(false);
expect(Util.isEgal(0, -0)).to.equal(true);
expect(Util.isEgal("abc", "abc")).to.equal(true);
expect(Util.isEgal("abc", "cba")).to.equal(false);
expect(Util.isEgal(2.0, 2.0)).to.equal(true);
expect(Util.isEgal(19.1, 19.2)).to.equal(false);
expect(Util.isEgal(true, true)).to.equal(true);
expect(Util.isEgal(false, false)).to.equal(true);
expect(Util.isEgal(false, true)).to.equal(false);
expect(Util.isEgal(false, null)).to.equal(false);
expect(Util.isEgal("123", 123)).to.equal(false);
});
it("distinguishes null from undefined inputs", function () {
expect(Util.isEgal(null, null)).to.equal(true);
expect(Util.isEgal(undefined, undefined)).to.equal(true);
expect(Util.isEgal(undefined, null)).to.equal(false);
});
it("treats NaN as equal to NaN", function () {
expect(Util.isEgal(NaN, NaN)).to.equal(true);
});

it("compares arrays", function () {
expect(Util.isEgal(
[1, "abc", true],
[1, "abc", true]
)).to.equal(true);
expect(Util.isEgal(
[1, "abc", false],
[1, "abc", true]
)).to.equal(false);
});
it("compares objects", function () {
expect(Util.isEgal(
{a: 1, b: "abc", c: true},
{a: 1, b: "abc", c: true}
)).to.equal(true);
expect(Util.isEgal(
{a: 1, b: "abc", c: true},
{a: 1, b: "abc", c: false}
)).to.equal(false);
expect(Util.isEgal(
{a: 1, b: "abc", c: true},
{a: 1, b: "abc", c: true, d: null}
)).to.equal(false);
});

it("compares nested arrays", function () {
expect(Util.isEgal(
[[1,2,3], "b", "c"],
[[1,2,3], "b", "c"]
)).to.equal(true);
expect(Util.isEgal(
[[1,2,3], "b", "c"],
[[1,4,3], "b", "c"]
)).to.equal(false);
});

it("compares nested objects", function () {
expect(Util.isEgal({
a: [{a: 1}, {b: 2}, {c: 3}, 4],
b: {a: [1,2,3], b: [4,5,6], c: 7}
}, {
a: [{a: 1}, {b: 2}, {c: 3}, 4],
b: {a: [1,2,3], b: [4,5,6], c: 7}
})).to.equal(true);
expect(Util.isEgal({
a: [{a: 1}, {b: 2}, {c: 3}, 4],
b: {a: [1,2,3], b: [4,5,6], c: 7}
}, {
a: [{a: 1}, {b: 2}, {c: 3}, 4],
b: {a: [1,2,3], b: [4,5], c: 7}
})).to.equal(false);
});
});

describe("deepCopy", function () {

it("it returns an actual deep copy", function () {
Expand Down

1 comment on commit 42d0eda

@philipnrmn
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case you're wondering about the choice of method signature:
http://wiki.ecmascript.org/doku.php?id=harmony:egal

Also this.

Please sign in to comment.