Skip to content

Commit fea38a1

Browse files
committed
Correct an issue with soft comparisons where null is the first argument
and the second argument is a string. Oof. Releasing v5.0.1
1 parent e9779e5 commit fea38a1

12 files changed

+135
-21
lines changed

defaultMethods.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -839,14 +839,14 @@ function createComparator (name, func) {
839839
const b = runOptimizedOrFallback(args[1], engine, context, above)
840840
if (strict || (typeof a === 'string' && typeof b === 'string')) return func(a, b)
841841
if (Number.isNaN(+precoerceNumber(a))) throw NaN
842-
if (Number.isNaN(+precoerceNumber(b))) throw NaN
842+
if (Number.isNaN(+precoerceNumber(b)) && a !== null) throw NaN
843843
return func(+a, +b)
844844
}
845845
let prev = runOptimizedOrFallback(args[0], engine, context, above)
846846
for (let i = 1; i < args.length; i++) {
847847
const current = runOptimizedOrFallback(args[i], engine, context, above)
848848
if (strict || (typeof current === 'string' && typeof prev === 'string')) if (!func(prev, current)) return false
849-
if (Number.isNaN(+precoerceNumber(current))) throw NaN
849+
if (Number.isNaN(+precoerceNumber(current)) && prev !== null) throw NaN
850850
if (i === 1 && Number.isNaN(+precoerceNumber(prev))) throw NaN
851851
if (!func(+prev, +current)) return false
852852
prev = current
@@ -860,14 +860,14 @@ function createComparator (name, func) {
860860
const b = await runOptimizedOrFallback(args[1], engine, context, above)
861861
if (strict || (typeof a === 'string' && typeof b === 'string')) return func(a, b)
862862
if (Number.isNaN(+precoerceNumber(a))) throw NaN
863-
if (Number.isNaN(+precoerceNumber(b))) throw NaN
863+
if (Number.isNaN(+precoerceNumber(b)) && a !== null) throw NaN
864864
return func(+a, +b)
865865
}
866866
let prev = await runOptimizedOrFallback(args[0], engine, context, above)
867867
for (let i = 1; i < args.length; i++) {
868868
const current = await runOptimizedOrFallback(args[i], engine, context, above)
869869
if (strict || (typeof current === 'string' && typeof prev === 'string')) if (!func(prev, current)) return false
870-
if (Number.isNaN(+precoerceNumber(current))) throw NaN
870+
if (Number.isNaN(+precoerceNumber(current)) && prev !== null) throw NaN
871871
if (i === 1 && Number.isNaN(+precoerceNumber(prev))) throw NaN
872872
if (!func(+prev, +current)) return false
873873
prev = current

optimizer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function checkIdioms (logic, engine, above) {
146146
: function comparisonFunc (a, b) {
147147
if (typeof a === 'string' && typeof b === 'string') return _comparisonFunc(a, b)
148148
if (Number.isNaN(+precoerceNumber(a))) throw NaN
149-
if (Number.isNaN(+precoerceNumber(b))) throw NaN
149+
if (Number.isNaN(+precoerceNumber(b)) && a !== null) throw NaN
150150
return _comparisonFunc(+a, +b)
151151
}
152152

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-logic-engine",
3-
"version": "5.0.0",
3+
"version": "5.0.1",
44
"description": "Construct complex rules with JSON & process them.",
55
"main": "./dist/cjs/index.js",
66
"module": "./dist/esm/index.js",

suites/comparison/greaterThan.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@
149149
"data": {},
150150
"error": { "type": "NaN" }
151151
},
152+
{
153+
"description": "> with (null, As)",
154+
"rule": { ">": [null, "A"] },
155+
"data": {},
156+
"result": false
157+
},
152158
{
153159
"description": "> with (null, 1)",
154160
"rule": { ">": [null, 1] },
@@ -209,4 +215,4 @@
209215
"data": {},
210216
"error": { "type": "NaN" }
211217
}
212-
]
218+
]

suites/comparison/greaterThanEquals.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@
102102
"error": { "type": "NaN" }
103103
},
104104
{
105-
"description": ">= with (1n, As)",
106-
"rule": { ">=": [1, "A"] },
107-
"data": {},
108-
"error": { "type": "NaN" }
105+
"description": ">= with (null, As)",
106+
"rule": { ">=": [null, "A"] },
107+
"data": {},
108+
"result": false
109109
},
110110
{
111111
"description": ">= with (null, 1)",
@@ -167,4 +167,4 @@
167167
"data": {},
168168
"error": { "type": "NaN" }
169169
}
170-
]
170+
]

suites/comparison/lessThan.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@
209209
"data": {},
210210
"error": { "type": "NaN" }
211211
},
212+
{
213+
"description": "< with (null, As)",
214+
"rule": { "<": [null, "A"] },
215+
"data": {},
216+
"result": false
217+
},
212218
{
213219
"description": "< with (null, 1)",
214220
"rule": { "<": [null, 1] },
@@ -269,4 +275,4 @@
269275
"data": {},
270276
"error": { "type": "NaN" }
271277
}
272-
]
278+
]

suites/comparison/lessThanEquals.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
"data": {},
6060
"error": { "type": "NaN" }
6161
},
62+
{
63+
"description": "<= with (null, As)",
64+
"rule": { "<=": [null, "A"] },
65+
"data": {},
66+
"result": false
67+
},
6268
{
6369
"description": "<= with (null, 1)",
6470
"rule": { "<=": [null, 1] },
@@ -119,4 +125,4 @@
119125
"data": {},
120126
"error": { "type": "NaN" }
121127
}
122-
]
128+
]

suites/comparison/softEquals.json

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,30 @@
149149
"data": {},
150150
"error": { "type": "NaN" }
151151
},
152+
{
153+
"description": "== with (null, As)",
154+
"rule": { "==": [null, "A"] },
155+
"data": {},
156+
"result": false
157+
},
158+
{
159+
"description": "== with (null, null)",
160+
"rule": { "==": [null, null] },
161+
"data": {},
162+
"result": true
163+
},
164+
{
165+
"description": "== with (null, null, null)",
166+
"rule": { "==": [null, null, null] },
167+
"data": {},
168+
"result": true
169+
},
170+
{
171+
"description": "== with (null, null, As)",
172+
"rule": { "==": [null, null, "A"] },
173+
"data": {},
174+
"result": false
175+
},
152176
{
153177
"description": "== with (null, 1)",
154178
"rule": { "==": [null, 1] },
@@ -209,4 +233,4 @@
209233
"data": {},
210234
"error": { "type": "NaN" }
211235
}
212-
]
236+
]

suites/comparison/softNotEquals.json

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,30 @@
143143
"data": {},
144144
"error": { "type": "NaN" }
145145
},
146+
{
147+
"description": "!= with (null, As)",
148+
"rule": { "!=": [null, "A"] },
149+
"data": {},
150+
"result": true
151+
},
152+
{
153+
"description": "== with (null, null)",
154+
"rule": { "!=": [null, null] },
155+
"data": {},
156+
"result": false
157+
},
158+
{
159+
"description": "!= with (null, null, null)",
160+
"rule": { "!=": [null, null, null] },
161+
"data": {},
162+
"result": false
163+
},
164+
{
165+
"description": "!= with (null, null, As)",
166+
"rule": { "!=": [null, null, "A"] },
167+
"data": {},
168+
"result": false
169+
},
146170
{
147171
"description": "!= with (null, 1)",
148172
"rule": { "!=": [null, 1] },
@@ -203,4 +227,4 @@
203227
"data": {},
204228
"error": { "type": "NaN" }
205229
}
206-
]
230+
]

suites/comparison/strictEquals.json

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,30 @@
149149
"data": {},
150150
"result": false
151151
},
152+
{
153+
"description": "=== with (null, As)",
154+
"rule": { "===": [null, "A"] },
155+
"data": {},
156+
"result": false
157+
},
158+
{
159+
"description": "=== with (null, null)",
160+
"rule": { "===": [null, null] },
161+
"data": {},
162+
"result": true
163+
},
164+
{
165+
"description": "=== with (null, null, null)",
166+
"rule": { "===": [null, null, null] },
167+
"data": {},
168+
"result": true
169+
},
170+
{
171+
"description": "=== with (null, null, As)",
172+
"rule": { "===": [null, null, "A"] },
173+
"data": {},
174+
"result": false
175+
},
152176
{
153177
"description": "=== with (null, 1)",
154178
"rule": { "===": [null, 1] },
@@ -185,4 +209,4 @@
185209
"data": {},
186210
"result": false
187211
}
188-
]
212+
]

suites/comparison/strictNotEquals.json

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"rule": { "!==": ["2024-01-01", "2024-01-01"] },
8989
"data": {},
9090
"result": false
91-
},
91+
},
9292
{
9393
"description": "!== with (2023n, 2024-01-01s)",
9494
"rule": { "!==": [2023, "2024-01-01"] },
@@ -143,6 +143,30 @@
143143
"data": {},
144144
"result": true
145145
},
146+
{
147+
"description": "!== with (null, As)",
148+
"rule": { "!==": [null, "A"] },
149+
"data": {},
150+
"result": true
151+
},
152+
{
153+
"description": "!== with (null, null)",
154+
"rule": { "!==": [null, null] },
155+
"data": {},
156+
"result": false
157+
},
158+
{
159+
"description": "!== with (null, null, null)",
160+
"rule": { "!==": [null, null, null] },
161+
"data": {},
162+
"result": false
163+
},
164+
{
165+
"description": "!== with (null, null, As)",
166+
"rule": { "!==": [null, null, "A"] },
167+
"data": {},
168+
"result": false
169+
},
146170
{
147171
"description": "!== with (null, 1)",
148172
"rule": { "!==": [null, 1] },
@@ -179,4 +203,4 @@
179203
"data": {},
180204
"result": true
181205
}
182-
]
206+
]

utilities/downgrade.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
export function precoerceNumber (item) {
66
if (Number.isNaN(item)) throw NaN
77
if (!item) return item
8-
if (typeof item === 'object') throw NaN
8+
if (item && typeof item === 'object') throw NaN
99
return item
1010
}
1111

@@ -29,7 +29,7 @@ export function assertSize (arr, size) {
2929
export function compareCheck (item, prev, strict) {
3030
if (strict || (typeof item === 'string' && typeof prev === 'string')) return item
3131

32-
if (Number.isNaN(+precoerceNumber(item))) throw NaN
32+
if (Number.isNaN(+precoerceNumber(item)) && prev !== null) throw NaN
3333
if (Number.isNaN(+precoerceNumber(prev))) throw NaN
3434

3535
// The following two checks allow us to handle null == 0 and 0 == null; it's honestly

0 commit comments

Comments
 (0)