Skip to content

Commit 6a7c280

Browse files
JasonEtcoharttle
authored andcommitted
feat: export Operators from operator.ts
1 parent bc87e19 commit 6a7c280

File tree

4 files changed

+46
-46
lines changed

4 files changed

+46
-46
lines changed

src/liquid-options.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Template } from './template/template'
33
import { Cache } from './cache/cache'
44
import { LRU } from './cache/lru'
55
import { FS } from './fs/fs'
6-
import { operatorImpls, OperatorMap } from './render/operator'
6+
import { Operators, OperatorMap } from './render/operator'
77

88
export interface LiquidOptions {
99
/** 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 `["."]` */
@@ -102,7 +102,7 @@ export const defaultOptions: NormalizedFullOptions = {
102102
lenientIf: false,
103103
globals: {},
104104
keepOutputType: false,
105-
operators: operatorImpls
105+
operators: Operators
106106
}
107107

108108
export function normalize (options?: LiquidOptions): NormalizedOptions {

src/render/operator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface OperatorMap {
77
[key: string]: (lhs: any, rhs: any, ctx: Context) => boolean;
88
}
99

10-
export const operatorImpls: OperatorMap = {
10+
export const Operators: OperatorMap = {
1111
'==': (l: any, r: any) => {
1212
if (isComparable(l)) return l.equals(r)
1313
if (isComparable(r)) return r.equals(l)

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ export { Tokenizer } from './parser/tokenizer'
1919
export { Hash } from './template/tag/hash'
2020
export { evalToken, evalQuotedToken } from './render/expression'
2121
export { toPromise, toThenable, toValue } from './util/async'
22-
export { OperatorMap } from './render/operator'
22+
export { Operators, OperatorMap } from './render/operator'

test/unit/render/expression.ts

+42-42
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { Expression } from '../../../src/render/expression'
22
import { expect } from 'chai'
33
import { Context } from '../../../src/context/context'
44
import { toThenable } from '../../../src/util/async'
5-
import { operatorImpls } from '../../../src/render/operator'
5+
import { Operators } from '../../../src/render/operator'
66

77
describe('Expression', function () {
88
const ctx = new Context({})
99

1010
it('should throw when context not defined', done => {
11-
toThenable(new Expression('foo', operatorImpls).value(undefined!))
11+
toThenable(new Expression('foo', Operators).value(undefined!))
1212
.then(() => done(new Error('should not resolved')))
1313
.catch(err => {
1414
expect(err.message).to.match(/context not defined/)
@@ -18,19 +18,19 @@ describe('Expression', function () {
1818

1919
describe('single value', function () {
2020
it('should eval literal', async function () {
21-
expect(await toThenable(new Expression('2.4', operatorImpls).value(ctx))).to.equal(2.4)
22-
expect(await toThenable(new Expression('"foo"', operatorImpls).value(ctx))).to.equal('foo')
23-
expect(await toThenable(new Expression('false', operatorImpls).value(ctx))).to.equal(false)
21+
expect(await toThenable(new Expression('2.4', Operators).value(ctx))).to.equal(2.4)
22+
expect(await toThenable(new Expression('"foo"', Operators).value(ctx))).to.equal('foo')
23+
expect(await toThenable(new Expression('false', Operators).value(ctx))).to.equal(false)
2424
})
2525
it('should eval range expression', async function () {
2626
const ctx = new Context({ two: 2 })
27-
expect(await toThenable(new Expression('(2..4)', operatorImpls).value(ctx))).to.deep.equal([2, 3, 4])
28-
expect(await toThenable(new Expression('(two..4)', operatorImpls).value(ctx))).to.deep.equal([2, 3, 4])
27+
expect(await toThenable(new Expression('(2..4)', Operators).value(ctx))).to.deep.equal([2, 3, 4])
28+
expect(await toThenable(new Expression('(two..4)', Operators).value(ctx))).to.deep.equal([2, 3, 4])
2929
})
3030
it('should eval literal', async function () {
31-
expect(await toThenable(new Expression('2.4', operatorImpls).value(ctx))).to.equal(2.4)
32-
expect(await toThenable(new Expression('"foo"', operatorImpls).value(ctx))).to.equal('foo')
33-
expect(await toThenable(new Expression('false', operatorImpls).value(ctx))).to.equal(false)
31+
expect(await toThenable(new Expression('2.4', Operators).value(ctx))).to.equal(2.4)
32+
expect(await toThenable(new Expression('"foo"', Operators).value(ctx))).to.equal('foo')
33+
expect(await toThenable(new Expression('false', Operators).value(ctx))).to.equal(false)
3434
})
3535

3636
it('should eval property access', async function () {
@@ -39,112 +39,112 @@ describe('Expression', function () {
3939
coo: 'bar',
4040
doo: { foo: 'bar', bar: { foo: 'bar' } }
4141
})
42-
expect(await toThenable(new Expression('foo.bar', operatorImpls).value(ctx))).to.equal('BAR')
43-
expect(await toThenable(new Expression('foo["bar"]', operatorImpls).value(ctx))).to.equal('BAR')
44-
expect(await toThenable(new Expression('foo[coo]', operatorImpls).value(ctx))).to.equal('BAR')
45-
expect(await toThenable(new Expression('foo[doo.foo]', operatorImpls).value(ctx))).to.equal('BAR')
46-
expect(await toThenable(new Expression('foo[doo["foo"]]', operatorImpls).value(ctx))).to.equal('BAR')
47-
expect(await toThenable(new Expression('doo[coo].foo', operatorImpls).value(ctx))).to.equal('bar')
42+
expect(await toThenable(new Expression('foo.bar', Operators).value(ctx))).to.equal('BAR')
43+
expect(await toThenable(new Expression('foo["bar"]', Operators).value(ctx))).to.equal('BAR')
44+
expect(await toThenable(new Expression('foo[coo]', Operators).value(ctx))).to.equal('BAR')
45+
expect(await toThenable(new Expression('foo[doo.foo]', Operators).value(ctx))).to.equal('BAR')
46+
expect(await toThenable(new Expression('foo[doo["foo"]]', Operators).value(ctx))).to.equal('BAR')
47+
expect(await toThenable(new Expression('doo[coo].foo', Operators).value(ctx))).to.equal('bar')
4848
})
4949
})
5050

5151
describe('simple expression', function () {
5252
it('should return false for "1==2"', async () => {
53-
expect(await toThenable(new Expression('1==2', operatorImpls).value(ctx))).to.equal(false)
53+
expect(await toThenable(new Expression('1==2', Operators).value(ctx))).to.equal(false)
5454
})
5555
it('should return true for "1<2"', async () => {
56-
expect(await toThenable(new Expression('1<2', operatorImpls).value(ctx))).to.equal(true)
56+
expect(await toThenable(new Expression('1<2', Operators).value(ctx))).to.equal(true)
5757
})
5858
it('should return true for "1 < 2"', async () => {
59-
expect(await toThenable(new Expression('1 < 2', operatorImpls).value(ctx))).to.equal(true)
59+
expect(await toThenable(new Expression('1 < 2', Operators).value(ctx))).to.equal(true)
6060
})
6161
it('should return true for "1 < 2"', async () => {
62-
expect(await toThenable(new Expression('1 < 2', operatorImpls).value(ctx))).to.equal(true)
62+
expect(await toThenable(new Expression('1 < 2', Operators).value(ctx))).to.equal(true)
6363
})
6464
it('should return true for "2 <= 2"', async () => {
65-
expect(await toThenable(new Expression('2 <= 2', operatorImpls).value(ctx))).to.equal(true)
65+
expect(await toThenable(new Expression('2 <= 2', Operators).value(ctx))).to.equal(true)
6666
})
6767
it('should return true for "one <= two"', async () => {
6868
const ctx = new Context({ one: 1, two: 2 })
69-
expect(await toThenable(new Expression('one <= two', operatorImpls).value(ctx))).to.equal(true)
69+
expect(await toThenable(new Expression('one <= two', Operators).value(ctx))).to.equal(true)
7070
})
7171
it('should return false for "x contains "x""', async () => {
7272
const ctx = new Context({ x: 'XXX' })
73-
expect(await toThenable(new Expression('x contains "x"', operatorImpls).value(ctx))).to.equal(false)
73+
expect(await toThenable(new Expression('x contains "x"', Operators).value(ctx))).to.equal(false)
7474
})
7575
it('should return true for "x contains "X""', async () => {
7676
const ctx = new Context({ x: 'XXX' })
77-
expect(await toThenable(new Expression('x contains "X"', operatorImpls).value(ctx))).to.equal(true)
77+
expect(await toThenable(new Expression('x contains "X"', Operators).value(ctx))).to.equal(true)
7878
})
7979
it('should return false for "1 contains "x""', async () => {
8080
const ctx = new Context({ x: 'XXX' })
81-
expect(await toThenable(new Expression('1 contains "x"', operatorImpls).value(ctx))).to.equal(false)
81+
expect(await toThenable(new Expression('1 contains "x"', Operators).value(ctx))).to.equal(false)
8282
})
8383
it('should return false for "y contains "x""', async () => {
8484
const ctx = new Context({ x: 'XXX' })
85-
expect(await toThenable(new Expression('y contains "x"', operatorImpls).value(ctx))).to.equal(false)
85+
expect(await toThenable(new Expression('y contains "x"', Operators).value(ctx))).to.equal(false)
8686
})
8787
it('should return false for "z contains "x""', async () => {
8888
const ctx = new Context({ x: 'XXX' })
89-
expect(await toThenable(new Expression('z contains "x"', operatorImpls).value(ctx))).to.equal(false)
89+
expect(await toThenable(new Expression('z contains "x"', Operators).value(ctx))).to.equal(false)
9090
})
9191
it('should return true for "(1..5) contains 3"', async () => {
9292
const ctx = new Context({ x: 'XXX' })
93-
expect(await toThenable(new Expression('(1..5) contains 3', operatorImpls).value(ctx))).to.equal(true)
93+
expect(await toThenable(new Expression('(1..5) contains 3', Operators).value(ctx))).to.equal(true)
9494
})
9595
it('should return false for "(1..5) contains 6"', async () => {
9696
const ctx = new Context({ x: 'XXX' })
97-
expect(await toThenable(new Expression('(1..5) contains 6', operatorImpls).value(ctx))).to.equal(false)
97+
expect(await toThenable(new Expression('(1..5) contains 6', Operators).value(ctx))).to.equal(false)
9898
})
9999
it('should return true for ""<=" == "<=""', async () => {
100-
expect(await toThenable(new Expression('"<=" == "<="', operatorImpls).value(ctx))).to.equal(true)
100+
expect(await toThenable(new Expression('"<=" == "<="', Operators).value(ctx))).to.equal(true)
101101
})
102102
})
103103

104104
it('should allow space in quoted value', async function () {
105105
const ctx = new Context({ space: ' ' })
106-
expect(await toThenable(new Expression('" " == space', operatorImpls).value(ctx))).to.equal(true)
106+
expect(await toThenable(new Expression('" " == space', Operators).value(ctx))).to.equal(true)
107107
})
108108

109109
describe('escape', () => {
110110
it('should escape quote', async function () {
111111
const ctx = new Context({ quote: '"' })
112-
expect(await toThenable(new Expression('"\\"" == quote', operatorImpls).value(ctx))).to.equal(true)
112+
expect(await toThenable(new Expression('"\\"" == quote', Operators).value(ctx))).to.equal(true)
113113
})
114114
it('should escape square bracket', async function () {
115115
const ctx = new Context({ obj: { ']': 'bracket' } })
116-
expect(await toThenable(new Expression('obj["]"] == "bracket"', operatorImpls).value(ctx))).to.equal(true)
116+
expect(await toThenable(new Expression('obj["]"] == "bracket"', Operators).value(ctx))).to.equal(true)
117117
})
118118
})
119119

120120
describe('complex expression', function () {
121121
it('should support value or value', async function () {
122-
expect(await toThenable(new Expression('false or true', operatorImpls).value(ctx))).to.equal(true)
122+
expect(await toThenable(new Expression('false or true', Operators).value(ctx))).to.equal(true)
123123
})
124124
it('should support < and contains', async function () {
125-
expect(await toThenable(new Expression('1 < 2 and x contains "x"', operatorImpls).value(ctx))).to.equal(false)
125+
expect(await toThenable(new Expression('1 < 2 and x contains "x"', Operators).value(ctx))).to.equal(false)
126126
})
127127
it('should support < or contains', async function () {
128-
expect(await toThenable(new Expression('1 < 2 or x contains "x"', operatorImpls).value(ctx))).to.equal(true)
128+
expect(await toThenable(new Expression('1 < 2 or x contains "x"', Operators).value(ctx))).to.equal(true)
129129
})
130130
it('should support value and !=', async function () {
131131
const ctx = new Context({ empty: '' })
132-
expect(await toThenable(new Expression('empty and empty != ""', operatorImpls).value(ctx))).to.equal(false)
132+
expect(await toThenable(new Expression('empty and empty != ""', Operators).value(ctx))).to.equal(false)
133133
})
134134
it('should recognize quoted value', async function () {
135-
expect(await toThenable(new Expression('">"', operatorImpls).value(ctx))).to.equal('>')
135+
expect(await toThenable(new Expression('">"', Operators).value(ctx))).to.equal('>')
136136
})
137137
it('should evaluate from right to left', async function () {
138-
expect(await toThenable(new Expression('true or false and false', operatorImpls).value(ctx))).to.equal(true)
139-
expect(await toThenable(new Expression('true and false and false or true', operatorImpls).value(ctx))).to.equal(false)
138+
expect(await toThenable(new Expression('true or false and false', Operators).value(ctx))).to.equal(true)
139+
expect(await toThenable(new Expression('true and false and false or true', Operators).value(ctx))).to.equal(false)
140140
})
141141
it('should recognize property access', async function () {
142142
const ctx = new Context({ obj: { foo: true } })
143-
expect(await toThenable(new Expression('obj["foo"] and true', operatorImpls).value(ctx))).to.equal(true)
143+
expect(await toThenable(new Expression('obj["foo"] and true', Operators).value(ctx))).to.equal(true)
144144
})
145145
it('should allow nested property access', async function () {
146146
const ctx = new Context({ obj: { foo: 'FOO' }, keys: { "what's this": 'foo' } })
147-
expect(await toThenable(new Expression('obj[keys["what\'s this"]]', operatorImpls).value(ctx))).to.equal('FOO')
147+
expect(await toThenable(new Expression('obj[keys["what\'s this"]]', Operators).value(ctx))).to.equal('FOO')
148148
})
149149
})
150150
})

0 commit comments

Comments
 (0)