-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(use-data-query): use local stable value hashing function
- Loading branch information
ismay
committed
Aug 10, 2021
1 parent
524eb3e
commit 147ba28
Showing
6 changed files
with
91 additions
and
10 deletions.
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
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,40 @@ | ||
import { stableValueHash } from './stableValueHash' | ||
|
||
describe('stableValueHash', () => { | ||
it('sorts objects before hashing', () => { | ||
const one = { | ||
a: { | ||
one: 1, | ||
two: 2, | ||
three: 3, | ||
}, | ||
b: [1, 2, 3], | ||
c: 'c', | ||
} | ||
const two = { | ||
c: 'c', | ||
b: [1, 2, 3], | ||
a: { | ||
three: 3, | ||
two: 2, | ||
one: 1, | ||
}, | ||
} | ||
|
||
expect(stableValueHash(one)).toEqual(stableValueHash(two)) | ||
}) | ||
|
||
it('can handle primitives', () => { | ||
const one = undefined | ||
const two = 'string' | ||
const three = 3 | ||
const four = null | ||
const five = true | ||
|
||
expect(stableValueHash(one)).toMatchInlineSnapshot(`undefined`) | ||
expect(stableValueHash(two)).toMatchInlineSnapshot(`"\\"string\\""`) | ||
expect(stableValueHash(three)).toMatchInlineSnapshot(`"3"`) | ||
expect(stableValueHash(four)).toMatchInlineSnapshot(`"null"`) | ||
expect(stableValueHash(five)).toMatchInlineSnapshot(`"true"`) | ||
}) | ||
}) |
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,48 @@ | ||
function hasObjectPrototype(o: any): boolean { | ||
return Object.prototype.toString.call(o) === '[object Object]' | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/explicit-module-boundary-types | ||
export function isPlainObject(o: any): o is Object { | ||
if (!hasObjectPrototype(o)) { | ||
return false | ||
} | ||
|
||
// If has modified constructor | ||
const ctor = o.constructor | ||
if (typeof ctor === 'undefined') { | ||
return true | ||
} | ||
|
||
// If has modified prototype | ||
const prot = ctor.prototype | ||
if (!hasObjectPrototype(prot)) { | ||
return false | ||
} | ||
|
||
// If constructor does not have an Object-specific method | ||
if (!Object.prototype.hasOwnProperty.call(prot, 'isPrototypeOf')) { | ||
return false | ||
} | ||
|
||
// Most likely a plain Object | ||
return true | ||
} | ||
|
||
/** | ||
* Hashes the value into a stable hash. | ||
*/ | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export function stableValueHash(value: any): string { | ||
return JSON.stringify(value, (_, val) => | ||
isPlainObject(val) | ||
? Object.keys(val) | ||
.sort() | ||
.reduce((result, key) => { | ||
result[key] = val[key] | ||
return result | ||
}, {} as any) | ||
: val | ||
) | ||
} |
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
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
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