diff --git a/src/liquid-options.ts b/src/liquid-options.ts index 1a0ebf8b7..4224482f1 100644 --- a/src/liquid-options.ts +++ b/src/liquid-options.ts @@ -3,7 +3,7 @@ import { Template } from './template/template' import { Cache } from './cache/cache' import { LRU } from './cache/lru' import { FS } from './fs/fs' -import { Operators, OperatorMap } from './render/operator' +import { defaultOperators, Operators } from './render/operator' export interface LiquidOptions { /** A directory or an array of directories from where to resolve layout and include templates, and the filename passed to `.renderFile()`. If it's an array, the files are looked up in the order they occur in the array. Defaults to `["."]` */ @@ -49,7 +49,7 @@ export interface LiquidOptions { /** Whether or not to keep value type when writing the Output. Defaults to `false`. */ keepOutputType?: boolean; /** An object of operators for conditional statements. Defaults to the regular Liquid operators. */ - operators?: OperatorMap; + operators?: Operators; } interface NormalizedOptions extends LiquidOptions { @@ -78,7 +78,7 @@ export interface NormalizedFullOptions extends NormalizedOptions { greedy: boolean; globals: object; keepOutputType: boolean; - operators: OperatorMap; + operators: Operators; } export const defaultOptions: NormalizedFullOptions = { @@ -102,7 +102,7 @@ export const defaultOptions: NormalizedFullOptions = { lenientIf: false, globals: {}, keepOutputType: false, - operators: Operators + operators: defaultOperators } export function normalize (options?: LiquidOptions): NormalizedOptions { diff --git a/src/render/expression.ts b/src/render/expression.ts index 1e71886b6..05395985e 100644 --- a/src/render/expression.ts +++ b/src/render/expression.ts @@ -11,16 +11,16 @@ import { parseStringLiteral } from '../parser/parse-string-literal' import { Context } from '../context/context' import { range, toValue } from '../util/underscore' import { Tokenizer } from '../parser/tokenizer' -import { OperatorMap } from '../render/operator' +import { Operators } from '../render/operator' import { UndefinedVariableError, InternalUndefinedVariableError } from '../util/error' export class Expression { private operands: any[] = [] private postfix: Token[] private lenient: boolean - private operators: OperatorMap + private operators: Operators - public constructor (str: string, operators: OperatorMap, lenient = false) { + public constructor (str: string, operators: Operators, lenient = false) { const tokenizer = new Tokenizer(str) this.postfix = [...toPostfix(tokenizer.readExpression())] this.lenient = lenient @@ -75,7 +75,7 @@ export function evalQuotedToken (token: QuotedToken) { return parseStringLiteral(token.getText()) } -function evalOperatorToken (operators: OperatorMap, token: OperatorToken, lhs: any, rhs: any, ctx: Context) { +function evalOperatorToken (operators: Operators, token: OperatorToken, lhs: any, rhs: any, ctx: Context) { const impl = operators[token.operator] return impl(lhs, rhs, ctx) } diff --git a/src/render/operator.ts b/src/render/operator.ts index 427e6403f..0e0a25378 100644 --- a/src/render/operator.ts +++ b/src/render/operator.ts @@ -3,11 +3,11 @@ import { Context } from '../context/context' import { isFunction } from '../util/underscore' import { isTruthy } from '../render/boolean' -export interface OperatorMap { +export interface Operators { [key: string]: (lhs: any, rhs: any, ctx: Context) => boolean; } -export const Operators: OperatorMap = { +export const defaultOperators: Operators = { '==': (l: any, r: any) => { if (isComparable(l)) return l.equals(r) if (isComparable(r)) return r.equals(l) diff --git a/src/types.ts b/src/types.ts index 034f7b698..9e5da28cd 100644 --- a/src/types.ts +++ b/src/types.ts @@ -19,4 +19,4 @@ export { Tokenizer } from './parser/tokenizer' export { Hash } from './template/tag/hash' export { evalToken, evalQuotedToken } from './render/expression' export { toPromise, toThenable, toValue } from './util/async' -export { Operators, OperatorMap } from './render/operator' +export { defaultOperators, Operators } from './render/operator' diff --git a/test/integration/liquid/operators-option.ts b/test/integration/liquid/operators-option.ts index 11f04b214..7d4578f29 100644 --- a/test/integration/liquid/operators-option.ts +++ b/test/integration/liquid/operators-option.ts @@ -1,5 +1,5 @@ import { expect } from 'chai' -import { Liquid, Operators } from '../../../src/liquid' +import { Liquid, defaultOperators } from '../../../src/liquid' describe('LiquidOptions#operators', function () { let engine: Liquid @@ -7,7 +7,7 @@ describe('LiquidOptions#operators', function () { beforeEach(function () { engine = new Liquid({ operators: { - ...Operators, + ...defaultOperators, isFooBar: (l, r) => l === 'foo' && r === 'bar' } }) diff --git a/test/unit/render/expression.ts b/test/unit/render/expression.ts index 0d52e65a1..e134cefcf 100644 --- a/test/unit/render/expression.ts +++ b/test/unit/render/expression.ts @@ -2,13 +2,13 @@ import { Expression } from '../../../src/render/expression' import { expect } from 'chai' import { Context } from '../../../src/context/context' import { toThenable } from '../../../src/util/async' -import { Operators } from '../../../src/render/operator' +import { defaultOperators } from '../../../src/render/operator' describe('Expression', function () { const ctx = new Context({}) it('should throw when context not defined', done => { - toThenable(new Expression('foo', Operators).value(undefined!)) + toThenable(new Expression('foo', defaultOperators).value(undefined!)) .then(() => done(new Error('should not resolved'))) .catch(err => { expect(err.message).to.match(/context not defined/) @@ -18,19 +18,19 @@ describe('Expression', function () { describe('single value', function () { it('should eval literal', async function () { - expect(await toThenable(new Expression('2.4', Operators).value(ctx))).to.equal(2.4) - expect(await toThenable(new Expression('"foo"', Operators).value(ctx))).to.equal('foo') - expect(await toThenable(new Expression('false', Operators).value(ctx))).to.equal(false) + expect(await toThenable(new Expression('2.4', defaultOperators).value(ctx))).to.equal(2.4) + expect(await toThenable(new Expression('"foo"', defaultOperators).value(ctx))).to.equal('foo') + expect(await toThenable(new Expression('false', defaultOperators).value(ctx))).to.equal(false) }) it('should eval range expression', async function () { const ctx = new Context({ two: 2 }) - expect(await toThenable(new Expression('(2..4)', Operators).value(ctx))).to.deep.equal([2, 3, 4]) - expect(await toThenable(new Expression('(two..4)', Operators).value(ctx))).to.deep.equal([2, 3, 4]) + expect(await toThenable(new Expression('(2..4)', defaultOperators).value(ctx))).to.deep.equal([2, 3, 4]) + expect(await toThenable(new Expression('(two..4)', defaultOperators).value(ctx))).to.deep.equal([2, 3, 4]) }) it('should eval literal', async function () { - expect(await toThenable(new Expression('2.4', Operators).value(ctx))).to.equal(2.4) - expect(await toThenable(new Expression('"foo"', Operators).value(ctx))).to.equal('foo') - expect(await toThenable(new Expression('false', Operators).value(ctx))).to.equal(false) + expect(await toThenable(new Expression('2.4', defaultOperators).value(ctx))).to.equal(2.4) + expect(await toThenable(new Expression('"foo"', defaultOperators).value(ctx))).to.equal('foo') + expect(await toThenable(new Expression('false', defaultOperators).value(ctx))).to.equal(false) }) it('should eval property access', async function () { @@ -39,112 +39,112 @@ describe('Expression', function () { coo: 'bar', doo: { foo: 'bar', bar: { foo: 'bar' } } }) - expect(await toThenable(new Expression('foo.bar', Operators).value(ctx))).to.equal('BAR') - expect(await toThenable(new Expression('foo["bar"]', Operators).value(ctx))).to.equal('BAR') - expect(await toThenable(new Expression('foo[coo]', Operators).value(ctx))).to.equal('BAR') - expect(await toThenable(new Expression('foo[doo.foo]', Operators).value(ctx))).to.equal('BAR') - expect(await toThenable(new Expression('foo[doo["foo"]]', Operators).value(ctx))).to.equal('BAR') - expect(await toThenable(new Expression('doo[coo].foo', Operators).value(ctx))).to.equal('bar') + expect(await toThenable(new Expression('foo.bar', defaultOperators).value(ctx))).to.equal('BAR') + expect(await toThenable(new Expression('foo["bar"]', defaultOperators).value(ctx))).to.equal('BAR') + expect(await toThenable(new Expression('foo[coo]', defaultOperators).value(ctx))).to.equal('BAR') + expect(await toThenable(new Expression('foo[doo.foo]', defaultOperators).value(ctx))).to.equal('BAR') + expect(await toThenable(new Expression('foo[doo["foo"]]', defaultOperators).value(ctx))).to.equal('BAR') + expect(await toThenable(new Expression('doo[coo].foo', defaultOperators).value(ctx))).to.equal('bar') }) }) describe('simple expression', function () { it('should return false for "1==2"', async () => { - expect(await toThenable(new Expression('1==2', Operators).value(ctx))).to.equal(false) + expect(await toThenable(new Expression('1==2', defaultOperators).value(ctx))).to.equal(false) }) it('should return true for "1<2"', async () => { - expect(await toThenable(new Expression('1<2', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('1<2', defaultOperators).value(ctx))).to.equal(true) }) it('should return true for "1 < 2"', async () => { - expect(await toThenable(new Expression('1 < 2', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('1 < 2', defaultOperators).value(ctx))).to.equal(true) }) it('should return true for "1 < 2"', async () => { - expect(await toThenable(new Expression('1 < 2', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('1 < 2', defaultOperators).value(ctx))).to.equal(true) }) it('should return true for "2 <= 2"', async () => { - expect(await toThenable(new Expression('2 <= 2', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('2 <= 2', defaultOperators).value(ctx))).to.equal(true) }) it('should return true for "one <= two"', async () => { const ctx = new Context({ one: 1, two: 2 }) - expect(await toThenable(new Expression('one <= two', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('one <= two', defaultOperators).value(ctx))).to.equal(true) }) it('should return false for "x contains "x""', async () => { const ctx = new Context({ x: 'XXX' }) - expect(await toThenable(new Expression('x contains "x"', Operators).value(ctx))).to.equal(false) + expect(await toThenable(new Expression('x contains "x"', defaultOperators).value(ctx))).to.equal(false) }) it('should return true for "x contains "X""', async () => { const ctx = new Context({ x: 'XXX' }) - expect(await toThenable(new Expression('x contains "X"', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('x contains "X"', defaultOperators).value(ctx))).to.equal(true) }) it('should return false for "1 contains "x""', async () => { const ctx = new Context({ x: 'XXX' }) - expect(await toThenable(new Expression('1 contains "x"', Operators).value(ctx))).to.equal(false) + expect(await toThenable(new Expression('1 contains "x"', defaultOperators).value(ctx))).to.equal(false) }) it('should return false for "y contains "x""', async () => { const ctx = new Context({ x: 'XXX' }) - expect(await toThenable(new Expression('y contains "x"', Operators).value(ctx))).to.equal(false) + expect(await toThenable(new Expression('y contains "x"', defaultOperators).value(ctx))).to.equal(false) }) it('should return false for "z contains "x""', async () => { const ctx = new Context({ x: 'XXX' }) - expect(await toThenable(new Expression('z contains "x"', Operators).value(ctx))).to.equal(false) + expect(await toThenable(new Expression('z contains "x"', defaultOperators).value(ctx))).to.equal(false) }) it('should return true for "(1..5) contains 3"', async () => { const ctx = new Context({ x: 'XXX' }) - expect(await toThenable(new Expression('(1..5) contains 3', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('(1..5) contains 3', defaultOperators).value(ctx))).to.equal(true) }) it('should return false for "(1..5) contains 6"', async () => { const ctx = new Context({ x: 'XXX' }) - expect(await toThenable(new Expression('(1..5) contains 6', Operators).value(ctx))).to.equal(false) + expect(await toThenable(new Expression('(1..5) contains 6', defaultOperators).value(ctx))).to.equal(false) }) it('should return true for ""<=" == "<=""', async () => { - expect(await toThenable(new Expression('"<=" == "<="', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('"<=" == "<="', defaultOperators).value(ctx))).to.equal(true) }) }) it('should allow space in quoted value', async function () { const ctx = new Context({ space: ' ' }) - expect(await toThenable(new Expression('" " == space', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('" " == space', defaultOperators).value(ctx))).to.equal(true) }) describe('escape', () => { it('should escape quote', async function () { const ctx = new Context({ quote: '"' }) - expect(await toThenable(new Expression('"\\"" == quote', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('"\\"" == quote', defaultOperators).value(ctx))).to.equal(true) }) it('should escape square bracket', async function () { const ctx = new Context({ obj: { ']': 'bracket' } }) - expect(await toThenable(new Expression('obj["]"] == "bracket"', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('obj["]"] == "bracket"', defaultOperators).value(ctx))).to.equal(true) }) }) describe('complex expression', function () { it('should support value or value', async function () { - expect(await toThenable(new Expression('false or true', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('false or true', defaultOperators).value(ctx))).to.equal(true) }) it('should support < and contains', async function () { - expect(await toThenable(new Expression('1 < 2 and x contains "x"', Operators).value(ctx))).to.equal(false) + expect(await toThenable(new Expression('1 < 2 and x contains "x"', defaultOperators).value(ctx))).to.equal(false) }) it('should support < or contains', async function () { - expect(await toThenable(new Expression('1 < 2 or x contains "x"', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('1 < 2 or x contains "x"', defaultOperators).value(ctx))).to.equal(true) }) it('should support value and !=', async function () { const ctx = new Context({ empty: '' }) - expect(await toThenable(new Expression('empty and empty != ""', Operators).value(ctx))).to.equal(false) + expect(await toThenable(new Expression('empty and empty != ""', defaultOperators).value(ctx))).to.equal(false) }) it('should recognize quoted value', async function () { - expect(await toThenable(new Expression('">"', Operators).value(ctx))).to.equal('>') + expect(await toThenable(new Expression('">"', defaultOperators).value(ctx))).to.equal('>') }) it('should evaluate from right to left', async function () { - expect(await toThenable(new Expression('true or false and false', Operators).value(ctx))).to.equal(true) - expect(await toThenable(new Expression('true and false and false or true', Operators).value(ctx))).to.equal(false) + expect(await toThenable(new Expression('true or false and false', defaultOperators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('true and false and false or true', defaultOperators).value(ctx))).to.equal(false) }) it('should recognize property access', async function () { const ctx = new Context({ obj: { foo: true } }) - expect(await toThenable(new Expression('obj["foo"] and true', Operators).value(ctx))).to.equal(true) + expect(await toThenable(new Expression('obj["foo"] and true', defaultOperators).value(ctx))).to.equal(true) }) it('should allow nested property access', async function () { const ctx = new Context({ obj: { foo: 'FOO' }, keys: { "what's this": 'foo' } }) - expect(await toThenable(new Expression('obj[keys["what\'s this"]]', Operators).value(ctx))).to.equal('FOO') + expect(await toThenable(new Expression('obj[keys["what\'s this"]]', defaultOperators).value(ctx))).to.equal('FOO') }) }) })