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

Alias Support #95

Open
wants to merge 5 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
node_modules
tests/*.json
# tests/*.json
yarn-error.log
yarn.lock

Expand Down
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
# json-logic-js
# json-logic-js-graphql


This package adds support for GraphQL Query variables! Operators like `>` `===` etc doent supported as keys in GraphQL Query

## Operation Alias:

| Name | Operation |
|- |- |
| equal | == |
| strictEqual | === |
| notEqual | != |
| strictNotEqual | !== |
| greater | > |
| greaterOrEqual | >= |
| less | < |
| lessOrEqual | <= |
| booleanCasting | !! |
| not | ! |
| modulo | % |
| add | + |
| multiply | * |
| substract | - |
| divide | / |


Original Readme:

This parser accepts [JsonLogic](http://jsonlogic.com) rules and executes them in JavaScript.

Expand Down
28 changes: 28 additions & 0 deletions logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,34 @@ http://ricostacruz.com/cheatsheets/umdjs.html
},
};

var alias = {
equal: '==',
strictEqual: '===',
notEqual: '!=',
strictNotEqual: '!==',
greater: '>',
greaterOrEqual: '>=',
less: '<',
lessOrEqual: '<=',
booleanCasting: '!!',
not: '!',
modulo: '%',
add: '+',
multiply: '*',
substract: '-',
divide: '/',
}

for (var key in alias) {
if (alias.hasOwnProperty(key)) {
operations[key] = operations[alias[key]]
}
}

jsonLogic.list_operators = function() {
return Object.keys(operations)
};

jsonLogic.is_logic = function(logic) {
return (
typeof logic === "object" && // An object
Expand Down
82 changes: 82 additions & 0 deletions tests/rule_like.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
[
"Pattern checks for type",
[ 1, "number", true ],
[ "falafel", "number", false ],
[ [], "number", false ],
[ {"var":"a"}, "number", false],

[ 1, "string", false ],
[ "falafel", "string", true ],
[ [], "string", false],
[ {"var":"a"}, "string", false],

[ 1, "array", false ],
[ "falafel", "array", false],
[ [], "array", true],
[ [1], "array", true],
[ [1,2], "array", true],
[ {"var":"a"}, "array", false],

"Wildcards",
[ 1, "@", true ],
[ "falafel", "@", true],
[ [], "@", true],
[ {"var":"a"}, "@", true],
[ {"cat":"falafel"}, {"@":"falafel"}, true],
[ {"cat":"kebab"}, {"@":"falafel"}, false],
[ {"cat":"kebab"}, {"@":"@"}, true],


"Pattern literally matches a primitive in the rule",
[1,1,true],
[1,2,false],
[1,"falafel", false],
["falafel", "falafel", true],
["falafel", "kebab", false],

"Array content matches",
[ [1,2,3], [1,2,3], true],
[ [1,2,69], [1,2,3], false],
"Array order matters",
[ [1,2,3], [3,2,1], false],

"Arrays of types",
[ [1], ["number"], true],
[ [1], ["string"], false],
[ ["falafel"], ["string"], true],
[ [1,"falafel",[]], ["number","string","array"], true],

"Taxes, rules of different specificity",
[
{"*" : [0.01, {"var":"goods"}]},
{"*":["number", "@"]},
true
],
[
{"*" : [0.01, {"var":"goods"}]},
{"*":["number", {"@":"@"}]},
true
],
[
{"*" : [0.01, {"var":"goods"}]},
{"*":["number", {"var":"@"}]},
true
],
[
{"*" : [0.01, 5000]},
{"*":["number", {"var":"@"}]},
false
],
[
{"*" : [0.01, {"+":[{"var":"goods"}, {"var":"services"}]}]},
{"*":["number", {"+":"@"}]},
true
],
[
{"*" : [0.01, {"+":[{"var":"goods"}, {"var":"services"}]}]},
{"*":["number", {"+":"array"}]},
true
],

"EOF, I like this so I don't have to think about trailing commas"
]
8 changes: 4 additions & 4 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var jsonLogic = require("../logic.js");
var http = require("http");
var https = require("https");
var fs = require("fs");

var download = function(url, dest, cb) {
var file = fs.createWriteStream(dest);
http.get(url, function(response) {
https.get(url, function(response) {
response.pipe(file);
file.on("finish", function() {
file.close(cb); // close() is async, call cb after close completes.
Expand Down Expand Up @@ -57,7 +57,7 @@ var remote_or_cache = function(remote_url, local_file, description, runner) {
};

remote_or_cache(
"http://jsonlogic.com/tests.json",
"https://jsonlogic.com/tests.json",
"tests.json",
"applies() tests",
function(test) {
Expand All @@ -76,7 +76,7 @@ remote_or_cache(
);

remote_or_cache(
"http://jsonlogic.com/rule_like.json",
"https://jsonlogic.com/rule_like.json",
"rule_like.json",
"rule_like() tests",
function(test) {
Expand Down
Loading