This repository has been archived by the owner on Sep 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapset.ts
93 lines (81 loc) · 3.52 KB
/
mapset.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { DataSet } from './dataset';
import { MemSet } from './memarrayvisitor';
import { Select, Expand, Filter, Top, Skip, Property, Order } from './expressions';
import { Utility } from './core';
export class MapSet<T> extends DataSet<T>{
constructor(private source: DataSet<T>, private mapFn: ((element: T, index: number, items: T[]) => any) | string, expressions: Array<any> = [],private mapFnEx?:((element: T, beforeElement:any, index: Number, items: T[]) => any)) {
super(expressions);
}
private createMemset(expressions) {
if (typeof this.mapFn === "function") {
let filters = expressions.filter(this.onlySortandElimination);
return this.source.query.apply(this.source, expressions.filter(this.onlyRange))
.then((response) => {
let result = Array.isArray(response) ? response.map(this.mapFn as any) : (this.mapFn as any)(response, -1, null);
let set = new MemSet(result);
set = set.query.apply(set, expressions.filter(this.onlySelect));
let memset = filters.length != 0 ? set.query.apply(set,filters) : set;
return {
set: memset
};
});
}
if (typeof this.mapFn === "string") {
let filters = expressions.filter(this.onlySortandElimination);
let exps = expressions.filter(this.onlyRange).concat([new Expand([{
property: new Property(this.mapFn),
expressions: expressions.filter(this.onlySelect)
}])]);
return this.source.query.apply(this.source, exps).then((response) => {
let result = Array.isArray(response) ? response.map((x,i,array) => {
let result = x[this.mapFn as string];
if(this.mapFnEx == null) return result;
return this.mapFnEx(result,x,i,array);
}) : ()=>{
let result = response[this.mapFn as string];
if(this.mapFnEx != null) {
return this.mapFnEx(result,response,-1,null);
}
return result;
};
let set = new MemSet(result as any);
return {
set: (filters.length != 0 ? set.query.apply(set,filters) : set)
};
});
}
throw new Error('Not support');
}
query(...expression: Array<any>) {
return new MapSet<T>(this.source, this.mapFn, [].concat(this.expressions).concat(expression || []));
}
private onlySelect(x) {
let types = [Select, Expand]
return types.some(t => Utility.instanceof(x,t));
}
private onlyRange(x) {
let types = [Skip, Top]
return types.some(t => Utility.instanceof(x,t));
}
private onlySortandElimination(x) {
let types = [Filter, Order]
return types.some(t => Utility.instanceof(x,t));
}
add(item) {
return Promise.reject('Mapset: not support add');
}
update(item) {
return Promise.reject('Mapset: not support update');
}
delete(item) {
return Promise.reject('Mapset: not support delete');
}
get(...expression: Array<any>) {
let allExpressions = [].concat(this.expressions || []).concat(expression || []);
return this.createMemset(allExpressions).then((e) => {
return e.set.then((response) => {
return response;
});
});
}
}