-
Notifications
You must be signed in to change notification settings - Fork 0
/
assigns.js
54 lines (49 loc) · 1.3 KB
/
assigns.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//#!py
/**
* @desc 深层级assign
* @include isObject isPureObject isArray eachObject addArrayValues
* @param {object} target
* @param {...object} source
* @param {array<string>} [cmds] - ['push', 'add', 'ifNull', 'nonNull', 'hasKey']
*/
function assigns(target, ...args)
target = isObject(target) ? target : {}
let arrayMode, ifNull, nonNull, hasKey
if isArray(args[args.length - 1])
const cmds = args.pop()
arrayMode = ['push', 'add'].find(key=> cmds.includes(key))
ifNull = cmds.includes('ifNull')
nonNull = cmds.includes('nonNull')
hasKey = cmds.includes('hasKey')
args.forEach((obj)=> {
if isObject(obj)
assign(target, obj)
})
return target
function assign(target, obj)
eachObject(obj, (value, key)=> {
if hasKey
if !target.hasOwnProperty(key)
return
if isArray(value)
if arrayMode && isArray(target[key])
if arrayMode === 'push'
target[key].push(...value)
return
else if arrayMode === 'add'
addArrayValues(target[key], value)
return
target[key] = value.slice(0)
else if isPureObject(value)
if target[key] === undefined
target[key] = {}
assign(target[key], value)
else
if ifNull
if target[key] != null
return
if nonNull
if value == null
return
target[key] = value
})