Skip to content

Commit a02bd55

Browse files
committed
Refactor: change function behavior
BREAKING CHANGE: rules are now functions where input and config are passed immediatelly
1 parent cd322a9 commit a02bd55

File tree

3 files changed

+46
-34
lines changed

3 files changed

+46
-34
lines changed

lib/rules/availableRules.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
import getConfig from '../getConfig';
2-
3-
const configuration = getConfig();
4-
51
const rules = {
6-
endWithDot: {
7-
message: 'The commit message can not end with a dot',
8-
check: (input) => {
2+
endWithDot: input => ({
3+
message: () => 'The commit message can not end with a dot',
4+
check: () => {
95
if (input[input.length - 1] === '.') return false;
6+
107
return true;
118
},
12-
},
13-
maxChar: {
14-
message: `The commit message is not allowed to be longer as ${configuration.rules['max-char']}. Consider writing a body.`,
15-
check: (input) => {
16-
if (input.length > configuration.rules['max-char']) return false;
9+
}),
10+
maxChar: (input, config) => ({
11+
message: () => `The commit message is not allowed to be longer as ${config.rules['max-char']}. Consider writing a body.`,
12+
check: () => {
13+
if (input.length > config.rules['max-char']) return false;
14+
1715
return true;
1816
},
19-
},
20-
minChar: {
21-
message: `The commit message has to be at least ${configuration.rules['min-char']} character.`,
22-
check: (input) => {
23-
if (input.length < configuration.rules['min-char']) return false;
17+
}),
18+
minChar: (input, config) => ({
19+
message: () => `The commit message has to be at least ${config.rules['min-char']} character.`,
20+
check: () => {
21+
if (input.length < config.rules['min-char']) return false;
22+
2423
return true;
2524
},
26-
},
25+
}),
2726
};
2827

2928
export default rules;

lib/rules/ruleWarningMessages.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import entries from 'object.entries';
22
import getConfig from '../getConfig';
33
import rules from './availableRules';
44

5-
const configuration = getConfig();
5+
const config = getConfig();
66

77
const ruleWarningMessages = (input) => {
88
let warningMessage = '';
9-
const configRuleEntries = entries(configuration.rules);
9+
const configRuleEntries = entries(config.rules);
1010

1111
configRuleEntries.forEach((rule) => {
1212
const camelCaseRuleName = (rule[0]).replace(/-([a-z])/g, g => (g[1].toUpperCase()));
13-
const ruleIs = rules[camelCaseRuleName].check(input);
13+
const ruleIs = rules[camelCaseRuleName](input, config).check();
1414

15-
if (!ruleIs) warningMessage += `${rules[camelCaseRuleName].message}\n`;
15+
if (!ruleIs) warningMessage += `${rules[camelCaseRuleName](input, config).message()}\n`;
1616
});
1717

1818
return warningMessage;

test/rules/availableRules.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
import test from 'ava';
2+
23
import rules from '../../lib/rules/availableRules';
34

45
test('rules endWithDot', (t) => {
5-
const endWithDot = rules.endWithDot.check('input with dot.');
6-
const endWithoutDot = rules.endWithDot.check('input with dot');
7-
t.deepEqual(endWithDot, false);
8-
t.deepEqual(endWithoutDot, true);
6+
const rulesObj = {
7+
'end-with-dot': false,
8+
};
9+
const endWithDot = rules.endWithDot('input with dot.', { rules: rulesObj }).check();
10+
const endWithoutDot = rules.endWithDot('input with dot', { rules: rulesObj }).check();
11+
12+
t.false(endWithDot);
13+
t.true(endWithoutDot);
914
});
1015

1116
test('rules minChar', (t) => {
12-
const notMinChar = rules.minChar.check('less');
13-
const minChar = rules.minChar.check('this are more than 10 characters');
14-
t.deepEqual(notMinChar, false);
15-
t.deepEqual(minChar, true);
17+
const rulesObj = {
18+
'min-char': 10,
19+
};
20+
const notMinChar = rules.minChar('less', { rules: rulesObj }).check();
21+
const minChar = rules.minChar('this are more than 10 characters', { rules: rulesObj }).check();
22+
23+
t.false(notMinChar);
24+
t.true(minChar);
1625
});
1726

1827
test('rules mxChar', (t) => {
19-
const moreThanMaxChar = rules.maxChar.check('this are more than 72 characters, believe me or not but the value moreThanMaxChar will be false ;-P');
20-
const lessThanMaxChar = rules.maxChar.check('this are less than 72 characters');
21-
t.deepEqual(moreThanMaxChar, false);
22-
t.deepEqual(lessThanMaxChar, true);
28+
const rulesObj = {
29+
'max-char': 72,
30+
};
31+
const moreThanMaxChar = rules.maxChar('this are more than 72 characters, believe me or not but the value moreThanMaxChar will be false ;-P', { rules: rulesObj }).check();
32+
const lessThanMaxChar = rules.maxChar('this are less than 72 characters', { rules: rulesObj }).check();
33+
34+
t.false(moreThanMaxChar);
35+
t.true(lessThanMaxChar);
2336
});

0 commit comments

Comments
 (0)