-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
filter.ts
36 lines (32 loc) · 1.04 KB
/
filter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { evalValue } from '../../render/syntax'
import Scope from '../../scope/scope'
import { isArray } from '../../util/underscore'
import { FilterImpl } from './filter-impl'
export type FilterArgs = Array<string|[string?, string?]>
export class Filter {
name: string
impl: FilterImpl
args: FilterArgs
private static impls: {[key: string]: FilterImpl} = {}
constructor (name: string, args: FilterArgs, strictFilters: boolean) {
const impl = Filter.impls[name]
if (!impl && strictFilters) throw new TypeError(`undefined filter: ${name}`)
this.name = name
this.impl = impl || (x => x)
this.args = 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
}
static clear () {
Filter.impls = {}
}
}