isdict

function isdict( tab ) --> res

Description

Checks if tab is a dictionary table. If tab is not a table returns the function nil and an error message.

Parameters

tab

Table that should be checked.

Return Values

res

Is true if tab is a dictionary table, otherwise false.

Code

--ZFUNC-isdict-v1
local function isdict( tab ) --> res
   if type( tab ) ~= "table" then return nil, "Argument is not a table! It is: "..type( tab ) end

   --check if all the table keys are numerical and count their number
   local count = 0
   for k, v in pairs( tab ) do
      if type( k ) == "number" then count = count + 1 else return true end
   end

   --all keys are numerical. now let's see if they are sequential and start with 1
   for i = 1, count do
      if not tab[ i ] and type( tab[ i ] ) ~= "nil" then return true end
   end

   return false
end

return isdict

Examples

local t = require( "tapered" )
local isdict = require( "isdict" )

t.ok( isdict{ name="rupert pupkin", color="red"  } )
t.nok( isdict{ "a", "b", "c", "d" } )

t.done()

Inspired by

See also