-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.ts
42 lines (39 loc) · 809 Bytes
/
filters.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
37
38
39
40
41
42
// deno-lint-ignore-file no-explicit-any
export interface FilterFn {
(this: any, ...args: any[]): any
}
export const defaultFilterDict: Record<string, FilterFn> = {
pack(this: any[]) {
return Object.assign({}, ...this)
},
compact(this: any[]) {
return this.filter(Boolean)
},
flatten(this: any[][]) {
return this.reduce((r, a) => r.concat(a))
},
first(this: any[]) {
return this[0]
},
last(this: any[]) {
return this[this.length - 1]
},
get(this: any, key: any) {
return this[key]
},
Number() {
return Number(this)
},
String() {
return String(this)
},
Boolean() {
return Boolean(this)
},
Date() {
return new Date(this)
},
}
export function defineFilter(name: string, filter: FilterFn) {
defaultFilterDict[name] = filter
}