Skip to content

val Proposal Implementation #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
62b9e81
Separate out the val proposal
TotalTechGeek Dec 9, 2024
2960e69
Make the val implementation more robust, so that it can run as optima…
TotalTechGeek Dec 9, 2024
0c51cbe
Address short circuiting in and / or interpreted
TotalTechGeek Dec 13, 2024
2844d1f
Merge branch 'master' into proposal/val
TotalTechGeek Dec 13, 2024
a28316d
Add array thing
TotalTechGeek Dec 20, 2024
0df7807
Merge branch 'master' into proposal/val
TotalTechGeek Dec 26, 2024
95adac6
Improve val testing
TotalTechGeek Dec 26, 2024
c197e74
Add a simple optimization to inline chained val logic.
TotalTechGeek Dec 26, 2024
d1a3b4a
disableInline not flagged properly in val, corrected.
TotalTechGeek Dec 26, 2024
75e096b
Erase varTop buildState
TotalTechGeek Dec 26, 2024
d9b89bd
Add implementation for exists, and coalescing, and test suites for each
TotalTechGeek Jan 3, 2025
4422db5
Touch up coalesce test suite
TotalTechGeek Jan 3, 2025
5d8ab20
Remove the legacy methods from the default methods javascript file
TotalTechGeek Jan 3, 2025
3609975
Merge remote-tracking branch 'origin/master' into proposal/val
TotalTechGeek Jan 3, 2025
ddd886e
Some of the rewrites made things mildly slower, so I looked for an op…
TotalTechGeek Jan 3, 2025
83f8b4d
Further improvements and optimizations (some carried over from the pr…
TotalTechGeek Jan 3, 2025
2292bf9
Add annotation to work around exotic build issue...
TotalTechGeek Jan 3, 2025
cf0ec8a
Expand test suite
TotalTechGeek Jan 3, 2025
2b22100
Commit one more test (I guess this guy didn't get added earlier)
TotalTechGeek Jan 4, 2025
e52f768
Switch more tests to `val`, make further optimizations.
TotalTechGeek Jan 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions asyncLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import defaultMethods from './defaultMethods.js'
import LogicEngine from './logic.js'
import { isSync } from './constants.js'
import { isSync, OriginalImpl } from './constants.js'
import declareSync from './utilities/declareSync.js'
import { buildAsync } from './compiler.js'
import omitUndefined from './utilities/omitUndefined.js'
Expand Down Expand Up @@ -75,6 +75,13 @@ class AsyncLogicEngine {
if (this.isData(logic, func)) return logic
if (!this.methods[func]) throw new Error(`Method '${func}' was not found in the Logic Engine.`)

// A small but useful micro-optimization for some of the most common functions.
// Later on, I could define something to shut this off if var / val are redefined.
if ((func === 'var' || func === 'val') && this.methods[func][OriginalImpl]) {
const input = (!data || typeof data !== 'object') ? data : this.fallback.run(data, context, { above })
return this.methods[func].method(input, context, above, this)
}

if (typeof this.methods[func] === 'function') {
const input = (!data || typeof data !== 'object') ? [data] : await this.run(data, context, { above })
const result = await this.methods[func](coerceArray(input), context, above, this)
Expand Down Expand Up @@ -210,5 +217,5 @@ class AsyncLogicEngine {
return logic
}
}
Object.assign(AsyncLogicEngine.prototype.truthy, { IDENTITY: true })
Object.assign(AsyncLogicEngine.prototype.truthy, { [OriginalImpl]: true })
export default AsyncLogicEngine
10 changes: 5 additions & 5 deletions async_optimizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function getMethod (logic, engine, methodName, above) {
}

let args = logic[methodName]
if (!args || typeof args !== 'object') args = [args]
if ((!args || typeof args !== 'object') && !method.optimizeUnary) args = [args]

if (Array.isArray(args)) {
const optimizedArgs = args.map(l => optimize(l, engine, above))
Expand All @@ -50,12 +50,12 @@ function getMethod (logic, engine, methodName, above) {

if (isSync(optimizedArgs) && (method.method || method[Sync])) {
const called = method.method ? method.method : method
return declareSync((data, abv) => called(coerceArray(typeof optimizedArgs === 'function' ? optimizedArgs(data, abv) : optimizedArgs, method.optimizeUnary), data, abv || above, engine), true)
if (method.optimizeUnary) return declareSync((data, abv) => called(typeof optimizedArgs === 'function' ? optimizedArgs(data, abv) : optimizedArgs, data, abv || above, engine.fallback), true)
return declareSync((data, abv) => called(coerceArray(typeof optimizedArgs === 'function' ? optimizedArgs(data, abv) : optimizedArgs), data, abv || above, engine), true)
}

return async (data, abv) => {
return called(coerceArray(typeof optimizedArgs === 'function' ? await optimizedArgs(data, abv) : optimizedArgs, method.optimizeUnary), data, abv || above, engine)
}
if (method.optimizeUnary) return async (data, abv) => called(typeof optimizedArgs === 'function' ? await optimizedArgs(data, abv) : optimizedArgs, data, abv || above, engine)
return async (data, abv) => called(coerceArray(typeof optimizedArgs === 'function' ? await optimizedArgs(data, abv) : optimizedArgs), data, abv || above, engine)
}
}

Expand Down
2 changes: 2 additions & 0 deletions compatibility.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Sync } from './constants.js'
import defaultMethods from './defaultMethods.js'
const oldAll = defaultMethods.all

const all = {
[Sync]: oldAll[Sync],
method: (args, context, above, engine) => {
if (Array.isArray(args)) {
const first = engine.run(args[0], context, above)
Expand Down
2 changes: 1 addition & 1 deletion compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function buildString (method, buildState = {}) {
}

let coerce = engine.methods[func].optimizeUnary ? '' : 'coerceArray'
if (!coerce && Array.isArray(lower) && lower.length === 1) lower = lower[0]
if (!coerce && Array.isArray(lower) && lower.length === 1 && !Array.isArray(lower[0])) lower = lower[0]
else if (coerce && Array.isArray(lower)) coerce = ''

const argumentsDict = [', context', ', context, above', ', context, above, engine']
Expand Down
5 changes: 3 additions & 2 deletions constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

export const Sync = Symbol.for('json_logic_sync')
export const Compiled = Symbol.for('json_logic_compiled')
export const EfficientTop = Symbol.for('json_logic_efficientTop')
export const OriginalImpl = Symbol.for('json_logic_original')
export const Unfound = Symbol.for('json_logic_unfound')

/**
* Checks if an item is synchronous.
Expand All @@ -22,6 +23,6 @@ export function isSync (item) {

export default {
Sync,
EfficientTop,
OriginalImpl,
isSync
}
Loading
Loading