diff --git a/@commitlint/prompt/src/library/get-forced-case-fn.js b/@commitlint/prompt/src/library/get-forced-case-fn.js index 7e1d913c73..e0adaa5525 100644 --- a/@commitlint/prompt/src/library/get-forced-case-fn.js +++ b/@commitlint/prompt/src/library/get-forced-case-fn.js @@ -1,4 +1,4 @@ -import getForcedCase from './get-forced-case'; +import {camelCase, kebabCase, snakeCase, upperFirst, startCase} from 'lodash'; /** * Get forced case for rule @@ -7,18 +7,58 @@ import getForcedCase from './get-forced-case'; */ export default function getForcedCaseFn(rule) { const noop = input => input; - const lowerCase = input => String.prototype.toLowerCase.call(input); - const upperCase = input => String.prototype.toUpperCase.call(input); if (!rule) { return noop; } - const forcedCase = getForcedCase(rule); + const [config] = rule; - if (forcedCase === null) { + if (!Array.isArray(config)) { return noop; } - return forcedCase === 'lowerCase' ? lowerCase : upperCase; + const [level] = config; + + if (level === 0) { + return; + } + + const [, when] = config; + + if (when === 'neve') { + return; + } + + const [, , target] = config; + + if (Array.isArray(target)) { + return noop; + } + + switch (target) { + case 'camel-case': + return input => camelCase(input); + case 'kebab-case': + return input => kebabCase(input); + case 'snake-case': + return input => snakeCase(input); + case 'pascal-case': + return input => upperFirst(camelCase(input)); + case 'start-case': + return input => startCase(input); + case 'upper-case': + case 'uppercase': + return input => input.toUpperCase(); + case 'sentence-case': + case 'sentencecase': + return input => + `${input.charAt(0).toUpperCase()}${input.substring(1).toLowerCase()}`; + case 'lower-case': + case 'lowercase': + case 'lowerCase': // Backwards compat config-angular v4 + return input => input.toLowerCase() === input; + default: + throw new TypeError(`Unknown target case "${rule[2]}"`); + } } diff --git a/@commitlint/prompt/src/library/get-forced-case.js b/@commitlint/prompt/src/library/get-forced-case.js deleted file mode 100644 index 22eb1e9c78..0000000000 --- a/@commitlint/prompt/src/library/get-forced-case.js +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Get forced case for rule - * @param {object} rule to parse - * @return {string|null} transform function applying the enforced case - */ -export default function getForcedCase(rule) { - if (!rule) { - return null; - } - - const [, [severity, applicable, value]] = rule; - const negated = applicable === 'never'; - - if (severity === 0) { - return null; - } - - if (negated) { - return value === 'lowerCase' ? 'upperCase' : 'lowerCase'; - } - - return value === 'lowerCase' ? 'lowerCase' : 'upperCase'; -} diff --git a/@commitlint/prompt/src/library/get-forced-case.js-fn.js b/@commitlint/prompt/src/library/get-forced-case.js-fn.js deleted file mode 100644 index 7e1d913c73..0000000000 --- a/@commitlint/prompt/src/library/get-forced-case.js-fn.js +++ /dev/null @@ -1,24 +0,0 @@ -import getForcedCase from './get-forced-case'; - -/** - * Get forced case for rule - * @param {object} rule to parse - * @return {fn} transform function applying the enforced case - */ -export default function getForcedCaseFn(rule) { - const noop = input => input; - const lowerCase = input => String.prototype.toLowerCase.call(input); - const upperCase = input => String.prototype.toUpperCase.call(input); - - if (!rule) { - return noop; - } - - const forcedCase = getForcedCase(rule); - - if (forcedCase === null) { - return noop; - } - - return forcedCase === 'lowerCase' ? lowerCase : upperCase; -} diff --git a/@commitlint/prompt/src/library/get-prompt.js b/@commitlint/prompt/src/library/get-prompt.js index 70a9641643..d98c499666 100644 --- a/@commitlint/prompt/src/library/get-prompt.js +++ b/@commitlint/prompt/src/library/get-prompt.js @@ -2,7 +2,6 @@ import chalk from 'chalk'; import enumRuleIsActive from './enum-rule-is-active'; import format from './format'; -import getForcedCase from './get-forced-case'; import getForcedCaseFn from './get-forced-case-fn'; import getForcedLeadingFn from './get-forced-leading-fn'; import getHasName from './get-has-name'; @@ -78,7 +77,6 @@ function getPrompt(type, context = {}) { const caseRule = rules.filter(getHasName('case'))[0]; - const forcedCase = getForcedCase(caseRule); const forceCaseFn = getForcedCaseFn(caseRule); const leadingBlankRule = rules.filter(getHasName('leading-blank'))[0]; @@ -219,7 +217,6 @@ function getPrompt(type, context = {}) { required: mayNotBeEmpty, 'tab-completion': typeof enumRule !== 'undefined', header: typeof settings.header !== 'undefined', - case: forcedCase, 'multi-line': settings.multiline })}` );