Skip to content

Commit 045a7fd

Browse files
authored
Update test framework [fixes npm audit] (#104)
* efficiency: short-circuit some and none, fewer not needed intermediate objects * update test framework
1 parent c1dd82f commit 045a7fd

File tree

5 files changed

+308
-302
lines changed

5 files changed

+308
-302
lines changed

Diff for: gulpfile.js

-18
This file was deleted.

Diff for: logic.js

+26-8
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ http://ricostacruz.com/cheatsheets/umdjs.html
220220
var current;
221221
var scopedLogic;
222222
var scopedData;
223-
var filtered;
224223
var initial;
225224

226225
// easy syntax for unary operators, like {"var" : "x"} instead of strict {"var" : ["x"]}
@@ -314,7 +313,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html
314313
scopedData = jsonLogic.apply(values[0], data);
315314
scopedLogic = values[1];
316315
// All of an empty set is false. Note, some and none have correct fallback after the for loop
317-
if ( ! scopedData.length) {
316+
if ( ! Array.isArray(scopedData) || ! scopedData.length) {
318317
return false;
319318
}
320319
for (i=0; i < scopedData.length; i+=1) {
@@ -324,11 +323,31 @@ http://ricostacruz.com/cheatsheets/umdjs.html
324323
}
325324
return true; // All were truthy
326325
} else if (op === "none") {
327-
filtered = jsonLogic.apply({filter: values}, data);
328-
return filtered.length === 0;
326+
scopedData = jsonLogic.apply(values[0], data);
327+
scopedLogic = values[1];
328+
329+
if ( ! Array.isArray(scopedData) || ! scopedData.length) {
330+
return true;
331+
}
332+
for (i=0; i < scopedData.length; i+=1) {
333+
if ( jsonLogic.truthy( jsonLogic.apply(scopedLogic, scopedData[i]) )) {
334+
return false; // First truthy, short circuit
335+
}
336+
}
337+
return true; // None were truthy
329338
} else if (op === "some") {
330-
filtered = jsonLogic.apply({filter: values}, data);
331-
return filtered.length > 0;
339+
scopedData = jsonLogic.apply(values[0], data);
340+
scopedLogic = values[1];
341+
342+
if ( ! Array.isArray(scopedData) || ! scopedData.length) {
343+
return false;
344+
}
345+
for (i=0; i < scopedData.length; i+=1) {
346+
if ( jsonLogic.truthy( jsonLogic.apply(scopedLogic, scopedData[i]) )) {
347+
return true; // First truthy, short circuit
348+
}
349+
}
350+
return false; // None were truthy
332351
}
333352

334353
// Everyone else gets immediate depth-first recursion
@@ -346,7 +365,6 @@ http://ricostacruz.com/cheatsheets/umdjs.html
346365
var sub_ops = String(op).split(".");
347366
var operation = operations;
348367
for (i = 0; i < sub_ops.length; i++) {
349-
350368
if (!operation.hasOwnProperty(sub_ops[i])) {
351369
throw new Error("Unrecognized operation " + op +
352370
" (failed at " + sub_ops.slice(0, i+1).join(".") + ")");
@@ -377,7 +395,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html
377395
collection.push(values[0]);
378396
} else {
379397
// Recursion!
380-
values.map(function(val) {
398+
values.forEach(function(val) {
381399
collection.push.apply(collection, jsonLogic.uses_data(val) );
382400
});
383401
}

Diff for: package.json

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
"directories": {
77
"test": "tests"
88
},
9-
"dependencies": {},
109
"devDependencies": {
11-
"eslint": "^7.11.0",
10+
"eslint": "^7.32.0",
1211
"eslint-config-google": "^0.14.0",
13-
"gulp": "^3.9.0",
14-
"qunit": "^0.7.7",
15-
"request": "^2.65.0"
12+
"qunit": "^2.16.0"
1613
},
1714
"scripts": {
18-
"test": "gulp test"
15+
"test": "qunit ./tests/tests.js"
1916
},
2017
"repository": {
2118
"type": "git",

Diff for: tests/testrunner.js

-17
This file was deleted.

0 commit comments

Comments
 (0)