perf: use polymophism instead duck test

This commit is contained in:
harttle
2019-03-25 20:11:23 +08:00
parent 64dd057552
commit 82d7673554
18 changed files with 120 additions and 98 deletions
+4 -7
View File
@@ -1,4 +1,4 @@
import { create, stringify } from '../../util/underscore'
import { stringify, isFunction } from '../../util/underscore'
import assert from '../../util/assert'
import Context from '../../context/context'
import ITagImpl from './itag-impl'
@@ -21,7 +21,8 @@ export default class Tag extends Template<TagToken> implements ITemplate {
const impl = Tag.impls[token.name]
assert(impl, `tag ${token.name} not found`)
this.impl = create<ITagImplOptions, ITagImpl>(impl)
this.impl = Object.create(impl)
this.impl.liquid = liquid
if (this.impl.parse) {
this.impl.parse(token, tokens)
@@ -30,11 +31,7 @@ export default class Tag extends Template<TagToken> implements ITemplate {
async render (ctx: Context) {
const hash = await Hash.create(this.token.args, ctx)
const impl = this.impl
if (typeof impl.render !== 'function') {
return ''
}
const html = await impl.render(ctx, hash)
return stringify(html)
return isFunction(impl.render) ? stringify(await impl.render(ctx, hash)) : ''
}
static register (name: string, tag: ITagImplOptions) {
Tag.impls[name] = tag
+2 -2
View File
@@ -4,8 +4,8 @@ import Context from '../context/context'
export default class Value {
private strictFilters: boolean
initial: string
filters: Array<Filter> = []
private initial: string
private filters: Array<Filter> = []
/**
* @param str value string, like: "i have a dream | truncate: 3