Skip to content

Commit

Permalink
feat: export toValueSync & defaultOptions to evaluate expression, see #…
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Aug 14, 2022
1 parent 753e8f9 commit e874b40
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/parser/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Tokenizer {

constructor (
public input: string,
private trie: Trie,
private trie: Trie = defaultOptions.operatorsTrie,
public file: string = ''
) {
this.N = input.length
Expand Down
2 changes: 1 addition & 1 deletion src/render/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class Expression {
public constructor (tokens: IterableIterator<Token>) {
this.postfix = [...toPostfix(tokens)]
}
public * evaluate (ctx: Context, lenient: boolean): Generator<unknown, unknown, unknown> {
public * evaluate (ctx: Context, lenient?: boolean): Generator<unknown, unknown, unknown> {
assert(ctx, 'unable to evaluate: context not defined')
const operands: any[] = []
for (const token of this.postfix) {
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ export { Tokenizer } from './parser/tokenizer'
export { Hash } from './template/tag/hash'
export { Value } from './template/value'
export { evalToken, evalQuotedToken } from './render/expression'
export { toPromise, toThenable } from './util/async'
export { toPromise, toThenable, toValueSync } from './util/async'
export { defaultOperators, Operators } from './render/operator'
export { createTrie, Trie } from './util/operator-trie'
export { toValue } from './util/underscore'
export { TimezoneDate } from './util/timezone-date'
export * as filters from './builtin/filters'
export * as tags from './builtin/tags'
export { defaultOptions } from './liquid-options'
8 changes: 7 additions & 1 deletion test/e2e/issues.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Liquid, Drop } from '../..'
import { Tokenizer, Context, Liquid, Drop, defaultOptions, toValueSync } from '../..'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import * as sinon from 'sinon'
Expand Down Expand Up @@ -260,4 +260,10 @@ describe('Issues', function () {
const engine = new Liquid()
expect(() => engine.parse('{% assign headshot = https://testurl.com/not_enclosed_in_quotes.jpg %}')).to.throw(/unexpected token at ":/)
})
it('#527 export Liquid Expression', () => {
const tokenizer = new Tokenizer('a > b', defaultOptions.operatorsTrie)
const expression = tokenizer.readExpression()
const result = toValueSync(expression.evaluate(new Context({ a: 1, b: 2 })))
expect(result).to.equal(false)
})
})
2 changes: 1 addition & 1 deletion test/integration/liquid/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('Liquid', function () {
describe('#enderToNodeStream', function () {
const engine = new Liquid()
it('should render a simple value', async () => {
const stream = await engine.renderToNodeStream(engine.parse('{{"foo"}}'))
const stream = engine.renderToNodeStream(engine.parse('{{"foo"}}'))
expect(drainStream(stream)).to.eventually.equal('foo')
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/unit/parser/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Tokenizer', function () {
const trie = createTrie(defaultOperators)

it('should read quoted', () => {
expect(new Tokenizer('"foo" ff', trie).readQuoted()!.getText()).to.equal('"foo"')
expect(new Tokenizer('"foo" ff').readQuoted()!.getText()).to.equal('"foo"')
expect(new Tokenizer(' "foo"ff', trie).readQuoted()!.getText()).to.equal('"foo"')
})
it('should read value', () => {
Expand Down

0 comments on commit e874b40

Please sign in to comment.