-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.ts
198 lines (184 loc) · 5.44 KB
/
pattern.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { ajv } from "./utils";
/**
* The word separator symbol.
*/
const wordSeparator = ".";
/**
* The valid symbols that can be contained within an event's name.
*/
const eventNameCharSet = new Set(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz" +
"0123456789" +
"-_$:" +
wordSeparator
);
/**
* Single word wildcard. Matches exactly one word, without checking
* its contents.
*/
const singleWordWildCard = "*";
/**
* Multiple word wildcard. Matches zero or more words, without
* checking the contents of any word matched.
*/
const multipleWordWildCard = "#";
/**
* The valid symbols that can be contained within a string pattern.
*/
const patternCharSet = new Set(eventNameCharSet)
.add(singleWordWildCard)
.add(multipleWordWildCard);
/**
* Identifies a valid event name. A valid name is comprised of
* recognized symbols and contains 'words' of at least one symbol in
* length.
*
* @param name The name to test.
* @returns Whether the given name is a valid event name.
*/
export const isValidEventName = (name: string): boolean =>
Array.from(name).every((symbol) => eventNameCharSet.has(symbol)) &&
name.split(wordSeparator).every((word) => word.length > 0);
/**
* Identifies a valid string pattern. A valid string pattern is
* comprised of recognized symbols and contains 'words' of at least
* one symbol in length. A word may also be a single wildcard symbol,
* which can be used to match against one event name word (using the
* '*' wildcard), or many sequential event name words (using the '#'
* wildcard).
*
* @param pattern The pattern string to test.
* @returns Whether the given pattern string is valid.
*/
const isValidPatternString = (pattern: string): boolean =>
Array.from(pattern).every((symbol) => patternCharSet.has(symbol)) &&
pattern
.split(wordSeparator)
.every(
(word) =>
word.length > 0 &&
(word.length === 1 ||
(word.indexOf(singleWordWildCard) === -1 &&
word.indexOf(multipleWordWildCard) === -1))
);
/**
* A pattern can be built from others using basic 'and', 'or' and
* 'not' combinators.
*/
export type Pattern =
| string
| { and: Pattern[] }
| { or: Pattern[] }
| { not: Pattern };
/**
* Provide the type definition as a JSON schema.
*/
export const patternSchema = {
anyOf: [
{ type: "string" },
{
type: "object",
properties: { and: { type: "array" } },
additionalProperties: false,
required: ["and"],
},
{
type: "object",
properties: { or: { type: "array" } },
additionalProperties: false,
required: ["or"],
},
{
type: "object",
properties: { not: { anyOf: [{ type: "string" }, { type: "object" }] } },
additionalProperties: false,
required: ["not"],
},
],
};
/**
* Validate a pattern non-strictly, using only the schema.
*/
const validatePatternSurface = ajv.compile(patternSchema);
/**
* Validate a pattern fully.
*
* @param thing A pattern object.
* @returns Whether the given pattern object is valid.
*/
export const isValidPattern = (thing: unknown): boolean => {
if (!validatePatternSurface(thing)) {
return false;
}
const pattern = thing as Pattern;
if (typeof pattern === "string") {
return isValidPatternString(pattern);
} else if ("and" in pattern) {
return pattern.and.every(isValidPattern);
} else if ("or" in pattern) {
return pattern.or.every(isValidPattern);
} else if ("not" in pattern) {
return isValidPattern(pattern.not);
} else {
return false;
}
};
/**
* Checks that a word sequence matches a pattern-word sequence.
*
* @param sWords The word sequence to check.
* @param pWords The pattern-word sequence to check against.
* @returns Whether the words match the pattern-words.
*/
const wordsMatchPatternWords = (
sWords: string[],
pWords: string[]
): boolean => {
if (pWords.length === 1 && pWords[0] === multipleWordWildCard) return true;
if (sWords.length === 0 && pWords.length === 0) return true;
if (sWords.length === 0 || pWords.length === 0) return false;
const [sWord, ...sRest] = sWords;
const [pWord, ...pRest] = pWords;
if (pWord === multipleWordWildCard) {
return (
wordsMatchPatternWords(sRest, pWords) ||
wordsMatchPatternWords(sRest, pRest) ||
wordsMatchPatternWords(sWords, pRest)
);
} else if (pWord === singleWordWildCard) {
return wordsMatchPatternWords(sRest, pRest);
} else {
return sWord === pWord && wordsMatchPatternWords(sRest, pRest);
}
};
/**
* Checks that a given sequence of words matches against a given
* pattern.
*
* @param sWords The word sequence to check.
* @param p The pattern to check against.
* @returns Whether the words match the pattern.
*/
const wordsMatchPattern = (sWords: string[], p: Pattern): boolean => {
if (typeof p === "string") {
return wordsMatchPatternWords(sWords, p.split(wordSeparator));
} else if ("and" in p) {
return p.and.every((subP) => wordsMatchPattern(sWords, subP));
} else if ("or" in p) {
return p.or.some((subP) => wordsMatchPattern(sWords, subP));
} else if ("not" in p) {
return !wordsMatchPattern(sWords, p.not);
} else {
return false;
}
};
/**
* Checks that a given string matches against a given pattern.
*
* @param s The string to check.
* @param p The pattern to check against.
* @returns Whether the string matches the pattern.
*/
export const match = (s: string, p: Pattern) =>
wordsMatchPattern(s.split(wordSeparator), p);