-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.ts
41 lines (37 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
37
38
39
40
41
import Vue from 'vue';
import { parentMethods } from './utils/tools';
interface FilterOptions {
read?: Function;
write?: Function;
filter?: Function;
}
export default function Filter(filterName: string, local?: boolean) {
return function (target: any): any {
let newInstance = new target();
parentMethods(target, newInstance);
let options: FilterOptions = {};
if (newInstance.filter) {
options.filter = newInstance.filter.bind(newInstance);
} else {
for (let key in newInstance) {
let isFunc = typeof newInstance[key] === 'function';
if ((key === 'read' || key === 'write') && isFunc) {
options[key] = newInstance[key].bind(newInstance);
}
}
}
if (!filterName) {
console.warn(`[vue-ts-decorate] Parameter 'filterName' must be set in filters`);
}
if (local) {
return { [filterName]: options.filter ? options.filter : options };
} else {
if (options.filter) {
Vue.filter(filterName, options.filter);
} else {
Vue.filter(filterName, <any>options);
}
return Vue.filter(filterName);
}
};
}