mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-17 21:40:39 -07:00
feat: promise support for drops, working on #65
This commit is contained in:
@@ -19,9 +19,13 @@ export class Filter {
|
||||
this.impl = impl || (x => x)
|
||||
this.args = args
|
||||
}
|
||||
render (value: any, scope: Scope): any {
|
||||
const args = this.args.map(arg => isArray(arg) ? [arg[0], evalValue(arg[1], scope)] : evalValue(arg, scope))
|
||||
return this.impl.apply(null, [value, ...args])
|
||||
async render (value: any, scope: Scope) {
|
||||
const argv: any[] = []
|
||||
for(let arg of this.args) {
|
||||
if (isArray(arg)) argv.push([arg[0], await evalValue(arg[1], scope)])
|
||||
else argv.push(await evalValue(arg, scope))
|
||||
}
|
||||
return this.impl.apply(null, [value, ...argv])
|
||||
}
|
||||
static register (name: string, filter: FilterImpl) {
|
||||
Filter.impls[name] = filter
|
||||
|
||||
@@ -10,13 +10,15 @@ import Scope from '../../scope/scope'
|
||||
*/
|
||||
export default class Hash {
|
||||
[key: string]: any
|
||||
constructor (markup: string, scope: Scope) {
|
||||
static async create (markup: string, scope: Scope) {
|
||||
const instance = new Hash()
|
||||
let match
|
||||
hashCapture.lastIndex = 0
|
||||
while ((match = hashCapture.exec(markup))) {
|
||||
const k = match[1]
|
||||
const v = match[2]
|
||||
this[k] = evalValue(v, scope)
|
||||
instance[k] = await evalValue(v, scope)
|
||||
}
|
||||
return instance
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ export default class Tag extends Template<TagToken> implements ITemplate {
|
||||
}
|
||||
}
|
||||
async render (scope: Scope) {
|
||||
const hash = new Hash(this.token.args, scope)
|
||||
const hash = await Hash.create(this.token.args, scope)
|
||||
const impl = this.impl
|
||||
if (typeof impl.render !== 'function') {
|
||||
return ''
|
||||
|
||||
@@ -47,10 +47,12 @@ export default class Value {
|
||||
}
|
||||
this.filters.push(new Filter(name, args, this.strictFilters))
|
||||
}
|
||||
value (scope: Scope) {
|
||||
return this.filters.reduce(
|
||||
(prev, filter) => filter.render(prev, scope),
|
||||
evalExp(this.initial, scope))
|
||||
async value (scope: Scope) {
|
||||
let val = await evalExp(this.initial, scope)
|
||||
for (let filter of this.filters) {
|
||||
val = await filter.render(val, scope)
|
||||
}
|
||||
return val
|
||||
}
|
||||
static tokenize (str: string): Array<'|' | ',' | ':' | string> {
|
||||
const tokens = []
|
||||
|
||||
Reference in New Issue
Block a user