mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 05:50:43 -07:00
perf: introduce AST to avoid reparse
This commit is contained in:
+3
-77
@@ -1,6 +1,5 @@
|
||||
import { Drop } from '../drop/drop'
|
||||
import { __assign } from 'tslib'
|
||||
import { assert } from '../util/assert'
|
||||
import { NormalizedFullOptions, defaultOptions } from '../liquid-options'
|
||||
import { Scope } from './scope'
|
||||
import { isArray, isNil, isString, isFunction, toLiquid } from '../util/underscore'
|
||||
@@ -34,13 +33,12 @@ export class Context {
|
||||
return [this.globals, this.environments, ...this.scopes]
|
||||
.reduce((ctx, val) => __assign(ctx, val), {})
|
||||
}
|
||||
public get (path: string) {
|
||||
const paths = this.parseProp(path)
|
||||
public get (paths: string[]) {
|
||||
const scope = this.findScope(paths[0])
|
||||
return this.getFromScope(scope, paths)
|
||||
}
|
||||
public getFromScope (scope: object, paths: string[] | string) {
|
||||
if (!isArray(paths)) paths = this.parseProp(paths)
|
||||
if (typeof paths === 'string') paths = paths.split('.')
|
||||
return paths.reduce((scope, path) => {
|
||||
scope = readProperty(scope, path)
|
||||
if (isNil(scope) && this.opts.strictVariables) {
|
||||
@@ -61,67 +59,11 @@ export class Context {
|
||||
private findScope (key: string) {
|
||||
for (let i = this.scopes.length - 1; i >= 0; i--) {
|
||||
const candidate = this.scopes[i]
|
||||
if (key in candidate) {
|
||||
return candidate
|
||||
}
|
||||
if (key in candidate) return candidate
|
||||
}
|
||||
if (key in this.environments) return this.environments
|
||||
return this.globals
|
||||
}
|
||||
|
||||
/*
|
||||
* Parse property access sequence from access string
|
||||
* @example
|
||||
* accessSeq("foo.bar") // ['foo', 'bar']
|
||||
* accessSeq("foo['bar']") // ['foo', 'bar']
|
||||
* accessSeq("foo['b]r']") // ['foo', 'b]r']
|
||||
* accessSeq("foo[bar.coo]") // ['foo', 'bar'], for bar.coo == 'bar'
|
||||
*/
|
||||
private parseProp (str: string) {
|
||||
str = String(str)
|
||||
const seq: string[] = []
|
||||
const push = () => name.length && (seq.push(name), (name = ''))
|
||||
let name = ''
|
||||
let j
|
||||
let i = 0
|
||||
while (i < str.length) {
|
||||
switch (str[i]) {
|
||||
case '[':
|
||||
push()
|
||||
|
||||
const delemiter = str[i + 1]
|
||||
if (/['"]/.test(delemiter)) { // foo["bar"]
|
||||
j = str.indexOf(delemiter, i + 2)
|
||||
assert(j !== -1, `unbalanced ${delemiter}: ${str}`)
|
||||
name = str.slice(i + 2, j)
|
||||
push()
|
||||
i = j + 2
|
||||
} else { // foo[bar.coo]
|
||||
j = matchRightBracket(str, i + 1)
|
||||
assert(j !== -1, `unbalanced []: ${str}`)
|
||||
name = str.slice(i + 1, j)
|
||||
if (!/^[+-]?\d+$/.test(name)) { // foo[bar] vs. foo[1]
|
||||
name = String(this.get(name))
|
||||
}
|
||||
push()
|
||||
i = j + 1
|
||||
}
|
||||
break
|
||||
case '.':// foo.bar, foo[0].bar
|
||||
push()
|
||||
i++
|
||||
break
|
||||
default:// foo.bar
|
||||
name += str[i++]
|
||||
}
|
||||
}
|
||||
push()
|
||||
|
||||
if (!seq.length) {
|
||||
throw new TypeError(`invalid path:"${str}"`)
|
||||
}
|
||||
return seq
|
||||
}
|
||||
}
|
||||
|
||||
export function readProperty (obj: Scope, key: string) {
|
||||
@@ -152,19 +94,3 @@ function readSize (obj: Scope) {
|
||||
if (isArray(obj) || isString(obj)) return obj.length
|
||||
return obj['size']
|
||||
}
|
||||
|
||||
function matchRightBracket (str: string, begin: number) {
|
||||
let stack = 1 // count of '[' - count of ']'
|
||||
for (let i = begin; i < str.length; i++) {
|
||||
if (str[i] === '[') {
|
||||
stack++
|
||||
}
|
||||
if (str[i] === ']') {
|
||||
stack--
|
||||
if (stack === 0) {
|
||||
return i
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user