Skip to content

Commit 0d33eaf

Browse files
feat: feature: isEqual. Adds the isEqual function. All tests passing … (#7)
* feat: feature: isEqual. Adds the isEqual function. All tests passing | Fix: Small bug fix in tests issue #4 * feat: feature: isEqual. Adds the isEqual function. All tests passing | Fix: Small bug fix in tests issue #4 * start of new changes * small change * figured it out * updated readme Co-authored-by: John McDonald <[email protected]>
1 parent 37febb2 commit 0d33eaf

File tree

4 files changed

+96
-19
lines changed

4 files changed

+96
-19
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ This is a tiny library of javascript functions, currently comprising:
3030
- sieveOfE (sieve of Eratosthenes)
3131
- peek
3232
- sort
33-
- isEqual (in progress)
33+
- isEqual
3434
- inPlaceShuffle
3535
- regularShuffle
36+

src/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
export { default as flatten } from "./flatten.js";
2-
export { default as deepCopy } from "./deepCopy.js";
3-
export { default as isPrime } from "./isPrime.js";
4-
export { default as peek } from "./peek.js";
5-
export { default as sieveOfE } from "./sieveOfE.js";
6-
export { default as sort } from "./sort.js";
7-
export { default as camelCase } from "./camelCase.js";
1+
export { default as flatten } from './flatten.js';
2+
export { default as deepCopy } from './deepCopy.js';
3+
export { default as isPrime } from './isPrime.js';
4+
export { default as peek } from './peek.js';
5+
export { default as sieveOfE } from './sieveOfE.js';
6+
export { default as sort } from './sort.js';
7+
export { default as camelCase } from './camelCase.js';
8+
export { default as isEqual } from './isEqual';
89
export { default as inPlaceShuffle } from "./inPlaceShuffle.js";
910
export { default as regularShuffle } from "./regularShuffle.js";
1011
export { default as randomInteger } from "./randomInteger.js";

src/isEqual.js

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,76 @@
1-
// in progress
1+
/**
2+
* compares two objects or arrays
3+
* @param {Array or Object} a - The Array or Object to compare
4+
* @param {Array or Object} b - The Array or Object to compare
5+
* @return {boolean}
6+
*/
7+
8+
// Helper returns a value's internal object [[Class]];
9+
const getClass = (obj) => {
10+
return Object.prototype.toString.call(obj);
11+
};
12+
13+
const isEqual = (a, b) => {
14+
// Assumes that both params will be of the same data structure type.
15+
// If arrays:
16+
if (Array.isArray(a)) {
17+
for (let i = 0; i <= a.length; i++) {
18+
if (a[i] !== b[i]) {
19+
return false;
20+
} else {
21+
return true;
22+
}
23+
}
24+
// If objects:
25+
} else {
26+
// If a and b reference the same value, return true:
27+
if (a === b) return true;
28+
29+
// If a and b are !both Objects, return false:
30+
if (typeof a != typeof b) return false;
31+
32+
// If type is number:
33+
// TODO
34+
35+
// Get internal [[class]]:
36+
const aClass = getClass(a);
37+
const bClass = getClass(b);
38+
// If classes are different, return false:
39+
if (aClass != bClass) return false;
40+
41+
// If String, Number, or Boolean objects:
42+
if (
43+
aClass == '[object Boolean]' ||
44+
aClass == '[object String]' ||
45+
aClass == '[object Error]'
46+
) {
47+
if (a.toString() != b.toString()) return false;
48+
}
49+
50+
// Grab the keys:
51+
const aKeys = Object.keys(a);
52+
const bKeys = Object.keys(b);
53+
54+
// if !same # of keys, return false:
55+
if (aKeys.length !== bKeys.length) {
56+
return false;
57+
}
58+
59+
// Check if they have the same keys:
60+
if (
61+
!aKeys.every((key) => {
62+
return b.hasOwnProperty(key);
63+
})
64+
) {
65+
return false;
66+
}
67+
68+
// Check key values - recursion:
69+
return aKeys.every((key) => {
70+
return isEqual(a[key], b[key]);
71+
});
72+
}
73+
return false;
74+
};
75+
76+
module.exports = isEqual;

src/tests/isEqual.test.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
const expect = require("chai").expect;
2-
const isEqual = require("../").isEqual;
1+
const expect = require('chai').expect;
2+
const isEqual = require('../').isEqual;
33

4-
describe("isEqual", () => {
5-
xit("compares two objects and returns true if they have identical keys and values", () => {
6-
const objOne = { name: { first: "John" } };
7-
const objTwo = { name: { first: "John" } };
4+
describe('isEqual', () => {
5+
it('compares two objects and returns true if they have identical keys and values', () => {
6+
const objOne = { name: { first: 'John' } };
7+
const objTwo = { name: { first: 'John' } };
88
expect(isEqual(objOne, objTwo)).to.equal(true);
99
});
10-
xit("compares two objects and returns false if they have non-identical keys and values", () => {
11-
const objOne = { name: { first: "John" } };
12-
const objTwo = { name: { first: "Pete" } };
10+
it('compares two objects and returns false if they have non-identical keys and values', () => {
11+
const objOne = { name: { first: 'John' } };
12+
const objTwo = { name: { first: 'Pete' } };
1313
expect(isEqual(objOne, objTwo)).to.equal(false);
1414
});
15-
xit("compares two arrays and returns true is they have identical values", () => {
15+
it('compares two arrays and returns true is they have identical values', () => {
1616
const arrOne = [1, [2, [3]]];
1717
const arrTwo = [1, [2, [3]]];
1818
expect(isEqual(arrOne, arrTwo)).to.equal(true);

0 commit comments

Comments
 (0)