From e84a0c500a2104dc82b8278d7778144351aa71b3 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Fri, 19 Oct 2012 18:35:14 +0200 Subject: update l-number l-boolean --- lualibs-boolean.lua | 66 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 25 deletions(-) (limited to 'lualibs-boolean.lua') diff --git a/lualibs-boolean.lua b/lualibs-boolean.lua index be7ec7d..2b94de7 100644 --- a/lualibs-boolean.lua +++ b/lualibs-boolean.lua @@ -6,36 +6,60 @@ if not modules then modules = { } end modules ['l-boolean'] = { license = "see context related readme files" } -boolean = boolean or { } - local type, tonumber = type, tonumber +boolean = boolean or { } +local boolean = boolean + function boolean.tonumber(b) - if b then return 1 else return 0 end + if b then return 1 else return 0 end -- test and return or return end function toboolean(str,tolerant) - if tolerant then - local tstr = type(str) - if tstr == "string" then - return str == "true" or str == "yes" or str == "on" or str == "1" or str == "t" - elseif tstr == "number" then - return tonumber(str) ~= 0 - elseif tstr == "nil" then - return false - else - return str - end + if str == nil then + return false + elseif str == false then + return false + elseif str == true then + return true + elseif str == "true" then + return true + elseif str == "false" then + return false + elseif not tolerant then + return false + elseif str == 0 then + return false + elseif (tonumber(str) or 0) > 0 then + return true + else + return str == "yes" or str == "on" or str == "t" + end +end + +string.toboolean = toboolean + +function string.booleanstring(str) + if str == nil then + return false + elseif str == false then + return false + elseif str == true then + return true elseif str == "true" then return true elseif str == "false" then return false + elseif str == 0 then + return false + elseif (tonumber(str) or 0) > 0 then + return true else - return str + return str == "yes" or str == "on" or str == "t" end end -function string.is_boolean(str) +function string.is_boolean(str,default) if type(str) == "string" then if str == "true" or str == "yes" or str == "on" or str == "t" then return true @@ -43,13 +67,5 @@ function string.is_boolean(str) return false end end - return nil -end - -function boolean.alwaystrue() - return true -end - -function boolean.falsetrue() - return false + return default end -- cgit v1.2.3 From 44173ba49717a356edf1c09f892f6909cad43481 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Sun, 7 Apr 2013 18:10:32 +0200 Subject: update l-boolean --- lualibs-boolean.lua | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'lualibs-boolean.lua') diff --git a/lualibs-boolean.lua b/lualibs-boolean.lua index 2b94de7..f087f1a 100644 --- a/lualibs-boolean.lua +++ b/lualibs-boolean.lua @@ -15,7 +15,7 @@ function boolean.tonumber(b) if b then return 1 else return 0 end -- test and return or return end -function toboolean(str,tolerant) +function toboolean(str,tolerant) -- global if str == nil then return false elseif str == false then @@ -40,18 +40,16 @@ end string.toboolean = toboolean function string.booleanstring(str) - if str == nil then - return false - elseif str == false then + if str == "0" then return false - elseif str == true then - return true - elseif str == "true" then + elseif str == "1" then return true - elseif str == "false" then + elseif str == "" then return false - elseif str == 0 then + elseif str == "false" then return false + elseif str == "true" then + return true elseif (tonumber(str) or 0) > 0 then return true else -- cgit v1.2.3