Skip to content

Commit

Permalink
feat: add a shallowEqual helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Jul 8, 2020
1 parent 0f96781 commit 321acbf
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/utils/__tests__/shallowEqual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import shallowEqual from '../shallowEqual';

test('true when both objects are empty', () => {
const result = shallowEqual({}, {});

expect(result).toBe(true);
});

test('true when both objects are referentially equal', () => {
const obj = { a: 1, b: 2 };

const result = shallowEqual(obj, obj);

expect(result).toBe(true);
});

test('true when all values are the same', () => {
const result = shallowEqual({ a: 1, b: 2 }, { a: 1, b: 2 });

expect(result).toBe(true);
});

test('true when values are referentially equal', () => {
const nestedObj = { a: 1 };

const result = shallowEqual({ key: nestedObj }, { key: nestedObj });

expect(result).toBe(true);
});

test('false when values are not shallow', () => {
const result = shallowEqual({ a: {} }, { a: {} });

expect(result).toBe(false);
});

test('false when one value has too many keys', () => {
const result = shallowEqual({ a: 1 }, { a: 1, b: 2 });

expect(result).toBe(false);
});

test('false when value is undefined but key is not present in second object', () => {
const result = shallowEqual({ a: undefined }, {});

expect(result).toBe(false);
});

test('true when all array items are equal', () => {
const result = shallowEqual([1, 2], [1, 2]);

expect(result).toBe(true);
});

test('false when array length differs', () => {
const result = shallowEqual([1], [1, 2]);

expect(result).toBe(false);
});

test('true when both values are null', () => {
const result = shallowEqual(null, null);

expect(result).toBe(true);
});

test('true when both values are undefined', () => {
const result = shallowEqual(undefined, undefined);

expect(result).toBe(true);
});

test('false when one of the values is null', () => {
const result = shallowEqual({}, null);

expect(result).toBe(false);
});

test('false when one of the values is undefined', () => {
const result = shallowEqual({}, undefined);

expect(result).toBe(false);
});

test('true when values are not objects and are equal', () => {
const result = shallowEqual(1, 1);

expect(result).toBe(true);
});

test('false when values are not objects and are not equal', () => {
const result = shallowEqual(1, 2);

expect(result).toBe(false);
});

test('false when one value is not an object', () => {
const result = shallowEqual({}, 2);

expect(result).toBe(false);
});
22 changes: 22 additions & 0 deletions src/utils/shallowEqual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const hasOwnProperty = Object.prototype.hasOwnProperty;

const shallowEqual = (a, b) => {
if (a === b) {
return true;
}

if (typeof a !== 'object' || !a || typeof b !== 'object' || !b) {
return false;
}

const aKeys = Object.keys(a);
const bKeys = Object.keys(b);

if (aKeys.length !== bKeys.length) {
return false;
}

return aKeys.every((key) => hasOwnProperty.call(b, key) && a[key] === b[key]);
};

export default shallowEqual;

0 comments on commit 321acbf

Please sign in to comment.