Skip to content

Commit 741a2b9

Browse files
committed
Fix top in json-logic-engine
1 parent 65f9714 commit 741a2b9

File tree

5 files changed

+29
-23
lines changed

5 files changed

+29
-23
lines changed

asyncLogic.js

+15-16
Original file line numberDiff line numberDiff line change
@@ -212,24 +212,23 @@ class AsyncLogicEngine {
212212
this.fallback.truthy = this.truthy
213213
// @ts-ignore
214214
this.fallback.allowFunctions = this.allowFunctions
215-
if (top) {
216-
const constructedFunction = await buildAsync(logic, { engine: this, above, async: true, state: {} })
217-
218-
const result = declareSync((...args) => {
219-
if (top === true) {
220-
try {
221-
const result = typeof constructedFunction === 'function' ? constructedFunction(...args) : constructedFunction
222-
return Promise.resolve(result)
223-
} catch (err) {
224-
return Promise.reject(err)
225-
}
215+
const constructedFunction = await buildAsync(logic, { engine: this, above, async: true })
216+
217+
const result = declareSync((...args) => {
218+
if (top === true) {
219+
try {
220+
const result = typeof constructedFunction === 'function' ? constructedFunction(...args) : constructedFunction
221+
return Promise.resolve(result)
222+
} catch (err) {
223+
return Promise.reject(err)
226224
}
225+
}
227226

228-
return typeof constructedFunction === 'function' ? constructedFunction(...args) : constructedFunction
229-
}, top !== true && isSync(constructedFunction))
230-
return typeof constructedFunction === 'function' || top === true ? result : constructedFunction
231-
}
232-
return logic
227+
return typeof constructedFunction === 'function' ? constructedFunction(...args) : constructedFunction
228+
}, top !== true && isSync(constructedFunction))
229+
230+
if (top === false && constructedFunction.deterministic) return result()
231+
return (typeof constructedFunction === 'function' || top === true) ? result : constructedFunction
233232
}
234233
}
235234
export default AsyncLogicEngine

compiler.js

+1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ function processBuiltString (method, str, buildState) {
326326
return Object.assign(
327327
(typeof globalThis !== 'undefined' ? globalThis : global).eval(final)(values, methods, notTraversed, asyncIterators, engine, above, coerceArray, precoerceNumber, assertSize, compareCheck), {
328328
[Sync]: !buildState.asyncDetected,
329+
deterministic: !str.includes('('),
329330
aboveDetected: typeof str === 'string' && str.includes(', above')
330331
})
331332
}

general.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ describe('Various Test Cases', () => {
134134
for (const engine of [...normalEngines, ...permissiveEngines]) await testEngine(engine, { var: 'toString' }, 'hello', null)
135135
})
136136

137+
it('is able to return the constant value', async () => {
138+
for (const engine of [...normalEngines, ...permissiveEngines]) {
139+
const items = [1, 'hello', [1, 2], [{}]]
140+
for (const item of items) {
141+
if (JSON.stringify(await engine.build(item, { top: false })) !== JSON.stringify(item)) throw new Error('Should have returned the same value, ' + item)
142+
}
143+
}
144+
})
145+
137146
it('is able to return functions if enabled', async () => {
138147
try {
139148
for (const engine of [...normalEngines, ...permissiveEngines]) {

logic.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,9 @@ class LogicEngine {
184184
*/
185185
build (logic, options = {}) {
186186
const { above = [], top = true } = options
187-
if (top) {
188-
const constructedFunction = build(logic, { state: {}, engine: this, above })
189-
if (typeof constructedFunction === 'function' || top === true) return (...args) => typeof constructedFunction === 'function' ? constructedFunction(...args) : constructedFunction
190-
return constructedFunction
191-
}
192-
return logic
187+
const constructedFunction = build(logic, { engine: this, above })
188+
if (top === false && constructedFunction.deterministic) return constructedFunction()
189+
return constructedFunction
193190
}
194191
}
195192
export default LogicEngine

package.json

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

0 commit comments

Comments
 (0)