diff --git a/@commitlint/cz-commitlint/src/utils/case-fn.ts b/@commitlint/cz-commitlint/src/utils/case-fn.ts index f51b39c7be..1f4c50f284 100644 --- a/@commitlint/cz-commitlint/src/utils/case-fn.ts +++ b/@commitlint/cz-commitlint/src/utils/case-fn.ts @@ -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; @@ -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}"`); - } -} diff --git a/@commitlint/ensure/src/case.ts b/@commitlint/ensure/src/case.ts index 13701efde5..fcd8b35827 100644 --- a/@commitlint/ensure/src/case.ts +++ b/@commitlint/ensure/src/case.ts @@ -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; @@ -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}"`); - } -} diff --git a/@commitlint/ensure/src/index.ts b/@commitlint/ensure/src/index.ts index 982e128525..379e60beb1 100644 --- a/@commitlint/ensure/src/index.ts +++ b/@commitlint/ensure/src/index.ts @@ -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}; diff --git a/@commitlint/ensure/src/to-case.ts b/@commitlint/ensure/src/to-case.ts new file mode 100644 index 0000000000..84cee84ac0 --- /dev/null +++ b/@commitlint/ensure/src/to-case.ts @@ -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}"`); + } +} diff --git a/@commitlint/prompt/package.json b/@commitlint/prompt/package.json index cfccefc1b2..0b2dc1dba4 100644 --- a/@commitlint/prompt/package.json +++ b/@commitlint/prompt/package.json @@ -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" }, diff --git a/@commitlint/prompt/src/library/get-forced-case-fn.test.ts b/@commitlint/prompt/src/library/get-forced-case-fn.test.ts index 0b630876bb..f112745506 100644 --- a/@commitlint/prompt/src/library/get-forced-case-fn.test.ts +++ b/@commitlint/prompt/src/library/get-forced-case-fn.test.ts @@ -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', () => { @@ -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', diff --git a/@commitlint/prompt/src/library/get-forced-case-fn.ts b/@commitlint/prompt/src/library/get-forced-case-fn.ts index 74b285afa8..ae9bddad5e 100644 --- a/@commitlint/prompt/src/library/get-forced-case-fn.ts +++ b/@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 {toCase} from '@commitlint/ensure'; import {RuleEntry} from './types'; import {ruleIsActive, ruleIsNotApplicable} from './utils'; +import {TargetCaseType} from '@commitlint/types'; /** * Get forced case for rule @@ -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); }