-
Notifications
You must be signed in to change notification settings - Fork 2
/
caseConvert.js
107 lines (87 loc) · 2 KB
/
caseConvert.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
#!/usr/bin/env node
import chalk from 'chalk'
import Case from 'case'
import Conf from 'conf'
import logSymbols from 'log-symbols'
import clipboardy from './clipboardyAdapter.js'
import helpString from './help.js'
const config = new Conf({
projectName: 'case-cli',
configName: 'case-cli',
clearInvalidConfig: true
})
export default (type, srcs) => {
let printResult = ''
let src = srcs.join(' ')
if (!src) {
try {
src = clipboardy.readSync()
} catch (err) {
console.error(`${logSymbols.error} ${err.message}`)
process.exit(1)
}
}
const writeLog = (applyColor) => (log) => {
console.log(applyColor(`${logSymbols.success} ${log}`))
printResult = log
}
const writeWhiteLog = writeLog(chalk.whiteBright)
if (srcs[0] === 'set') {
config.set('default-case', srcs[1])
writeWhiteLog(`Default case is set by '${srcs[1]}'`)
return
}
switch (type) {
case 'u':
case 'upper':
writeWhiteLog(Case.upper(src))
break
case 'l':
case 'lower':
writeWhiteLog(Case.lower(src))
break
case 's':
case 'snake':
writeWhiteLog(Case.snake(src))
break
case 'k':
case 'kebab':
writeWhiteLog(Case.kebab(src))
break
case 'h':
case 'header':
writeWhiteLog(Case.header(src))
break
case 'c':
case 'camel':
writeWhiteLog(Case.camel(src))
break
case 'p':
case 'pascal':
writeWhiteLog(Case.pascal(src))
break
case 'r':
case 'random':
writeWhiteLog(Case.random(src))
break
case 't':
case 'title':
writeWhiteLog(Case.title(src))
break
case 'constant':
writeWhiteLog(Case.constant(src))
break
case 'sentence':
writeWhiteLog(Case.sentence(src))
break
default: {
if (config.has('default-case')) {
writeWhiteLog(Case[config.get('default-case')](src))
} else {
console.log(chalk.whiteBright(helpString))
}
break
}
}
return printResult
}