mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
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
77
78
79
80
81
82
83
'use strict'
Object.defineProperty(exports, '__esModule', {
  value: true
})
const isArray = Array.isArray
 
function isPlainObject (a) {
  if (a === null) {
    return false
  }
  return typeof a === 'object'
}
 
function mergeWith (objects, customizer) {
  const [first, ...rest] = objects
  let ret = first
  rest.forEach(a => {
    ret = mergeTo(ret, a, customizer)
  })
  return ret
}
 
function mergeTo (a, b, customizer) {
  const ret = {}
  Object.keys(a)
    .concat(Object.keys(b))
    .forEach(k => {
      const v = customizer(a[k], b[k], k)
      ret[k] = typeof v === 'undefined' ? a[k] : v
    })
  return ret
}
 
function mergeWithRule (a, b, k, matchField) {
  if (!isArray(a)) {
    return a
  }
  const bMatchItems = []
  const ret = a.map(aItem => {
    if (!matchField) {
      return aItem
    }
    // 暂不考虑重复
    const bMatchItem = b.find(bItem => aItem[matchField] === bItem[matchField])
    if (bMatchItem) {
      bMatchItems.push(bMatchItem)
      return mergeWith([aItem, bMatchItem], createCustomizer(k))
    }
    return aItem
  })
  return ret.concat(b.filter(bItem => !bMatchItems.includes(bItem)))
}
 
function customizeArray (a, b, k) {
  if (k === 'pages' || k === 'subPackages.pages') {
    return mergeWithRule(a, b, k, 'path')
  } else if (k === 'subPackages') {
    return mergeWithRule(a, b, k, 'root')
  }
  return b
}
 
function customizeObject (a, b, k) {
  return mergeWith([a, b], createCustomizer(k))
}
 
function createCustomizer (key) {
  return function customizer (a, b, k) {
    const newKey = key ? `${key}.${k}` : k
    if (isArray(a) && isArray(b)) {
      return customizeArray(a, b, newKey)
    }
    if (isPlainObject(a) && isPlainObject(b)) {
      return customizeObject(a, b, newKey)
    }
    return b
  }
}
 
function merge (pagesJsons) {
  return mergeWith(pagesJsons, createCustomizer())
}
exports.default = merge