Repository navigation
Expand file tree
/
Copy pathmerge.js
More file actions
76 lines (71 loc) · 1.71 KB
/
Copy pathmerge.js
File metadata and controls
76 lines (71 loc) · 1.71 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { isObject } from './base'
import { hasOwn } from './object'
import { setByPath, aIsSubPathOfB } from './path'
function doMergeData (target, source) {
Object.keys(source).forEach((srcKey) => {
if (hasOwn(target, srcKey)) {
target[srcKey] = source[srcKey]
} else {
let processed = false
const tarKeys = Object.keys(target)
for (let i = 0; i < tarKeys.length; i++) {
const tarKey = tarKeys[i]
if (aIsSubPathOfB(tarKey, srcKey)) {
delete target[tarKey]
target[srcKey] = source[srcKey]
processed = true
continue
}
const subPath = aIsSubPathOfB(srcKey, tarKey)
if (subPath) {
setByPath(target[tarKey], subPath, source[srcKey])
processed = true
break
}
}
if (!processed) {
target[srcKey] = source[srcKey]
}
}
})
return target
}
function mergeData (target, ...sources) {
if (target) {
sources.forEach((source) => {
if (source) doMergeData(target, source)
})
}
return target
}
// 用于合并i18n语言集
function mergeObj (target, ...sources) {
if (isObject(target)) {
for (const source of sources) {
if (isObject(source)) {
Object.keys(source).forEach((key) => {
if (isObject(source[key]) && isObject(target[key])) {
mergeObj(target[key], source[key])
} else {
target[key] = source[key]
}
})
}
}
}
return target
}
function mergeObjectArray (arr) {
const res = {}
for (let i = 0; i < arr.length; i++) {
if (arr[i]) {
Object.assign(res, arr[i])
}
}
return res
}
export {
mergeData,
mergeObj,
mergeObjectArray
}