-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(kitsu-core): ensure linkedRelationships does not overwrite meta o…
…bject references (#783) * Fix: Pick the meta object from actual resource rather from cached resource if it is different * Fix code review comments * Address code review comments * docs: fix jsdoc syntax to use Object instead of object * test(deepEqual): add additional tests to cover uncovered branches * test(linkRelationships): add additional test to cover isDeepEqual path Co-authored-by: wopian <wopian@wopian.me>
- Loading branch information
1 parent
d34e871
commit 97ba151
Showing
4 changed files
with
480 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Compare two objects equality | ||
* | ||
* @param {Object} left Object to compare against the right object | ||
* @param {Object} right Object to compare against the left object | ||
* @returns {boolean} Whether the objects are equal | ||
* @example <caption>Deep equality check</caption> | ||
* isDeepEqual({ | ||
* firstName: 'John', | ||
* lastName: 'Doe', | ||
* age: 35 | ||
* },{ | ||
* firstName: 'John', | ||
* lastName: 'Doe', | ||
* age: 35 | ||
* }) // true | ||
*/ | ||
export const isDeepEqual = (left, right) => { | ||
if (!left || !right) { | ||
return left === right | ||
} | ||
|
||
const leftKeys = Object.keys(left) | ||
const rightKeys = Object.keys(right) | ||
|
||
if (leftKeys.length !== rightKeys.length) return false | ||
|
||
for (const key of leftKeys) { | ||
const leftValue = left[key] | ||
const rightValue = right[key] | ||
|
||
const isObjects = isObject(leftValue) && isObject(rightValue) | ||
|
||
if ((isObjects && !isDeepEqual(leftValue, rightValue)) || | ||
(!isObjects && leftValue !== rightValue) | ||
) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
/** | ||
* Check for Object | ||
* | ||
* @param {Object} object Value to check if it is an object | ||
* @returns {boolean} Whether the value is an object | ||
* @private | ||
* @example <caption>Check for object</caption> | ||
* isObject({ | ||
* firstName: 'John', | ||
* lastName: 'Doe', | ||
* age: 35 | ||
* }) // true | ||
*/ | ||
const isObject = (object) => { | ||
return object != null && typeof object === 'object' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { isDeepEqual } from './' | ||
|
||
describe('kitsu-core', () => { | ||
describe('isDeepEqual', () => { | ||
const people = { | ||
one: { | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
age: 35 | ||
}, | ||
two: { | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
age: 35 | ||
}, | ||
three: { | ||
firstName: 'Akash', | ||
lastName: 'Thakur', | ||
age: 35 | ||
}, | ||
four: { | ||
firstName: 'Jane', | ||
lastName: 'Doe' | ||
}, | ||
five: { | ||
address: { | ||
street: '123 Main St' | ||
} | ||
}, | ||
six: { | ||
address: { | ||
street: '123 Main St' | ||
} | ||
}, | ||
seven: { | ||
address: { | ||
street: '456 Main St' | ||
} | ||
} | ||
} | ||
|
||
it('checks indentical objects are equal', () => { | ||
expect.assertions(1) | ||
expect(isDeepEqual(people.one, people.two)).toBe(true) | ||
}) | ||
|
||
it('checks different objects are not equal', () => { | ||
expect.assertions(1) | ||
expect(isDeepEqual(people.one, people.three)).toBe(false) | ||
}) | ||
|
||
it('checks objects have the same number of keys', () => { | ||
expect.assertions(1) | ||
expect(isDeepEqual(people.one, people.four)).toBe(false) | ||
}) | ||
|
||
it('checks nested objects are equal', () => { | ||
expect.assertions(2) | ||
expect(isDeepEqual(people.five, people.six)).toBe(true) | ||
expect(isDeepEqual(people.five, people.seven)).toBe(false) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.