Skip to content

Commit

Permalink
fix: reuse casing functionality from ensure in prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed Jan 12, 2021
1 parent 6a41f92 commit 9a45a33
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 36 deletions.
9 changes: 5 additions & 4 deletions @commitlint/ensure/src/case.ts
Expand Up @@ -5,9 +5,7 @@ import upperFirst from 'lodash/upperFirst';
import startCase from 'lodash/startCase';
import {TargetCaseType} from '@commitlint/types';

export default ensureCase;

function ensureCase(
export default function ensureCase(
raw: string = '',
target: TargetCaseType = 'lowercase'
): boolean {
Expand All @@ -26,7 +24,10 @@ function ensureCase(
return transformed === input;
}

function toCase(input: string, target: TargetCaseType): string {
/**
* @internal
*/
export function toCase(input: string, target: TargetCaseType): string {
switch (target) {
case 'camel-case':
return camelCase(input);
Expand Down
2 changes: 1 addition & 1 deletion @commitlint/prompt/package.json
Expand Up @@ -45,8 +45,8 @@
"dependencies": {
"@commitlint/load": "^11.0.0",
"@commitlint/types": "^11.0.0",
"@commitlint/ensure": "^11.0.0",
"chalk": "^4.0.0",
"lodash": "^4.17.19",
"inquirer": "^6.5.2"
},
"gitHead": "cb565dfcca3128380b9b3dc274aedbcae34ce5ca"
Expand Down
33 changes: 3 additions & 30 deletions @commitlint/prompt/src/library/get-forced-case-fn.ts
@@ -1,10 +1,7 @@
import camelCase from 'lodash/camelCase';
import kebabCase from 'lodash/kebabCase';
import snakeCase from 'lodash/snakeCase';
import upperFirst from 'lodash/upperFirst';
import startCase from 'lodash/startCase';
import {RuleEntry} from './types';
import {ruleIsActive, ruleIsNotApplicable} from './utils';
import {toCase} from '@commitlint/ensure/lib/case';
import {TargetCaseType} from '@commitlint/types';

/**
* Get forced case for rule
Expand All @@ -26,29 +23,5 @@ export default function getForcedCaseFn(
return noop;
}

switch (target) {
case 'camel-case':
return (input: string) => camelCase(input);
case 'kebab-case':
return (input: string) => kebabCase(input);
case 'snake-case':
return (input: string) => snakeCase(input);
case 'pascal-case':
return (input: string) => upperFirst(camelCase(input));
case 'start-case':
return (input: string) => startCase(input);
case 'upper-case':
case 'uppercase':
return (input: string) => input.toUpperCase();
case 'sentence-case':
case 'sentencecase':
return (input: string) =>
`${input.charAt(0).toUpperCase()}${input.substring(1).toLowerCase()}`;
case 'lower-case':
case 'lowercase':
case 'lowerCase': // Backwards compat config-angular v4
return (input: string) => input.toLowerCase();
default:
throw new TypeError(`Unknown target case "${target}"`);
}
return (input: string) => toCase(input, target as TargetCaseType);
}
6 changes: 5 additions & 1 deletion @commitlint/prompt/tsconfig.json
Expand Up @@ -7,5 +7,9 @@
},
"include": ["./src"],
"exclude": ["./src/**/*.test.ts", "./lib/**/*"],
"references": [{"path": "../types"}]
"references": [
{"path": "../types"},
{"path": "../load"},
{"path": "../ensure"}
]
}

0 comments on commit 9a45a33

Please sign in to comment.