-
Notifications
You must be signed in to change notification settings - Fork 104
/
utils.mjs
48 lines (40 loc) · 1.21 KB
/
utils.mjs
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
import * as fs from 'fs'
export const kebab = (s) => s.toLowerCase().replace(/[^\w.]/g, '-')
export const camelCase = (s) => {
const matches = Array.from(s.matchAll(/[a-zA-Z0-9]+/g))
return (
matches[0][0].toLowerCase() +
matches
.slice(1)
.map(([item]) => item[0].toUpperCase() + item.substr(1).toLowerCase())
.join('')
)
}
export const cleanFilename = (filename) =>
filename
.toLowerCase()
.replace(/[^\w.]/g, '_')
.replace(/^_+|_+$/g, '')
export const ensureDirExists = (dir) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
}
export function mergeDeep(target, source) {
const isObject = (obj) => obj && typeof obj === 'object'
if (!isObject(target) || !isObject(source)) {
return source
}
Object.keys(source).forEach((key) => {
const targetValue = target[key]
const sourceValue = source[key]
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
target[key] = targetValue.concat(sourceValue)
} else if (isObject(targetValue) && isObject(sourceValue)) {
target[key] = mergeDeep(Object.assign({}, targetValue), sourceValue)
} else {
target[key] = sourceValue
}
})
return target
}