Skip to content

Commit

Permalink
fix(prompt): apply forced cases properly
Browse files Browse the repository at this point in the history
closes #145
  • Loading branch information
marionebl committed Nov 24, 2017
1 parent e08a3ce commit 3a569a7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 56 deletions.
52 changes: 46 additions & 6 deletions @commitlint/prompt/src/library/get-forced-case-fn.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getForcedCase from './get-forced-case';
import {camelCase, kebabCase, snakeCase, upperFirst, startCase} from 'lodash';

/**
* Get forced case for rule
Expand All @@ -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]}"`);
}
}
23 changes: 0 additions & 23 deletions @commitlint/prompt/src/library/get-forced-case.js

This file was deleted.

24 changes: 0 additions & 24 deletions @commitlint/prompt/src/library/get-forced-case.js-fn.js

This file was deleted.

3 changes: 0 additions & 3 deletions @commitlint/prompt/src/library/get-prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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
})}`
);
Expand Down

0 comments on commit 3a569a7

Please sign in to comment.