Skip to content

Commit

Permalink
Merge pull request #89 from jwadhams/hotfix/handle-falsy-data
Browse files Browse the repository at this point in the history
Handle falsy data
  • Loading branch information
jwadhams authored Oct 20, 2020
2 parents a15f528 + d17c377 commit 28c28ed
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 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
4 changes: 1 addition & 3 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,8 +218,6 @@ http://ricostacruz.com/cheatsheets/umdjs.html
return logic;
}

data = 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 28c28ed

Please sign in to comment.