Skip to content

Commit 9a45a33

Browse files
committed
fix: reuse casing functionality from ensure in prompt
1 parent 6a41f92 commit 9a45a33

File tree

4 files changed

+14
-36
lines changed

4 files changed

+14
-36
lines changed

@commitlint/ensure/src/case.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import upperFirst from 'lodash/upperFirst';
55
import startCase from 'lodash/startCase';
66
import {TargetCaseType} from '@commitlint/types';
77

8-
export default ensureCase;
9-
10-
function ensureCase(
8+
export default function ensureCase(
119
raw: string = '',
1210
target: TargetCaseType = 'lowercase'
1311
): boolean {
@@ -26,7 +24,10 @@ function ensureCase(
2624
return transformed === input;
2725
}
2826

29-
function toCase(input: string, target: TargetCaseType): string {
27+
/**
28+
* @internal
29+
*/
30+
export function toCase(input: string, target: TargetCaseType): string {
3031
switch (target) {
3132
case 'camel-case':
3233
return camelCase(input);

@commitlint/prompt/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
"dependencies": {
4646
"@commitlint/load": "^11.0.0",
4747
"@commitlint/types": "^11.0.0",
48+
"@commitlint/ensure": "^11.0.0",
4849
"chalk": "^4.0.0",
49-
"lodash": "^4.17.19",
5050
"inquirer": "^6.5.2"
5151
},
5252
"gitHead": "cb565dfcca3128380b9b3dc274aedbcae34ce5ca"
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';
61
import {RuleEntry} from './types';
72
import {ruleIsActive, ruleIsNotApplicable} from './utils';
3+
import {toCase} from '@commitlint/ensure/lib/case';
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
}

@commitlint/prompt/tsconfig.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@
77
},
88
"include": ["./src"],
99
"exclude": ["./src/**/*.test.ts", "./lib/**/*"],
10-
"references": [{"path": "../types"}]
10+
"references": [
11+
{"path": "../types"},
12+
{"path": "../load"},
13+
{"path": "../ensure"}
14+
]
1115
}

0 commit comments

Comments
 (0)