refactor(array): typing issue

Assigning a variable to a class seems to break private/protected
access with LLS. Revert this in the future if fixed.
This commit is contained in:
Linden
2024-07-26 15:21:09 +10:00
parent 2b9f49febd
commit 026d3de4ef

View File

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