Skip to content

Commit 4da8b0b

Browse files
committed
refactor(core): add message util
1 parent aa61745 commit 4da8b0b

22 files changed

+72
-61
lines changed

Diff for: @commitlint/core/src/library/message.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default message;
2+
3+
function message(input = []) {
4+
return input.filter(Boolean).join(' ');
5+
}

Diff for: @commitlint/core/src/library/message.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import test from 'ava';
2+
import message from './message';
3+
4+
test('should return an empty string for empty input', t => {
5+
t.deepEqual(message(), '');
6+
});
7+
8+
test('should return an empty string for empty input array', t => {
9+
t.deepEqual(message([]), '');
10+
});
11+
12+
test('should filter falsy values', t => {
13+
t.deepEqual(message([null, 'some', null, 'message', null]), 'some message');
14+
});

Diff for: @commitlint/core/src/rules/body-case.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ensureCase from '../library/ensure-case';
2+
import message from '../library/message';
23

34
export default (parsed, when, value) => {
45
const {body} = parsed;
@@ -12,8 +13,6 @@ export default (parsed, when, value) => {
1213
const result = ensureCase(body, value);
1314
return [
1415
negated ? !result : result,
15-
[`body must`, negated ? `not` : null, `be ${value}`]
16-
.filter(Boolean)
17-
.join(' ')
16+
message([`body must`, negated ? `not` : null, `be ${value}`])
1817
];
1918
};

Diff for: @commitlint/core/src/rules/body-empty.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import ensureNotEmpty from '../library/ensure-not-empty';
2+
import message from '../library/message';
23

34
export default (parsed, when) => {
45
const negated = when === 'never';
56
const notEmpty = ensureNotEmpty(parsed.body);
67

78
return [
89
negated ? notEmpty : !notEmpty,
9-
['body', negated ? 'may not' : 'must', 'be empty'].filter(Boolean).join(' ')
10+
message(['body', negated ? 'may not' : 'must', 'be empty'])
1011
];
1112
};

Diff for: @commitlint/core/src/rules/body-leading-blank.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import toLines from '../library/to-lines';
2+
import message from '../library/message';
23

34
export default (parsed, when) => {
45
// Flunk if no body is found
@@ -14,8 +15,6 @@ export default (parsed, when) => {
1415

1516
return [
1617
negated ? !succeeds : succeeds,
17-
['body', negated ? 'may not' : 'must', 'have leading blank line']
18-
.filter(Boolean)
19-
.join(' ')
18+
message(['body', negated ? 'may not' : 'must', 'have leading blank line'])
2019
];
2120
};

Diff for: @commitlint/core/src/rules/body-tense.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ensureTense from '../library/ensure-tense';
2+
import message from '../library/message';
23

34
export default (parsed, when, value) => {
45
const tenses = Array.isArray(value) ? value : value.allowed || [];
@@ -13,12 +14,10 @@ export default (parsed, when, value) => {
1314

1415
return [
1516
negated ? !matches : matches,
16-
[
17+
message([
1718
`tense of body must`,
1819
negated ? `not` : null,
1920
`be ${value}. Verbs in other tenses: ${offenders}`
20-
]
21-
.filter(Boolean)
22-
.join(' ')
21+
])
2322
];
2423
};

Diff for: @commitlint/core/src/rules/footer-empty.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import ensureNotEmpty from '../library/ensure-not-empty';
2+
import message from '../library/message';
23

34
export default (parsed, when) => {
45
const negated = when === 'never';
56
const notEmpty = ensureNotEmpty(parsed.footer);
67

78
return [
89
negated ? notEmpty : !notEmpty,
9-
['footer', negated ? 'may not' : 'must', 'be empty']
10-
.filter(Boolean)
11-
.join(' ')
10+
message(['footer', negated ? 'may not' : 'must', 'be empty'])
1211
];
1312
};

Diff for: @commitlint/core/src/rules/footer-leading-blank.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import toLines from '../library/to-lines';
2+
import message from '../library/message';
23

34
export default (parsed, when) => {
45
// Flunk if no footer is found
@@ -14,8 +15,6 @@ export default (parsed, when) => {
1415

1516
return [
1617
negated ? !succeeds : succeeds,
17-
['footer', negated ? 'may not' : 'must', 'have leading blank line']
18-
.filter(Boolean)
19-
.join(' ')
18+
message(['footer', negated ? 'may not' : 'must', 'have leading blank line'])
2019
];
2120
};

Diff for: @commitlint/core/src/rules/footer-tense.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ensureTense from '../library/ensure-tense';
2+
import message from '../library/message';
23

34
export default (parsed, when, value) => {
45
const tenses = Array.isArray(value) ? value : value.allowed || [];
@@ -15,12 +16,10 @@ export default (parsed, when, value) => {
1516

1617
return [
1718
negated ? !matches : matches,
18-
[
19+
message([
1920
`tense of footer must`,
2021
negated ? `not` : null,
2122
`be ${value}. Verbs in other tenses: ${offenders}`
22-
]
23-
.filter(Boolean)
24-
.join(' ')
23+
])
2524
];
2625
};

Diff for: @commitlint/core/src/rules/lang.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// TODO: this should be named subject-lang
1+
import message from '../library/message';
22
import ensureLanguage from '../library/ensure-language';
33

44
export default (parsed, when, value) => {
@@ -7,12 +7,10 @@ export default (parsed, when, value) => {
77

88
return [
99
negated ? !matches : matches,
10-
[
10+
message([
1111
'commit',
1212
negated ? 'may not' : 'must',
1313
`be in languague "${value}", was one of: ${detected.join(', ')}`
14-
]
15-
.filter(Boolean)
16-
.join(' ')
14+
])
1715
];
1816
};

Diff for: @commitlint/core/src/rules/scope-case.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import message from '../library/message';
12
import ensureCase from '../library/ensure-case';
23

34
export default (parsed, when, value) => {
@@ -12,8 +13,6 @@ export default (parsed, when, value) => {
1213
const result = ensureCase(scope, value);
1314
return [
1415
negated ? !result : result,
15-
[`scope must`, negated ? `not` : null, `be ${value}`]
16-
.filter(Boolean)
17-
.join(' ')
16+
message([`scope must`, negated ? `not` : null, `be ${value}`])
1817
];
1918
};

Diff for: @commitlint/core/src/rules/scope-empty.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import ensureNotEmpty from '../library/ensure-not-empty';
2+
import message from '../library/message';
23

34
export default (parsed, when = 'never') => {
45
const negated = when === 'always';
56
const notEmpty = ensureNotEmpty(parsed.scope);
67
return [
78
negated ? !notEmpty : notEmpty,
8-
['scope', negated ? 'must' : 'may not', 'be empty']
9-
.filter(Boolean)
10-
.join(' ')
9+
message(['scope', negated ? 'must' : 'may not', 'be empty'])
1110
];
1211
};

Diff for: @commitlint/core/src/rules/scope-enum.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ensureEnum from '../library/ensure-enum';
2+
import message from '../library/message';
23

34
export default (parsed, when, value) => {
45
if (!parsed.scope) {
@@ -10,8 +11,10 @@ export default (parsed, when, value) => {
1011

1112
return [
1213
negated ? !result : result,
13-
[`scope must`, negated ? `not` : null, `be one of [${value.join(', ')}]`]
14-
.filter(Boolean)
15-
.join(' ')
14+
message([
15+
`scope must`,
16+
negated ? `not` : null,
17+
`be one of [${value.join(', ')}]`
18+
])
1619
];
1720
};

Diff for: @commitlint/core/src/rules/signed-off-by.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import toLines from '../library/to-lines';
2+
import message from '../library/message';
23

34
export default (parsed, when, value) => {
45
const lines = toLines(parsed.raw).filter(Boolean);
@@ -9,8 +10,6 @@ export default (parsed, when, value) => {
910

1011
return [
1112
negated ? !hasSignedOffBy : hasSignedOffBy,
12-
['message', negated ? 'must not' : 'must', 'be signed off']
13-
.filter(Boolean)
14-
.join(' ')
13+
message(['message', negated ? 'must not' : 'must', 'be signed off'])
1514
];
1615
};

Diff for: @commitlint/core/src/rules/subject-case.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ensureCase from '../library/ensure-case';
2+
import message from '../library/message';
23

34
export default (parsed, when, value) => {
45
const {subject} = parsed;
@@ -12,8 +13,6 @@ export default (parsed, when, value) => {
1213
const result = ensureCase(subject, value);
1314
return [
1415
negated ? !result : result,
15-
[`subject must`, negated ? `not` : null, `be ${value}`]
16-
.filter(Boolean)
17-
.join(' ')
16+
message([`subject must`, negated ? `not` : null, `be ${value}`])
1817
];
1918
};

Diff for: @commitlint/core/src/rules/subject-empty.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import ensureNotEmpty from '../library/ensure-not-empty';
2+
import message from '../library/message';
23

34
export default (parsed, when) => {
45
const negated = when === 'never';
56
const notEmpty = ensureNotEmpty(parsed.subject);
67

78
return [
89
negated ? notEmpty : !notEmpty,
9-
['message', negated ? 'may not' : 'must', 'be empty']
10-
.filter(Boolean)
11-
.join(' ')
10+
message(['message', negated ? 'may not' : 'must', 'be empty'])
1211
];
1312
};

Diff for: @commitlint/core/src/rules/subject-full-stop.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import message from '../library/message';
2+
13
export default (parsed, when, value) => {
24
const input = parsed.subject;
35

@@ -10,8 +12,6 @@ export default (parsed, when, value) => {
1012

1113
return [
1214
negated ? !hasStop : hasStop,
13-
['message', negated ? 'may not' : 'must', 'end with full stop']
14-
.filter(Boolean)
15-
.join(' ')
15+
message(['message', negated ? 'may not' : 'must', 'end with full stop'])
1616
];
1717
};

Diff for: @commitlint/core/src/rules/subject-leading-capital.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// TODO
22
// * rename this to "subject-first-character"
3+
import message from '../library/message';
34
import ensureCase from '../library/ensure-case';
45

56
export default (parsed, when = 'always', value = 'uppercase') => {
@@ -14,8 +15,6 @@ export default (parsed, when = 'always', value = 'uppercase') => {
1415

1516
return [
1617
negated ? !result : result,
17-
[`message must`, negated ? `not` : null, `be ${value}`]
18-
.filter(Boolean)
19-
.join(' ')
18+
message([`message must`, negated ? `not` : null, `be ${value}`])
2019
];
2120
};

Diff for: @commitlint/core/src/rules/subject-tense.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ensureTense from '../library/ensure-tense';
2+
import message from '../library/message';
23

34
export default (parsed, when, value) => {
45
const tenses = Array.isArray(value) ? value : value.allowed || [];
@@ -15,12 +16,10 @@ export default (parsed, when, value) => {
1516

1617
return [
1718
negated ? !matches : matches,
18-
[
19+
message([
1920
`tense of subject must`,
2021
negated ? `not` : null,
2122
`be ${value}. Verbs in other tenses: ${offenders}`
22-
]
23-
.filter(Boolean)
24-
.join(' ')
23+
])
2524
];
2625
};

Diff for: @commitlint/core/src/rules/type-case.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ensureCase from '../library/ensure-case';
2+
import message from '../library/message';
23

34
export default (parsed, when, value) => {
45
const {type} = parsed;
@@ -12,8 +13,6 @@ export default (parsed, when, value) => {
1213
const result = ensureCase(type, value);
1314
return [
1415
negated ? !result : result,
15-
[`subject must`, negated ? `not` : null, `be ${value}`]
16-
.filter(Boolean)
17-
.join(' ')
16+
message([`subject must`, negated ? `not` : null, `be ${value}`])
1817
];
1918
};

Diff for: @commitlint/core/src/rules/type-empty.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import ensureNotEmpty from '../library/ensure-not-empty';
2+
import message from '../library/message';
23

34
export default (parsed, when) => {
45
const negated = when === 'never';
56
const notEmpty = ensureNotEmpty(parsed.type);
67
return [
78
negated ? notEmpty : !notEmpty,
8-
['type', negated ? 'may not' : 'must', 'be empty'].filter(Boolean).join(' ')
9+
message(['type', negated ? 'may not' : 'must', 'be empty'])
910
];
1011
};

Diff for: @commitlint/core/src/rules/type-enum.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ensureEnum from '../library/ensure-enum';
2+
import message from '../library/message';
23

34
export default (parsed, when, value) => {
45
const {type: input} = parsed;
@@ -12,8 +13,10 @@ export default (parsed, when, value) => {
1213

1314
return [
1415
negated ? !result : result,
15-
[`scope must`, negated ? `not` : null, `be one of [${value.join(', ')}]`]
16-
.filter(Boolean)
17-
.join(' ')
16+
message([
17+
`scope must`,
18+
negated ? `not` : null,
19+
`be one of [${value.join(', ')}]`
20+
])
1821
];
1922
};

0 commit comments

Comments
 (0)