Skip to content

Commit

Permalink
Alternate proposal to handle falsy data, it seems like the core probl…
Browse files Browse the repository at this point in the history
…em is that var couldn't handle the top level data object being undefined
  • Loading branch information
jwadhams committed Oct 19, 2020
1 parent 12fbedd commit d17c377
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
node_modules
tests/*.json
yarn-error.log
yarn.lock

# editor and IDE remnants
*~
Expand Down
6 changes: 1 addition & 5 deletions logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html
}
var sub_props = String(a).split(".");
for(var i = 0; i < sub_props.length; i++) {
if(data === null) {
if(data === null || data === undefined) {
return not_found;
}
// Descending into data
Expand Down Expand Up @@ -218,10 +218,6 @@ http://ricostacruz.com/cheatsheets/umdjs.html
return logic;
}

if (data === null || data === undefined) {
data = {};
}

var op = jsonLogic.get_operator(logic);
var values = logic[op];
var i;
Expand Down
6 changes: 6 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ QUnit.test( "logging", function( assert ) {

QUnit.test( "edge cases", function( assert ) {
assert.equal( jsonLogic.apply(), undefined, "Called with no arguments" );

assert.equal( jsonLogic.apply({ var: "" }, 0), 0, "Var when data is 'falsy'" );
assert.equal( jsonLogic.apply({ var: "" }, null), null, "Var when data is null" );
assert.equal( jsonLogic.apply({ var: "" }, undefined), undefined, "Var when data is undefined" );

assert.equal( jsonLogic.apply({ var: ["a", "fallback"] }, undefined), "fallback", "Fallback works when data is a non-object" );
});

QUnit.test( "Expanding functionality with add_operator", function( assert) {
Expand Down

0 comments on commit d17c377

Please sign in to comment.