Skip to content

Commit 39b8bc7

Browse files
committed
Add more tests and another optimization
1 parent bcdc7d4 commit 39b8bc7

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

async.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const modes = [
55
new AsyncLogicEngine(undefined, { disableInterpretedOptimization: true })
66
]
77

8+
for (const engine of modes) {
9+
engine.addMethod('as1', async (n) => n + 1, { async: true, deterministic: true })
10+
}
11+
812
modes.forEach((logic) => {
913
describe('+', () => {
1014
test('it should be able to add two numbers together', async () => {
@@ -867,5 +871,23 @@ modes.forEach((logic) => {
867871
await logic.run([{ test: true }, { test: true }])
868872
).toStrictEqual(['1337', '1337'])
869873
})
874+
875+
test('async + map + deterministic (trying to trigger avoidInlineAsync)', async () => {
876+
expect(
877+
await (await logic.build({
878+
map: [{ var: 'arr' }, {
879+
map: [[1, 2, 3], { as1: { var: '' } }]
880+
}]
881+
}))({ arr: [1, 2, 3] })
882+
).toStrictEqual([[2, 3, 4], [2, 3, 4], [2, 3, 4]])
883+
884+
expect(
885+
await logic.run({
886+
map: [{ var: 'arr' }, {
887+
map: [[1, 2, 3], { as1: { var: '' } }]
888+
}]
889+
}, { arr: [1, 2, 3] })
890+
).toStrictEqual([[2, 3, 4], [2, 3, 4], [2, 3, 4]])
891+
})
870892
})
871893
})

compiler.js

+3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ function buildString (method, buildState = {}) {
185185
} else if (!buildState.avoidInlineAsync) {
186186
processing.push(engine.run(method).then((i) => pushValue(i)))
187187
return `__%%%${processing.length - 1}%%%__`
188+
} else {
189+
buildState.asyncDetected = true
190+
return `(await ${pushValue(engine.run(method))})`
188191
}
189192
}
190193

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-logic-engine",
3-
"version": "3.0.2",
3+
"version": "3.0.3",
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)