forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase.js
54 lines (49 loc) · 1.53 KB
/
case.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
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';
export default ensureCase;
function ensureCase(raw = '', target = 'lowercase') {
// We delete any content together with quotes because he can contains proper names (example `refactor: `Eslint` configuration`).
// We need trim string because content with quotes can be at the beginning or end of a line
const input = String(raw)
.replace(/`.*?`|".*?"|'.*?'/g, '')
.trim();
const transformed = toCase(input, target);
if (transformed === '' || transformed.match(/^\d/)) {
return true;
}
return transformed === input;
}
function toCase(input, target) {
switch (target) {
case 'camel-case':
return camelCase(input);
case 'kebab-case':
return kebabCase(input);
case 'snake-case':
return snakeCase(input);
case 'pascal-case':
return upperFirst(camelCase(input));
case 'start-case':
return startCase(input);
case 'upper-case':
case 'uppercase':
return input.toUpperCase();
case 'sentence-case':
case 'sentencecase': {
const [word] = input.split(' ');
return `${toCase(word.charAt(0), 'upper-case')}${toCase(
word.slice(1),
'lower-case'
)}${input.slice(word.length)}`;
}
case 'lower-case':
case 'lowercase':
case 'lowerCase': // Backwards compat config-angular v4
return input.toLowerCase();
default:
throw new TypeError(`ensure-case: Unknown target case "${target}"`);
}
}