forked from Siderite/LInQer
/
LInQer.Slim.ts
478 lines (441 loc) · 13.3 KB
/
LInQer.Slim.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
namespace Linqer {
/// wrapper class over iterable instances that exposes the methods usually found in .NET LINQ
export class Enumerable implements Iterable<any>, IUsesQuickSort {
_src: IterableType;
_generator: () => Iterator<any>;
_useQuickSort: boolean;
_canSeek: boolean;
_count: null | (() => number);
_tryGetAt: null | ((index: number) => { value: any } | null);
_wasIterated: boolean;
constructor(src: IterableType) {
_ensureIterable(src);
this._src = src;
const iteratorFunction: (() => Iterator<any>) = (src as Iterable<any>)[Symbol.iterator];
if (iteratorFunction) {
this._generator = iteratorFunction.bind(src);
} else {
this._generator = src as (() => Iterator<any>);
}
this._useQuickSort = (src as IUsesQuickSort)._useQuickSort !== undefined
? (src as IUsesQuickSort)._useQuickSort
: true;
this._canSeek = false;
this._count = null;
this._tryGetAt = null;
this._wasIterated = false;
}
/// Wraps an iterable item into an Enumerable if it's not already one
static from(iterable: IterableType): Enumerable {
if (iterable instanceof Enumerable) return iterable;
return new Enumerable(iterable);
}
/// the Enumerable instance exposes the same iterator as the wrapped iterable or generator function
[Symbol.iterator](): Iterator<any> {
this._wasIterated = true;
return this._generator();
}
/// returns an empty Enumerable
static empty(): Enumerable {
const result = new Enumerable([]);
result._count = () => 0;
result._tryGetAt = (index: number) => null;
result._canSeek = true;
return result;
}
/// generates a sequence of integral numbers within a specified range.
static range(start: number, count: number): Enumerable {
const gen = function* () {
for (let i = 0; i < count; i++) {
yield start + i;
}
};
const result = new Enumerable(gen);
result._count = () => count;
result._tryGetAt = index => {
if (index >= 0 && index < count) return { value: start + index };
return null;
};
result._canSeek = true;
return result;
}
/// Generates a sequence that contains one repeated value.
static repeat(item: any, count: number): Enumerable {
const gen = function* () {
for (let i = 0; i < count; i++) {
yield item;
}
};
const result = new Enumerable(gen);
result._count = () => count;
result._tryGetAt = index => {
if (index >= 0 && index < count) return { value: item };
return null;
};
result._canSeek = true;
return result;
}
/// Concatenates two sequences by appending iterable to the existing one.
concat(iterable: IterableType): Enumerable {
_ensureIterable(iterable);
const self: Enumerable = this;
const gen = function* () {
for (const item of self) {
yield item;
}
for (const item of Enumerable.from(iterable)) {
yield item;
}
};
const result = new Enumerable(gen);
const other = Enumerable.from(iterable);
result._count = () => self.count() + other.count();
_ensureInternalTryGetAt(this);
_ensureInternalTryGetAt(other);
result._canSeek = self._canSeek && other._canSeek;
if (self._canSeek) {
result._tryGetAt = index => {
return self._tryGetAt!(index) || other._tryGetAt!(index - self.count());
};
}
return result;
}
/// Returns the number of elements in a sequence.
count(): number {
_ensureInternalCount(this);
return this._count!();
}
/// Returns distinct elements from a sequence. WARNING: using a comparer makes this slower
distinct(equalityComparer: IEqualityComparer = EqualityComparer.default): Enumerable {
const self: Enumerable = this;
const gen = equalityComparer === EqualityComparer.default
? function* () {
const distinctValues = new Set();
for (const item of self) {
const size = distinctValues.size;
distinctValues.add(item);
if (size < distinctValues.size) {
yield item;
}
}
}
: function* () {
const values = [];
for (const item of self) {
let unique = true;
for (const prevItem of values) {
if (equalityComparer(item, prevItem)) {
unique = false;
break;
}
}
if (unique) yield item;
values.push(item);
}
};
return new Enumerable(gen);
}
/// Returns the element at a specified index in a sequence.
elementAt(index: number): any {
_ensureInternalTryGetAt(this);
const result = this._tryGetAt!(index);
if (!result) throw new Error('Index out of range');
return result.value;
}
/// Returns the element at a specified index in a sequence or a default value if the index is out of range.
elementAtOrDefault(index: number): any | undefined {
_ensureInternalTryGetAt(this);
const result = this._tryGetAt!(index);
if (!result) return undefined;
return result.value;
}
/// Returns the first element of a sequence.
first(): any {
return this.elementAt(0);
}
/// Returns the first element of a sequence, or a default value if no element is found.
firstOrDefault(): any | undefined {
return this.elementAtOrDefault(0);
}
/// Returns the last element of a sequence.
last(): any {
_ensureInternalTryGetAt(this);
if (!this._canSeek) {
let result = null;
let found = false;
for (const item of this) {
result = item;
found = true;
}
if (found) return result;
throw new Error('The enumeration is empty');
}
const count = this.count();
return this.elementAt(count - 1);
}
/// Returns the last element of a sequence, or a default value if no element is found.
lastOrDefault(): any | undefined {
_ensureInternalTryGetAt(this);
if (!this._canSeek) {
let result = undefined;
for (const item of this) {
result = item;
}
return result;
}
const count = this.count();
return this.elementAtOrDefault(count - 1);
}
/// Returns the count, minimum and maximum value in a sequence of values.
/// A custom function can be used to establish order (the result 0 means equal, 1 means larger, -1 means smaller)
stats(comparer?: IComparer): { count: number, min: any, max: any } {
if (comparer) {
_ensureFunction(comparer);
} else {
comparer = _defaultComparer;
}
const agg = {
count: 0,
min: undefined,
max: undefined
};
for (const item of this) {
if (typeof agg.min === 'undefined' || comparer(item, agg.min) < 0) agg.min = item;
if (typeof agg.max === 'undefined' || comparer(item, agg.max) > 0) agg.max = item;
agg.count++;
}
return agg;
}
/// Returns the minimum value in a sequence of values.
/// A custom function can be used to establish order (the result 0 means equal, 1 means larger, -1 means smaller)
min(comparer?: IComparer): any {
const stats = this.stats(comparer);
return stats.count === 0
? undefined
: stats.min;
}
/// Returns the maximum value in a sequence of values.
/// A custom function can be used to establish order (the result 0 means equal, 1 means larger, -1 means smaller)
max(comparer?: IComparer): any {
const stats = this.stats(comparer);
return stats.count === 0
? undefined
: stats.max;
}
/// Projects each element of a sequence into a new form.
select(selector: ISelector): Enumerable {
_ensureFunction(selector);
const self: Enumerable = this;
const gen = function* () {
let index = 0;
for (const item of self) {
yield selector(item, index);
index++;
}
};
const result = new Enumerable(gen);
_ensureInternalCount(this);
result._count = this._count;
_ensureInternalTryGetAt(self);
result._canSeek = self._canSeek;
result._tryGetAt = index => {
const res = self._tryGetAt!(index);
if (!res) return res;
return { value: selector(res.value) };
};
return result;
}
/// Bypasses a specified number of elements in a sequence and then returns the remaining elements.
skip(nr: number): Enumerable {
const self: Enumerable = this;
const gen = function* () {
let nrLeft = nr;
for (const item of self) {
if (nrLeft > 0) {
nrLeft--;
} else {
yield item;
}
}
};
const result = new Enumerable(gen);
result._count = () => Math.max(0, self.count() - nr);
_ensureInternalTryGetAt(this);
result._canSeek = this._canSeek;
result._tryGetAt = index => self._tryGetAt!(index + nr);
return result;
}
/// Computes the sum of a sequence of numeric values.
sum(): number | undefined {
const stats = this.sumAndCount();
return stats.count === 0
? undefined
: stats.sum;
}
/// Computes the sum of a sequence of numeric values.
sumAndCount(): { sum: number, count: number } {
const agg = {
count: 0,
sum: 0
};
for (const item of this) {
agg.sum = agg.count === 0
? _toNumber(item)
: agg.sum + _toNumber(item);
agg.count++;
}
return agg;
}
/// Returns a specified number of contiguous elements from the start of a sequence.
take(nr: number): Enumerable {
const self: Enumerable = this;
const gen = function* () {
let nrLeft = nr;
for (const item of self) {
if (nrLeft > 0) {
yield item;
nrLeft--;
}
if (nrLeft <= 0) {
break;
}
}
};
const result = new Enumerable(gen);
result._count = () => Math.min(nr, self.count());
_ensureInternalTryGetAt(this);
result._canSeek = self._canSeek;
if (self._canSeek) {
result._tryGetAt = index => {
if (index >= nr) return null;
return self._tryGetAt!(index);
};
}
return result;
}
/// creates an array from an Enumerable
toArray(): any[] {
return Array.from(this);
}
/// similar to toArray, but returns a seekable Enumerable (itself if already seekable) that can do count and elementAt without iterating
toList(): Enumerable {
_ensureInternalTryGetAt(this);
if (this._canSeek) return this;
return Enumerable.from(Array.from(this));
}
/// Filters a sequence of values based on a predicate.
where(condition: IFilter): Enumerable {
_ensureFunction(condition);
const self: Enumerable = this;
const gen = function* () {
let index = 0;
for (const item of self) {
if (condition(item, index)) {
yield item;
}
index++;
}
};
return new Enumerable(gen);
}
}
export function _ensureIterable(src: IterableType): void {
if (src) {
if ((src as Iterable<any>)[Symbol.iterator]) return;
if (typeof src === 'function' && (src as Function).constructor.name === 'GeneratorFunction') return;
}
throw new Error('the argument must be iterable!');
}
export function _ensureFunction(f: Function): void {
if (!f || typeof f !== 'function') throw new Error('the argument needs to be a function!');
}
function _toNumber(obj: any): number {
return typeof obj === 'number'
? obj
: Number.NaN;
}
export function _toArray(enumerable: IterableType) {
if (!enumerable) return [];
if (Array.isArray(enumerable)) return enumerable;
return Array.from(enumerable);
}
export function _ensureInternalCount(enumerable: Enumerable) {
if (enumerable._count) return;
const src = enumerable._src as any;
if (typeof src !== 'function' && typeof src.length === 'number') {
enumerable._count = () => src.length;
return;
}
if (typeof src.size === 'number') {
enumerable._count = () => src.size;
return;
}
enumerable._count = () => {
let x = 0;
for (const item of enumerable) x++;
return x;
};
}
export function _ensureInternalTryGetAt(enumerable: Enumerable) {
if (enumerable._tryGetAt) return;
enumerable._canSeek = true;
if (typeof enumerable._src === 'string') {
enumerable._tryGetAt = index => {
if (index < (enumerable._src as string).length) {
return { value: (enumerable._src as string).charAt(index) };
}
return null;
};
return;
}
if (Array.isArray(enumerable._src)) {
enumerable._tryGetAt = index => {
if (index >= 0 && index < (enumerable._src as any[]).length) {
return { value: (enumerable._src as any[])[index] };
}
return null;
};
return;
}
const src = enumerable._src as any;
if (typeof enumerable._src !== 'function' && typeof src.length === 'number') {
enumerable._tryGetAt = index => {
if (index < src.length && typeof src[index] !== 'undefined') {
return { value: src[index] };
}
return null;
};
return;
}
enumerable._canSeek = false;
// TODO other specialized types? objects, maps, sets?
enumerable._tryGetAt = index => {
let x = 0;
for (const item of enumerable) {
if (index === x) return { value: item };
x++;
}
return null;
}
}
/// an extended iterable type that also supports generator functions
export type IterableType = Iterable<any> | (() => Iterator<any>) | Enumerable;
export type IComparer = (item1: any, item2: any) => -1 | 0 | 1;
export type ISelector<T = any> = (item: any, index?: number) => T;
export type IFilter = ISelector<boolean>;
export const _defaultComparer: IComparer = (item1, item2) => {
if (item1 > item2) return 1;
if (item1 < item2) return -1;
return 0;
};
export type IEqualityComparer = (item1: any, item2: any) => boolean;
/// default equality comparers
export const EqualityComparer = {
default: (item1: any, item2: any) => item1 == item2,
exact: (item1: any, item2: any) => item1 === item2,
};
/// an object that supports the quick sort use flag
interface IUsesQuickSort {
_useQuickSort: boolean;
}
}