Browse Source

string lower upper #2

tags/0.5.0^2
rucciva noneedtoknow 4 months ago
parent
commit
cc5861b24c
2 changed files with 44 additions and 0 deletions
  1. 14
    0
      logic.lua
  2. 30
    0
      spec/logic_spec.lua

+ 14
- 0
logic.lua View File

@@ -512,6 +512,20 @@ operations['isArray'] = function(closure, v)
return closure.opts.is_array(v)
end

operations['toUpperCase'] = function(_, v)
if type(v) ~= "string" then
return nil
end
return string.upper(v)
end

operations['toLowerCase'] = function(_, v)
if type(v) ~= "string" then
return nil
end
return string.lower(v)
end

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

+ 30
- 0
spec/logic_spec.lua View File

@@ -1216,6 +1216,36 @@ describe(
end
)

describe(
'json-logic string lower upper test',
function()
local function logic_test( test_table)
for i, t in ipairs(test_table) do
local res = logic_apply(logic.new_logic(t.operator, t.params), t.data)
assert.message('failed at index: ' .. i).are.equal(t.expected, res)
end
end
describe('given a string', function()
local test_table = {
{ operator = "toUpperCase", params = "something", expected = "SOMETHING"},
{ operator = "toUpperCase", params = "SOMETHING", expected = "SOMETHING"},
{ operator = "toUpperCase", params = "SomeThing", expected = "SOMETHING"},
{ operator = "toUpperCase", params = "someTHING", expected = "SOMETHING"},
{ operator = "toUpperCase", params = "sOmEtHiNg", expected = "SOMETHING"},
{ operator = "toLowerCase", params = "something", expected = "something"},
{ operator = "toLowerCase", params = "SOMETHING", expected = "something"},
{ operator = "toLowerCase", params = "SomeThing", expected = "something"},
{ operator = "toLowerCase", params = "someTHING", expected = "something"},
{ operator = "toLowerCase", params = "sOmEtHiNg", expected = "something"},
}
it('should correctly convert all character to uppercase or lowercase', function()
logic_test(test_table)
end)
end
)
end
)

describe(
'json-logic number test',
function()

Loading…
Cancel
Save