Skip to content

Commit e3b45f5

Browse files
committed
Rewrite cat, simplify some things w/r/t the precision downgrade... I guess I don't need it
1 parent 41ec175 commit e3b45f5

File tree

4 files changed

+32
-25
lines changed

4 files changed

+32
-25
lines changed

defaultMethods.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -520,11 +520,25 @@ const defaultMethods = {
520520
},
521521
'!': (value, _1, _2, engine) => Array.isArray(value) ? !engine.truthy(value[0]) : !engine.truthy(value),
522522
'!!': (value, _1, _2, engine) => Boolean(Array.isArray(value) ? engine.truthy(value[0]) : engine.truthy(value)),
523-
cat: (arr) => {
524-
if (typeof arr === 'string') return arr
525-
let res = ''
526-
for (let i = 0; i < arr.length; i++) res += arr[i]
527-
return res
523+
cat: {
524+
method: (arr) => {
525+
if (typeof arr === 'string') return arr
526+
if (!Array.isArray(arr)) return arr.toString()
527+
let res = ''
528+
for (let i = 0; i < arr.length; i++) res += arr[i].toString()
529+
return res
530+
},
531+
deterministic: true,
532+
traverse: true,
533+
optimizeUnary: true,
534+
compile: (data, buildState) => {
535+
if (typeof data === 'string') return JSON.stringify(data)
536+
if (typeof data === 'number') return '"' + JSON.stringify(data) + '"'
537+
if (!Array.isArray(data)) return false
538+
let res = buildState.compile`''`
539+
for (let i = 0; i < data.length; i++) res = buildState.compile`${res} + ${data[i]}`
540+
return buildState.compile`(${res})`
541+
}
528542
},
529543
keys: ([obj]) => typeof obj === 'object' ? Object.keys(obj) : [],
530544
pipe: {
@@ -843,14 +857,6 @@ defaultMethods['*'].compile = function (data, buildState) {
843857
return `(${buildString(data, buildState)}).reduce((a,b) => (+a)*(+b))`
844858
}
845859
}
846-
// @ts-ignore Allow custom attribute
847-
defaultMethods.cat.compile = function (data, buildState) {
848-
if (typeof data === 'string') return JSON.stringify(data)
849-
if (!Array.isArray(data)) return false
850-
let res = buildState.compile`''`
851-
for (let i = 0; i < data.length; i++) res = buildState.compile`${res} + ${data[i]}`
852-
return buildState.compile`(${res})`
853-
}
854860

855861
// @ts-ignore Allow custom attribute
856862
defaultMethods['!'].compile = function (

logic.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,7 @@ class LogicEngine {
154154
return res
155155
}
156156

157-
if (logic && typeof logic === 'object') {
158-
if (this.precision && !options.precise && logic.toNumber) return Number(logic)
159-
if (Object.keys(logic).length > 0) return this._parse(logic, data, above)
160-
}
157+
if (logic && typeof logic === 'object' && Object.keys(logic).length > 0) return this._parse(logic, data, above)
161158

162159
return logic
163160
}

precision/index.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ export function configurePrecision (engine, constructor) {
1717
return false
1818
},
1919
optimizeUnary: true,
20-
deterministic: true,
21-
precise: true
20+
deterministic: true
2221
})
2322

2423
engine.addMethod('-', {
@@ -38,8 +37,7 @@ export function configurePrecision (engine, constructor) {
3837
return false
3938
},
4039
optimizeUnary: true,
41-
deterministic: true,
42-
precise: true
40+
deterministic: true
4341
})
4442

4543
engine.addMethod('*', {
@@ -74,8 +72,7 @@ export function configurePrecision (engine, constructor) {
7472
}
7573
return false
7674
},
77-
deterministic: true,
78-
precise: true
75+
deterministic: true
7976
})
8077

8178
engine.addMethod('%', {
@@ -92,7 +89,6 @@ export function configurePrecision (engine, constructor) {
9289
}
9390
return false
9491
},
95-
deterministic: true,
96-
precise: true
92+
deterministic: true
9793
})
9894
}

precision/scratch.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import { LogicEngine } from '../index.js'
22
import { Decimal } from 'decimal.js'
33
import { configurePrecision } from './index.js'
44

5+
Decimal.prototype.toString = function () {
6+
return this.toFixed()
7+
}
8+
9+
Decimal.prototype.valueOf = function () {
10+
return this.toFixed()
11+
}
12+
513
const ieee754Engine = new LogicEngine()
614
const decimalEngine = new LogicEngine()
715
configurePrecision(decimalEngine, Decimal.clone({ precision: 100 }))

0 commit comments

Comments
 (0)