mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat: use evalValue to parse & render expression, #527
This commit is contained in:
+6
-5
@@ -90,15 +90,16 @@ export class Liquid {
|
|||||||
return this.renderToNodeStream(templates, scope, renderOptions)
|
return this.renderToNodeStream(templates, scope, renderOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
public _evalValue (str: string, ctx: Context): IterableIterator<any> {
|
public _evalValue (str: string, scopeOrContext?: object | Context): IterableIterator<any> {
|
||||||
const value = new Value(str, this)
|
const value = new Value(str, this)
|
||||||
|
const ctx = scopeOrContext instanceof Context ? scopeOrContext : new Context(scopeOrContext, this.options)
|
||||||
return value.value(ctx, false)
|
return value.value(ctx, false)
|
||||||
}
|
}
|
||||||
public async evalValue (str: string, ctx: Context): Promise<any> {
|
public async evalValue (str: string, scopeOrContext?: object | Context): Promise<any> {
|
||||||
return toPromise(this._evalValue(str, ctx))
|
return toPromise(this._evalValue(str, scopeOrContext))
|
||||||
}
|
}
|
||||||
public evalValueSync (str: string, ctx: Context): any {
|
public evalValueSync (str: string, scopeOrContext?: object | Context): any {
|
||||||
return toValueSync(this._evalValue(str, ctx))
|
return toValueSync(this._evalValue(str, scopeOrContext))
|
||||||
}
|
}
|
||||||
|
|
||||||
public registerFilter (name: string, filter: FilterImplOptions) {
|
public registerFilter (name: string, filter: FilterImplOptions) {
|
||||||
|
|||||||
+24
-4
@@ -1,12 +1,32 @@
|
|||||||
import { expect } from 'chai'
|
import { expect } from 'chai'
|
||||||
import { Liquid } from '../..'
|
import { Context, Liquid } from '../..'
|
||||||
|
|
||||||
describe('#evalValue()', function () {
|
describe('#evalValue()', function () {
|
||||||
var engine: Liquid
|
var engine: Liquid
|
||||||
beforeEach(() => { engine = new Liquid() })
|
beforeEach(() => { engine = new Liquid({ globals: { foo: 'FOO' } }) })
|
||||||
|
|
||||||
it('should eval value', async function () {
|
it('should support boolean', async function () {
|
||||||
const val = await engine.evalValue('true', { opts: {} } as any)
|
const val = await engine.evalValue('true')
|
||||||
expect(val).to.equal(true)
|
expect(val).to.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should support binary expression with Context', async function () {
|
||||||
|
const val = await engine.evalValue('a > b', { a: 1, b: 2 })
|
||||||
|
expect(val).to.equal(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should inherit Liquid options', async function () {
|
||||||
|
const val = await engine.evalValue('foo')
|
||||||
|
expect(val).to.equal('FOO')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should support passing Context', async function () {
|
||||||
|
const val = await engine.evalValue('a > b', new Context({ a: 1, b: 2 }))
|
||||||
|
expect(val).to.equal(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should respect options in passed in Context', async function () {
|
||||||
|
const val = await engine.evalValue('foo', new Context({}, { globals: { foo: 'BAR' } } as any))
|
||||||
|
expect(val).to.equal('BAR')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -266,4 +266,14 @@ describe('Issues', function () {
|
|||||||
const result = toValueSync(expression.evaluate(new Context({ a: 1, b: 2 })))
|
const result = toValueSync(expression.evaluate(new Context({ a: 1, b: 2 })))
|
||||||
expect(result).to.equal(false)
|
expect(result).to.equal(false)
|
||||||
})
|
})
|
||||||
|
it('#527 export Liquid Expression (evalValue)', async () => {
|
||||||
|
const liquid = new Liquid()
|
||||||
|
const result = await liquid.evalValue('a > b', { a: 1, b: 2 })
|
||||||
|
expect(result).to.equal(false)
|
||||||
|
})
|
||||||
|
it('#527 export Liquid Expression (evalValueSync)', async () => {
|
||||||
|
const liquid = new Liquid()
|
||||||
|
const result = liquid.evalValueSync('a > b', { a: 1, b: 2 })
|
||||||
|
expect(result).to.equal(false)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user