-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
171 lines (137 loc) · 3.55 KB
/
util.js
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
export const SPACE = "\xA0";
export const TAB = `${SPACE}${SPACE}${SPACE}${SPACE}`;
export const ce = React.createElement;
export const SHARE_URL = "https://3inwm6vv9c.execute-api.us-east-2.amazonaws.com/share-helios-script";
export function now() {
return (new Date()).getTime();
}
export function assert(cond, msg = "unexpected") {
if (!cond) {
throw new Error(msg);
}
}
export function assertDefined(x) {
if (x === undefined) {
throw new Error("undefined");
}
return x;
}
export function assertClass(x, class_) {
if (x == undefined || !(x instanceof class_)) {
throw new Error(`expected ${class_.name}`)
}
return x;
}
export function findElement(e, cond) {
while(e != null && !cond(e)) {
e = e.parentElement;
}
return e;
}
export function formatPaddedInt(i, nDigits, padChar = '0') {
let s = i.toString();
if (s.length < nDigits) {
let nPad = nDigits - s.length;
s = (new Array(nPad)).fill(padChar).join('') + s;
}
return s;
}
export function stringToLines(raw) {
return raw.replaceAll(" ", SPACE).replaceAll("\t", TAB).split("\n");
}
export function linesToString(lines) {
return lines.join("\n").replaceAll(SPACE, " ");
}
export function trimSpaces(line) {
let trimCount = TAB.length;
for (let i = 0; i < TAB.length; i++) {
if (line[i] == SPACE) {
continue;
} else {
trimCount = i;
break;
}
}
return [line.slice(trimCount), trimCount];
}
export function isWordChar(c) {
return (c >= "a" && c <= "z") || (c >= "A" && c <= "Z") || (c == "_") || (c >= "0" && c <= "9");
}
// fails silently if navigator.clipboard isn't defined
export function setClipboard(txt) {
console.log("writing to clipboard");
if (navigator.clipboard != undefined) {
navigator.clipboard.writeText(txt);
} else {
console.error("clipboard not available");
}
}
export class Vec {
constructor(x, y) {
this.x_ = x;
this.y_ = y;
}
get x() {
return this.x_;
}
get y() {
return this.y_;
}
eq(other) {
return this.x_ == other.x && this.y_ == other.y;
}
add(other) {
return new Vec(this.x_ + other.x, this.y_ + other.y);
}
sub(other) {
return new Vec(this.x_ - other.x, this.y_ - other.y);
}
isNaN() {
return Number.isNaN(this.x_) || Number.isNaN(this.y_);
}
}
export class FilePos extends Vec {
add(other) {
return new FilePos(this.x_ + other.x, this.y_ + other.y);
}
sub(other) {
return new FilePos(this.x_ - other.x, this.y_ - other.y);
}
// return bounded copy
bound(lines, wrap = true) {
let x = this.x_;
let y = this.y_;
if (y < 0) {
y = 0;
} else if (y >= lines.length) {
y = lines.length - 1;
}
if (x < 0) {
if (y > 0 && wrap) {
y -= 1;
x = lines[y].length
} else {
x = 0;
}
} else if (x > lines[y].length) {
if (y < lines.length - 1 && wrap) {
x = 0;
y += 1;
} else {
x = lines[y].length;
}
}
return new FilePos(x, y);
}
lessThan(other) {
if (this.y_ < other.y) {
return true;
} else if (other.y < this.y_) {
return false;
} else if (this.x_ < other.x_) {
return true;
} else {
return false;
}
}
}