Skip to content

Commit e874b40

Browse files
committed
feat: export toValueSync & defaultOptions to evaluate expression, see #527
1 parent 753e8f9 commit e874b40

File tree

6 files changed

+13
-6
lines changed

6 files changed

+13
-6
lines changed

src/parser/tokenizer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class Tokenizer {
3333

3434
constructor (
3535
public input: string,
36-
private trie: Trie,
36+
private trie: Trie = defaultOptions.operatorsTrie,
3737
public file: string = ''
3838
) {
3939
this.N = input.length

src/render/expression.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class Expression {
2020
public constructor (tokens: IterableIterator<Token>) {
2121
this.postfix = [...toPostfix(tokens)]
2222
}
23-
public * evaluate (ctx: Context, lenient: boolean): Generator<unknown, unknown, unknown> {
23+
public * evaluate (ctx: Context, lenient?: boolean): Generator<unknown, unknown, unknown> {
2424
assert(ctx, 'unable to evaluate: context not defined')
2525
const operands: any[] = []
2626
for (const token of this.postfix) {

src/types.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ export { Tokenizer } from './parser/tokenizer'
2121
export { Hash } from './template/tag/hash'
2222
export { Value } from './template/value'
2323
export { evalToken, evalQuotedToken } from './render/expression'
24-
export { toPromise, toThenable } from './util/async'
24+
export { toPromise, toThenable, toValueSync } from './util/async'
2525
export { defaultOperators, Operators } from './render/operator'
2626
export { createTrie, Trie } from './util/operator-trie'
2727
export { toValue } from './util/underscore'
2828
export { TimezoneDate } from './util/timezone-date'
2929
export * as filters from './builtin/filters'
3030
export * as tags from './builtin/tags'
31+
export { defaultOptions } from './liquid-options'

test/e2e/issues.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Liquid, Drop } from '../..'
1+
import { Tokenizer, Context, Liquid, Drop, defaultOptions, toValueSync } from '../..'
22
import { expect, use } from 'chai'
33
import * as chaiAsPromised from 'chai-as-promised'
44
import * as sinon from 'sinon'
@@ -260,4 +260,10 @@ describe('Issues', function () {
260260
const engine = new Liquid()
261261
expect(() => engine.parse('{% assign headshot = https://testurl.com/not_enclosed_in_quotes.jpg %}')).to.throw(/unexpected token at ":/)
262262
})
263+
it('#527 export Liquid Expression', () => {
264+
const tokenizer = new Tokenizer('a > b', defaultOptions.operatorsTrie)
265+
const expression = tokenizer.readExpression()
266+
const result = toValueSync(expression.evaluate(new Context({ a: 1, b: 2 })))
267+
expect(result).to.equal(false)
268+
})
263269
})

test/integration/liquid/liquid.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('Liquid', function () {
199199
describe('#enderToNodeStream', function () {
200200
const engine = new Liquid()
201201
it('should render a simple value', async () => {
202-
const stream = await engine.renderToNodeStream(engine.parse('{{"foo"}}'))
202+
const stream = engine.renderToNodeStream(engine.parse('{{"foo"}}'))
203203
expect(drainStream(stream)).to.eventually.equal('foo')
204204
})
205205
})

test/unit/parser/tokenizer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('Tokenizer', function () {
1717
const trie = createTrie(defaultOperators)
1818

1919
it('should read quoted', () => {
20-
expect(new Tokenizer('"foo" ff', trie).readQuoted()!.getText()).to.equal('"foo"')
20+
expect(new Tokenizer('"foo" ff').readQuoted()!.getText()).to.equal('"foo"')
2121
expect(new Tokenizer(' "foo"ff', trie).readQuoted()!.getText()).to.equal('"foo"')
2222
})
2323
it('should read value', () => {

0 commit comments

Comments
 (0)