mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-27 06:05:13 -07:00
refactor: remove src/render/value.ts
This commit is contained in:
@@ -83,7 +83,7 @@ export class Tokenizer {
|
|||||||
|
|
||||||
readFilterArg (): FilterArg | null {
|
readFilterArg (): FilterArg | null {
|
||||||
const key = this.readValue()
|
const key = this.readValue()
|
||||||
if (!key) return null
|
if (!key.size()) return null
|
||||||
this.readBlank()
|
this.readBlank()
|
||||||
if (this.peek() === ':') {
|
if (this.peek() === ':') {
|
||||||
this.read()
|
this.read()
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { assert } from '../util/assert'
|
import { assert } from '../util/assert'
|
||||||
import { isRange, rangeValue } from './range'
|
import { rangeLine } from '../parser/lexical'
|
||||||
import { Value } from './value'
|
import { parseLiteral } from '../parser/literal'
|
||||||
import { Context } from '../context/context'
|
import { Context } from '../context/context'
|
||||||
import { toValue } from '../util/underscore'
|
import { range, toValue } from '../util/underscore'
|
||||||
import { isOperator, precedence, operatorImpls } from './operator'
|
import { isOperator, precedence, operatorImpls } from './operator'
|
||||||
import { Tokenizer } from '../parser/tokenizer'
|
import { Tokenizer } from '../parser/tokenizer'
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ export class Expression {
|
|||||||
const tokenizer = new Tokenizer(str)
|
const tokenizer = new Tokenizer(str)
|
||||||
this.postfix = [...toPostfix(tokenizer.readExpression())]
|
this.postfix = [...toPostfix(tokenizer.readExpression())]
|
||||||
}
|
}
|
||||||
public * evaluate (ctx: Context) {
|
public * evaluate (ctx: Context): any {
|
||||||
assert(ctx, 'unable to evaluate: context not defined')
|
assert(ctx, 'unable to evaluate: context not defined')
|
||||||
|
|
||||||
for (const token of this.postfix) {
|
for (const token of this.postfix) {
|
||||||
@@ -25,7 +25,10 @@ export class Expression {
|
|||||||
this.operands.push(result)
|
this.operands.push(result)
|
||||||
} else if (isRange(token)) {
|
} else if (isRange(token)) {
|
||||||
this.operands.push(yield rangeValue(token, ctx))
|
this.operands.push(yield rangeValue(token, ctx))
|
||||||
} else this.operands.push(yield new Value(token).evaluate(ctx))
|
} else {
|
||||||
|
const literal = parseLiteral(token)
|
||||||
|
this.operands.push(literal !== undefined ? literal : yield ctx.get(token))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this.operands[0]
|
return this.operands[0]
|
||||||
}
|
}
|
||||||
@@ -48,3 +51,16 @@ function * toPostfix (tokens: IterableIterator<string>): IterableIterator<string
|
|||||||
yield ops.pop()!
|
yield ops.pop()!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function * rangeValue (token: string, ctx: Context) {
|
||||||
|
let match
|
||||||
|
if ((match = token.match(rangeLine))) {
|
||||||
|
const low = yield new Expression(match[1]).value(ctx)
|
||||||
|
const high = yield new Expression(match[2]).value(ctx)
|
||||||
|
return range(+low, +high + 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isRange (str: string) {
|
||||||
|
return rangeLine.test(str)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
import { rangeLine } from '../parser/lexical'
|
|
||||||
import { Context } from '../context/context'
|
|
||||||
import { range } from '../util/underscore'
|
|
||||||
import { Value } from './value'
|
|
||||||
|
|
||||||
export function isRange (str: string) {
|
|
||||||
return rangeLine.test(str)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function * rangeValue (token: string, ctx: Context) {
|
|
||||||
let match
|
|
||||||
if ((match = token.match(rangeLine))) {
|
|
||||||
const low = yield new Value(match[1]).value(ctx)
|
|
||||||
const high = yield new Value(match[2]).value(ctx)
|
|
||||||
return range(+low, +high + 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
import { toValue } from '../util/underscore'
|
|
||||||
import { Context } from '../context/context'
|
|
||||||
import { parseLiteral } from '../parser/literal'
|
|
||||||
|
|
||||||
export class Value {
|
|
||||||
private str: string
|
|
||||||
|
|
||||||
public constructor (str: string) {
|
|
||||||
this.str = str
|
|
||||||
}
|
|
||||||
|
|
||||||
public evaluate (ctx: Context) {
|
|
||||||
const literalValue = parseLiteral(this.str)
|
|
||||||
if (literalValue !== undefined) {
|
|
||||||
return literalValue
|
|
||||||
}
|
|
||||||
return ctx.get(this.str)
|
|
||||||
}
|
|
||||||
|
|
||||||
public value (ctx: Context) {
|
|
||||||
return toValue(this.evaluate(ctx))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
import { Value } from '../../../src/render/value'
|
|
||||||
import { Context } from '../../../src/context/context'
|
|
||||||
import { expect } from 'chai'
|
|
||||||
|
|
||||||
describe('Value', function () {
|
|
||||||
it('should eval number variable', async function () {
|
|
||||||
const ctx = new Context({ one: 1 })
|
|
||||||
expect(new Value('one').value(ctx)).to.equal(1)
|
|
||||||
})
|
|
||||||
it('question mark should be valid variable name', async function () {
|
|
||||||
const ctx = new Context({ 'has_value?': true })
|
|
||||||
expect(new Value('has_value?').value(ctx)).to.equal(true)
|
|
||||||
})
|
|
||||||
it('should eval string variable', async function () {
|
|
||||||
const ctx = new Context({ x: 'XXX' })
|
|
||||||
expect(new Value('x').value(ctx)).to.equal('XXX')
|
|
||||||
})
|
|
||||||
it('should eval null literal', async function () {
|
|
||||||
expect(new Value('null').value({} as any)).to.be.null
|
|
||||||
})
|
|
||||||
it('should eval nil literal', async function () {
|
|
||||||
expect(new Value('nil').value({} as any)).to.be.null
|
|
||||||
})
|
|
||||||
})
|
|
||||||
Reference in New Issue
Block a user