2024-04-13 11:15:26 +10:00
---@class Array : OxClass
2024-07-26 15:21:09 +10:00
lib.array = lib.class ( ' Array ' )
2024-04-13 11:15:26 +10:00
2024-04-19 03:09:39 +10:00
---@alias ArrayLike<T> Array | { [number]: T }
2024-04-16 14:15:25 +10:00
---@private
2024-07-26 15:21:09 +10:00
function lib . array : constructor ( ... )
2024-04-13 11:15:26 +10:00
local arr = { ... }
for i = 1 , # arr do
self [ i ] = arr [ i ]
end
end
2024-04-16 14:15:25 +10:00
---@private
2024-07-26 15:21:09 +10:00
function lib . array : __newindex ( index , value )
2024-04-13 11:15:26 +10:00
if type ( index ) ~= ' number ' then error ( ( " Cannot insert non-number index '%s' into an array. " ) : format ( index ) ) end
rawset ( self , index , value )
end
---Create a new array containing the elements from two arrays.
2024-04-19 03:09:39 +10:00
---@param arr ArrayLike
2024-07-26 15:21:09 +10:00
function lib . array : merge ( arr )
2024-04-13 11:15:26 +10:00
local newArr = table.clone ( self )
local length = # self
for i = 1 , # arr do
length += 1
newArr [ length ] = arr [ i ]
end
2024-07-26 15:21:09 +10:00
return lib.array : new ( table.unpack ( newArr ) )
2024-04-13 11:15:26 +10:00
end
---Tests if all elements in an array succeed in passing the provided test function.
---@param testFn fun(element: unknown): boolean
2024-07-26 15:21:09 +10:00
function lib . array : every ( testFn )
2024-04-13 11:15:26 +10:00
for i = 1 , # self do
if not testFn ( self [ i ] ) then
return false
end
end
return true
end
---Creates a new array containing the elements from an array thtat pass the test of the provided function.
---@param testFn fun(element: unknown): boolean
2024-07-26 15:21:09 +10:00
function lib . array : filter ( testFn )
2024-04-13 11:15:26 +10:00
local newArr = { }
local length = 0
for i = 1 , # self do
local element = self [ i ]
if testFn ( element ) then
length += 1
newArr [ length ] = element
end
end
2024-07-26 15:21:09 +10:00
return lib.array : new ( table.unpack ( newArr ) )
2024-04-13 11:15:26 +10:00
end
---Returns the first or last element of an array that passes the provided test function.
---@param testFn fun(element: unknown): boolean
---@param last? boolean
2024-07-26 15:21:09 +10:00
function lib . array : find ( testFn , last )
2024-04-13 11:15:26 +10:00
local a = last and # self or 1
local b = last and 1 or # self
local c = last and - 1 or 1
for i = a , b , c do
local element = self [ i ]
if testFn ( element ) then
return element
end
end
end
---Returns the first or last index of the first element of an array that passes the provided test function.
---@param testFn fun(element: unknown): boolean
---@param last? boolean
2024-07-26 15:21:09 +10:00
function lib . array : findIndex ( testFn , last )
2024-04-13 11:15:26 +10:00
local a = last and # self or 1
local b = last and 1 or # self
local c = last and - 1 or 1
for i = a , b , c do
local element = self [ i ]
if testFn ( element ) then
2024-05-02 08:07:25 +02:00
return i
2024-04-13 11:15:26 +10:00
end
end
end
---Returns the first or last index of the first element of an array that matches the provided value.
---@param value unknown
---@param last? boolean
2024-07-26 15:21:09 +10:00
function lib . array : indexOf ( value , last )
2024-04-13 11:15:26 +10:00
local a = last and # self or 1
local b = last and 1 or # self
local c = last and - 1 or 1
for i = a , b , c do
local element = self [ i ]
if element == value then
return element
end
end
end
---Executes the provided function for each element in an array.
---@param cb fun(element: unknown)
2024-07-26 15:21:09 +10:00
function lib . array : forEach ( cb )
2024-04-13 11:15:26 +10:00
for i = 1 , # self do
cb ( self [ i ] )
end
end
---Concatenates all array elements into a string, seperated by commas or the specified seperator.
---@param seperator? string
2024-07-26 15:21:09 +10:00
function lib . array : join ( seperator )
2024-04-13 11:15:26 +10:00
return table.concat ( self , seperator or ' , ' )
end
---Removes the last element from an array and returns the removed element.
2024-07-26 15:21:09 +10:00
function lib . array : pop ( )
2024-04-13 11:15:26 +10:00
return table.remove ( self )
end
---Adds the given elements to the end of an array and returns the new array length.
---@param ... any
2024-07-26 15:21:09 +10:00
function lib . array : push ( ... )
2024-04-13 11:15:26 +10:00
local elements = { ... }
local length = # self
for i = 1 , # elements do
length += 1
self [ length ] = elements [ i ]
end
return length
end
---Removes the first element from an array and returns the removed element.
2024-07-26 15:21:09 +10:00
function lib . array : shift ( )
2024-04-13 11:15:26 +10:00
return table.remove ( self , 1 )
end
2024-04-16 14:15:25 +10:00
---The "reducer" function is applied to every element within an array, with the previous element's result serving as the accumulator.\
---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.
---@generic T
---@param reducer fun(accumulator: T, currentValue: T, index?: number): T
---@param initialValue? T
---@return T
2024-07-26 15:21:09 +10:00
function lib . array : reduce ( reducer , initialValue )
2024-04-16 14:15:25 +10:00
local initialIndex = initialValue and 1 or 2
local accumulator = initialValue or self [ 1 ]
for i = initialIndex , # self do
accumulator = reducer ( accumulator , self [ i ] , i )
end
return accumulator
end
2024-04-19 03:09:39 +10:00
---Returns true if the given table is an instance of array or an array-like table.
---@param tbl ArrayLike
---@return boolean
2024-07-26 15:21:09 +10:00
function lib . array . isArray ( tbl )
2024-04-19 03:09:39 +10:00
if not type ( tbl ) == ' table ' then return false end
local tableType = table.type ( tbl )
2024-07-26 15:21:09 +10:00
if tableType == ' array ' or tableType == ' empty ' or lib.array . instanceOf ( tbl , lib.array ) then
2024-04-19 03:09:39 +10:00
return true
end
return false
end
2024-04-13 11:15:26 +10:00
return lib.array