mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
feat: export toValueSync & defaultOptions to evaluate expression, see #527
This commit is contained in:
@@ -33,7 +33,7 @@ export class Tokenizer {
|
|||||||
|
|
||||||
constructor (
|
constructor (
|
||||||
public input: string,
|
public input: string,
|
||||||
private trie: Trie,
|
private trie: Trie = defaultOptions.operatorsTrie,
|
||||||
public file: string = ''
|
public file: string = ''
|
||||||
) {
|
) {
|
||||||
this.N = input.length
|
this.N = input.length
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export class Expression {
|
|||||||
public constructor (tokens: IterableIterator<Token>) {
|
public constructor (tokens: IterableIterator<Token>) {
|
||||||
this.postfix = [...toPostfix(tokens)]
|
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')
|
assert(ctx, 'unable to evaluate: context not defined')
|
||||||
const operands: any[] = []
|
const operands: any[] = []
|
||||||
for (const token of this.postfix) {
|
for (const token of this.postfix) {
|
||||||
|
|||||||
+2
-1
@@ -21,10 +21,11 @@ export { Tokenizer } from './parser/tokenizer'
|
|||||||
export { Hash } from './template/tag/hash'
|
export { Hash } from './template/tag/hash'
|
||||||
export { Value } from './template/value'
|
export { Value } from './template/value'
|
||||||
export { evalToken, evalQuotedToken } from './render/expression'
|
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 { defaultOperators, Operators } from './render/operator'
|
||||||
export { createTrie, Trie } from './util/operator-trie'
|
export { createTrie, Trie } from './util/operator-trie'
|
||||||
export { toValue } from './util/underscore'
|
export { toValue } from './util/underscore'
|
||||||
export { TimezoneDate } from './util/timezone-date'
|
export { TimezoneDate } from './util/timezone-date'
|
||||||
export * as filters from './builtin/filters'
|
export * as filters from './builtin/filters'
|
||||||
export * as tags from './builtin/tags'
|
export * as tags from './builtin/tags'
|
||||||
|
export { defaultOptions } from './liquid-options'
|
||||||
|
|||||||
+7
-1
@@ -1,4 +1,4 @@
|
|||||||
import { Liquid, Drop } from '../..'
|
import { Tokenizer, Context, Liquid, Drop, defaultOptions, toValueSync } from '../..'
|
||||||
import { expect, use } from 'chai'
|
import { expect, use } from 'chai'
|
||||||
import * as chaiAsPromised from 'chai-as-promised'
|
import * as chaiAsPromised from 'chai-as-promised'
|
||||||
import * as sinon from 'sinon'
|
import * as sinon from 'sinon'
|
||||||
@@ -260,4 +260,10 @@ describe('Issues', function () {
|
|||||||
const engine = new Liquid()
|
const engine = new Liquid()
|
||||||
expect(() => engine.parse('{% assign headshot = https://testurl.com/not_enclosed_in_quotes.jpg %}')).to.throw(/unexpected token at ":/)
|
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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -199,7 +199,7 @@ describe('Liquid', function () {
|
|||||||
describe('#enderToNodeStream', function () {
|
describe('#enderToNodeStream', function () {
|
||||||
const engine = new Liquid()
|
const engine = new Liquid()
|
||||||
it('should render a simple value', async () => {
|
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')
|
expect(drainStream(stream)).to.eventually.equal('foo')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ describe('Tokenizer', function () {
|
|||||||
const trie = createTrie(defaultOperators)
|
const trie = createTrie(defaultOperators)
|
||||||
|
|
||||||
it('should read quoted', () => {
|
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"')
|
expect(new Tokenizer(' "foo"ff', trie).readQuoted()!.getText()).to.equal('"foo"')
|
||||||
})
|
})
|
||||||
it('should read value', () => {
|
it('should read value', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user