Skip to content

Commit 30f7842

Browse files
committed
Switch to compatibility by default.
1 parent 6a066ad commit 30f7842

File tree

4 files changed

+40
-77
lines changed

4 files changed

+40
-77
lines changed

asyncLogic.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import declareSync from './utilities/declareSync.js'
88
import { buildAsync } from './compiler.js'
99
import omitUndefined from './utilities/omitUndefined.js'
1010
import { optimize } from './async_optimizer.js'
11-
import { applyPatches } from './compatibility.js'
1211
import { coerceArray } from './utilities/coerceArray.js'
1312

1413
/**
@@ -24,7 +23,7 @@ class AsyncLogicEngine {
2423
* - In mainline: empty arrays are falsey; in our implementation, they are truthy.
2524
*
2625
* @param {Object} methods An object that stores key-value pairs between the names of the commands & the functions they execute.
27-
* @param {{ disableInline?: Boolean, disableInterpretedOptimization?: boolean, permissive?: boolean, compatible?: boolean }} options
26+
* @param {{ disableInline?: Boolean, disableInterpretedOptimization?: boolean, permissive?: boolean }} options
2827
*/
2928
constructor (
3029
methods = defaultMethods,
@@ -38,8 +37,6 @@ class AsyncLogicEngine {
3837
this.async = true
3938
this.fallback = new LogicEngine(methods, options)
4039

41-
if (options.compatible) applyPatches(this)
42-
4340
this.optimizedMap = new WeakMap()
4441
this.missesSinceSeen = 0
4542

@@ -58,6 +55,8 @@ class AsyncLogicEngine {
5855
* @returns
5956
*/
6057
truthy (value) {
58+
if (Array.isArray(value) && value.length === 0) return false
59+
if (Number.isNaN(value)) return true
6160
return value
6261
}
6362

@@ -216,5 +215,4 @@ class AsyncLogicEngine {
216215
return logic
217216
}
218217
}
219-
Object.assign(AsyncLogicEngine.prototype.truthy, { [OriginalImpl]: true })
220218
export default AsyncLogicEngine

compatibility.js

-34
This file was deleted.

defaultMethods.js

+33-27
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function isSyncDeep (method, engine, buildState) {
5252
return true
5353
}
5454

55+
const oldAll = createArrayIterativeMethod('every', true)
5556
const defaultMethods = {
5657
'+': (data) => {
5758
if (!data) return 0
@@ -278,17 +279,13 @@ const defaultMethods = {
278279
},
279280
deterministic: (data, buildState) => isDeterministic(data, buildState.engine, buildState),
280281
compile: (data, buildState) => {
281-
if (!buildState.engine.truthy[OriginalImpl]) {
282-
let res = buildState.compile``
283-
if (Array.isArray(data) && data.length) {
284-
for (let i = 0; i < data.length; i++) res = buildState.compile`${res} engine.truthy(prev = ${data[i]}) ? prev : `
285-
res = buildState.compile`${res} prev`
286-
return res
287-
}
288-
return false
282+
let res = buildState.compile``
283+
if (Array.isArray(data) && data.length) {
284+
for (let i = 0; i < data.length; i++) res = buildState.compile`${res} engine.truthy(prev = ${data[i]}) ? prev : `
285+
res = buildState.compile`${res} prev`
286+
return res
289287
}
290-
if (Array.isArray(data) && data.length) return `(${data.map((i) => buildString(i, buildState)).join(' || ')})`
291-
return `(${buildString(data, buildState)}).reduce((a,b) => a||b, false)`
288+
return false
292289
},
293290
lazy: true
294291
},
@@ -323,17 +320,13 @@ const defaultMethods = {
323320
lazy: true,
324321
deterministic: (data, buildState) => isDeterministic(data, buildState.engine, buildState),
325322
compile: (data, buildState) => {
326-
if (!buildState.engine.truthy[OriginalImpl]) {
327-
let res = buildState.compile``
328-
if (Array.isArray(data) && data.length) {
329-
for (let i = 0; i < data.length; i++) res = buildState.compile`${res} !engine.truthy(prev = ${data[i]}) ? prev : `
330-
res = buildState.compile`${res} prev`
331-
return res
332-
}
333-
return false
323+
let res = buildState.compile``
324+
if (Array.isArray(data) && data.length) {
325+
for (let i = 0; i < data.length; i++) res = buildState.compile`${res} !engine.truthy(prev = ${data[i]}) ? prev : `
326+
res = buildState.compile`${res} prev`
327+
return res
334328
}
335-
if (Array.isArray(data) && data.length) return `(${data.map((i) => buildString(i, buildState)).join(' && ')})`
336-
return `(${buildString(data, buildState)}).reduce((a,b) => a&&b, true)`
329+
return false
337330
}
338331
},
339332
substr: ([string, from, end]) => {
@@ -441,7 +434,25 @@ const defaultMethods = {
441434
},
442435
map: createArrayIterativeMethod('map'),
443436
some: createArrayIterativeMethod('some', true),
444-
all: createArrayIterativeMethod('every', true),
437+
all: {
438+
[Sync]: oldAll[Sync],
439+
method: (args, context, above, engine) => {
440+
if (Array.isArray(args)) {
441+
const first = engine.run(args[0], context, above)
442+
if (Array.isArray(first) && first.length === 0) return false
443+
}
444+
return oldAll.method(args, context, above, engine)
445+
},
446+
asyncMethod: async (args, context, above, engine) => {
447+
if (Array.isArray(args)) {
448+
const first = await engine.run(args[0], context, above)
449+
if (Array.isArray(first) && first.length === 0) return false
450+
}
451+
return oldAll.asyncMethod(args, context, above, engine)
452+
},
453+
deterministic: oldAll.deterministic,
454+
lazy: oldAll.lazy
455+
},
445456
none: {
446457
[Sync]: (data, buildState) => isSyncDeep(data, buildState.engine, buildState),
447458
lazy: true,
@@ -450,12 +461,7 @@ const defaultMethods = {
450461
return !defaultMethods.some.method(val, context, above, engine)
451462
},
452463
asyncMethod: async (val, context, above, engine) => {
453-
return !(await defaultMethods.some.asyncMethod(
454-
val,
455-
context,
456-
above,
457-
engine
458-
))
464+
return !(await defaultMethods.some.asyncMethod(val, context, above, engine))
459465
},
460466
compile: (data, buildState) => {
461467
const result = defaultMethods.some.compile(data, buildState)

logic.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { build } from './compiler.js'
77
import declareSync from './utilities/declareSync.js'
88
import omitUndefined from './utilities/omitUndefined.js'
99
import { optimize } from './optimizer.js'
10-
import { applyPatches } from './compatibility.js'
1110
import { coerceArray } from './utilities/coerceArray.js'
1211
import { OriginalImpl } from './constants.js'
1312

@@ -17,14 +16,9 @@ import { OriginalImpl } from './constants.js'
1716
class LogicEngine {
1817
/**
1918
* Creates a new instance of the Logic Engine.
20-
*
21-
* "compatible" applies a few patches to make it compatible with the preferences of mainline JSON Logic.
22-
* The main changes are:
23-
* - In mainline: "all" will return false if the array is empty; by default, we return true.
24-
* - In mainline: empty arrays are falsey; in our implementation, they are truthy.
25-
*
19+
*
2620
* @param {Object} methods An object that stores key-value pairs between the names of the commands & the functions they execute.
27-
* @param {{ disableInline?: Boolean, disableInterpretedOptimization?: Boolean, permissive?: boolean, compatible?: boolean }} options
21+
* @param {{ disableInline?: Boolean, disableInterpretedOptimization?: Boolean, permissive?: boolean }} options
2822
*/
2923
constructor (
3024
methods = defaultMethods,
@@ -37,8 +31,6 @@ class LogicEngine {
3731
this.optimizedMap = new WeakMap()
3832
this.missesSinceSeen = 0
3933

40-
if (options.compatible) applyPatches(this)
41-
4234
/** @type {{ disableInline?: Boolean, disableInterpretedOptimization?: Boolean }} */
4335
this.options = { disableInline: options.disableInline, disableInterpretedOptimization: options.disableInterpretedOptimization }
4436
if (!this.isData) {
@@ -54,6 +46,8 @@ class LogicEngine {
5446
* @returns
5547
*/
5648
truthy (value) {
49+
if (Array.isArray(value) && value.length === 0) return false
50+
if (Number.isNaN(value)) return true
5751
return value
5852
}
5953

@@ -186,5 +180,4 @@ class LogicEngine {
186180
return logic
187181
}
188182
}
189-
Object.assign(LogicEngine.prototype.truthy, { [OriginalImpl]: true })
190183
export default LogicEngine

0 commit comments

Comments
 (0)