summaryrefslogtreecommitdiff
path: root/lualibs-boolean.lua
diff options
context:
space:
mode:
authorPhilipp Gesang <megas.kapaneus@gmail.com>2012-10-19 18:35:14 +0200
committerPhilipp Gesang <megas.kapaneus@gmail.com>2012-10-19 18:35:14 +0200
commite84a0c500a2104dc82b8278d7778144351aa71b3 (patch)
tree2cf9b21cca6c231445d854c9c239ee6d3b731c25 /lualibs-boolean.lua
parent9e40ecb0b88b556a55b7b5bf3b7c58b5a8368501 (diff)
downloadlualibs-e84a0c500a2104dc82b8278d7778144351aa71b3.tar.gz
update l-number l-boolean
Diffstat (limited to 'lualibs-boolean.lua')
-rw-r--r--lualibs-boolean.lua66
1 files changed, 41 insertions, 25 deletions
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