1
0

Better has_uniform_structure.

This commit is contained in:
Jon Michael Aanes 2017-10-22 12:29:43 +02:00
parent ef0a4c82ed
commit a9d4d35fc1

View File

@ -209,13 +209,17 @@ local function has_uniform_structure (t)
-- Find the key and value types. -- Find the key and value types.
local first_key = next(t) local first_key = next(t)
if first_key == nil then return true end if first_key == nil then return true end
local key_type, value_type = type(first_key), type(t[first_key]) local key_type, value_type = type(first_key), type(t[first_key])
local nr_elems = (value_type == 'table') and nr_elements_in_table(t[first_key]) or nil
-- Ensure every other key value pair is the same. -- Ensure every other key value pair is the same.
for key, value in pairs(t) do for key, value in pairs(t) do
if type(key) ~= key_type or type(value) ~= value_type then if type(key) ~= key_type or type(value) ~= value_type then
return false return false
end end
if nr_elems and nr_elems ~= nr_elements_in_table(value) then
return false
end
end end
return true return true