forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.ts
More file actions
273 lines (233 loc) · 6.26 KB
/
array.ts
File metadata and controls
273 lines (233 loc) · 6.26 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
Array.prototype.at = function (index) {
if (index < 0) {
const length = this.length
return this[length + index]
}
return this[index]
}
Array.prototype.map = function (f) {
const res: any[] = []
const length = this.length
for (let i = 0; i < length; ++i) {
res.push(f(this[i], i, this))
}
return res
}
Array.prototype.forEach = function (f) {
const length = this.length
for (let i = 0; i < length; ++i) {
f(this[i], i, this)
}
}
Array.prototype.find = function (f) {
const length = this.length
for (let i = 0; i < length; ++i) {
if (f(this[i], i, this)) return this[i]
}
return undefined
}
Array.prototype.findIndex = function (f) {
const length = this.length
for (let i = 0; i < length; ++i) {
if (f(this[i], i, this)) return i
}
return -1
}
Array.prototype.findLast = function (f) {
const length = this.length
for (let i = length - 1; i >= 0; i--) {
if (f(this[i], i, this)) return this[i]
}
return undefined
}
Array.prototype.findLastIndex = function (f) {
const length = this.length
for (let i = length - 1; i >= 0; i--) {
if (f(this[i], i, this)) return i
}
return -1
}
Array.prototype.with = function <T>(index: number, value: T): T[] {
if (isNaN(index) || typeof index !== "number") {
throw new TypeError("Index must be a number")
}
if (index < -this.length || index >= this.length) {
throw new RangeError("Index out of bounds")
}
if (index < 0) {
index = this.length + index
}
const newArray = [...this]
newArray[index] = value
return newArray
}
Array.prototype.filter = function (f) {
const res: any[] = []
const length = this.length
for (let i = 0; i < length; ++i) {
if (f(this[i], i, this)) res.push(this[i])
}
return res
}
Array.prototype.every = function (f) {
const length = this.length
for (let i = 0; i < length; ++i) {
if (!f(this[i], i, this)) return false
}
return true
}
Array.prototype.fill = function (value, start, end) {
const length = this.length
let startIndex = start ?? 0
if (startIndex < -length) {
startIndex = 0
}
if (startIndex < 0) {
startIndex = startIndex + length
}
let endIndex = end ?? length
if (endIndex >= length) {
endIndex = length
}
if (endIndex < 0) {
endIndex = endIndex + length
}
if (endIndex < -length) {
endIndex = 0
}
for (let i = startIndex; i < endIndex; ++i) {
this[i] = value
}
return this
}
Array.prototype.some = function (f) {
const length = this.length
for (let i = 0; i < length; ++i) {
if (f(this[i], i, this)) return true
}
return false
}
Array.prototype.includes = function (el, fromIndex) {
const length = this.length
const start = fromIndex || 0
for (let i = start; i < length; ++i) {
if (el === this[i]) return true
}
return false
}
Array.prototype.pop = function () {
const length = this.length - 1
if (length < 0) return undefined
const r = this[length]
this.insert(length, -1)
return r
}
Array.prototype.shift = function () {
if (this.length === 0) return undefined
const r = this[0]
this.insert(0, -1)
return r
}
Array.prototype.unshift = function (...elts: any[]) {
this.insert(0, elts.length)
for (let i = 0; i < elts.length; ++i) this[i] = elts[i]
return this.length
}
Array.prototype.indexOf = function (elt, from) {
const length = this.length
if (from == undefined) from = 0
while (from < length) {
if (this[from] === elt) return from
from++
}
return -1
}
Array.prototype.lastIndexOf = function (elt, from) {
if (from == undefined) from = this.length - 1
while (from >= 0) {
if (this[from] === elt) return from
from--
}
return -1
}
Array.prototype.reduce = function (callbackfn: any, initialValue: any) {
const len = this.length
for (let i = 0; i < len; ++i) {
initialValue = callbackfn(initialValue, this[i], i)
}
return initialValue
}
Array.prototype.sort = function <T>(compareFn?: (a: T, b: T) => number) {
if (!compareFn) {
compareFn = (a: any, b: any) => {
a = a + ""
b = b + ""
if (a < b) return -1
else if (a > b) return 1
else return 0
}
}
for (let i = 1; i < this.length; i++) {
const current = this[i]
let j = i - 1
while (j >= 0 && compareFn(this[j], current) > 0) {
this[j + 1] = this[j]
j--
}
this[j + 1] = current
}
return this
}
Array.prototype.keys = function () {
console.log ('KEYS', this.length);
return Array(this.length).fill(0).map((_, i) => i);
}
Array.prototype.reverse = function () {
const len = this.length;
const middle = Math.floor(len / 2);
let lower = 0;
while (lower !== middle) {
const upper = len - lower - 1;
const lowerValue = this[lower];
this[lower] = this[upper];
this[upper] = lowerValue;
lower++;
}
return this;
}
Buffer.prototype.set = function (other: Buffer, trgOff?: number) {
if (!trgOff) trgOff = 0
this.blitAt(trgOff, other, 0, other.length)
}
Buffer.prototype.concat = function (other: Buffer) {
const r = Buffer.alloc(this.length + other.length)
r.set(this)
r.set(other, this.length)
return r
}
Buffer.prototype.slice = function (start?: number, end?: number) {
if (end === undefined) end = this.length
if (start === undefined) start = 0
const len = end - start
if (len <= 0 || start >= this.length) return Buffer.alloc(0)
const r = Buffer.alloc(len)
r.blitAt(0, this, start, len)
return r
}
Buffer.concat = function (...buffers: Buffer[]) {
let size = 0
for (const b of buffers) {
size += b.length
}
const r = Buffer.alloc(size)
size = 0
for (const b of buffers) {
r.blitAt(size, b, 0, b.length)
size += b.length
}
return r
}
Buffer.prototype.lastIndexOf = function (byte, startOffset, endOffset) {
if (endOffset == undefined) endOffset = this.length
return this.indexOf(byte, startOffset, -endOffset)
}