-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathtype-case.ts
39 lines (32 loc) · 876 Bytes
/
type-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 typeCase: SyncRule<TargetCaseType | TargetCaseType[]> = (
parsed,
when = 'always',
value = []
) => {
const {type} = parsed;
if (!type) {
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(type, check.case);
return negated(check.when) ? !r : r;
});
const list = checks.map(c => c.case).join(', ');
return [
negated(when) ? !result : result,
message([`type must`, negated(when) ? `not` : null, `be ${list}`])
];
};