From e874b4060b46195e05a5cc6690626bdaa532154c Mon Sep 17 00:00:00 2001 From: Harttle Date: Mon, 15 Aug 2022 01:53:48 +0800 Subject: [PATCH] feat: export toValueSync & defaultOptions to evaluate expression, see #527 --- src/parser/tokenizer.ts | 2 +- src/render/expression.ts | 2 +- src/types.ts | 3 ++- test/e2e/issues.ts | 8 +++++++- test/integration/liquid/liquid.ts | 2 +- test/unit/parser/tokenizer.ts | 2 +- 6 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/parser/tokenizer.ts b/src/parser/tokenizer.ts index 7f02313ab0..fc472f5b8c 100644 --- a/src/parser/tokenizer.ts +++ b/src/parser/tokenizer.ts @@ -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 diff --git a/src/render/expression.ts b/src/render/expression.ts index a206c9c388..77c517f23c 100644 --- a/src/render/expression.ts +++ b/src/render/expression.ts @@ -20,7 +20,7 @@ export class Expression { public constructor (tokens: IterableIterator) { this.postfix = [...toPostfix(tokens)] } - public * evaluate (ctx: Context, lenient: boolean): Generator { + public * evaluate (ctx: Context, lenient?: boolean): Generator { assert(ctx, 'unable to evaluate: context not defined') const operands: any[] = [] for (const token of this.postfix) { diff --git a/src/types.ts b/src/types.ts index 1c48f8adce..731f48c58d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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' diff --git a/test/e2e/issues.ts b/test/e2e/issues.ts index f81a7be792..db7e5c26fc 100644 --- a/test/e2e/issues.ts +++ b/test/e2e/issues.ts @@ -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' @@ -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) + }) }) diff --git a/test/integration/liquid/liquid.ts b/test/integration/liquid/liquid.ts index 921cf0b40a..766ad5d63d 100644 --- a/test/integration/liquid/liquid.ts +++ b/test/integration/liquid/liquid.ts @@ -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') }) }) diff --git a/test/unit/parser/tokenizer.ts b/test/unit/parser/tokenizer.ts index 8ec6b0b945..d5e5d6d0e8 100644 --- a/test/unit/parser/tokenizer.ts +++ b/test/unit/parser/tokenizer.ts @@ -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', () => {