-
Notifications
You must be signed in to change notification settings - Fork 4
/
Object.js
27 lines (24 loc) · 839 Bytes
/
Object.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { isBasicObject } from 'source/common/check'
import { compareStringLocale } from 'source/common/compare'
const objectMergeDeep = (object, mergeObject) => {
Object.entries(mergeObject).forEach(([ key, mergeValue ]) => {
const objectValue = object[ key ]
if (objectValue === mergeValue) return
object[ key ] = isBasicObject(objectValue) && isBasicObject(mergeValue) // do not merge array, just replace
? objectMergeDeep(objectValue, mergeValue)
: mergeValue
})
return object
}
const objectSortKey = (object, compareKeyFunc = compareStringLocale) => {
Object.keys(object).sort(compareKeyFunc).forEach((key) => {
const value = object[ key ]
delete object[ key ] // change key order by delete & set
object[ key ] = value
})
return object
}
export {
objectMergeDeep,
objectSortKey
}