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
128 lines (113 loc) · 3.06 KB
/
Copy patharray.ts
File metadata and controls
128 lines (113 loc) · 3.06 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
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.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.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
}
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
}