-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
203 lines (183 loc) · 4 KB
/
util.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* eslint no-restricted-syntax:1, no-prototype-builtins:1 */
/**
* Created by scott on 16-3-30.
*/
'use strict'
const _ = require('lodash')
const fs = require('fs')
const path = require('path')
const util = {
isEmpty,
checkFileExist,
assignWithValid,
mergeWithValid,
sortObjectKeys,
filterDeep,
mergeDeep,
getFileGlobPattern
}
module.exports = util
/**
* return the same obj with sorted keys
*
* @static
* @memberOf util
* @param obj
* @returns {*}
*/
function sortObjectKeys(obj) {
const sortedKeys = Object.keys(obj).sort()
return sortedKeys.reduce(function (newObj, key) {
newObj[key] = obj[key]
return newObj
}, {})
}
/**
* merge the sources into target without overriding the valid value with empty value
*
* @static
* @memberOf util
* @param {...Object} sources The source objects.
* @returns {Object}
*/
function assignWithValid(...sources) {
sources.push(assignPredict)
return _.assignWith(...sources)
}
/**
* to check if the property can be overrode
*
* @param older
* @param newer
* @returns {*}
*/
function assignPredict(older, newer) {
return isEmpty(newer) ? older : newer
}
function mergeWithValid(...sources) {
sources.push(mergeCustomizer)
return _.assignWith(...sources)
}
function mergeCustomizer(older, newer) {
if (Array.isArray(older)) {
return older.concat(newer)
} else if (_.isObject(older) && _.isObject(newer)) {
return assignWithValid(older, newer)
}
return isEmpty(newer) ? older : newer
}
/**
* check the value to be valid value
*
* @param val
* @returns {boolean}
*/
function isEmpty(val) {
return val === undefined || val === null
}
/**
* check if the file of target path exists relative to `cwd`
*
* @param {String} targetPath
* @return {Boolean}
*/
function checkFileExist(targetPath) {
return fs.existsSync(path.resolve(process.cwd(), targetPath))
}
/**
* filter some values deep into a collection according to the keyPath
*
* @example
*
* let col = { a: { b: { c: { d: 'd'} }, e: { c: { d: 'f'} } } }
* filterDeep(col, 'c.d')
* // [{ c: { d: 'd'} }, { c: { d: 'f'} }]
*
* @param collection
* @param keyPath
* @returns {Array}
*/
function filterDeep(collection, keyPath) {
const splitPath = keyPath.split('.')
const id = splitPath[0]
const candidate = findDeep(collection, id, [])
if (splitPath.length <= 1) {
return candidate
} else {
return _.filter(candidate, _.drop(splitPath).join('.'))
}
}
/**
* merge some values to an object deep into a collection according to the keyPath
*
* @example
*
* let col = { a: { b: { c: { d: 'd'} }, e: { c: { f: 'g'} } } }
* mergeDeep(col, 'c.d')
* // { d: 'd', f: 'g'}
*
* @param collection
* @param keyPath
* @returns {*}
*/
function mergeDeep(collection, keyPath) {
const splitPath = keyPath.split('.')
const id = splitPath[0]
const candidate = findDeep(collection, id, [])
if (splitPath.length <= 1) {
return candidate.reduce(function (result, val) {
Object.assign(result, val)
return result
})
} else {
const prop = _.drop(splitPath).join('.')
return candidate.reduce(function (result, val) {
let value = _.get(val, prop)
if (value) {
Object.assign(result, value)
}
return result
}, {})
}
}
/**
*
* @param collection
* @param key
* @param result
* @returns {*}
* @private
*/
function findDeep(collection, key, result) {
if (Array.isArray(collection)) {
for (let col of collection) {
findDeep(col, key, result)
}
} else if (_.isObject(collection)) {
if (collection[key]) {
result.push(collection[key])
} else {
for (let objKey in collection) {
if (collection.hasOwnProperty(objKey)) {
findDeep(collection[objKey], key, result)
}
}
}
}
return result
}
/**
* 构造 插件数组 glob 匹配规则
*
* @param {String} prefix
* @param {Array<string>} plugins
* @param {String} postfix
* @returns {string}
*/
function getFileGlobPattern(prefix, plugins, postfix) {
if (plugins.length === 1) {
return path.join(prefix, plugins[0], postfix)
} else {
return path.join(prefix, `{${plugins.join(',')}}`, postfix)
}
}