Skip to content
This repository was archived by the owner on Apr 29, 2025. It is now read-only.

Commit a2a7114

Browse files
committed
feat(array): add additional array class methods
- merge now supports merging multiple arrays. - added includes, map, reverse, toReversed.
1 parent 4c460e0 commit a2a7114

File tree

1 file changed

+65
-13
lines changed

1 file changed

+65
-13
lines changed

imports/array/shared.lua

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ function lib.array:__newindex(index, value)
1919
rawset(self, index, value)
2020
end
2121

22-
---Create a new array containing the elements from two arrays.
23-
---@param arr ArrayLike
24-
function lib.array:merge(arr)
22+
---Create a new array containing the elements of two or more arrays.
23+
---@param ... ArrayLike
24+
function lib.array:merge(...)
2525
local newArr = table.clone(self)
2626
local length = #self
27+
local arrays = { ... }
2728

28-
for i = 1, #arr do
29-
length += 1
30-
newArr[length] = arr[i]
29+
for i = 1, #arrays do
30+
local arr = arrays[i]
31+
32+
for j = 1, #arr do
33+
length += 1
34+
newArr[length] = arr[j]
35+
end
3136
end
3237

3338
return lib.array:new(table.unpack(newArr))
@@ -122,12 +127,35 @@ function lib.array:forEach(cb)
122127
end
123128
end
124129

130+
---Determines if a given element exists inside an array.
131+
---@param element unknown The value to find in the array.
132+
---@param fromIndex? number The position in the array to begin searching from.
133+
function lib.array:includes(element, fromIndex)
134+
for i = (fromIndex or 1), #self do
135+
if self[i] == element then return true end
136+
end
137+
138+
return false
139+
end
140+
125141
---Concatenates all array elements into a string, seperated by commas or the specified seperator.
126142
---@param seperator? string
127143
function lib.array:join(seperator)
128144
return table.concat(self, seperator or ',')
129145
end
130146

147+
---Create a new array containing the results from calling the provided function on every element in an array.
148+
---@param cb fun(element: unknown, index: number, array: self): unknown
149+
function lib.array:map(cb)
150+
local arr = {}
151+
152+
for i = 1, #self do
153+
arr[i] = cb(self[i], i, self)
154+
end
155+
156+
return lib.array:new(table.unpack(arr))
157+
end
158+
131159
---Removes the last element from an array and returns the removed element.
132160
function lib.array:pop()
133161
return table.remove(self)
@@ -147,11 +175,6 @@ function lib.array:push(...)
147175
return length
148176
end
149177

150-
---Removes the first element from an array and returns the removed element.
151-
function lib.array:shift()
152-
return table.remove(self, 1)
153-
end
154-
155178
---The "reducer" function is applied to every element within an array, with the previous element's result serving as the accumulator.\
156179
---If an initial value is provided, it's used as the accumulator for index 1; otherwise, index 1 itself serves as the initial value, and iteration begins from index 2.
157180
---@generic T
@@ -169,14 +192,43 @@ function lib.array:reduce(reducer, initialValue)
169192
return accumulator
170193
end
171194

195+
---Reverses the elements inside an array.
196+
function lib.array:reverse()
197+
local i, j = 1, #self
198+
199+
while i < j do
200+
self[i], self[j] = self[j], self[i]
201+
i += 1
202+
j -= 1
203+
end
204+
205+
return self
206+
end
207+
208+
---Removes the first element from an array and returns the removed element.
209+
function lib.array:shift()
210+
return table.remove(self, 1)
211+
end
212+
213+
---Creates a new array with reversed elements from the given array.
214+
function lib.array:toReversed()
215+
local reversed = lib.array:new()
216+
217+
for i = #self, 1, -1 do
218+
reversed:push(self[i])
219+
end
220+
221+
return reversed
222+
end
223+
172224
---Returns true if the given table is an instance of array or an array-like table.
173225
---@param tbl ArrayLike
174226
---@return boolean
175227
function lib.array.isArray(tbl)
176-
if not type(tbl) == 'table' then return false end
177-
178228
local tableType = table.type(tbl)
179229

230+
if not tableType then return false end
231+
180232
if tableType == 'array' or tableType == 'empty' or lib.array.instanceOf(tbl, lib.array) then
181233
return true
182234
end

0 commit comments

Comments
 (0)