-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathbody-case.ts
39 lines (32 loc) · 884 Bytes
/
body-case.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {case as ensureCase} from '@commitlint/ensure';
import message from '@commitlint/message';
import {TargetCaseType, SyncRule} from '@commitlint/types';
const negated = (when?: string) => when === 'never';
export const bodyCase: SyncRule<TargetCaseType | TargetCaseType[]> = (
parsed,
when = 'always',
value = []
) => {
const {body} = parsed;
if (!body) {
return [true];
}
const checks = (Array.isArray(value) ? value : [value]).map((check) => {
if (typeof check === 'string') {
return {
when: 'always',
case: check,
};
}
return check;
});
const result = checks.some((check) => {
const r = ensureCase(body, check.case);
return negated(check.when) ? !r : r;
});
const list = checks.map((c) => c.case).join(', ');
return [
negated(when) ? !result : result,
message([`body must`, negated(when) ? `not` : null, `be ${list}`]),
];
};