Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit 7566f8a

Browse files
committed
fix: broken tests
1 parent 21bb46d commit 7566f8a

File tree

10 files changed

+89
-21
lines changed

10 files changed

+89
-21
lines changed

src/createJsonLogic.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,25 @@ import isArray from './helpers/isArray';
22
import isLogic from './helpers/isLogic';
33
import getOperator from './helpers/getOperator';
44

5-
function createJsonLogic(operations = {}, visitors = {}) {
6-
Object.keys(operations).forEach(function(name) {
7-
const operation = operations[name];
5+
function createJsonLogic(_operations, _visitors) {
6+
const operations = {};
7+
const visitors = {};
88

9-
addOperation(operation.code || name , operation);
10-
});
9+
if (_operations) {
10+
Object.keys(_operations).forEach(function(name) {
11+
const operation = _operations[name];
1112

12-
Object.keys(visitors).forEach(function(name) {
13-
const visitor = visitors[name];
13+
addOperation(operation.code || name , operation);
14+
});
15+
}
1416

15-
addVisitor(visitor.code || name , visitor);
16-
});
17+
if (_visitors) {
18+
Object.keys(_visitors).forEach(function (name) {
19+
const visitor = _visitors[name];
20+
21+
addVisitor(visitor.code || name, visitor);
22+
});
23+
}
1724

1825
function addOperation(name, code) {
1926
operations[name] = code;
@@ -25,7 +32,8 @@ function createJsonLogic(operations = {}, visitors = {}) {
2532

2633
function addVisitor(name, code) {
2734
if (isArray(name)) {
28-
name.forEach(addVisitor);
35+
name.forEach((key) => addVisitor(key, code));
36+
return;
2937
}
3038

3139
visitors[name] = code;
@@ -34,6 +42,7 @@ function createJsonLogic(operations = {}, visitors = {}) {
3442
function removeVisitor(name) {
3543
if (isArray(name)) {
3644
name.forEach(removeVisitor);
45+
return;
3746
}
3847

3948
delete visitors[name];
@@ -75,8 +84,13 @@ function createJsonLogic(operations = {}, visitors = {}) {
7584
// The operation is called with "data" bound to its "this" and "values" passed as arguments.
7685
// Structured commands like % or > can name formal arguments while flexible commands (like missing or merge) can operate on the pseudo-array arguments
7786
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
78-
if(typeof operations[op] === "function") {
79-
return operations[op].apply(data, values);
87+
const operator = operations[op];
88+
if(typeof operator === "function") {
89+
if (operator.withApply) {
90+
values.unshift(apply);
91+
}
92+
93+
return operator.apply(data, values);
8094
}else if(op.indexOf(".") > 0) { // Contains a dot, and not in the 0th position
8195
var sub_ops = String(op).split(".");
8296
var operation = operations;

src/helpers/truthy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import isArray from './isArray';
55
66
Spec and rationale here: http://jsonlogic.com/truthy
77
*/
8-
function truthy(a) {
8+
function truthy(value) {
99
if(isArray(value) && value.length === 0) {
1010
return false;
1111
}

src/operations/accessor/missing.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import isArray from '../../helpers/isArray';
22
import variable from './variable'
33

4-
function missing() {
4+
function missing(apply, ...args) {
55
/*
66
Missing can receive many keys as many arguments, like {"missing:[1,2]}
77
Missing can also receive *one* argument that is an array of keys,
@@ -10,11 +10,11 @@ function missing() {
1010
*/
1111

1212
var missing = [];
13-
var keys = isArray(arguments[0]) ? arguments[0] : arguments;
13+
var keys = isArray(args[0]) ? args[0] : args;
1414

1515
for(var i = 0; i < keys.length; i++) {
1616
var key = keys[i];
17-
var value = variable.call(this, {"var": key});
17+
var value = apply({"var": key}, this);
1818
if(value === null || value === "") {
1919
missing.push(key);
2020
}
@@ -23,4 +23,6 @@ function missing() {
2323
return missing;
2424
};
2525

26+
missing.withApply = true;
27+
2628
export default missing;
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import missing from './missing';
22

3-
function missingSome(need_count, options) {
3+
function missingSome(apply, need_count, options) {
44
// missing_some takes two arguments, how many (minimum) items must be present, and an array of keys (just like 'missing') to check for presence.
5-
var are_missing = missing.call(this, {"missing": options});
5+
const are_missing = apply({"missing": options}, this);
66

77
if(options.length - are_missing.length >= need_count) {
88
return [];
@@ -12,5 +12,6 @@ function missingSome(need_count, options) {
1212
}
1313

1414
missingSome.code = 'missing_some';
15+
missingSome.withApply = true;
1516

1617
export default missingSome;

src/operations/arithmetic/substract.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ function substract(a, b) {
66
}
77
}
88

9+
substract.code = '-';
10+
911
export default substract;

src/operations/logic/strictNotEqual.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ function strictNotEqual(a, b) {
22
return a !== b;
33
}
44

5-
strictNotEqual.code = '1==';
5+
strictNotEqual.code = '!==';
66

77
export default strictNotEqual;

src/operations/logic/truthy.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
export default from '../../helpers/truthy';
1+
import truthy from '../../helpers/truthy'
2+
3+
truthy.code = '!!';
4+
5+
export default truthy;

src/visitors/accessor/missing.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import isArray from '../../helpers/isArray';
2+
import variable from '../../operations/accessor/variable'
3+
4+
function missing() {
5+
/*
6+
Missing can receive many keys as many arguments, like {"missing:[1,2]}
7+
Missing can also receive *one* argument that is an array of keys,
8+
which typically happens if it's actually acting on the output of another command
9+
(like 'if' or 'merge')
10+
*/
11+
12+
const missing = [];
13+
const keys = isArray(arguments[0]) ? arguments[0] : arguments;
14+
15+
for(let i = 0; i < keys.length; i++) {
16+
const key = keys[i];
17+
const value = variable.call(this, {"var": key}, arguments);
18+
19+
if(value === null || value === "") {
20+
missing.push(key);
21+
}
22+
}
23+
24+
return missing;
25+
};
26+
27+
export default missing;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import missing from './missing';
2+
3+
function missingSome(need_count, options) {
4+
// missing_some takes two arguments, how many (minimum) items must be present, and an array of keys (just like 'missing') to check for presence.
5+
var are_missing = missing.call(this, {"missing": options});
6+
7+
if(options.length - are_missing.length >= need_count) {
8+
return [];
9+
}else{
10+
return are_missing;
11+
}
12+
}
13+
14+
missingSome.code = 'missing_some';
15+
16+
export default missingSome;

src/visitors/logic/condition.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import truthy from '../../helpers/truthy';
22

33
function condition(apply, data, values) {
4+
let i;
5+
46
/* 'if' should be called with a odd number of parameters, 3 or greater
57
This works on the pattern:
68
if( 0 ){ 1 }else{ 2 };
@@ -14,7 +16,7 @@ function condition(apply, data, values) {
1416
given one parameter, evaluate and return it. (it's an Else and all the If/ElseIf were false)
1517
given 0 parameters, return NULL (not great practice, but there was no Else)
1618
*/
17-
for(let i = 0; i < values.length - 1; i += 2) {
19+
for(i = 0; i < values.length - 1; i += 2) {
1820
if( truthy( apply(values[i], data) ) ) {
1921
return apply(values[i+1], data);
2022
}

0 commit comments

Comments
 (0)