Browse Source

added isArray and typeof

tags/0.4.0^2
rucciva noneedtoknow 6 months ago
parent
commit
9281f39729
2 changed files with 99 additions and 19 deletions
  1. 29
    19
      logic.lua
  2. 70
    0
      spec/logic_spec.lua

+ 29
- 19
logic.lua View File

@@ -447,7 +447,7 @@ operations['missing'] = function(closure, ...)
return missing
end

operations['missing_some'] = function(closure, minimum, keys)
operations['missingSome'] = function(closure, minimum, keys)
local missing = operations.missing(closure, unpack(keys))
if #keys - #missing >= minimum then
return closure.opts.array()
@@ -493,6 +493,25 @@ operations['length'] = function(_, obj)
return 0
end

operations['typeof'] = function(_, v)
local t = type(v)
if t == 'nil' then
return 'undefined'
end
if t == 'table' then
return 'object'
end
return t
end

operations['isArray'] = function(closure, v)
return closure.opts.is_array(v)
end

-- snake-case alias to be compatible with original json logic
operations['missing_some'] = operations['missingSome']
operations['is_array'] = operations['isArray']

local function get_operator(tab)
for k, _ in pairs(tab) do
return k
@@ -562,8 +581,7 @@ local recurser = {}
-- If the first evaluates falsy, jump to the next pair (e.g, 0,1 to 2,3)
-- given one parameter, evaluate and return it. (it's an Else and all the If/ElseIf were false)
-- given 0 parameters, return NULL (not great practice, but there was no Else)
recurser['if'] =
function(stack, closure, last_child_result)
recurser['if'] = function(stack, closure, last_child_result)
local op = get_operator(closure.logic)

-- zero or one length
@@ -611,8 +629,7 @@ end

recurser['?:'] = recurser['if']

recurser['and'] =
function(stack, closure, last_child_result)
recurser['and'] = function(stack, closure, last_child_result)
local op = get_operator(closure.logic)

-- zero length
@@ -657,8 +674,7 @@ recurser['and'] =
return closure, last_child_result
end

recurser['or'] =
function(stack, closure, last_child_result)
recurser['or'] = function(stack, closure, last_child_result)
local op = get_operator(closure.logic)

-- zero length
@@ -704,8 +720,7 @@ recurser['or'] =
return closure, last_child_result
end

recurser['filter'] =
function(stack, closure, last_child_result)
recurser['filter'] = function(stack, closure, last_child_result)
local op = get_operator(closure.logic)

-- zero length
@@ -770,8 +785,7 @@ recurser['filter'] =
return closure, last_child_result
end

recurser['map'] =
function(stack, closure, last_child_result)
recurser['map'] = function(stack, closure, last_child_result)
local op = get_operator(closure.logic)

-- zero length
@@ -838,8 +852,7 @@ recurser['map'] =
return closure, last_child_result
end

recurser['reduce'] =
function(stack, closure, last_child_result)
recurser['reduce'] = function(stack, closure, last_child_result)
local op = get_operator(closure.logic)
-- zero length
if #closure.logic[op] == 0 then
@@ -909,8 +922,7 @@ recurser['reduce'] =
return closure, last_child_result
end

recurser['all'] =
function(stack, closure, last_child_result)
recurser['all'] = function(stack, closure, last_child_result)
local op = get_operator(closure.logic)

-- zero length
@@ -976,8 +988,7 @@ recurser['all'] =
return closure, last_child_result
end

recurser['some'] =
function(stack, closure, last_child_result)
recurser['some'] = function(stack, closure, last_child_result)
local op = get_operator(closure.logic)

-- zero length
@@ -1042,8 +1053,7 @@ recurser['some'] =
return closure, last_child_result
end

recurser['none'] =
function(stack, closure, last_child_result)
recurser['none'] = function(stack, closure, last_child_result)
local op = get_operator(closure.logic)
-- zero length
if #closure.logic[op] == 0 then

+ 70
- 0
spec/logic_spec.lua View File

@@ -1007,6 +1007,76 @@ describe(
end
)

describe("json-logic 'typeof' test", function()
local function logic_test(test_table)
for i, t in ipairs(test_table) do
local res = logic_apply({typeof = t.params}, t.data)
assert.message('failed at index: ' .. i).are.same(t.expected, res)
end
end
describe(
'given a value',
function()
local test_table = {
{params = array( 37 ), expected = 'number'},
{params = array( 0/0 ), expected = 'number'},
{params = array( '' ), expected = 'string'},
{params = array( 'bla' ), expected = 'string'},
{params = array( true ), expected = 'boolean'},
{params = array( false ), expected = 'boolean'},
{params = array( undefined ), expected = 'undefined'},
{params = array( undeclaredVariable ), expected = 'undefined'},
{params = array( {a= 1} ), expected = 'object'},
{params = array( array(1) ), expected = 'object'},
{params = array( function()end ), expected = 'function'},
}
it(
'should tell the type of the value',
function()
logic_test(test_table)
end
)
end
)
end)

describe("json-logic 'isArray' or 'is_array' test", function()
local function logic_test(test_table)
for i, t in ipairs(test_table) do
local res = logic_apply({isArray = t.params}, t.data)
assert.message('failed at index: ' .. i).are.same(t.expected, res)

res = logic_apply({is_array = t.params}, t.data)
assert.message('failed at index: ' .. i).are.same(t.expected, res)
end
end
describe(
'given a value',
function()
local test_table = {
{params = array( 0 ), expected = false},
{params = array( '' ), expected = false},
{params = array( 'bla' ), expected = false},
{params = array( {} ), expected = false},
{params = array( {1,2,3} ), expected = false},
{params = array( {a= 1} ), expected = false},
{params = array( true ), expected = false},
{params = array( false ), expected = false},
{params = array( undefined ), expected = false},
{params = array( undeclaredVariable ), expected = false},
{params = array( array(4,5,6) ), expected = true},
{params = array( array(4,5,array(1,2,3)) ), expected = true},
}
it(
'should tell the type of the value',
function()
logic_test(test_table)
end
)
end
)
end)

describe(
"json-logic 'join' test",
function()

Loading…
Cancel
Save