aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEtiene <dalcol@etiene.net>2014-03-12 08:32:45 +0000
committerEtiene <dalcol@etiene.net>2014-03-12 08:32:45 +0000
commit5753af8eb67dc1bddddfa699ae6e1bea7e94e6ec (patch)
tree566e934081197e175672e412a48461292706a119
parent173643e389b88ee34caa62477ea3b15b2fad71e0 (diff)
downloadvalua-5753af8eb67dc1bddddfa699ae6e1bea7e94e6ec.tar.gz
valua-5753af8eb67dc1bddddfa699ae6e1bea7e94e6ec.tar.bz2
valua-5753af8eb67dc1bddddfa699ae6e1bea7e94e6ec.zip
Fixing empty and not_empty for working not just with strings
-rw-r--r--valua-test.lua11
-rw-r--r--valua.lua25
2 files changed, 20 insertions, 16 deletions
diff --git a/valua-test.lua b/valua-test.lua
index 1d53ecf..0291aaf 100644
--- a/valua-test.lua
+++ b/valua-test.lua
@@ -28,16 +28,19 @@ local test_values = {
'29/02/2016',
'a@a.com',
'asd123',
- 5.7
+ 5.7,
+ {},
+ {3,46}
}
local tests = {
{v:new().type("string").len(3,5),{1,false}},
{v:new().type("number").len(3,5), {1,false}},
- {v:new().not_empty(),{2,true,3,false,4,false}},
+ {v:new().type("table").empty(),{15,true,16,false,1,false}},
+ {v:new().not_empty(),{2,true,3,false,4,false,16,true,5,true,6,true}},
{v:new().len(2,10),{2,true}},
{v:new().type("number"),{2,false}},
- {v:new().empty(),{3,true,4,true}},
+ {v:new().empty(),{3,true,4,true,5,false,6,false}},
{v:new().boolean(),{1,false,5,true}},
{v:new().compare("hey"),{1,false,2,true}},
{v:new().number().min(45),{2,false,6,false,7,true}},
@@ -59,4 +62,4 @@ for n,t in ipairs(tests) do
for i = 1, #t[2], 2 do
check(t[1],test_values[t[2][i]],t[2][i+1],n)
end
-end
+end \ No newline at end of file
diff --git a/valua.lua b/valua.lua
index 03fb4c8..e280af5 100644
--- a/valua.lua
+++ b/valua.lua
@@ -22,6 +22,7 @@
local valua = {}
local tinsert,setmetatable,len,match,tonumber = table.insert,setmetatable,string.len,string.match,tonumber
+local next,type,floor,ipairs = next,type,math.floor, ipairs
-- CORE
-- Caution, this is confusing
@@ -74,21 +75,11 @@ end
-- aux funcs
local function empty(v)
- return not v or len(v) == 0
+ return not v or (type(v)=='string' and len(v) == 0) or (type(v)=='table' and not next(v))
end
--
-- String
-function valua._empty(value)
- if not empty(value) then return false,"must be empty" end
- return true
-end
-
-function valua._not_empty(value)
- if empty(value) then return false,"must not be empty" end
- return true
-end
-
function valua._len(value,min,max)
local len = len(value or '')
if len < min or len >max then return false,"should have "..min.."-"..max.." characters" end
@@ -140,7 +131,7 @@ function valua._max(value,n)
end
function valua._integer(value)
- if math.floor(value) ~= value then return false, "must be an integer" end
+ if floor(value) ~= value then return false, "must be an integer" end
return true
end
--
@@ -180,6 +171,16 @@ end
--
-- Abstract
+function valua._empty(value)
+ if not empty(value) then return false,"must be empty" end
+ return true
+end
+
+function valua._not_empty(value)
+ if empty(value) then return false,"must not be empty" end
+ return true
+end
+
function valua._type(value,value_type)
if type(value) ~= value_type then return false,"must be a "..value_type end
return true