Skip to content

refactor(cz-commitlint,prompt): remove duplication in case conversion #2794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 1 addition & 34 deletions @commitlint/cz-commitlint/src/utils/case-fn.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import _ from 'lodash';
import camelCase from 'lodash/camelCase';
import kebabCase from 'lodash/kebabCase';
import snakeCase from 'lodash/snakeCase';
import startCase from 'lodash/startCase';
import upperFirst from 'lodash/upperFirst';
import {ruleIsActive, ruleIsNotApplicable} from './rules';
import {TargetCaseType} from '@commitlint/types';
import {case as ensureCase} from '@commitlint/ensure';
import {case as ensureCase, toCase} from '@commitlint/ensure';
import {Rule} from '../types';

export type CaseFn = (input: string | string[], delimiter?: string) => string;
Expand Down Expand Up @@ -47,30 +41,3 @@ export default function getCaseFn(rule?: Rule): CaseFn {
.join(delimiter);
};
}

function toCase(input: string, target: TargetCaseType): string {
switch (target) {
case 'camel-case':
return camelCase(input);
case 'kebab-case':
return kebabCase(input);
case 'snake-case':
return snakeCase(input);
case 'pascal-case':
return upperFirst(camelCase(input));
case 'start-case':
return startCase(input);
case 'upper-case':
case 'uppercase':
return input.toUpperCase();
case 'sentence-case':
case 'sentencecase':
return input.charAt(0).toUpperCase() + input.slice(1);
case 'lower-case':
case 'lowercase':
case 'lowerCase': // Backwards compat config-angular v4
return input.toLowerCase();
default:
throw new TypeError(`Unknown target case "${target}"`);
}
}
33 changes: 1 addition & 32 deletions @commitlint/ensure/src/case.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
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 toCase from './to-case';
import {TargetCaseType} from '@commitlint/types';

export default ensureCase;
Expand All @@ -25,30 +21,3 @@ function ensureCase(

return transformed === input;
}

function toCase(input: string, target: TargetCaseType): string {
switch (target) {
case 'camel-case':
return camelCase(input);
case 'kebab-case':
return kebabCase(input);
case 'snake-case':
return snakeCase(input);
case 'pascal-case':
return upperFirst(camelCase(input));
case 'start-case':
return startCase(input);
case 'upper-case':
case 'uppercase':
return input.toUpperCase();
case 'sentence-case':
case 'sentencecase':
return input.charAt(0).toUpperCase() + input.slice(1);
case 'lower-case':
case 'lowercase':
case 'lowerCase': // Backwards compat config-angular v4
return input.toLowerCase();
default:
throw new TypeError(`ensure-case: Unknown target case "${target}"`);
}
}
3 changes: 2 additions & 1 deletion @commitlint/ensure/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import maxLength from './max-length';
import maxLineLength from './max-line-length';
import minLength from './min-length';
import notEmpty from './not-empty';
import toCase from './to-case';

export {ensureCase as case};
export {ensureEnum as enum};
export {maxLength, maxLineLength, minLength, notEmpty};
export {maxLength, maxLineLength, minLength, notEmpty, toCase};
33 changes: 33 additions & 0 deletions @commitlint/ensure/src/to-case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {TargetCaseType} from '@commitlint/types';
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';

export default function toCase(input: string, target: TargetCaseType): string {
switch (target) {
case 'camel-case':
return camelCase(input);
case 'kebab-case':
return kebabCase(input);
case 'snake-case':
return snakeCase(input);
case 'pascal-case':
return upperFirst(camelCase(input));
case 'start-case':
return startCase(input);
case 'upper-case':
case 'uppercase':
return input.toUpperCase();
case 'sentence-case':
case 'sentencecase':
return upperFirst(input);
case 'lower-case':
case 'lowercase':
case 'lowerCase': // Backwards compat config-angular v4
return input.toLowerCase();
default:
throw new TypeError(`to-case: Unknown target case "${target}"`);
}
}
2 changes: 1 addition & 1 deletion @commitlint/prompt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
"commitizen": "4.2.4"
},
"dependencies": {
"@commitlint/ensure": "^14.0.0",
"@commitlint/load": "^14.0.0",
"@commitlint/types": "^14.0.0",
"chalk": "^4.0.0",
"lodash": "^4.17.19",
"throat": "^6.0.0",
"vorpal": "^1.12.0"
},
Expand Down
17 changes: 9 additions & 8 deletions @commitlint/prompt/src/library/get-forced-case-fn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ test('should not apply', () => {
});

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

expect(() =>
getForcedCaseFn(['name', [RuleConfigSeverity.Warning, 'always', 'foo']])
).toThrow('Unknown target case "foo"');
rule = getForcedCaseFn([
'name',
[RuleConfigSeverity.Warning, 'always', 'foo'],
]);
expect(() => rule('test')).toThrow('Unknown target case "foo"');
});

test('should convert text correctly', () => {
Expand Down Expand Up @@ -88,13 +89,13 @@ test('should convert text correctly', () => {
'name',
[RuleConfigSeverity.Warning, 'always', 'sentence-case'],
]);
expect(rule('TEST_FOOBar-baz baz')).toBe('Test_foobar-baz baz');
expect(rule('TEST_FOOBar-baz baz')).toBe('TEST_FOOBar-baz baz');

rule = getForcedCaseFn([
'name',
[RuleConfigSeverity.Warning, 'always', 'sentencecase'],
]);
expect(rule('TEST_FOOBar-baz baz')).toBe('Test_foobar-baz baz');
expect(rule('TEST_FOOBar-baz baz')).toBe('TEST_FOOBar-baz baz');

rule = getForcedCaseFn([
'name',
Expand Down
33 changes: 3 additions & 30 deletions @commitlint/prompt/src/library/get-forced-case-fn.ts
Original file line number Diff line number Diff line change
@@ -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 {toCase} from '@commitlint/ensure';
import {RuleEntry} from './types';
import {ruleIsActive, ruleIsNotApplicable} from './utils';
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);
}