forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
144 lines (134 loc) Β· 3.3 KB
/
utils.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import {
QualifiedRules,
RuleConfigSeverity,
EnumRuleOptions,
EnumRuleExtendableOptions,
} from '@commitlint/types';
import {RuleEntry} from './types';
/**
* Get name for a given rule id
* @param id of the rule
* @return name of the rule
*/
export function getRuleName(id: string): string {
const fragments = id.split('-');
return fragments.length > 1 ? fragments.slice(1).join('-') : fragments[0];
}
/**
* Get prefix for a given rule id
* @param id of the rule
* @return prefix of the rule
*/
export function getRulePrefix(id: string): string | null {
const fragments = id.split('-');
return fragments.length > 1 ? fragments[0] : null;
}
/**
* Get a predicate matching rule definitions with a given name
*/
export function getHasName(name: string) {
return <C = unknown, T extends RuleEntry<C> = RuleEntry<C>>(
rule: RuleEntry
): rule is Exclude<T, [string, undefined]> => getRuleName(rule[0]) === name;
}
/**
* Check if a rule definition is active
* @param rule to check
* @return if the rule definition is active
*/
export function ruleIsActive<
C = unknown,
T extends RuleEntry<C> = RuleEntry<C>
>(
rule: T
): rule is Exclude<T, [string, Readonly<[RuleConfigSeverity.Disabled]>]> {
const [, value] = rule;
if (value && Array.isArray(value)) {
return value[0] > RuleConfigSeverity.Disabled;
}
return false;
}
/**
* Check if a rule definition is applicable
* @param rule to check
* @return if the rule definition is applicable
*/
export function ruleIsApplicable<C>(
rule: RuleEntry<C>
): rule is
| [string, Readonly<[RuleConfigSeverity, 'always']>]
| [string, Readonly<[RuleConfigSeverity, 'always', C]>] {
const [, value] = rule;
if (value && Array.isArray(value)) {
return value[1] === 'always';
}
return false;
}
/**
* Check if a rule definition is applicable
* @param rule to check
* @return if the rule definition is applicable
*/
export function ruleIsNotApplicable(
rule: RuleEntry
): rule is
| [string, Readonly<[RuleConfigSeverity, 'never']>]
| [string, Readonly<[RuleConfigSeverity, 'never', unknown]>] {
const [, value] = rule;
if (value && Array.isArray(value)) {
return value[1] === 'never';
}
return false;
}
function enumConfigIsExtendable(
config?: EnumRuleOptions
): config is EnumRuleExtendableOptions {
return !Array.isArray(config);
}
export function enumRuleIsActive(
rule: RuleEntry<EnumRuleOptions>
): rule is [
string,
Readonly<
[
RuleConfigSeverity.Warning | RuleConfigSeverity.Error,
'always',
EnumRuleOptions
]
>
] {
let config: EnumRuleOptions | undefined;
return (
ruleIsActive(rule) &&
ruleIsApplicable(rule) &&
!!(config = rule[1][2]) &&
(!enumConfigIsExtendable(config)
? config.length > 0
: Array.isArray(config.values) && config.values.length > 0)
);
}
/**
* Get rules for a given prefix
* @param prefix to search in rule names
* @param rules rules to search in
* @return rules matching the prefix search
*/
export function getRules<C = unknown>(
prefix: string,
rules: QualifiedRules
): RuleEntry<C>[] {
return Object.entries(rules).filter(
(rule): rule is RuleEntry<C> => getRulePrefix(rule[0]) === prefix
);
}
export function getMaxLength(rule?: RuleEntry): number {
if (
rule &&
ruleIsActive(rule) &&
ruleIsApplicable(rule) &&
typeof rule[1][2] === 'number'
) {
return rule[1][2];
}
return Infinity;
}