-
Notifications
You must be signed in to change notification settings - Fork 2
/
methods.ts
261 lines (207 loc) · 8.7 KB
/
methods.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import { IMethods, IIterator } from "../intefaces";
import { Utils } from '../utils';
import {
WhereClause, SelectClause, SelectManyClause, JoinClause, LeftJoinClause, OrderByClause,
OrderByDescendingClause, GroupByClause, GroupJoinClause, FirstClause, LastClause, CountClause,
SumClause, AvarageClause, MinClause, MaxClause, SingleClause, TakeClause, SkipWhileClause,
SkipClause, TakeWhileClause, AnyClause, ContainsClause, AllClause, DistinctClause, ConcatClause
} from "../methods";
import { Queryable } from './queryable';
export class IteratorMethods<T> implements IMethods<T> {
// Contains all iterators
_iteratorCollection: Array<IIterator<T>> = [];
// Contains initial source
_source: T[] | Promise<T[]>;
_data: any[];
constructor(iteratorCollection: Array<IIterator<T>>, source: T[] | Promise<T[]>) {
this._iteratorCollection = iteratorCollection;
this._source = source;
}
clone(): IMethods<T> {
const _cloneCollection = Object.assign([], this._iteratorCollection);
return new IteratorMethods(_cloneCollection, this._source);
}
where(iterator: (entity: T) => boolean): IMethods<T> {
this._iteratorCollection.push(new WhereClause(iterator));
return this;
}
select<S>(iterator: (entity: T) => S): IMethods<S> {
this._iteratorCollection.push(new SelectClause(iterator))
return this as any;
}
selectMany<S>(iterator: (entity: T) => S): IMethods<S> {
this._iteratorCollection.push(new SelectManyClause(iterator))
return this as any;
}
join<S, U>(source: S[] | Promise<S[]>, iterator: (aEntity: T, bEntity: S) => boolean): IMethods<U> {
this._iteratorCollection.push(new JoinClause(source, iterator));
return this as any;
}
leftJoin<S, U>(source: S[] | Promise<S[]>, iterator: (aEntity: T, bEntity: S) => boolean): IMethods<U> {
this._iteratorCollection.push(new LeftJoinClause(source, iterator));
return this as any;
}
orderBy(iterator: (entity: T) => void): IMethods<T> {
this._iteratorCollection.push(new OrderByClause(iterator));
return this as any;
}
orderByDescending(iterator: (entity: T) => void): IMethods<T> {
this._iteratorCollection.push(new OrderByDescendingClause(iterator));
return this as any;
}
groupBy(iterator: (entity: T) => any): IMethods<{ key: any; items: T[]; }> {
this._iteratorCollection.push(new GroupByClause(iterator));
return this as any;
}
groupJoin<S>(source: S[],
joinIterator: (aEntity: T, bEntity: S) => boolean,
groupIterator: (entity: { x: T; y: S; }) => any): IMethods<{ key: any; items: T[]; }> {
this._iteratorCollection.push(new GroupJoinClause(source, joinIterator, groupIterator));
return this as any;
}
distinct(comparer?: (aEntity: T, bEntity: T) => boolean): IMethods<T> {
this._iteratorCollection.push(new DistinctClause(comparer));
return this;
}
concat(another: T[] | Promise<T[]>): IMethods<T> {
this._iteratorCollection.push(new ConcatClause(another));
return this;
}
// Return S[] | Promise<S[]>
toList<S>(): any {
return this.runIterators();
}
first(iterator?: (entity: T) => boolean): Promise<T> {
return this.filterReturn(this.toList(), (data) => {
return new FirstClause(iterator).execute(data) as T;
});
}
firstOrDefault(iterator?: (entity: T) => boolean): Promise<T> {
return this.filterReturn(this.toList(), (data) => {
return (new FirstClause(iterator).execute(data) || null) as T;
});
}
last(iterator?: (entity: T) => boolean): Promise<T> {
return this.filterReturn(this.toList(), (data) => {
return new LastClause(iterator).execute(data) as T;
});
}
lastOrDefault(iterator?: (entity: T) => boolean): Promise<T> {
return this.filterReturn(this.toList(), (data) => {
return (new LastClause(iterator).execute(data) || null) as T;
});
}
count(iterator?: (entity: T) => boolean): Promise<number> {
return this.filterReturn(this.toList(), (data) => {
return (new CountClause(iterator).execute(data) || null) as number;
}) as any;
}
sum<S>(iterator: (entity: T) => S): Promise<number> {
return this.filterReturn(this.toList(), (data) => {
return (new SumClause(iterator).execute(data) || null) as number;
});
}
avarage<S>(iterator: (entity: T) => S): Promise<number> {
return this.filterReturn(this.toList(), (data) => {
return (new AvarageClause(iterator).execute(data) || 0) as number;
});
}
min<S>(iterator: (entity: T) => S): Promise<number> {
return this.filterReturn(this.toList(), (data) => {
return (new MinClause(iterator).execute(data) || null) as number;
});
}
max<S>(iterator: (entity: T) => S): Promise<number> {
return this.filterReturn(this.toList(), (data) => {
return (new MaxClause(iterator).execute(data) || null) as number;
});
}
single(iterator?: (entity: T) => boolean): Promise<T> {
return this.filterReturn(this.toList(), (data) => {
if (!data) throw new Error("Single require source is not null");
return new SingleClause(iterator).execute(data);
});
}
singleOrDefault(iterator?: (entity: T) => boolean): Promise<T> {
return this.filterReturn(this.toList(), (data) => {
return new SingleClause(iterator).execute(data) || null;
});
}
take(value: number): IMethods<T> {
this._iteratorCollection.push(new TakeClause(value));
return this;
}
skip(value: number): IMethods<T> {
this._iteratorCollection.push(new SkipClause(value));
return this;
}
skipWhile(iterator: (entity: T) => boolean): IMethods<T> {
this._iteratorCollection.push(new SkipWhileClause(iterator));
return this;
}
takeWhile(iterator: (entity: T) => boolean): IMethods<T> {
this._iteratorCollection.push(new TakeWhileClause(iterator));
return this;
}
any<T>(iterator: (entity: T) => boolean): Promise<boolean> {
return this.filterReturn(this.toList(), (data) => {
return new AnyClause(iterator).execute(data);
});
}
all<T>(iterator: (entity: T) => boolean): Promise<boolean> {
return this.filterReturn(this.toList(), (data) => {
return new AllClause(iterator).execute(data);
});
}
contains(entity: T): Promise<boolean> {
return this.filterReturn(this.toList(), (data) => {
return new ContainsClause(entity).execute(data);
});
}
// Private functions
// This function detect the input parameter is Promise or plain array data
// if is promise => call promise and return from callback
// opposite => return from callback
private filterReturn(obj: T[] | Promise<T[]>, promiseCallback: Function) {
if (Utils.isPromise(obj)) {
return (obj as Promise<T[]>).then((data: T[]) => {
return promiseCallback(data);
})
} else
return promiseCallback(obj);
}
runIterators(): Promise<T[]> | T[] {
let _result: T[] = [];
let _nextSources = {};
let _promises = [];
for (let i = 0, li = this._iteratorCollection.length; i < li; i++) {
let _iterator = this._iteratorCollection[i];
if (!_iterator.hasSource()) continue;
if (Utils.isPromise(_iterator.nextSource))
_promises.push(_iterator.nextSource);
else
_promises.push(Promise.resolve(_iterator.nextSource));
}
if (Utils.isPromise(this._source))
_promises.unshift(this._source);
else
_promises.unshift(Promise.resolve(this._source));
return new Promise(resolve => {
Promise.all(_promises).then((responseDatas: any[]) => {
let _index = 0;
_result = responseDatas[0]; // Set from method's source
for (let i = 0, li = this._iteratorCollection.length; i < li; i++) {
let _iterator = this._iteratorCollection[i];
if (_iterator.hasSource()) {
_iterator.replaceBySyncSource(responseDatas[_index + 1]);
_index += 1;
}
}
this._iteratorCollection.forEach((ite: IIterator<T>) => {
_result = ite.execute(_result) as T[];
});
resolve(_result);
});
});
}
}