Skip to content

Commit

Permalink
fix(graphite): added Object key sort prior to JSON.stringify comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
drewsmith committed Jun 28, 2017
1 parent b5f08f3 commit ab4643b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ export function isTrue (v: any): boolean %checks {
export function isFalse (v: any): boolean %checks {
return v === false
}

/**
* Sorts an object by key to ensure seraialized equality
* by succinct key order. Guaranteed to at least return an
* empty object.
*/
export function keySort (obj: Object): Object {
if (isObject(obj) === false) {
return {}
}
const sorted = {}
Object.keys(obj).sort().forEach(key => {
sorted[key] = obj[key]
})
return sorted
}

/**
* Check if value is primitive
*/
Expand Down Expand Up @@ -240,7 +257,7 @@ export function looseEqual (a: mixed, b: mixed): boolean {
const isObjectB = isObject(b)
if (isObjectA && isObjectB) {
try {
return JSON.stringify(a) === JSON.stringify(b)
return JSON.stringify(keySort(a)) === JSON.stringify(keySort(b))
} catch (e) {
// possible circular reference
return a === b
Expand Down
55 changes: 55 additions & 0 deletions test/unit/modules/shared/key-sort.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { keySort, looseEqual } from 'shared/util'

describe('keySort', () => {
const chars = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+./'
const randomWord = () => {
const letters = []
for (let cnt = 0; cnt < 10; cnt++) {
letters.push(chars.charAt(Math.floor(Math.random() * chars.length)))
}
return letters.join('')
}
const unsortedObject = () => {
const unsorted = {}
chars.split('').map(c => {
unsorted[c + '' + randomWord()] = randomWord()
})
return unsorted
}

it('returns Object for empty Object argument', () => {
expect(looseEqual(keySort({}), {})).toBe(true)
})

it('returns Object for Array argument', () => {
expect(looseEqual(keySort([]), {})).toBe(true)
})

it('returns Object for String argument', () => {
expect(looseEqual(keySort('Test String'), {})).toBe(true)
})

it('returns Object for Number argument', () => {
expect(looseEqual(keySort(Number.MAX_SAFE_INTEGER), {})).toBe(true)
})

it('returns Object for Undefined argument', () => {
expect(looseEqual(keySort(undefined), {})).toBe(true)
})

it('returns Object for Null argument', () => {
expect(looseEqual(keySort(null), {})).toBe(true)
})

it('Sorts an unsorted Object, which are equal', () => {
const unsortedTestObj = unsortedObject()
const sortedObject = keySort(unsortedTestObj)
expect(looseEqual(sortedObject, sortedObject)).toBe(true)
})

it('does not unsort a sorted object', () => {
const sortedObject = keySort(unsortedObject())
const sortedObjectCompare = keySort(sortedObject)
expect(looseEqual(sortedObject, sortedObjectCompare)).toBe(true)
})
})

0 comments on commit ab4643b

Please sign in to comment.