forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-forced-case-fn.js
68 lines (57 loc) · 1.47 KB
/
get-forced-case-fn.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
import camelCase from 'lodash/camelCase';
import kebabCase from 'lodash/kebabCase';
import snakeCase from 'lodash/snakeCase';
import upperFirst from 'lodash/upperFirst';
import startCase from 'lodash/startCase';
/**
* Get forced case for rule
* @param {object} rule to parse
* @return {fn} transform function applying the enforced case
*/
export default function getForcedCaseFn(rule) {
const noop = input => input;
if (!rule) {
return noop;
}
const [config] = rule;
if (!Array.isArray(config)) {
return noop;
}
const [level] = config;
if (level === 0) {
return;
}
const [, when] = config;
if (when === 'never') {
return;
}
const [, , target] = config;
if (Array.isArray(target)) {
return noop;
}
switch (target) {
case 'camel-case':
return input => camelCase(input);
case 'kebab-case':
return input => kebabCase(input);
case 'snake-case':
return input => snakeCase(input);
case 'pascal-case':
return input => upperFirst(camelCase(input));
case 'start-case':
return input => startCase(input);
case 'upper-case':
case 'uppercase':
return input => input.toUpperCase();
case 'sentence-case':
case 'sentencecase':
return input =>
`${input.charAt(0).toUpperCase()}${input.substring(1).toLowerCase()}`;
case 'lower-case':
case 'lowercase':
case 'lowerCase': // Backwards compat config-angular v4
return input => input.toLowerCase() === input;
default:
throw new TypeError(`Unknown target case "${rule[2]}"`);
}
}