mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-19 07:56:02 +01:00
feat(array): isArray method
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
---@class Array : OxClass
|
---@class Array : OxClass
|
||||||
local Array = lib.class('Array')
|
local Array = lib.class('Array')
|
||||||
|
|
||||||
|
---@alias ArrayLike<T> Array | { [number]: T }
|
||||||
|
|
||||||
---@private
|
---@private
|
||||||
function Array:constructor(...)
|
function Array:constructor(...)
|
||||||
local arr = { ... }
|
local arr = { ... }
|
||||||
@@ -18,7 +20,7 @@ function Array:__newindex(index, value)
|
|||||||
end
|
end
|
||||||
|
|
||||||
---Create a new array containing the elements from two arrays.
|
---Create a new array containing the elements from two arrays.
|
||||||
---@param arr Array | any[]
|
---@param arr ArrayLike
|
||||||
function Array:merge(arr)
|
function Array:merge(arr)
|
||||||
local newArr = table.clone(self)
|
local newArr = table.clone(self)
|
||||||
local length = #self
|
local length = #self
|
||||||
@@ -167,6 +169,21 @@ function Array:reduce(reducer, initialValue)
|
|||||||
return accumulator
|
return accumulator
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Returns true if the given table is an instance of array or an array-like table.
|
||||||
|
---@param tbl ArrayLike
|
||||||
|
---@return boolean
|
||||||
|
function Array.isArray(tbl)
|
||||||
|
if not type(tbl) == 'table' then return false end
|
||||||
|
|
||||||
|
local tableType = table.type(tbl)
|
||||||
|
|
||||||
|
if tableType == 'array' or tableType == 'empty' or Array.instanceOf(tbl, Array) then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
lib.array = Array
|
lib.array = Array
|
||||||
|
|
||||||
return lib.array
|
return lib.array
|
||||||
|
|||||||
@@ -3,13 +3,13 @@
|
|||||||
---
|
---
|
||||||
---Provides non-neglibible performance gains due to msgpacking all arguments _once_, instead of per-target.
|
---Provides non-neglibible performance gains due to msgpacking all arguments _once_, instead of per-target.
|
||||||
---@param eventName string
|
---@param eventName string
|
||||||
---@param targetIds number | number[]
|
---@param targetIds number | ArrayLike<number>
|
||||||
---@param ... any
|
---@param ... any
|
||||||
function lib.triggerClientEvent(eventName, targetIds, ...)
|
function lib.triggerClientEvent(eventName, targetIds, ...)
|
||||||
local payload = msgpack.pack_args(...)
|
local payload = msgpack.pack_args(...)
|
||||||
local payloadLen = #payload
|
local payloadLen = #payload
|
||||||
|
|
||||||
if type(targetIds) == 'table' and table.type(targetIds) == 'array' then
|
if lib.array.isArray(targetIds) then
|
||||||
for i = 1, #targetIds do
|
for i = 1, #targetIds do
|
||||||
TriggerClientEventInternal(eventName, targetIds[i] --[[@as string]], payload, payloadLen)
|
TriggerClientEventInternal(eventName, targetIds[i] --[[@as string]], payload, payloadLen)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user