forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
282 lines (252 loc) · 9.8 KB
/
utils.ts
File metadata and controls
282 lines (252 loc) · 9.8 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
namespace pxsim.util {
export function injectPolyphils() {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill
if (!Array.prototype.fill) {
Object.defineProperty(Array.prototype, 'fill', {
writable: true,
enumerable: true,
value: function (value: Array<any>) {
// Steps 1-2.
if (this == null) {
throw new TypeError('this is null or not defined');
}
let O = Object(this);
// Steps 3-5.
let len = O.length >>> 0;
// Steps 6-7.
let start = arguments[1];
let relativeStart = start >> 0;
// Step 8.
let k = relativeStart < 0 ?
Math.max(len + relativeStart, 0) :
Math.min(relativeStart, len);
// Steps 9-10.
let end = arguments[2];
let relativeEnd = end === undefined ?
len : end >> 0;
// Step 11.
let final = relativeEnd < 0 ?
Math.max(len + relativeEnd, 0) :
Math.min(relativeEnd, len);
// Step 12.
while (k < final) {
O[k] = value;
k++;
}
// Step 13.
return O;
}
});
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', {
writable: true,
enumerable: true,
value: function (predicate: (value: any, index: number, obj: any[]) => boolean) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
let o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
const len = o.length >>> 0;
// 3. If IsCallable(predicate) is false, throw a TypeError exception.
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
// 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
const thisArg = arguments[1];
// 5. Let k be 0.
let k = 0;
// 6. Repeat, while k < len
while (k < len) {
// a. Let Pk be ! ToString(k).
// b. Let kValue be ? Get(O, Pk).
// c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
// d. If testResult is true, return kValue.
const kValue = o[k];
if (predicate.call(thisArg, kValue, k, o)) {
return kValue;
}
// e. Increase k by 1.
k++;
}
// 7. Return undefined.
return undefined;
},
});
}
// Polyfill for Uint8Array.slice for IE and Safari
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.slice
// TODO: Move this polyfill to a more appropriate file. It is left here for now because moving it causes a crash in IE; see PXT issue #1301.
if (!Uint8Array.prototype.slice) {
Object.defineProperty(Uint8Array.prototype, 'slice', {
value: Array.prototype.slice,
writable: true,
enumerable: true
});
}
if (!Uint16Array.prototype.slice) {
Object.defineProperty(Uint16Array.prototype, 'slice', {
value: Array.prototype.slice,
writable: true,
enumerable: true
});
}
if (!Uint32Array.prototype.slice) {
Object.defineProperty(Uint32Array.prototype, 'slice', {
value: Array.prototype.slice,
writable: true,
enumerable: true
});
}
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.fill
if (!Uint8Array.prototype.fill) {
Object.defineProperty(Uint8Array.prototype, 'fill', {
value: Array.prototype.fill,
writable: true,
enumerable: true
});
}
if (!Uint16Array.prototype.fill) {
Object.defineProperty(Uint16Array.prototype, 'fill', {
value: Array.prototype.fill,
writable: true,
enumerable: true
});
}
if (!Uint32Array.prototype.fill) {
Object.defineProperty(Uint32Array.prototype, 'fill', {
value: Array.prototype.fill,
writable: true,
enumerable: true
});
}
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.some
if (!Uint8Array.prototype.some) {
Object.defineProperty(Uint8Array.prototype, 'some', {
value: Array.prototype.some,
writable: true,
enumerable: true
});
}
if (!Uint16Array.prototype.some) {
Object.defineProperty(Uint16Array.prototype, 'some', {
value: Array.prototype.some,
writable: true,
enumerable: true
});
}
if (!Uint32Array.prototype.some) {
Object.defineProperty(Uint32Array.prototype, 'some', {
value: Array.prototype.some,
writable: true,
enumerable: true
});
}
// https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.reverse
if (!Uint8Array.prototype.reverse) {
Object.defineProperty(Uint8Array.prototype, 'reverse', {
value: Array.prototype.reverse,
writable: true,
enumerable: true
});
}
if (!Uint16Array.prototype.reverse) {
Object.defineProperty(Uint16Array.prototype, 'reverse', {
value: Array.prototype.reverse,
writable: true,
enumerable: true
});
}
if (!Uint32Array.prototype.reverse) {
Object.defineProperty(Uint32Array.prototype, 'reverse', {
value: Array.prototype.reverse,
writable: true,
enumerable: true
});
}
// Inject Math imul polyfill
if (!Math.imul) {
// for explanations see:
// http://stackoverflow.com/questions/3428136/javascript-integer-math-incorrect-results (second answer)
// (but the code below doesn't come from there; I wrote it myself)
// TODO use Math.imul if available
Math.imul = function (a: number, b: number): number {
const ah = (a >>> 16) & 0xffff;
const al = a & 0xffff;
const bh = (b >>> 16) & 0xffff;
const bl = b & 0xffff;
// the shift by 0 fixes the sign on the high part
// the final |0 converts the unsigned value into a signed value
return ((al * bl) + (((ah * bl + al * bh) << 16) >>> 0) | 0);
}
}
}
export class Lazy<T> {
private _value: T;
private _evaluated = false;
constructor(private _func: () => T) { }
get value(): T {
if (!this._evaluated) {
this._value = this._func();
this._evaluated = true;
}
return this._value;
}
}
export function getNormalizedParts(path: string): string[] {
path = path.replace(/\\/g, "/");
const parts: string[] = [];
path.split("/").forEach(part => {
if (part === ".." && parts.length) {
parts.pop();
}
else if (part && part !== ".") {
parts.push(part)
}
});
return parts;
}
export function normalizePath(path: string): string {
return getNormalizedParts(path).join("/");
}
export function relativePath(fromDir: string, toFile: string): string {
const fParts = getNormalizedParts(fromDir);
const tParts = getNormalizedParts(toFile);
let i = 0;
while (fParts[i] === tParts[i]) {
i++;
if (i === fParts.length || i === tParts.length) {
break;
}
}
const fRemainder = fParts.slice(i);
const tRemainder = tParts.slice(i);
for (let i = 0; i < fRemainder.length; i++) {
tRemainder.unshift("..");
}
return tRemainder.join("/");
}
export function pathJoin(...paths: string[]): string {
let result = "";
paths.forEach(path => {
path.replace(/\\/g, "/");
if (path.lastIndexOf("/") === path.length - 1) {
path = path.slice(0, path.length - 1)
}
result += "/" + path;
});
return result;
}
export function toArray<T>(a: ArrayLike<T> | ReadonlyArray<T>): T[] {
if (Array.isArray(a)) {
return a;
}
let r: T[] = []
for (let i = 0; i < a.length; ++i)
r.push(a[i])
return r
}
}