From 8e0500327246df8fa7c59ae38dd74b7e0932ff2a Mon Sep 17 00:00:00 2001 From: "Joseph D. Purcell" Date: Sat, 21 Jan 2023 21:03:51 -0500 Subject: [PATCH 1/2] Add is_operation tests --- tests/tests.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/tests.js b/tests/tests.js index 96798f51..99343b2a 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -307,4 +307,48 @@ QUnit.module('basic', () => { jsonLogic.apply({"or": [{"push": [true]}, {"push": [true]}]}); assert.deepEqual(i, [true]); }); + + QUnit.test( "Expanding functionality with is_operation ", function(assert) { + // Operations that are set directly on the operations variable. + var declaredOperations = ["!!", "!", "!=", "!==", "%", "*", "+", "-", "/", "<", "<=", "==", "===", ">", ">=", "cat", "in", "log", "max", "merge", "min", "missing", "missing_some", "substr", "var"]; + declaredOperations.forEach((operationName) => { + assert.equal(jsonLogic.is_operation(operationName), true, `is_operation should consider this an operation but it is NOT: ${operationName}`); + }); + + // Operations that are NOT set directly on the operations variable. + var undeclaredOperations = ["if", "?:", "and", "or", "filter", "map", "reduce", "all", "none", "some", "@"]; + undeclaredOperations.forEach((operationName) => { + assert.equal(jsonLogic.is_operation(operationName), true, `is_operation should consider this an operation but it is NOT: ${operationName}`); + }); + + // Handle all the absurd inputs. + var notOperations = [[], undefined, null, NaN, '', true, false, {}, {"var":"value"}, new Error(), Error]; + notOperations.forEach((operationName) => { + assert.equal(jsonLogic.is_operation(operationName), false, `is_operation should NOT consider this an operation but it is: ${operationName}`); + }); + + // A custom operation should not exist before it should, then it should exist. + var custom_operation = function(a) { + return a; + }; + assert.equal(jsonLogic.is_operation('custom_operation'), false, 'is_operation should NOT consider this an operation but it is: custom_operation'); + jsonLogic.add_operation("custom_operation", custom_operation); + assert.equal(jsonLogic.is_operation('custom_operation'), true, 'is_operation should consider this an operation but it is NOT: custom_operation'); + + // A custom operation using dot notation should not exist before it should, + // then it should exist. And, non-existing dot notation operations should + // not be considered operations. + var nested_operation = function(a) { + return a; + }; + nested_operation.sub_operation = function(a) { + return a; + }; + assert.equal(jsonLogic.is_operation('nested_operation'), false, 'is_operation should NOT consider this an operation but it is: nested_operation'); + assert.equal(jsonLogic.is_operation('nested_operation.sub_operation'), false, 'is_operation should NOT consider this an operation but it is: nested_operation.sub_operation'); + jsonLogic.add_operation("nested_operation", nested_operation); + assert.equal(jsonLogic.is_operation('nested_operation'), true, 'is_operation should consider this an operation but it is NOT: nested_operation'); + assert.equal(jsonLogic.is_operation('nested_operation.sub_operation'), true, 'is_operation should consider this an operation but it is NOT: nested_operation.sub_operation'); + assert.equal(jsonLogic.is_operation('nested_operation.not_an_operation'), false, 'is_operation should NOT consider this an operation but it is: nested_operation.not_an_operation'); + }); }); From beef06ae83c796d987c2043689730b4b95468523 Mon Sep 17 00:00:00 2001 From: "Joseph D. Purcell" Date: Sat, 21 Jan 2023 21:05:16 -0500 Subject: [PATCH 2/2] Add is_operation --- logic.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/logic.js b/logic.js index 27670879..e971fc82 100644 --- a/logic.js +++ b/logic.js @@ -181,6 +181,29 @@ http://ricostacruz.com/cheatsheets/umdjs.html ); }; + jsonLogic.is_operation = function(op) { + if (typeof op !== "string") { + return false; + } + if (operations.hasOwnProperty(op) && typeof operations[op] === "function") { + return true; + } else if (["if", "?:", "and", "or", "filter", "map", "reduce", "all", "none", "some", "@"].indexOf(op) !== -1) { + return true; + } else if (op.indexOf(".") > 0) { + var sub_ops = String(op).split("."); + var operation = operations; + for (var i = 0; i < sub_ops.length; i++) { + if (!operation.hasOwnProperty(sub_ops[i])) { + return false; + } + // Descending into operations + operation = operation[sub_ops[i]]; + } + return true; + } + return false; + }; + /* This helper will defer to the JsonLogic spec as a tie-breaker when different language interpreters define different behavior for the truthiness of primitives. E.g., PHP considers empty arrays to be falsy, but Javascript considers them to be truthy. JsonLogic, as an ecosystem, needs one consistent answer.