FANDOM


--
-- Array methods
--
-- For use in other modules, not designed for use through #invoke
--
-- @todo implement as an extension of the table class
--
 
local p = {}
 
--
-- Checks the table is an array
--
-- Essentially this just checks every key is a number
-- and that there are no gaps between numbers
--
function p.isArray ( array )
 
    -- check the type is a table or we won't be able to iterate through it
    if type( array) ~= 'table' then
        return false
    end
 
    -- empty array
    if #array == 0 then
        return true
    end
 
    -- check each key is a number
    for k, _ in pairs( array ) do
        if type( k ) ~= 'number' then
            return false
        end
    end
 
    -- check there's no skipped keys
    local i = 1
    while true do
        if array[i] == nil then
            return false
        end
 
        if i == #array then
            break
        end
 
        i = i + 1
    end
 
    return true
 
end
 
--
-- Checks an element exists in an array
--
function p.contains ( array, elem )
 
    if #array == 0 then
        return false
    end
 
    for i = 1, #array do
        if array[i] == elem then
            return true
        end
    end
 
    return false
 
end
 
--
-- Appends the contents of one array to another
--
function p.concat ( array1, array2 )
 
    for i = 1, #array2 do
        array1[#array1 + i] = array2[i]
    end
 
    return array1
 
end
 
--
-- Returns the intersection of two arrays
--
function p.intersection ( array1, array2 )
end
 
--
-- Returns the first index that an elements exists in an array
--
function p.indexOf ( array, elem )
 
    if #array == 0 then
        return nil
    end
 
    for i = 1, #array do
        if array[i] == elem then
            return i
        end
    end
 
    return nil
 
end
 
--
-- Returns the last index an element exists in an array
--
function p.lastIndexOf ( array, elem )
 
    local index
 
    if #array == 0 then
        return nil
    end
 
    for i = 1, #array do
        if array[i] == elem then
            index = i
        end
    end
 
    return index
 
end
 
--
-- Join an array into a string with a separator
--
-- Separator defaults to an empty string
--
function p.join ( array, separator )
 
    local str = ''
    local sep = separator or ''
 
    if #array == 0 then
        return str
    end
 
    for i = 1, #array do
        str = str .. array[i]
 
        if k ~= #array then
            str = str .. sep
        end
    end
 
    return str
 
end
 
--
-- Removes the last element from an array and returns that element
--
function p.pop ( array )
 
    if #array == 0 then
        return nil
    end
 
    local arr = {}
 
    for i = 1, #array - 1 do
        arr[i] = array[i]
    end
 
    return arr, array[#array]
 
end
 
--
-- Removes the first elements from an array and returns that element
--
function p.shift ( array )
 
    if #array == 0 then
        return nil
    end
 
    if #array == 1 then
        return {}, array[1]
    end
 
    local arr = {}
 
    for i = 2, #array do
        arr[i - 1] = array[i]
    end
 
    return arr, array[1]
 
end
 
--
-- Appends an element to an array
--
function p.push ( array, elem )
 
    array[#array + 1] = elem
    return array
 
end
 
--
-- Prepends an element to an array
--
function p.unshift ( array, elem )
 
    local arr = { elem }
 
    for i = 1, #array do
        arr[i + 1] = array[i]
    end
 
    return arr
 
end
 
--
-- Reverses the order of an array
--
function p.reverse ( array )
 
    local arr = {}
    local i = #array
    local j = 1
 
    while i > 0 do
        arr[j] = array[i]
        j = j + 1
        i = i - 1
    end
 
    return arr
 
end
 
--
-- Returns a slice of an array from between the supplied start and finish indexes
--
-- If the finish index is not supplied then it uses the end of the array as the finish index
--
function p.splice ( array, start, finish )
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.