Browse Source

string lower upper #2

tags/0.5.0^2
rucciva noneedtoknow 2 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)
512 512
     return closure.opts.is_array(v)
513 513
 end
514 514
 
515
+operations['toUpperCase'] = function(_, v)
516
+    if type(v) ~= "string" then
517
+        return nil
518
+    end
519
+    return string.upper(v)
520
+end
521
+
522
+operations['toLowerCase'] = function(_, v)
523
+    if type(v) ~= "string" then
524
+        return nil
525
+    end
526
+    return string.lower(v)
527
+end
528
+
515 529
 -- snake-case alias to be compatible with original json logic
516 530
 operations['missing_some'] = operations['missingSome']
517 531
 operations['is_array'] = operations['isArray']

+ 30
- 0
spec/logic_spec.lua View File

@@ -1216,6 +1216,36 @@ describe(
1216 1216
     end
1217 1217
 )
1218 1218
 
1219
+describe(
1220
+    'json-logic string lower upper test',
1221
+    function()
1222
+        local function logic_test( test_table)
1223
+            for i, t in ipairs(test_table) do
1224
+                local res = logic_apply(logic.new_logic(t.operator, t.params), t.data)
1225
+                assert.message('failed at index: ' .. i).are.equal(t.expected, res)
1226
+            end
1227
+        end
1228
+        describe('given a string', function()
1229
+            local test_table = {
1230
+                { operator = "toUpperCase", params = "something",  expected = "SOMETHING"},
1231
+                { operator = "toUpperCase", params = "SOMETHING",  expected = "SOMETHING"},
1232
+                { operator = "toUpperCase", params = "SomeThing",  expected = "SOMETHING"},
1233
+                { operator = "toUpperCase", params = "someTHING",  expected = "SOMETHING"},
1234
+                { operator = "toUpperCase", params = "sOmEtHiNg",  expected = "SOMETHING"},
1235
+                { operator = "toLowerCase", params = "something",  expected = "something"},
1236
+                { operator = "toLowerCase", params = "SOMETHING",  expected = "something"},
1237
+                { operator = "toLowerCase", params = "SomeThing",  expected = "something"},
1238
+                { operator = "toLowerCase", params = "someTHING",  expected = "something"},
1239
+                { operator = "toLowerCase", params = "sOmEtHiNg",  expected = "something"},
1240
+            }
1241
+            it('should correctly convert all character to uppercase or lowercase', function()
1242
+                logic_test(test_table)
1243
+            end)
1244
+        end
1245
+    )
1246
+    end
1247
+)
1248
+
1219 1249
 describe(
1220 1250
     'json-logic number test',
1221 1251
     function()

Loading…
Cancel
Save