forked from kiliman/tailwindui-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
50 lines (41 loc) · 1.23 KB
/
utils.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
const fs = require('fs')
module.exports.kebab = s => s.toLowerCase().replace(/[^\w.]/g, '-')
module.exports.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('')
)
}
module.exports.cleanFilename = filename =>
filename
.toLowerCase()
.replace(/[^\w.]/g, '_')
.replace(/^_+|_+$/g, '')
module.exports.ensureDirExists = dir => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true })
}
}
module.exports.mergeDeep = mergeDeep
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
}