Skip to content

Commit a416168

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

File tree

6 files changed

+44
-68
lines changed

6 files changed

+44
-68
lines changed

@commitlint/cz-commitlint/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
}
3737
},
3838
"dependencies": {
39+
"@commitlint/ensure": "^13.2.0",
3940
"@commitlint/load": "^13.2.1",
4041
"@commitlint/types": "^13.2.0",
4142
"chalk": "^4.1.0",

@commitlint/cz-commitlint/src/utils/case-fn.test.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,11 @@ test('should not apply', () => {
3232
});
3333

3434
test('should throw error on invalid casing', () => {
35-
expect(() => getCaseFn([RuleConfigSeverity.Warning, 'always'])).toThrow(
36-
'Unknown target case "undefined"'
37-
);
35+
let rule = getCaseFn([RuleConfigSeverity.Warning, 'always']);
36+
expect(() => rule('test')).toThrow('Unknown target case "undefined"');
3837

39-
expect(() =>
40-
getCaseFn([RuleConfigSeverity.Warning, 'always', 'foo'])
41-
).toThrow('Unknown target case "foo"');
38+
rule = getCaseFn([RuleConfigSeverity.Warning, 'always', 'foo']);
39+
expect(() => rule('test')).toThrow('Unknown target case "foo"');
4240
});
4341

4442
test('should convert text correctly', () => {
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import camelCase from 'lodash/camelCase';
2-
import kebabCase from 'lodash/kebabCase';
3-
import snakeCase from 'lodash/snakeCase';
4-
import startCase from 'lodash/startCase';
5-
import upperFirst from 'lodash/upperFirst';
1+
import {toCase} from '@commitlint/ensure';
2+
import {TargetCaseType} from '@commitlint/types';
63
import {Rule} from '../types';
74
import {ruleIsActive, ruleIsNotApplicable} from './rules';
85

@@ -26,28 +23,5 @@ export default function getCaseFn(rule?: Rule): CaseFn {
2623
return noop;
2724
}
2825

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

@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 upperFirst(input);
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)