-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathutils.ts
More file actions
47 lines (39 loc) · 1.2 KB
/
utils.ts
File metadata and controls
47 lines (39 loc) · 1.2 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
import fs from "fs";
import path from "path";
export function readGrammar(): string {
return fs.readFileSync(
path.resolve(__dirname, "../src/parser.pegjs"),
"utf-8"
);
}
export function writeGrammar(source: string) {
return fs.writeFileSync(
path.resolve(__dirname, "../src/parser.pegjs"),
source
);
}
export function toLines(str: string): string[] {
return str.split("\n");
}
export function fromLines(lines: string[]): string {
return lines.join("\n");
}
export function caseInsensitiveStringCompare(a: string, b: string): -1 | 0 | 1 {
const aa = a.toLocaleLowerCase();
const bb = b.toLowerCase();
return aa < bb ? -1 : aa > bb ? 1 : 0;
}
/**
* Extracts the lines between the start and end comments for a given type.
*/
export function extractRules(lines: string[], type: string) {
const startRegex = new RegExp(`^\\/\\*! ${type}:start `);
const endRegex = new RegExp(`^\\/\\*! ${type}:end `);
const startIndex = lines.findIndex((line) => startRegex.test(line)) + 1;
const endIndex = lines.findIndex((line) => endRegex.test(line));
return {
before: lines.slice(0, startIndex),
rules: lines.slice(startIndex, endIndex),
after: lines.slice(endIndex),
};
}