Skip to content

Commit 071368a

Browse files
committedAug 24, 2022
feat: use evalValue to parse & render expression, #527
1 parent 2089b9b commit 071368a

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed
 

‎src/liquid.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,16 @@ export class Liquid {
9090
return this.renderToNodeStream(templates, scope, renderOptions)
9191
}
9292

93-
public _evalValue (str: string, ctx: Context): IterableIterator<any> {
93+
public _evalValue (str: string, scopeOrContext?: object | Context): IterableIterator<any> {
9494
const value = new Value(str, this)
95+
const ctx = scopeOrContext instanceof Context ? scopeOrContext : new Context(scopeOrContext, this.options)
9596
return value.value(ctx, false)
9697
}
97-
public async evalValue (str: string, ctx: Context): Promise<any> {
98-
return toPromise(this._evalValue(str, ctx))
98+
public async evalValue (str: string, scopeOrContext?: object | Context): Promise<any> {
99+
return toPromise(this._evalValue(str, scopeOrContext))
99100
}
100-
public evalValueSync (str: string, ctx: Context): any {
101-
return toValueSync(this._evalValue(str, ctx))
101+
public evalValueSync (str: string, scopeOrContext?: object | Context): any {
102+
return toValueSync(this._evalValue(str, scopeOrContext))
102103
}
103104

104105
public registerFilter (name: string, filter: FilterImplOptions) {

‎test/e2e/eval-value.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
11
import { expect } from 'chai'
2-
import { Liquid } from '../..'
2+
import { Context, Liquid } from '../..'
33

44
describe('#evalValue()', function () {
55
var engine: Liquid
6-
beforeEach(() => { engine = new Liquid() })
6+
beforeEach(() => { engine = new Liquid({ globals: { foo: 'FOO' } }) })
77

8-
it('should eval value', async function () {
9-
const val = await engine.evalValue('true', { opts: {} } as any)
8+
it('should support boolean', async function () {
9+
const val = await engine.evalValue('true')
1010
expect(val).to.equal(true)
1111
})
12+
13+
it('should support binary expression with Context', async function () {
14+
const val = await engine.evalValue('a > b', { a: 1, b: 2 })
15+
expect(val).to.equal(false)
16+
})
17+
18+
it('should inherit Liquid options', async function () {
19+
const val = await engine.evalValue('foo')
20+
expect(val).to.equal('FOO')
21+
})
22+
23+
it('should support passing Context', async function () {
24+
const val = await engine.evalValue('a > b', new Context({ a: 1, b: 2 }))
25+
expect(val).to.equal(false)
26+
})
27+
28+
it('should respect options in passed in Context', async function () {
29+
const val = await engine.evalValue('foo', new Context({}, { globals: { foo: 'BAR' } } as any))
30+
expect(val).to.equal('BAR')
31+
})
1232
})

‎test/e2e/issues.ts

+10
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,14 @@ describe('Issues', function () {
266266
const result = toValueSync(expression.evaluate(new Context({ a: 1, b: 2 })))
267267
expect(result).to.equal(false)
268268
})
269+
it('#527 export Liquid Expression (evalValue)', async () => {
270+
const liquid = new Liquid()
271+
const result = await liquid.evalValue('a > b', { a: 1, b: 2 })
272+
expect(result).to.equal(false)
273+
})
274+
it('#527 export Liquid Expression (evalValueSync)', async () => {
275+
const liquid = new Liquid()
276+
const result = liquid.evalValueSync('a > b', { a: 1, b: 2 })
277+
expect(result).to.equal(false)
278+
})
269279
})

0 commit comments

Comments
 (0)
Please sign in to comment.