Skip to content

Commit 17c2dc7

Browse files
committed
Code cleanup and optimizations
1 parent a9641d5 commit 17c2dc7

File tree

7 files changed

+40
-38
lines changed

7 files changed

+40
-38
lines changed

asyncLogic.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,8 @@ class AsyncLogicEngine {
8989
}
9090

9191
if (typeof this.methods[func] === 'object') {
92-
const { asyncMethod, method, traverse } = this.methods[func]
93-
const shouldTraverse = typeof traverse === 'undefined' ? true : traverse
94-
const parsedData = shouldTraverse ? ((!data || typeof data !== 'object') ? [data] : coerceArray(await this.run(data, context, { above }))) : data
92+
const { asyncMethod, method, lazy } = this.methods[func]
93+
const parsedData = !lazy ? ((!data || typeof data !== 'object') ? [data] : coerceArray(await this.run(data, context, { above }))) : data
9594
const result = await (asyncMethod || method)(parsedData, context, above, this)
9695
return Array.isArray(result) ? Promise.all(result) : result
9796
}
@@ -102,7 +101,7 @@ class AsyncLogicEngine {
102101
/**
103102
*
104103
* @param {String} name The name of the method being added.
105-
* @param {((args: any, context: any, above: any[], engine: AsyncLogicEngine) => any) | { traverse?: Boolean, method?: (args: any, context: any, above: any[], engine: AsyncLogicEngine) => any, asyncMethod?: (args: any, context: any, above: any[], engine: AsyncLogicEngine) => Promise<any>, deterministic?: Function | Boolean }} method
104+
* @param {((args: any, context: any, above: any[], engine: AsyncLogicEngine) => any) | { lazy?: Boolean, traverse?: Boolean, method?: (args: any, context: any, above: any[], engine: AsyncLogicEngine) => any, asyncMethod?: (args: any, context: any, above: any[], engine: AsyncLogicEngine) => Promise<any>, deterministic?: Function | Boolean }} method
106105
* @param {{ deterministic?: Boolean, async?: Boolean, sync?: Boolean, optimizeUnary?: boolean }} annotations This is used by the compiler to help determine if it can optimize the function being generated.
107106
*/
108107
addMethod (
@@ -115,9 +114,9 @@ class AsyncLogicEngine {
115114
if (typeof async !== 'undefined') sync = !async
116115

117116
if (typeof method === 'function') {
118-
if (async) method = { asyncMethod: method, traverse: true }
119-
else method = { method, traverse: true }
120-
} else method = { ...method }
117+
if (async) method = { asyncMethod: method, lazy: false }
118+
else method = { method, lazy: false }
119+
} else method = { ...method, lazy: typeof method.traverse !== 'undefined' ? !method.traverse : method.lazy }
121120

122121
Object.assign(method, omitUndefined({ deterministic, optimizeUnary }))
123122
// @ts-ignore

async_optimizer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function getMethod (logic, engine, methodName, above) {
1717
const method = engine.methods[methodName]
1818
const called = method.asyncMethod ? method.asyncMethod : method.method ? method.method : method
1919

20-
if (method.traverse === false) {
20+
if (method.lazy) {
2121
if (typeof method[Sync] === 'function' && method[Sync](logic, { engine })) {
2222
const called = method.method ? method.method : method
2323
return declareSync((data, abv) => called(logic[methodName], data, abv || above, engine.fallback), true)

compatibility.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const all = {
1919
return oldAll.asyncMethod(args, context, above, engine)
2020
},
2121
deterministic: oldAll.deterministic,
22-
traverse: oldAll.traverse
22+
lazy: oldAll.lazy
2323
}
2424

2525
function truthy (value) {

compiler.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export function isDeterministic (method, engine, buildState) {
8888
if (lower === undefined) return true
8989
if (!engine.methods[func]) throw new Error(`Method '${func}' was not found in the Logic Engine.`)
9090

91-
if (engine.methods[func].traverse === false) {
91+
if (engine.methods[func].lazy) {
9292
return typeof engine.methods[func].deterministic === 'function'
9393
? engine.methods[func].deterministic(lower, buildState)
9494
: engine.methods[func].deterministic
@@ -119,7 +119,7 @@ function isDeepSync (method, engine) {
119119
const lower = method[func]
120120
if (!isSync(engine.methods[func])) return false
121121

122-
if (engine.methods[func].traverse === false) {
122+
if (engine.methods[func].lazy) {
123123
if (typeof engine.methods[func][Sync] === 'function' && engine.methods[func][Sync](method, { engine })) return true
124124
return false
125125
}
@@ -194,7 +194,7 @@ function buildString (method, buildState = {}) {
194194
}
195195

196196
let lower = method[func]
197-
if ((!lower || typeof lower !== 'object') && (typeof engine.methods[func].traverse === 'undefined' || engine.methods[func].traverse)) lower = [lower]
197+
if ((!lower || typeof lower !== 'object') && (!engine.methods[func].lazy)) lower = [lower]
198198

199199
if (engine.methods[func] && engine.methods[func].compile) {
200200
let str = engine.methods[func].compile(lower, buildState)
@@ -220,7 +220,7 @@ function buildString (method, buildState = {}) {
220220
const argCount = countArguments(asyncDetected ? engine.methods[func].asyncMethod : engine.methods[func].method)
221221
const argumentsNeeded = argumentsDict[argCount - 1] || argumentsDict[2]
222222

223-
if (engine.methods[func] && (typeof engine.methods[func].traverse === 'undefined' ? true : engine.methods[func].traverse)) {
223+
if (engine.methods[func] && !engine.methods[func].lazy) {
224224
return makeAsync(`engine.methods["${func}"]${asyncDetected ? '.asyncMethod' : '.method'}(${coerce}(` + buildString(lower, buildState) + ')' + argumentsNeeded + ')')
225225
} else {
226226
notTraversed.push(lower)

defaultMethods.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ function isDeterministic (method, engine, buildState) {
2121
if (engine.isData(method, func)) return true
2222
if (!engine.methods[func]) throw new Error(`Method '${func}' was not found in the Logic Engine.`)
2323

24-
if (engine.methods[func].traverse === false) {
24+
if (engine.methods[func].lazy) {
2525
return typeof engine.methods[func].deterministic === 'function'
2626
? engine.methods[func].deterministic(lower, buildState)
2727
: engine.methods[func].deterministic
2828
}
29+
2930
return typeof engine.methods[func].deterministic === 'function'
3031
? engine.methods[func].deterministic(lower, buildState)
3132
: engine.methods[func].deterministic &&
@@ -44,7 +45,7 @@ function isSyncDeep (method, engine, buildState) {
4445
const lower = method[func]
4546
if (engine.isData(method, func)) return true
4647
if (!engine.methods[func]) throw new Error(`Method '${func}' was not found in the Logic Engine.`)
47-
if (engine.methods[func].traverse === false) return typeof engine.methods[func][Sync] === 'function' ? engine.methods[func][Sync](lower, buildState) : engine.methods[func][Sync]
48+
if (engine.methods[func].lazy) return typeof engine.methods[func][Sync] === 'function' ? engine.methods[func][Sync](lower, buildState) : engine.methods[func][Sync]
4849
return typeof engine.methods[func][Sync] === 'function' ? engine.methods[func][Sync](lower, buildState) : engine.methods[func][Sync] && isSyncDeep(lower, engine, buildState)
4950
}
5051

@@ -121,7 +122,7 @@ const defaultMethods = {
121122
min: (data) => Math.min(...data),
122123
in: ([item, array]) => (array || []).includes(item),
123124
preserve: {
124-
traverse: false,
125+
lazy: true,
125126
method: declareSync((i) => i, true),
126127
[Sync]: () => true
127128
},
@@ -182,7 +183,7 @@ const defaultMethods = {
182183

183184
return engine.run(onFalse, context, { above })
184185
},
185-
traverse: false
186+
lazy: true
186187
},
187188
'<': (args) => {
188189
if (args.length === 2) return args[0] < args[1]
@@ -289,7 +290,7 @@ const defaultMethods = {
289290
if (Array.isArray(data) && data.length) return `(${data.map((i) => buildString(i, buildState)).join(' || ')})`
290291
return `(${buildString(data, buildState)}).reduce((a,b) => a||b, false)`
291292
},
292-
traverse: false
293+
lazy: true
293294
},
294295
'??': defineCoalesce(),
295296
try: defineCoalesce(downgrade),
@@ -319,7 +320,7 @@ const defaultMethods = {
319320
}
320321
return item
321322
},
322-
traverse: false,
323+
lazy: true,
323324
deterministic: (data, buildState) => isDeterministic(data, buildState.engine, buildState),
324325
compile: (data, buildState) => {
325326
if (!buildState.engine.truthy[OriginalImpl]) {
@@ -352,7 +353,6 @@ const defaultMethods = {
352353
const result = defaultMethods.val.method(key, context, above, engine, Unfound)
353354
return result !== Unfound
354355
},
355-
traverse: true,
356356
deterministic: false
357357
},
358358
val: {
@@ -444,7 +444,7 @@ const defaultMethods = {
444444
all: createArrayIterativeMethod('every', true),
445445
none: {
446446
[Sync]: (data, buildState) => isSyncDeep(data, buildState.engine, buildState),
447-
traverse: false,
447+
lazy: true,
448448
// todo: add async build & build
449449
method: (val, context, above, engine) => {
450450
return !defaultMethods.some.method(val, context, above, engine)
@@ -580,7 +580,7 @@ const defaultMethods = {
580580
defaultValue
581581
)
582582
},
583-
traverse: false
583+
lazy: true
584584
},
585585
'!': (value, _1, _2, engine) => Array.isArray(value) ? !engine.truthy(value[0]) : !engine.truthy(value),
586586
'!!': (value, _1, _2, engine) => Boolean(Array.isArray(value) ? engine.truthy(value[0]) : engine.truthy(value)),
@@ -598,7 +598,6 @@ const defaultMethods = {
598598
return res
599599
},
600600
deterministic: true,
601-
traverse: true,
602601
optimizeUnary: true,
603602
compile: (data, buildState) => {
604603
if (typeof data === 'string') return JSON.stringify(data)
@@ -611,7 +610,7 @@ const defaultMethods = {
611610
},
612611
keys: ([obj]) => typeof obj === 'object' ? Object.keys(obj) : [],
613612
pipe: {
614-
traverse: false,
613+
lazy: true,
615614
[Sync]: (data, buildState) => isSyncDeep(data, buildState.engine, buildState),
616615
method: (args, context, above, engine) => {
617616
if (!Array.isArray(args)) throw new Error('Data for pipe must be an array')
@@ -638,7 +637,7 @@ const defaultMethods = {
638637
}
639638
},
640639
eachKey: {
641-
traverse: false,
640+
lazy: true,
642641
[Sync]: (data, buildState) => isSyncDeep(Object.values(data[Object.keys(data)[0]]), buildState.engine, buildState),
643642
method: (object, context, above, engine) => {
644643
const result = Object.keys(object).reduce((accumulator, key) => {
@@ -744,7 +743,7 @@ function defineCoalesce (func) {
744743
}
745744
return `(${buildString(data, buildState)}).reduce((a,b) => ${funcCall}(a) ?? b, null)`
746745
},
747-
traverse: false
746+
lazy: true
748747
}
749748
}
750749

@@ -814,7 +813,7 @@ function createArrayIterativeMethod (name, useTruthy = false) {
814813

815814
return buildState.compile`(${selector} || [])[${name}]((i, x, z) => ${useTruthyMethod}(${method}(i, x, ${aboveArray})))`
816815
},
817-
traverse: false
816+
lazy: true
818817
}
819818
}
820819
defaultMethods['?:'] = defaultMethods.if

logic.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ class LogicEngine {
6464
* @param {*} above The context above (can be used for handlebars-style data traversal.)
6565
* @returns {{ result: *, func: string }}
6666
*/
67-
_parse (logic, context, above) {
68-
const [func] = Object.keys(logic)
67+
_parse (logic, context, above, func) {
6968
const data = logic[func]
7069

7170
if (this.isData(logic, func)) return logic
@@ -85,9 +84,8 @@ class LogicEngine {
8584
}
8685

8786
if (typeof this.methods[func] === 'object') {
88-
const { method, traverse } = this.methods[func]
89-
const shouldTraverse = typeof traverse === 'undefined' ? true : traverse
90-
const parsedData = shouldTraverse ? ((!data || typeof data !== 'object') ? [data] : coerceArray(this.run(data, context, { above }))) : data
87+
const { method, lazy } = this.methods[func]
88+
const parsedData = !lazy ? ((!data || typeof data !== 'object') ? [data] : coerceArray(this.run(data, context, { above }))) : data
9189
return method(parsedData, context, above, this)
9290
}
9391

@@ -97,12 +95,12 @@ class LogicEngine {
9795
/**
9896
*
9997
* @param {String} name The name of the method being added.
100-
* @param {((args: any, context: any, above: any[], engine: LogicEngine) => any) |{ traverse?: Boolean, method: (args: any, context: any, above: any[], engine: LogicEngine) => any, deterministic?: Function | Boolean }} method
98+
* @param {((args: any, context: any, above: any[], engine: LogicEngine) => any) |{ lazy?: Boolean, traverse?: Boolean, method: (args: any, context: any, above: any[], engine: LogicEngine) => any, deterministic?: Function | Boolean }} method
10199
* @param {{ deterministic?: Boolean, optimizeUnary?: Boolean }} annotations This is used by the compiler to help determine if it can optimize the function being generated.
102100
*/
103101
addMethod (name, method, { deterministic, optimizeUnary } = {}) {
104-
if (typeof method === 'function') method = { method, traverse: true }
105-
else method = { ...method }
102+
if (typeof method === 'function') method = { method, lazy: false }
103+
else method = { ...method, lazy: typeof method.traverse !== 'undefined' ? !method.traverse : method.lazy }
106104
Object.assign(method, omitUndefined({ deterministic, optimizeUnary }))
107105
this.methods[name] = declareSync(method)
108106
}
@@ -161,7 +159,13 @@ class LogicEngine {
161159
return res
162160
}
163161

164-
if (logic && typeof logic === 'object' && Object.keys(logic).length > 0) return this._parse(logic, data, above)
162+
if (logic && typeof logic === 'object') {
163+
const keys = Object.keys(logic)
164+
if (keys.length > 0) {
165+
const func = keys[0]
166+
return this._parse(logic, data, above, func)
167+
}
168+
}
165169

166170
return logic
167171
}

optimizer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function getMethod (logic, engine, methodName, above) {
1414
const method = engine.methods[methodName]
1515
const called = method.method ? method.method : method
1616

17-
if (method.traverse === false) {
17+
if (method.lazy) {
1818
const args = logic[methodName]
1919
return (data, abv) => called(args, data, abv || above, engine)
2020
}
@@ -30,7 +30,7 @@ function getMethod (logic, engine, methodName, above) {
3030
return called(evaluatedArgs, data, abv || above, engine)
3131
}
3232
} else {
33-
let optimizedArgs = optimize(args, engine, above)
33+
const optimizedArgs = optimize(args, engine, above)
3434
if (method.optimizeUnary) {
3535
if (typeof optimizedArgs === 'function') return (data, abv) => called(optimizedArgs(data, abv), data, abv || above, engine)
3636
return (data, abv) => called(optimizedArgs, data, abv || above, engine)

0 commit comments

Comments
 (0)