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

Commit 21bb46d

Browse files
committed
fix: cached isArray
1 parent 8bebfc1 commit 21bb46d

File tree

11 files changed

+29
-15
lines changed

11 files changed

+29
-15
lines changed

src/createJsonLogic.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import isArray from './helpers/isArray';
12
import isLogic from './helpers/isLogic';
23
import getOperator from './helpers/getOperator';
34

@@ -23,15 +24,15 @@ function createJsonLogic(operations = {}, visitors = {}) {
2324
}
2425

2526
function addVisitor(name, code) {
26-
if (Array.isArray(name)) {
27+
if (isArray(name)) {
2728
name.forEach(addVisitor);
2829
}
2930

3031
visitors[name] = code;
3132
}
3233

3334
function removeVisitor(name) {
34-
if (Array.isArray(name)) {
35+
if (isArray(name)) {
3536
name.forEach(removeVisitor);
3637
}
3738

@@ -40,7 +41,7 @@ function createJsonLogic(operations = {}, visitors = {}) {
4041

4142
function apply(logic, data) {
4243
// Does this array contain logic? Only one way to find out.
43-
if(Array.isArray(logic)) {
44+
if(isArray(logic)) {
4445
return logic.map(function(l) {
4546
return apply(l, data);
4647
});
@@ -57,7 +58,7 @@ function createJsonLogic(operations = {}, visitors = {}) {
5758
let i;
5859

5960
// easy syntax for unary operators, like {"var" : "x"} instead of strict {"var" : ["x"]}
60-
if( ! Array.isArray(values)) {
61+
if( ! isArray(values)) {
6162
values = [values];
6263
}
6364

src/helpers/isArray.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default Array.isArray;

src/helpers/isLogic.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import isArray from './isArray';
2+
13
function isLogic(logic) {
24
return (
35
typeof logic === "object" && // An object
46
logic !== null && // but not null
5-
!Array.isArray(logic) && // and not an array
7+
!isArray(logic) && // and not an array
68
Object.keys(logic).length === 1 // with exactly one key
79
);
810
}

src/helpers/ruleLike.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import isArray from './isArray';
12
import isLogic from './isLogic';
23
import getOperator from './getOperator';
34
import getValues from './getValues';
@@ -18,7 +19,7 @@ function ruleLike(rule, pattern) {
1819
}
1920
if(pattern === "array") {
2021
// !logic test might be superfluous in JavaScript
21-
return Array.isArray(rule) && ! isLogic(rule);
22+
return isArray(rule) && ! isLogic(rule);
2223
}
2324

2425
if(isLogic(pattern)) {
@@ -37,8 +38,8 @@ function ruleLike(rule, pattern) {
3738
return false; // pattern is logic, rule isn't, can't be eq
3839
}
3940

40-
if(Array.isArray(pattern)) {
41-
if(Array.isArray(rule)) {
41+
if(isArray(pattern)) {
42+
if(isArray(rule)) {
4243
if(pattern.length !== rule.length) {
4344
return false;
4445
}

src/helpers/truthy.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import isArray from './isArray';
2+
13
/*
24
This helper will defer to the JsonLogic spec as a tie-breaker when different language interpreters define different behavior for the truthiness of primitives. E.g., PHP considers empty arrays to be falsy, but Javascript considers them to be truthy. JsonLogic, as an ecosystem, needs one consistent answer.
35
46
Spec and rationale here: http://jsonlogic.com/truthy
57
*/
68
function truthy(a) {
7-
if(Array.isArray(value) && value.length === 0) {
9+
if(isArray(value) && value.length === 0) {
810
return false;
911
}
1012

src/helpers/usesData.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import isArray from './isArray';
12
import isLogic from './isLogic';
23
import getOperator from './getOperator';
34
import arrayUnique from './arrayUnique';
@@ -9,7 +10,7 @@ function usesData(logic) {
910
var op = getOperator(logic);
1011
var values = logic[op];
1112

12-
if( ! Array.isArray(values)) {
13+
if( ! isArray(values)) {
1314
values = [values];
1415
}
1516

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export createJsonLogic from './createJsonLogic';
1+
import createJsonLogic from './createJsonLogic';
22
import * as operations from './operations';
33
import * as visitors from './visitors';
44
import isLogic from "./helpers/isLogic";

src/operations/accessor/missing.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import isArray from '../../helpers/isArray';
12
import variable from './variable'
23

34
function missing() {
@@ -9,7 +10,7 @@ function missing() {
910
*/
1011

1112
var missing = [];
12-
var keys = Array.isArray(arguments[0]) ? arguments[0] : arguments;
13+
var keys = isArray(arguments[0]) ? arguments[0] : arguments;
1314

1415
for(var i = 0; i < keys.length; i++) {
1516
var key = keys[i];

src/visitors/array/filter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import isArray from '../../helpers/isArray';
12
import truthy from "../../helpers/truthy";
23

34
function filter(apply, data, values) {
45
const scopedData = apply(values[0], data);
56
const scopedLogic = values[1];
67

7-
if ( ! Array.isArray(scopedData)) {
8+
if ( ! isArray(scopedData)) {
89
return [];
910
}
1011
// Return only the elements from the array in the first argument,

src/visitors/array/map.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import isArray from '../../helpers/isArray';
2+
13
function map(apply, data, values) {
24
const scopedData = apply(values[0], data);
35
const scopedLogic = values[1];
46

5-
if ( ! Array.isArray(scopedData)) {
7+
if ( ! isArray(scopedData)) {
68
return [];
79
}
810

0 commit comments

Comments
 (0)