Skip to content

Commit 5199ccf

Browse files
committed
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 65a4991 commit 5199ccf

File tree

3 files changed

+13
-39
lines changed

3 files changed

+13
-39
lines changed

@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)