-
-
Notifications
You must be signed in to change notification settings - Fork 269
/
line-type.ts
30 lines (28 loc) · 935 Bytes
/
line-type.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
/**
* Collection of the various types of lines encountered in a TAP stream
*/
export const lineTypes = {
testPoint: /^(not )?ok(?: ([0-9]+))?(?:(?: -)?( .*?))?(\{?)\n$/,
pragma: /^pragma ([+-])([a-zA-Z0-9_-]+)\n$/,
bailout: /^bail out!(.*)\n$/i,
version: /^TAP version ([0-9]+)\n$/i,
childVersion: /^( )+TAP version ([0-9]+)\n$/i,
plan: /^([0-9]+)\.\.([0-9]+)(?:\s+(?:#\s*(.*)))?\n$/,
subtest: /^# Subtest(?:: (.*))?\n$/,
subtestIndent: /^ # Subtest(?:: (.*))?\n$/,
comment: /^\s*#.*\n$/,
} as const
/**
* The type of a line, and its constituent parsed pieces
*/
export type ParsedLine = [string, RegExpMatchArray]
/**
* Determine the type of line, and parse it into a {@link ParsedLine}
*/
export const lineType = (line: string): ParsedLine | null => {
for (const [t, pattern] of Object.entries(lineTypes)) {
const match = line.match(pattern)
if (match) return [t, match]
}
return null
}