Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 22 additions & 16 deletions compatible.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ for (const file of files) {
}
}

function correction (x) {
// eslint-disable-next-line no-compare-neg-zero
if (x === -0) return 0
return x
}

// eslint-disable-next-line no-labels
inline: {
const logic = new LogicEngine(undefined, { compatible: true })
Expand All @@ -25,13 +31,13 @@ inline: {
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)}`, () => {
expect(logic.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
expect(correction(logic.run(testCase[0], testCase[1]))).toStrictEqual(testCase[2])
})

test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (async)`, async () => {
expect(await asyncLogic.run(testCase[0], testCase[1])).toStrictEqual(
expect(correction(await asyncLogic.run(testCase[0], testCase[1]))).toStrictEqual(
testCase[2]
)
})
Expand All @@ -40,26 +46,26 @@ inline: {
testCase[1]
)} (built)`, () => {
const f = logic.build(testCase[0])
expect(f(testCase[1])).toStrictEqual(testCase[2])
expect(correction(f(testCase[1]))).toStrictEqual(testCase[2])
})

test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (asyncBuilt)`, async () => {
const f = await asyncLogic.build(testCase[0])
expect(await f(testCase[1])).toStrictEqual(testCase[2])
expect(correction(await f(testCase[1]))).toStrictEqual(testCase[2])
})

test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (noOptimization)`, () => {
expect(logicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
expect(correction(logicWithoutOptimization.run(testCase[0], testCase[1]))).toStrictEqual(testCase[2])
})

test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (asyncNoOptimization)`, async () => {
expect(await asyncLogicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(
expect(correction(await asyncLogicWithoutOptimization.run(testCase[0], testCase[1]))).toStrictEqual(
testCase[2]
)
})
Expand All @@ -68,14 +74,14 @@ inline: {
testCase[1]
)} (builtNoOptimization)`, () => {
const f = logicWithoutOptimization.build(testCase[0])
expect(f(testCase[1])).toStrictEqual(testCase[2])
expect(correction(f(testCase[1]))).toStrictEqual(testCase[2])
})

test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (asyncBuiltNoOptimization)`, async () => {
const f = await asyncLogicWithoutOptimization.build(testCase[0])
expect(await f(testCase[1])).toStrictEqual(testCase[2])
expect(correction(await f(testCase[1]))).toStrictEqual(testCase[2])
})
})
})
Expand All @@ -97,13 +103,13 @@ notInline: {
test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)}`, () => {
expect(logic.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
expect(correction(logic.run(testCase[0], testCase[1]))).toStrictEqual(testCase[2])
})

test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (async)`, async () => {
expect(await asyncLogic.run(testCase[0], testCase[1])).toStrictEqual(
expect(correction(await asyncLogic.run(testCase[0], testCase[1]))).toStrictEqual(
testCase[2]
)
})
Expand All @@ -112,26 +118,26 @@ notInline: {
testCase[1]
)} (built)`, () => {
const f = logic.build(testCase[0])
expect(f(testCase[1])).toStrictEqual(testCase[2])
expect(correction(f(testCase[1]))).toStrictEqual(testCase[2])
})

test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (asyncBuilt)`, async () => {
const f = await asyncLogic.build(testCase[0])
expect(await f(testCase[1])).toStrictEqual(testCase[2])
expect(correction(await f(testCase[1]))).toStrictEqual(testCase[2])
})

test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (noOptimization)`, () => {
expect(logicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(testCase[2])
expect(correction(logicWithoutOptimization.run(testCase[0], testCase[1]))).toStrictEqual(testCase[2])
})

test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (asyncNoOptimization)`, async () => {
expect(await asyncLogicWithoutOptimization.run(testCase[0], testCase[1])).toStrictEqual(
expect(correction(await asyncLogicWithoutOptimization.run(testCase[0], testCase[1]))).toStrictEqual(
testCase[2]
)
})
Expand All @@ -140,14 +146,14 @@ notInline: {
testCase[1]
)} (builtNoOptimization)`, () => {
const f = logicWithoutOptimization.build(testCase[0])
expect(f(testCase[1])).toStrictEqual(testCase[2])
expect(correction(f(testCase[1]))).toStrictEqual(testCase[2])
})

test(`${JSON.stringify(testCase[0])} ${JSON.stringify(
testCase[1]
)} (asyncBuiltNoOptimization)`, async () => {
const f = await asyncLogicWithoutOptimization.build(testCase[0])
expect(await f(testCase[1])).toStrictEqual(testCase[2])
expect(correction(await f(testCase[1]))).toStrictEqual(testCase[2])
})
})
}
4 changes: 2 additions & 2 deletions compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function isDeepSync (method, engine) {

if (Array.isArray(method)) return method.every(i => isDeepSync(i, engine))

if (typeof method === 'object') {
if (method && typeof method === 'object') {
const func = Object.keys(method)[0]

const lower = method[func]
Expand Down Expand Up @@ -307,7 +307,7 @@ function processBuiltString (method, str, buildState) {
str = str.replace(`__%%%${x}%%%__`, item)
})

const final = `(values, methods, notTraversed, asyncIterators, engine, above, coerceArray) => ${buildState.asyncDetected ? 'async' : ''} (context ${buildState.extraArguments ? ',' + buildState.extraArguments : ''}) => { const result = ${str}; return result }`
const final = `(values, methods, notTraversed, asyncIterators, engine, above, coerceArray) => ${buildState.asyncDetected ? 'async' : ''} (context ${buildState.extraArguments ? ',' + buildState.extraArguments : ''}) => { let prev; const result = ${str}; return result }`
// console.log(str)
// console.log(final)
// eslint-disable-next-line no-eval
Expand Down
45 changes: 30 additions & 15 deletions defaultMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ function isSyncDeep (method, engine, buildState) {

const defaultMethods = {
'+': (data) => {
if (!data) return 0
if (typeof data === 'string') return +data
if (typeof data === 'number') return +data
if (typeof data === 'boolean') return +data
let res = 0
for (let i = 0; i < data.length; i++) res += +data[i]
return res
Expand All @@ -64,20 +66,22 @@ const defaultMethods = {
return res
},
'/': (data) => {
let res = data[0]
let res = +data[0]
for (let i = 1; i < data.length; i++) res /= +data[i]
return res
},
'-': (data) => {
if (!data) return 0
if (typeof data === 'string') return -data
if (typeof data === 'number') return -data
if (typeof data === 'boolean') return -data
if (data.length === 1) return -data[0]
let res = data[0]
for (let i = 1; i < data.length; i++) res -= +data[i]
return res
},
'%': (data) => {
let res = data[0]
let res = +data[0]
for (let i = 1; i < data.length; i++) res %= +data[i]
return res
},
Expand Down Expand Up @@ -446,7 +450,18 @@ const defaultMethods = {
return result ? buildState.compile`!(${result})` : false
}
},
merge: (arrays) => (Array.isArray(arrays) ? [].concat(...arrays) : [arrays]),
merge: (args) => {
if (!Array.isArray(args)) return [args]
const result = []
for (let i = 0; i < args.length; i++) {
if (Array.isArray(args[i])) {
for (let j = 0; j < args[i].length; j++) {
result.push(args[i][j])
}
} else result.push(args[i])
}
return result
},
every: createArrayIterativeMethod('every'),
filter: createArrayIterativeMethod('filter', true),
reduce: {
Expand Down Expand Up @@ -752,16 +767,16 @@ Object.keys(defaultMethods).forEach((item) => {
defaultMethods['<'].compile = function (data, buildState) {
if (!Array.isArray(data)) return false
if (data.length < 2) return false
let res = buildState.compile`(${data[0]} < ${data[1]})`
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && ${data[i - 1]} < ${data[i]})`
let res = buildState.compile`(${data[0]} < (prev = ${data[1]}))`
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && prev < ${data[i]})`
return res
}
// @ts-ignore Allow custom attribute
defaultMethods['<='].compile = function (data, buildState) {
if (!Array.isArray(data)) return false
if (data.length < 2) return false
let res = buildState.compile`(${data[0]} <= ${data[1]})`
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && ${data[i - 1]} <= ${data[i]})`
let res = buildState.compile`(${data[0]} <= (prev = ${data[1]}))`
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && prev <= ${data[i]})`
return res
}
// @ts-ignore Allow custom attribute
Expand All @@ -782,32 +797,32 @@ defaultMethods.max.compile = function (data, buildState) {
defaultMethods['>'].compile = function (data, buildState) {
if (!Array.isArray(data)) return false
if (data.length < 2) return false
let res = buildState.compile`(${data[0]} > ${data[1]})`
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && ${data[i - 1]} > ${data[i]})`
let res = buildState.compile`(${data[0]} > (prev = ${data[1]}))`
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && prev > ${data[i]})`
return res
}
// @ts-ignore Allow custom attribute
defaultMethods['>='].compile = function (data, buildState) {
if (!Array.isArray(data)) return false
if (data.length < 2) return false
let res = buildState.compile`(${data[0]} >= ${data[1]})`
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && ${data[i - 1]} >= ${data[i]})`
let res = buildState.compile`(${data[0]} >= (prev = ${data[1]}))`
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && prev >= ${data[i]})`
return res
}
// @ts-ignore Allow custom attribute
defaultMethods['=='].compile = function (data, buildState) {
if (!Array.isArray(data)) return false
if (data.length < 2) return false
let res = buildState.compile`(${data[0]} == ${data[1]})`
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && ${data[i - 1]} == ${data[i]})`
let res = buildState.compile`(${data[0]} == (prev = ${data[1]}))`
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && prev == ${data[i]})`
return res
}
// @ts-ignore Allow custom attribute
defaultMethods['!='].compile = function (data, buildState) {
if (!Array.isArray(data)) return false
if (data.length < 2) return false
let res = buildState.compile`(${data[0]} != ${data[1]})`
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && ${data[i - 1]} != ${data[i]})`
let res = buildState.compile`(${data[0]} != (prev = ${data[1]}))`
for (let i = 2; i < data.length; i++) res = buildState.compile`(${res} && prev != ${data[i]})`
return res
}
// @ts-ignore Allow custom attribute
Expand Down
6 changes: 6 additions & 0 deletions suites/chained.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,11 @@
},
"result": 149212,
"description": "Max with Logic Chaining (Complex)"
},
{
"description": "Addition Chained w/ Merge",
"rule": { "+": { "merge": [{ "val": "x" }, { "val": "y" }] }},
"result": 6,
"data": { "x": [1, 2], "y": 3 }
}
]
Loading
Loading