Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an is_operation method #123

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
44 changes: 44 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});