mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 12:20:40 -07:00
chore(TypeScript): refactor objects into classes
fix: `Nil`(null, undefined) now renders as empty string change: `parser.parseValue()` renamed to `parser.parseOutput` change: registered tags/filters become static and shared across different liquid instances
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import Token from 'src/parser/token'
|
||||
import ITemplate from 'src/template/itemplate'
|
||||
|
||||
type parseToken = (token: Token, remainTokens: Array<Token>) => ITemplate
|
||||
type eventHandler = ((arg?: Token | ITemplate) => void)
|
||||
|
||||
export default class ParseStream {
|
||||
private tokens: Array<Token>
|
||||
private handlers: {[key: string]: eventHandler} = {}
|
||||
private stopRequested: boolean
|
||||
private parseToken: parseToken
|
||||
|
||||
constructor (tokens: Array<Token>, parseToken: parseToken) {
|
||||
this.tokens = tokens
|
||||
this.parseToken = parseToken
|
||||
}
|
||||
on (name: string, cb: eventHandler) {
|
||||
this.handlers[name] = cb
|
||||
return this
|
||||
}
|
||||
trigger (event: string, arg?: Token | ITemplate) {
|
||||
const h = this.handlers[event]
|
||||
if (typeof h === 'function') {
|
||||
h(arg)
|
||||
return true
|
||||
}
|
||||
}
|
||||
start () {
|
||||
this.trigger('start')
|
||||
let token
|
||||
while (!this.stopRequested && (token = this.tokens.shift())) {
|
||||
if (this.trigger('token', token)) continue
|
||||
if (token.type === 'tag' &&
|
||||
this.trigger(`tag:${token.name}`, token)) {
|
||||
continue
|
||||
}
|
||||
const template = this.parseToken(token, this.tokens)
|
||||
this.trigger('template', template)
|
||||
}
|
||||
if (!this.stopRequested) this.trigger('end')
|
||||
return this
|
||||
}
|
||||
stop () {
|
||||
this.stopRequested = true
|
||||
return this
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user