-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathregex.ts
More file actions
79 lines (68 loc) · 1.77 KB
/
regex.ts
File metadata and controls
79 lines (68 loc) · 1.77 KB
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
import type { Options, Keywords, Prefixes, Actions } from "./mod.ts";
const reNomatch = /(?!.*)/;
function join(array: string[], joiner: string): string {
return array
.map(function (val) {
return val.trim();
})
.filter(function (val) {
return val.length;
})
.join(joiner);
}
function getNotesRegex(noteKeywords?: Keywords): RegExp {
if (!noteKeywords) {
return reNomatch;
}
return new RegExp(
"^[\\s|*]*(" + join(noteKeywords as string[], "|") + ")[:\\s]+(.*)",
"i",
);
}
function getReferencePartsRegex(
issuePrefixes?: Prefixes,
issuePrefixesCaseSensitive?: boolean,
): RegExp {
if (!issuePrefixes) {
return reNomatch;
}
const flags = issuePrefixesCaseSensitive ? "g" : "gi";
return new RegExp(
"(?:.*?)??\\s*([\\w-\\.\\/]*?)??(" +
join(issuePrefixes as string[], "|") +
")([\\w-]*\\d+)",
flags,
);
}
function getReferencesRegex(referenceActions?: Actions): RegExp {
if (!referenceActions) {
// matches everything
return /()(.+)/gi;
}
const joinedKeywords = join(referenceActions as string[], "|");
return new RegExp(
"(" + joinedKeywords + ")(?:\\s+(.*?))(?=(?:" + joinedKeywords + ")|$)",
"gi",
);
}
export interface ParsingRegex {
notes: RegExp;
referenceParts: RegExp;
references: RegExp;
mentions: RegExp;
}
export function regex(options: Options): ParsingRegex {
options = options || {};
const reNotes = getNotesRegex(options.noteKeywords);
const reReferenceParts = getReferencePartsRegex(
options.issuePrefixes,
options.issuePrefixesCaseSensitive,
);
const reReferences = getReferencesRegex(options.referenceActions);
return {
notes: reNotes,
referenceParts: reReferenceParts,
references: reReferences,
mentions: /@([\w-]+)/g,
};
}