Skip to content

Commit 7841a5d

Browse files
authored
refactor(cz-commitlint,prompt): remove duplication in case conversion (#2794)
* 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. * refactor(prompt): remove duplication in case conversion The @commitlint/prompt package was previously duplicating the case conversion logic also found in the @commitlint/ensure package.
1 parent 82665e3 commit 7841a5d

File tree

7 files changed

+50
-106
lines changed

7 files changed

+50
-106
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+
}

@commitlint/prompt/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
"commitizen": "4.2.4"
4242
},
4343
"dependencies": {
44+
"@commitlint/ensure": "^14.0.0",
4445
"@commitlint/load": "^14.0.0",
4546
"@commitlint/types": "^14.0.0",
4647
"chalk": "^4.0.0",
47-
"lodash": "^4.17.19",
4848
"throat": "^6.0.0",
4949
"vorpal": "^1.12.0"
5050
},

@commitlint/prompt/src/library/get-forced-case-fn.test.ts

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

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

39-
expect(() =>
40-
getForcedCaseFn(['name', [RuleConfigSeverity.Warning, 'always', 'foo']])
41-
).toThrow('Unknown target case "foo"');
38+
rule = getForcedCaseFn([
39+
'name',
40+
[RuleConfigSeverity.Warning, 'always', 'foo'],
41+
]);
42+
expect(() => rule('test')).toThrow('Unknown target case "foo"');
4243
});
4344

4445
test('should convert text correctly', () => {
@@ -88,13 +89,13 @@ test('should convert text correctly', () => {
8889
'name',
8990
[RuleConfigSeverity.Warning, 'always', 'sentence-case'],
9091
]);
91-
expect(rule('TEST_FOOBar-baz baz')).toBe('Test_foobar-baz baz');
92+
expect(rule('TEST_FOOBar-baz baz')).toBe('TEST_FOOBar-baz baz');
9293

9394
rule = getForcedCaseFn([
9495
'name',
9596
[RuleConfigSeverity.Warning, 'always', 'sentencecase'],
9697
]);
97-
expect(rule('TEST_FOOBar-baz baz')).toBe('Test_foobar-baz baz');
98+
expect(rule('TEST_FOOBar-baz baz')).toBe('TEST_FOOBar-baz baz');
9899

99100
rule = getForcedCaseFn([
100101
'name',
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
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 '@commitlint/ensure';
62
import {RuleEntry} from './types';
73
import {ruleIsActive, ruleIsNotApplicable} from './utils';
4+
import {TargetCaseType} from '@commitlint/types';
85

96
/**
107
* Get forced case for rule
@@ -26,29 +23,5 @@ export default function getForcedCaseFn(
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) =>
46-
`${input.charAt(0).toUpperCase()}${input.substring(1).toLowerCase()}`;
47-
case 'lower-case':
48-
case 'lowercase':
49-
case 'lowerCase': // Backwards compat config-angular v4
50-
return (input: string) => input.toLowerCase();
51-
default:
52-
throw new TypeError(`Unknown target case "${target}"`);
53-
}
26+
return (input: string) => toCase(input, target as TargetCaseType);
5427
}

0 commit comments

Comments
 (0)