1
Fork 0

POJOs: normalize order of symbol properties

This commit is contained in:
Conduitry 2018-03-18 04:18:44 -04:00
parent e6ffaf5f32
commit e1c35691e8
3 changed files with 16 additions and 1 deletions

View file

@ -1,3 +1,5 @@
import sortSymbols from './sortSymbols.js'
let ARRAY = Symbol()
let HOLE = Symbol()
let OBJECT = Symbol()
@ -14,7 +16,10 @@ let recurse = obj => {
for (let i = 0; i < obj.length; i++) i in obj ? recurse(obj[i]) : array.push(HOLE)
break
case Object.prototype: {
let temp = [...Object.getOwnPropertyNames(obj).sort(), ...Object.getOwnPropertySymbols(obj)]
let temp = [
...Object.getOwnPropertyNames(obj).sort(),
...sortSymbols(Object.getOwnPropertySymbols(obj)),
]
array.push(OBJECT, temp.length, ...temp)
temp.forEach(key => recurse(obj[key]))
break

4
src/sortSymbols.js Normal file
View file

@ -0,0 +1,4 @@
let lookup = new Map()
let get = symbol =>
lookup.has(symbol) ? lookup.get(symbol) : (lookup.set(symbol, lookup.size), lookup.size - 1)
export default symbols => symbols.sort((a, b) => get(a) - get(b))

View file

@ -42,6 +42,12 @@ assert.equal(call({ a: 0, b: 1 }), call({ b: 1, a: 0 }))
assert.notEqual(call({ a: 0, b: 1, [s]: 2 }), call({ [s]: 3, b: 1, a: 0 }))
}
{
let s1 = Symbol()
let s2 = Symbol()
assert.equal(call({ a: 0, [s1]: 1, [s2]: 2 }), call({ [s2]: 2, [s1]: 1, a: 0 }))
}
assert.equal(call(new Date(2000, 0, 1)), call(new Date(2000, 0, 1, 0, 0, 0)))
assert.equal(call([new Date(2000, 0, 1)]), call([new Date(2000, 0, 1, 0, 0, 0)]))