Skip to content

Commit 65a4991

Browse files
committed
refactor(cz-commitlint): remove duplication in case conversion
The @commitlint/cz-commitlint was previously duplicating the case conversion logic also found in the @commitlint/ensure package.
1 parent 0bb3487 commit 65a4991

File tree

4 files changed

+37
-67
lines changed

4 files changed

+37
-67
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
import _ from 'lodash';
2-
import camelCase from 'lodash/camelCase';
3-
import kebabCase from 'lodash/kebabCase';
4-
import snakeCase from 'lodash/snakeCase';
5-
import startCase from 'lodash/startCase';
6-
import upperFirst from 'lodash/upperFirst';
71
import {ruleIsActive, ruleIsNotApplicable} from './rules';
82
import {TargetCaseType} from '@commitlint/types';
9-
import {case as ensureCase} from '@commitlint/ensure';
3+
import {case as ensureCase, toCase} from '@commitlint/ensure';
104
import {Rule} from '../types';
115

126
export type CaseFn = (input: string | string[], delimiter?: string) => string;
@@ -47,30 +41,3 @@ export default function getCaseFn(rule?: Rule): CaseFn {
4741
.join(delimiter);
4842
};
4943
}
50-
51-
function toCase(input: string, target: TargetCaseType): string {
52-
switch (target) {
53-
case 'camel-case':
54-
return camelCase(input);
55-
case 'kebab-case':
56-
return kebabCase(input);
57-
case 'snake-case':
58-
return snakeCase(input);
59-
case 'pascal-case':
60-
return upperFirst(camelCase(input));
61-
case 'start-case':
62-
return startCase(input);
63-
case 'upper-case':
64-
case 'uppercase':
65-
return input.toUpperCase();
66-
case 'sentence-case':
67-
case 'sentencecase':
68-
return input.charAt(0).toUpperCase() + input.slice(1);
69-
case 'lower-case':
70-
case 'lowercase':
71-
case 'lowerCase': // Backwards compat config-angular v4
72-
return input.toLowerCase();
73-
default:
74-
throw new TypeError(`Unknown target case "${target}"`);
75-
}
76-
}

@commitlint/ensure/src/case.ts

+1-32
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import camelCase from 'lodash/camelCase';
2-
import kebabCase from 'lodash/kebabCase';
3-
import snakeCase from 'lodash/snakeCase';
4-
import upperFirst from 'lodash/upperFirst';
5-
import startCase from 'lodash/startCase';
1+
import toCase from './to-case';
62
import {TargetCaseType} from '@commitlint/types';
73

84
export default ensureCase;
@@ -25,30 +21,3 @@ function ensureCase(
2521

2622
return transformed === input;
2723
}
28-
29-
function toCase(input: string, target: TargetCaseType): string {
30-
switch (target) {
31-
case 'camel-case':
32-
return camelCase(input);
33-
case 'kebab-case':
34-
return kebabCase(input);
35-
case 'snake-case':
36-
return snakeCase(input);
37-
case 'pascal-case':
38-
return upperFirst(camelCase(input));
39-
case 'start-case':
40-
return startCase(input);
41-
case 'upper-case':
42-
case 'uppercase':
43-
return input.toUpperCase();
44-
case 'sentence-case':
45-
case 'sentencecase':
46-
return input.charAt(0).toUpperCase() + input.slice(1);
47-
case 'lower-case':
48-
case 'lowercase':
49-
case 'lowerCase': // Backwards compat config-angular v4
50-
return input.toLowerCase();
51-
default:
52-
throw new TypeError(`ensure-case: Unknown target case "${target}"`);
53-
}
54-
}

@commitlint/ensure/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import maxLength from './max-length';
44
import maxLineLength from './max-line-length';
55
import minLength from './min-length';
66
import notEmpty from './not-empty';
7+
import toCase from './to-case';
78

89
export {ensureCase as case};
910
export {ensureEnum as enum};
10-
export {maxLength, maxLineLength, minLength, notEmpty};
11+
export {maxLength, maxLineLength, minLength, notEmpty, toCase};

@commitlint/ensure/src/to-case.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {TargetCaseType} from '@commitlint/types';
2+
import camelCase from 'lodash/camelCase';
3+
import kebabCase from 'lodash/kebabCase';
4+
import snakeCase from 'lodash/snakeCase';
5+
import upperFirst from 'lodash/upperFirst';
6+
import startCase from 'lodash/startCase';
7+
8+
export default function toCase(input: string, target: TargetCaseType): string {
9+
switch (target) {
10+
case 'camel-case':
11+
return camelCase(input);
12+
case 'kebab-case':
13+
return kebabCase(input);
14+
case 'snake-case':
15+
return snakeCase(input);
16+
case 'pascal-case':
17+
return upperFirst(camelCase(input));
18+
case 'start-case':
19+
return startCase(input);
20+
case 'upper-case':
21+
case 'uppercase':
22+
return input.toUpperCase();
23+
case 'sentence-case':
24+
case 'sentencecase':
25+
return upperFirst(input);
26+
case 'lower-case':
27+
case 'lowercase':
28+
case 'lowerCase': // Backwards compat config-angular v4
29+
return input.toLowerCase();
30+
default:
31+
throw new TypeError(`to-case: Unknown target case "${target}"`);
32+
}
33+
}

0 commit comments

Comments
 (0)