-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdef-key-builder.js
55 lines (51 loc) · 1.47 KB
/
def-key-builder.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
const camelCase = require('lodash.camelcase')
const kebabCase = require('lodash.kebabcase')
function normalizeOpts (opts) {
opts = opts || {}
opts.extention = opts.extention || '.js'
return opts
}
function stripBase (file, opts) {
const fname = file.replace(opts.baseDir, '')
return fname
}
const funcs = {
camelCase: function (file, opts) {
opts = normalizeOpts(opts)
let fname = stripBase(file, opts)
fname = fname.replace(opts.extention, '')
return camelCase(fname)
},
kebabCase: function (file, opts) {
opts = normalizeOpts(opts)
let fname = stripBase(file, opts)
fname = fname.replace(opts.extention, '').replace(/\//g, ' ')
const parts = kebabCase(fname).split('-')
const newParts = []
parts.forEach((p, i) => {
if (parts[i + 1] && !isNaN(parts[i + 1])) {
newParts.push(p + parts[i + 1])
} else if (isNaN(p)) {
newParts.push(p)
}
})
return newParts.join('-')
},
tokenize: function (file, opts) {
opts = normalizeOpts(opts)
opts.token = opts.token || ','
return funcs.kebabCase(file, opts).replace(/-/g, opts.token)
},
snakeCase: function (file, opts) {
opts = normalizeOpts(opts)
opts.token = '_'
return funcs.tokenize(file, opts)
},
titleize: function (file, opts) {
opts = normalizeOpts(opts)
const result = funcs.camelCase(file, opts)
const first = result.substr(0, 1).toUpperCase()
return first + result.substr(1)
}
}
module.exports = funcs