Use 'pairs' instead of 'ipairs' so that functions don't break on lists with nil, fix various small bugs
This commit is contained in:
parent
153f30f381
commit
64fd49e33b
11 changed files with 62 additions and 25 deletions
|
@ -1,5 +1,5 @@
|
||||||
function all(list)
|
function all(list)
|
||||||
for _, value in ipairs(list) do
|
for _, value in pairs(list) do
|
||||||
if (value == nil) then
|
if (value == nil) then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
function containedIn(list, a)
|
function containedIn(list, a)
|
||||||
for _, value in ipairs(list) do
|
for _, value in pairs(list) do
|
||||||
if (value == a) then
|
if (value == a) then
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
function first_match_of(tags, result, order_of_keys, table)
|
function first_match_of(tags, result, order_of_keys, table)
|
||||||
for _, key in ipairs(order_of_keys) do
|
for _, key in pairs(order_of_keys) do
|
||||||
local v = tags[key]
|
local v = tags[key]
|
||||||
if (v ~= nil) then
|
if (v ~= nil) then
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ function head(ls)
|
||||||
if(ls == nil) then
|
if(ls == nil) then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
for _, v in ipairs(ls) do
|
for _, v in pairs(ls) do
|
||||||
if(v ~= nil) then
|
if(v ~= nil) then
|
||||||
return v
|
return v
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,22 @@
|
||||||
|
function applyIfNeeded(f, arg)
|
||||||
|
if(f == nil) then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
if(type(f) == "function") then
|
||||||
|
return f(arg)
|
||||||
|
else
|
||||||
|
return f
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function if_then_else_dotted(conditionf, thnf, elsef, arg)
|
function if_then_else_dotted(conditionf, thnf, elsef, arg)
|
||||||
local condition = conditionf(arg);
|
local condition = applyIfNeeded(conditionf, arg);
|
||||||
if (condition) then
|
if (condition) then
|
||||||
return thnf(arg)
|
return applyIfNeeded(thnf, arg)
|
||||||
else
|
else
|
||||||
if(elsef == nil) then
|
if(elsef == nil) then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
return elsef(arg) -- if no third parameter is given, 'els' will be nil
|
return applyIfNeeded(elsef, arg) -- if no third parameter is given, 'els' will be nil
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -1,10 +1,12 @@
|
||||||
function max(list)
|
function max(list)
|
||||||
local max
|
local max
|
||||||
for _, value in ipairs(list) do
|
for _, value in pairs(list) do
|
||||||
if (max == nil) then
|
if (value == nil) then
|
||||||
max = value
|
if (max == nil) then
|
||||||
elseif (max < value) then
|
max = value
|
||||||
max = value
|
elseif (max < value) then
|
||||||
|
max = value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,12 @@
|
||||||
function min(list)
|
function min(list)
|
||||||
local min
|
local min
|
||||||
for _, value in ipairs(list) do
|
for _, value in pairs(list) do
|
||||||
if (min == nil) then
|
if(value ~= nil) then
|
||||||
min = value
|
if (min == nil) then
|
||||||
elseif (min > value) then
|
min = value
|
||||||
min = value
|
elseif (value < min) then
|
||||||
|
min = value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
function multiply(list)
|
function multiply(list)
|
||||||
local factor = 1
|
local factor = 1
|
||||||
for _, value in ipairs(list) do
|
for _, value in pairs(list) do
|
||||||
factor = factor * value
|
if (value ~= nil) then
|
||||||
|
factor = factor * value
|
||||||
|
end
|
||||||
end
|
end
|
||||||
return factor;
|
return factor;
|
||||||
end
|
end
|
|
@ -22,6 +22,15 @@ function parse(string)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if(string:match("%d*:%d*")) then
|
||||||
|
-- duration in minute
|
||||||
|
local duration = 0
|
||||||
|
for part in string:gmatch "%d*" do
|
||||||
|
duration = duration * 60 + tonumber(part)
|
||||||
|
end
|
||||||
|
return duration
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
return tonumber(string)
|
return tonumber(string)
|
||||||
end
|
end
|
|
@ -1,10 +1,12 @@
|
||||||
function sum(list)
|
function sum(list)
|
||||||
local sum = 1
|
local sum = 0
|
||||||
for _, value in ipairs(list) do
|
for _, value in pairs(list) do
|
||||||
if(value == 'yes' or value == 'true') then
|
if(value ~= nil) then
|
||||||
value = 1
|
if(value == 'yes' or value == 'true') then
|
||||||
|
value = 1
|
||||||
|
end
|
||||||
|
sum = sum + value
|
||||||
end
|
end
|
||||||
sum = sum + value
|
|
||||||
end
|
end
|
||||||
return sum;
|
return sum;
|
||||||
end
|
end
|
|
@ -1,8 +1,17 @@
|
||||||
failed_tests = false
|
failed_tests = false
|
||||||
function unit_test(f, fname, index, expected, parameters, tags)
|
function unit_test(f, fname, index, expected, parameters, tags)
|
||||||
|
if (f == nil) then
|
||||||
|
print("Trying to unit test " .. fname .. " but this function is not defined")
|
||||||
|
failed_tests = true
|
||||||
|
return
|
||||||
|
end
|
||||||
local result = {attributes_to_keep = {}}
|
local result = {attributes_to_keep = {}}
|
||||||
local actual = f(parameters, tags, result)
|
local actual = f(parameters, tags, result)
|
||||||
if (tostring(actual) ~= expected) then
|
if(expected == "null" and actual == nil) then
|
||||||
|
-- OK!
|
||||||
|
elseif(tonumber(actual) and tonumber(expected) and tonumber(actual) == tonumber(expected)) then
|
||||||
|
-- OK!
|
||||||
|
elseif (tostring(actual) ~= expected) then
|
||||||
print("[" .. fname .. "] " .. index .. " failed: expected " .. expected .. " but got " .. tostring(actual))
|
print("[" .. fname .. "] " .. index .. " failed: expected " .. expected .. " but got " .. tostring(actual))
|
||||||
failed_tests = true
|
failed_tests = true
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue