Skip to content

Commit 0056bbd

Browse files
committed
Some additional style touch ups and eliminate an unused feature in the compiler that is kind of redundant.
1 parent eb3b313 commit 0056bbd

File tree

4 files changed

+33
-147
lines changed

4 files changed

+33
-147
lines changed

asyncLogic.js

+12-50
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,20 @@ class AsyncLogicEngine {
7272
const data = logic[func]
7373

7474
if (this.isData(logic, func)) return logic
75-
7675
if (!this.methods[func]) throw new Error(`Method '${func}' was not found in the Logic Engine.`)
7776

7877
if (typeof this.methods[func] === 'function') {
7978
const input = await this.run(data, context, { above })
80-
8179
const result = await this.methods[func](input, context, above, this)
8280
return Array.isArray(result) ? Promise.all(result) : result
8381
}
8482

8583
if (typeof this.methods[func] === 'object') {
8684
const { asyncMethod, method, traverse } = this.methods[func]
87-
const shouldTraverse =
88-
typeof traverse === 'undefined' ? true : traverse
89-
const parsedData = shouldTraverse
90-
? await this.run(data, context, { above })
91-
: data
92-
93-
const result = await (asyncMethod || method)(
94-
parsedData,
95-
context,
96-
above,
97-
this
98-
)
85+
const shouldTraverse = typeof traverse === 'undefined' ? true : traverse
86+
const parsedData = shouldTraverse ? await this.run(data, context, { above }) : data
87+
88+
const result = await (asyncMethod || method)(parsedData, context, above, this)
9989
return Array.isArray(result) ? Promise.all(result) : result
10090
}
10191

@@ -118,14 +108,9 @@ class AsyncLogicEngine {
118108
if (typeof async !== 'undefined') sync = !async
119109

120110
if (typeof method === 'function') {
121-
if (async) {
122-
method = { asyncMethod: method, traverse: true }
123-
} else {
124-
method = { method, traverse: true }
125-
}
126-
} else {
127-
method = { ...method }
128-
}
111+
if (async) method = { asyncMethod: method, traverse: true }
112+
else method = { method, traverse: true }
113+
} else method = { ...method }
129114

130115
Object.assign(method, omitUndefined({ deterministic, useContext }))
131116
// @ts-ignore
@@ -141,13 +126,7 @@ class AsyncLogicEngine {
141126
*/
142127
addModule (name, obj, annotations = {}) {
143128
Object.getOwnPropertyNames(obj).forEach((key) => {
144-
if (typeof obj[key] === 'function' || typeof obj[key] === 'object') {
145-
this.addMethod(
146-
`${name}${name ? '.' : ''}${key}`,
147-
obj[key],
148-
annotations
149-
)
150-
}
129+
if (typeof obj[key] === 'function' || typeof obj[key] === 'object') this.addMethod(`${name}${name ? '.' : ''}${key}`, obj[key], annotations)
151130
})
152131
}
153132

@@ -210,39 +189,22 @@ class AsyncLogicEngine {
210189
const { above = [], top = true } = options
211190
this.fallback.truthy = this.truthy
212191
if (top) {
213-
const constructedFunction = await buildAsync(logic, {
214-
engine: this,
215-
above,
216-
async: true,
217-
state: {}
218-
})
192+
const constructedFunction = await buildAsync(logic, { engine: this, above, async: true, state: {} })
219193

220194
const result = declareSync((...args) => {
221195
if (top === true) {
222196
try {
223-
const result =
224-
typeof constructedFunction === 'function'
225-
? constructedFunction(...args)
226-
: constructedFunction
197+
const result = typeof constructedFunction === 'function' ? constructedFunction(...args) : constructedFunction
227198
return Promise.resolve(result)
228199
} catch (err) {
229200
return Promise.reject(err)
230201
}
231202
}
232203

233-
const result =
234-
typeof constructedFunction === 'function'
235-
? constructedFunction(...args)
236-
: constructedFunction
237-
238-
return result
204+
return typeof constructedFunction === 'function' ? constructedFunction(...args) : constructedFunction
239205
}, top !== true && isSync(constructedFunction))
240-
241-
return typeof constructedFunction === 'function' || top === true
242-
? result
243-
: constructedFunction
206+
return typeof constructedFunction === 'function' || top === true ? result : constructedFunction
244207
}
245-
246208
return logic
247209
}
248210
}

compiler.js

+13-65
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ export function isDeterministic (method, engine, buildState) {
8989
function isDeepSync (method, engine) {
9090
if (!engine.async) return true
9191

92-
if (Array.isArray(method)) {
93-
return method.every(i => isDeepSync(i, engine))
94-
}
92+
if (Array.isArray(method)) return method.every(i => isDeepSync(i, engine))
9593

9694
if (typeof method === 'object') {
9795
const func = Object.keys(method)[0]
@@ -120,10 +118,10 @@ function buildString (method, buildState = {}) {
120118
const {
121119
notTraversed = [],
122120
functions = {},
123-
methods = [],
124-
state,
121+
// methods = [],
122+
// state,
125123
async,
126-
above = [],
124+
// above = [],
127125
processing = [],
128126
values = [],
129127
engine
@@ -147,16 +145,12 @@ function buildString (method, buildState = {}) {
147145

148146
function makeAsync (result) {
149147
buildState.asyncDetected = buildState.asyncDetected || asyncDetected
150-
151-
if (async && asyncDetected) {
152-
return `await ${result}`
153-
}
148+
if (async && asyncDetected) return `await ${result}`
154149
return result
155150
}
156151

157152
const func = method && Object.keys(method)[0]
158-
buildState.useContext =
159-
buildState.useContext || (engine.methods[func] || {}).useContext
153+
buildState.useContext = buildState.useContext || (engine.methods[func] || {}).useContext
160154

161155
if (method && typeof method === 'object') {
162156
if (!func) return pushValue(method)
@@ -192,62 +186,20 @@ function buildString (method, buildState = {}) {
192186
functions[func] = 1
193187
asyncDetected = !isSync(engine.methods[func])
194188

195-
return makeAsync(
196-
`gen["${func}"](` + buildString(method[func], buildState) + ')'
197-
)
189+
return makeAsync(`gen["${func}"](` + buildString(method[func], buildState) + ')')
198190
} else {
199191
if (engine.methods[func] && (typeof engine.methods[func].traverse === 'undefined' ? true : engine.methods[func].traverse)) {
200192
functions[func] = 1
201-
asyncDetected = Boolean(
202-
async && engine.methods[func] && engine.methods[func].asyncMethod
203-
)
193+
asyncDetected = Boolean(async && engine.methods[func] && engine.methods[func].asyncMethod)
204194

205-
return makeAsync(
206-
`gen["${func}"](` + buildString(method[func], buildState) + ')'
207-
)
195+
return makeAsync(`gen["${func}"](` + buildString(method[func], buildState) + ')')
208196
} else {
209-
if (engine.methods[func]) {
210-
if (async) {
211-
if (engine.methods[func].asyncBuild || engine.methods[func].build) {
212-
const builder =
213-
engine.methods[func].asyncBuild || engine.methods[func].build
214-
const result = builder(
215-
method[func],
216-
state,
217-
above,
218-
engine,
219-
buildState
220-
)
221-
methods.push(result)
222-
asyncDetected = !isSync(result)
223-
return makeAsync(`methods[${methods.length - 1}]()`)
224-
}
225-
} else {
226-
if (engine.methods[func].build) {
227-
methods.push(
228-
engine.methods[func].build(
229-
method[func],
230-
state,
231-
above,
232-
engine,
233-
buildState
234-
)
235-
)
236-
return makeAsync(`methods[${methods.length - 1}]()`)
237-
}
238-
}
239-
}
240-
241-
asyncDetected = Boolean(
242-
async && engine.methods[func] && engine.methods[func].asyncMethod
243-
)
197+
asyncDetected = Boolean(async && engine.methods[func] && engine.methods[func].asyncMethod)
244198

245199
functions[func] = 1
246200
notTraversed.push(method[func])
247201

248-
return makeAsync(
249-
`gen["${func}"](` + `notTraversed[${notTraversed.length - 1}]` + ')'
250-
)
202+
return makeAsync(`gen["${func}"](` + `notTraversed[${notTraversed.length - 1}]` + ')')
251203
}
252204
}
253205
}
@@ -354,18 +306,14 @@ function processBuiltString (method, str, buildState) {
354306
}
355307
})
356308

357-
if (!Object.keys(functions).length) {
358-
return method
359-
}
309+
if (!Object.keys(functions).length) return method
360310

361311
let copyStateCall = 'state[Override] = context;'
362312
// console.log(buildState.useContext)
363313

364314
if (!buildState.useContext) {
365315
copyStateCall = ''
366-
while (str.includes('state[Override]')) {
367-
str = str.replace('state[Override]', 'context')
368-
}
316+
str = str.replace(/state\[Override\]/g, 'context')
369317
}
370318

371319
methods.truthy = engine.truthy

logic.js

+7-31
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ class LogicEngine {
4141
this.options = { disableInline: options.disableInline, disableInterpretedOptimization: options.disableInterpretedOptimization }
4242
if (!this.isData) {
4343
if (!options.permissive) this.isData = () => false
44-
else {
45-
this.isData = (data, key) => !(key in this.methods)
46-
}
44+
else this.isData = (data, key) => !(key in this.methods)
4745
}
4846
}
4947

@@ -80,10 +78,7 @@ class LogicEngine {
8078
if (typeof this.methods[func] === 'object') {
8179
const { method, traverse } = this.methods[func]
8280
const shouldTraverse = typeof traverse === 'undefined' ? true : traverse
83-
const parsedData = shouldTraverse
84-
? this.run(data, context, { above })
85-
: data
86-
81+
const parsedData = shouldTraverse ? this.run(data, context, { above }) : data
8782
return method(parsedData, context, above, this)
8883
}
8984

@@ -97,11 +92,8 @@ class LogicEngine {
9792
* @param {{ deterministic?: Boolean, useContext?: Boolean }} annotations This is used by the compiler to help determine if it can optimize the function being generated.
9893
*/
9994
addMethod (name, method, { deterministic, useContext } = {}) {
100-
if (typeof method === 'function') {
101-
method = { method, traverse: true }
102-
} else {
103-
method = { ...method }
104-
}
95+
if (typeof method === 'function') method = { method, traverse: true }
96+
else method = { ...method }
10597

10698
Object.assign(method, omitUndefined({ useContext, deterministic }))
10799
this.methods[name] = declareSync(method)
@@ -115,13 +107,7 @@ class LogicEngine {
115107
*/
116108
addModule (name, obj, annotations) {
117109
Object.getOwnPropertyNames(obj).forEach((key) => {
118-
if (typeof obj[key] === 'function' || typeof obj[key] === 'object') {
119-
this.addMethod(
120-
`${name}${name ? '.' : ''}${key}`,
121-
obj[key],
122-
annotations
123-
)
124-
}
110+
if (typeof obj[key] === 'function' || typeof obj[key] === 'object') this.addMethod(`${name}${name ? '.' : ''}${key}`, obj[key], annotations)
125111
})
126112
}
127113

@@ -181,18 +167,8 @@ class LogicEngine {
181167
build (logic, options = {}) {
182168
const { above = [], top = true } = options
183169
if (top) {
184-
const constructedFunction = build(logic, {
185-
state: {},
186-
engine: this,
187-
above
188-
})
189-
if (typeof constructedFunction === 'function' || top === true) {
190-
return (...args) => {
191-
return typeof constructedFunction === 'function'
192-
? constructedFunction(...args)
193-
: constructedFunction
194-
}
195-
}
170+
const constructedFunction = build(logic, { state: {}, engine: this, above })
171+
if (typeof constructedFunction === 'function' || top === true) return (...args) => typeof constructedFunction === 'function' ? constructedFunction(...args) : constructedFunction
196172
return constructedFunction
197173
}
198174
return logic

package.json

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