Skip to content

Commit b1413cd

Browse files
committed
Add some fixes for multiple keys
1 parent 78a7a93 commit b1413cd

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

asyncLogic.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ class AsyncLogicEngine {
3535

3636
/**
3737
* An internal method used to parse through the JSON Logic at a lower level.
38-
* @param {String} func The name of the function being executed
39-
* @param {*} data The data to traverse / execute upon
38+
* @param {*} logic The logic being executed.
4039
* @param {*} context The context of the logic being run (input to the function.)
4140
* @param {*} above The context above (can be used for handlebars-style data traversal.)
42-
* @returns {Promise}
41+
* @returns {Promise<{ func: string, result: * }>}
4342
*/
44-
async _parse (func, data, context, above) {
43+
async _parse (logic, context, above) {
44+
const [func] = Object.keys(logic)
45+
const data = logic[func]
4546
if (this.methods[func]) {
4647
if (typeof this.methods[func] === 'function') {
4748
const input = await this.run(data, context, { above })
4849
if (this.options.yieldSupported && (await checkYield(input))) {
49-
return input
50+
return { result: input, func }
5051
}
5152
const result = await this.methods[func](input, context, above, this)
52-
53-
return Array.isArray(result) ? Promise.all(result) : result
53+
return { result: Array.isArray(result) ? Promise.all(result) : result, func }
5454
}
5555

5656
if (typeof this.methods[func] === 'object') {
@@ -62,7 +62,7 @@ class AsyncLogicEngine {
6262
: data
6363

6464
if (this.options.yieldSupported && (await checkYield(parsedData))) {
65-
return parsedData
65+
return { result: parsedData, func }
6666
}
6767

6868
const result = await (asyncMethod || method)(
@@ -71,10 +71,10 @@ class AsyncLogicEngine {
7171
above,
7272
this
7373
)
74-
return Array.isArray(result) ? Promise.all(result) : result
74+
return { result: Array.isArray(result) ? Promise.all(result) : result, func }
7575
}
7676
}
77-
if (this.options.permissive) return { [func]: data }
77+
if (this.options.permissive) return { result: logic, func }
7878
throw new Error(`Method '${func}' was not found in the Logic Engine.`)
7979
}
8080

@@ -151,8 +151,7 @@ class AsyncLogicEngine {
151151
}
152152

153153
if (logic && typeof logic === 'object') {
154-
const [func] = Object.keys(logic)
155-
const result = await this._parse(func, logic[func], data, above)
154+
const { func, result } = await this._parse(logic, data, above)
156155

157156
if (this.options.yieldSupported && (await checkYield(result))) {
158157
if (result instanceof YieldStructure) {

general.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ describe('Various Test Cases', () => {
8282
await testEngine(engine, { unknown: true }, {}, { unknown: true })
8383

8484
await testEngine(engine, {
85-
if: [true, { unknown: true }, 5]
86-
}, {}, { unknown: true })
85+
if: [true, { unknown: true, unknown2: 2 }, 5]
86+
}, {}, { unknown: true, unknown2: 2 })
8787
}
8888
})
8989
})

logic.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,30 +30,31 @@ class LogicEngine {
3030

3131
/**
3232
* An internal method used to parse through the JSON Logic at a lower level.
33-
* @param {String} func The name of the function being executed
34-
* @param {*} data The data to traverse / execute upon
33+
* @param {*} logic The logic being executed.
3534
* @param {*} context The context of the logic being run (input to the function.)
3635
* @param {*} above The context above (can be used for handlebars-style data traversal.)
37-
* @returns {*}
36+
* @returns {{ result: *, func: string }}
3837
*/
39-
_parse (func, data, context, above) {
38+
_parse (logic, context, above) {
39+
const [func] = Object.keys(logic)
40+
const data = logic[func]
4041
if (this.methods[func]) {
4142
if (typeof this.methods[func] === 'function') {
4243
const input = this.run(data, context, { above })
43-
if (this.options.yieldSupported && checkYield(input)) return input
44-
return this.methods[func](input, context, above, this)
44+
if (this.options.yieldSupported && checkYield(input)) return { result: input, func }
45+
return { result: this.methods[func](input, context, above, this), func }
4546
}
4647
if (typeof this.methods[func] === 'object') {
4748
const { method, traverse } = this.methods[func]
4849
const shouldTraverse = typeof traverse === 'undefined' ? true : traverse
4950
const parsedData = shouldTraverse
5051
? this.run(data, context, { above })
5152
: data
52-
if (this.options.yieldSupported && checkYield(parsedData)) return parsedData
53-
return method(parsedData, context, above, this)
53+
if (this.options.yieldSupported && checkYield(parsedData)) return { result: parsedData, func }
54+
return { result: method(parsedData, context, above, this), func }
5455
}
5556
}
56-
if (this.options.permissive) return { [func]: data }
57+
if (this.options.permissive) return { result: logic, func }
5758
throw new Error(`Method '${func}' was not found in the Logic Engine.`)
5859
}
5960

@@ -111,8 +112,7 @@ class LogicEngine {
111112
return result
112113
}
113114
if (logic && typeof logic === 'object') {
114-
const [func] = Object.keys(logic)
115-
const result = this._parse(func, logic[func], data, above)
115+
const { func, result } = this._parse(logic, data, above)
116116
if (this.options.yieldSupported && checkYield(result)) {
117117
if (result instanceof YieldStructure) {
118118
if (result._input) {

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": "1.2.1",
3+
"version": "1.2.2",
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)