-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy patharray.brs
266 lines (187 loc) · 6.92 KB
/
array.brs
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
'
' array.brs
'
'
function ArrayUtil() as Object
util = {
isArray: function(arr) as Boolean
return type(arr) = "roArray"
end function
contains: function(arr as Object, element as Dynamic) as Boolean
return m.indexOf(arr, element) >= 0
end function
indexOf: function(arr as Object, element as Dynamic) as Integer
if not m.isArray(arr) then return -1
size = arr.count()
if size = 0 then return -1
for i = 0 to size - 1
if arr[i] = element then return i
end for
return -1
end function
lastIndexOf: function(arr as Object, element as Dynamic) as Integer
if not m.isArray(arr) then return -1
size = arr.count()
if size = 0 then return -1
for i = size - 1 to 0 step -1
if arr[i] = element then return i
end for
return -1
end function
slice: function(arr as Object, fromIndex = 0 as Integer, toIndex=invalid as Dynamic)
if not m.isArray(arr) then return invalid
size = arr.count()
lastIndex = size - 1
slicedArr = []
if fromIndex < 0 then fromIndex = size + fromIndex
if toIndex = invalid then toIndex = lastIndex
if toIndex < 0 then toIndex = size + toIndex
if toIndex >= size then toIndex = lastIndex
if fromIndex >= size or fromIndex > toIndex then return slicedArr
for i = fromIndex to toIndex
slicedArr.push(arr[i])
end for
return slicedArr
end function
fill: function(arr as Object, value as Dynamic, startIndex=0 as Integer, endIndex=invalid as Dynamic)
if not m.isArray(arr) then return invalid
size = arr.count()
lastIndex = size - 1
filledArr = []
if size = 0 then return arr
if startIndex < 0 then startIndex = 0
if startIndex > lastIndex then startIndex = lastIndex
if endIndex = invalid then endIndex = lastIndex
if endIndex < startIndex then endIndex = startIndex
for i = 0 to lastIndex
if i >= startIndex and i <= endIndex then
filledArr.push(value)
else
filledArr.push(arr[i])
end if
end for
return filledArr
end function
flat: function(arr as Object, depth = 1 as Integer)
if not m.isArray(arr) then return invalid
size = arr.count()
if size = 0 then return arr
flattenArr = []
for each item in arr
if m.isArray(item) then
if depth > 1 then
flattenArr.append(m.flat(item, depth - 1))
else
flattenArr.append(item)
end if
else
flattenArr.push(item)
end if
end for
return flattenArr
end function
map: function(arr as Object, func as Function)
if not m.isArray(arr) then return invalid
size = arr.count()
mappedArr = []
if size = 0 then return mappedArr
for i = 0 to size - 1
mappedArr.push(func(arr[i], i, arr))
end for
return mappedArr
end function
reduce: function(arr as Object, func as Function, initialValue = invalid as Dynamic)
if not m.isArray(arr) then return invalid
size = arr.count()
startAt = 0
accumulator = initialValue
if size = 0 then return accumulator
if accumulator = invalid then
accumulator = arr[0]
startAt = 1
end if
for i = startAt to size - 1
accumulator = func(accumulator, arr[i], i, arr)
end for
return accumulator
end function
filter: function(arr as Object, func as Function)
if not m.isArray(arr) then return invalid
size = arr.count()
mappedArr = []
if size = 0 then return mappedArr
for i = 0 to size - 1
if func(arr[i], i, arr) then
mappedArr.push(arr[i])
end if
end for
return mappedArr
end function
find: function(arr as Object, func as Function)
if not m.isArray(arr) then return invalid
size = arr.count()
if size = 0 then return invalid
for i = 0 to size - 1
if func(arr[i], i, arr) then
return arr[i]
end if
end for
return invalid
end function
findIndex: function(arr as Object, func as Function) as Integer
if not m.isArray(arr) then return -1
size = arr.count()
if size = 0 then return -1
for i = 0 to size - 1
if func(arr[i], i, arr) then
return i
end if
end for
return -1
end function
every: function(arr as Object, func as Function) as Boolean
if not m.isArray(arr) then return true
size = arr.count()
if size = 0 then return true
for i = 0 to size - 1
if func(arr[i], i, arr) = false then
return false
end if
end for
return true
end function
some: function(arr as Object, func as Function) as Boolean
if not m.isArray(arr) then return false
size = arr.count()
if size = 0 then return false
for i = 0 to size - 1
if func(arr[i], i, arr) then
return true
end if
end for
return false
end function
groupBy: function(arr as Object, key as String)
if not m.isArray(arr) then return invalid
size = arr.count()
accumulator = {}
if size = 0 then return accumulator
for i = 0 to size - 1
element = arr[i]
if element = invalid then continue for
keyValue = element[key]
if keyValue = invalid then continue for
groupName = keyValue.toStr()
groupArray = accumulator[groupName]
if m.isArray(groupArray) then
groupArray.push(element)
else
accumulator[groupName] = []
accumulator[groupName].push(element)
end if
end for
return accumulator
end function
}
return util
end function