-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSequence.ts
More file actions
224 lines (202 loc) · 8.5 KB
/
Sequence.ts
File metadata and controls
224 lines (202 loc) · 8.5 KB
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
import { All } from "./all";
import { Any } from "./any";
import { AsIterable } from "./asIterable";
import { Associate } from "./associate";
import { AssociateBy } from "./associateBy";
import { Average } from "./average";
import { Chunk } from "./chunk";
import { Contains } from "./contains";
import { Count } from "./count";
import { Distinct } from "./distinct";
import { DistinctBy } from "./distinctBy";
import { Drop } from "./drop";
import { DropWhile } from "./dropWhile";
import { ElementAt } from "./elementAt";
import { ElementAtOrElse } from "./elementAtOrElse";
import { ElementAtOrNull } from "./elementAtOrNull";
import { Filter } from "./filter";
import { FilterIndexed } from "./filterIndexed";
import { FilterNot } from "./filterNot";
import { FilterNotNull } from "./filterNotNull";
import { First } from "./first";
import { FirstOrNull } from "./firstOrNull";
import { FlatMap } from "./flatMap";
import { Flatten } from "./flatten";
import { Fold } from "./fold";
import { FoldIndexed } from "./foldIndexed";
import { ForEach } from "./forEach";
import { ForEachIndexed } from "./forEachIndexed";
import { GroupBy } from "./groupBy";
import { IndexOf } from "./indexOf";
import { IndexOfFirst } from "./indexOfFirst";
import { IndexOfLast } from "./indexOfLast";
import { JoinToString } from "./joinToString";
import { Last } from "./last";
import { LastOrNull } from "./lastOrNull";
import { Map } from "./map";
import { MapIndexed } from "./mapIndexed";
import { MapNotNull } from "./mapNotNull";
import { Max } from "./max";
import { MaxBy } from "./maxBy";
import { MaxWith } from "./maxWith";
import { Merge } from "./merge";
import { Min } from "./min";
import { MinBy } from "./minBy";
import { Minus } from "./minus";
import { MinWith } from "./minWith";
import { None } from "./none";
import { OnEach } from "./onEach";
import { Partition } from "./partition";
import { Plus } from "./plus";
import { Reduce } from "./reduce";
import { ReduceIndexed } from "./reduceIndexed";
import { Reverse } from "./reverse";
import { Single } from "./single";
import { SingleOrNull } from "./singleOrNull";
import { Sorted } from "./sorted";
import { SortedBy } from "./sortedBy";
import { SortedByDescending } from "./sortedByDescending";
import { SortedDescending } from "./sortedDescending";
import { SortedWith } from "./sortedWith";
import { Sum } from "./sum";
import { SumBy } from "./sumBy";
import { Take } from "./take";
import { TakeWhile } from "./takeWhile";
import { ToArray } from "./toArray";
import { ToMap } from "./toMap";
import { ToSet } from "./toSet";
import { Unzip } from "./unzip";
import { WithIndex } from "./withIndex";
import { Zip } from "./zip";
import GeneratorIterator from "./GeneratorIterator";
import GeneratorSeedIterator from "./GeneratorSeedIterator";
/**
* A Sequence provides a fluent functional API consisting
* of various intermediate and terminal operations for processing the iterated data.
* The operations are evaluated lazily to avoid examining all the input data
* when it's not necessary. Sequences can be iterated only once.
*/
export interface Sequence<T> extends SequenceOperators<T> {
readonly iterator: Iterator<T>;
}
export default Sequence;
/**
* @hidden
*/
export interface SequenceOperators<T> extends All, Any, AsIterable, Associate, AssociateBy<T>, Average, Chunk, Contains, Count, Distinct, DistinctBy, Drop,
DropWhile, ElementAt, ElementAtOrElse, ElementAtOrNull, Filter, FilterIndexed, FilterNot, FilterNotNull, First, FirstOrNull, FlatMap, Flatten, Fold, FoldIndexed,
ForEach, ForEachIndexed, GroupBy, IndexOf, IndexOfFirst, IndexOfLast, JoinToString, Last, LastOrNull, Map, MapIndexed, MapNotNull, Max, MaxBy, MaxWith, Merge, Min, MinBy,
Minus, MinWith, None, OnEach, Partition, Plus, Reduce, ReduceIndexed, Reverse, Single, SingleOrNull, Sorted, SortedBy, SortedByDescending, SortedDescending, SortedWith,
Sum, SumBy, Take, TakeWhile, ToArray, ToMap, ToSet, Unzip, WithIndex, Zip {
}
class SequenceImpl<T> {
constructor(readonly iterator: Iterator<T>) {
}
}
applyMixins(SequenceImpl, [All, Any, AsIterable, Associate, AssociateBy, Average, Chunk, Contains, Count, Distinct, DistinctBy, Drop,
DropWhile, ElementAt, ElementAtOrElse, ElementAtOrNull, Filter, FilterIndexed, FilterNot, FilterNotNull, First, FirstOrNull, FlatMap, Flatten, Fold, FoldIndexed,
ForEach, ForEachIndexed, GroupBy, IndexOf, IndexOfFirst, IndexOfLast, JoinToString, Last, LastOrNull, Map, MapIndexed, MapNotNull, Max, MaxBy, MaxWith, Merge, Min, MinBy,
Minus, MinWith, None, OnEach, Partition, Plus, Reduce, ReduceIndexed, Reverse, Single, SingleOrNull, Sorted, SortedBy, SortedByDescending, SortedDescending, SortedWith,
Sum, SumBy, Take, TakeWhile, ToArray, ToMap, ToSet, Unzip, WithIndex, Zip]);
function applyMixins(derivedCtor: any, baseCtors: any[]) {
baseCtors.forEach(baseCtor => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
derivedCtor.prototype[name] = baseCtor.prototype[name];
});
});
}
export function sequenceOf<T>(...args: Array<T>): Sequence<T> {
return asSequence(args);
}
export function emptySequence<T>(): Sequence<T> {
return asSequence([]);
}
export function asSequence<T>(iterable: Iterable<T>): Sequence<T> {
if (iterable === null) {
throw new Error("Cannot create sequence for input: null");
}
if (iterable === undefined) {
throw new Error("Cannot create sequence for input: undefined");
}
if (iterable[Symbol.iterator] == null) {
throw new Error("Cannot create sequence for non-iterable input: " + iterable);
}
const iterator = iterable[Symbol.iterator]();
return createSequence<T>(iterator);
}
export function createSequence<T>(iterator: Iterator<T>): Sequence<T> {
return new SequenceImpl(iterator) as any;
}
export function isSequence<T>(object: any): object is Sequence<T> {
return object instanceof SequenceImpl;
}
export function extendSequence(mixin: { new(): any }) {
applyMixins(SequenceImpl, [mixin]);
}
export function generateSequence<T>(nextFunction: () => T | null | undefined): Sequence<T>;
export function generateSequence<T>(seedFunction: () => T | null | undefined, nextFunction: (item: T) => T | null | undefined): Sequence<T>;
export function generateSequence<T>(seed: T | null | undefined, nextFunction: (item: T) => T | null | undefined): Sequence<T>;
export function generateSequence<T>(a: any, b?: any): Sequence<T> {
if (typeof a === "function" && b == null) {
return createSequence<T>(new GeneratorIterator(a));
}
const seed = typeof a === "function" ? a() : a;
return seed != null
? createSequence<T>(new GeneratorSeedIterator(seed, b))
: emptySequence<T>();
}
export function range(start: number, endInclusive: number, step: number = 1): Sequence<number> {
if (start > endInclusive) {
throw new Error(`start [${start}] must be lower then endInclusive [${endInclusive}]`);
}
let current = start;
return generateSequence(() => {
try {
return current <= endInclusive
? current
: undefined;
} finally {
current += step;
}
});
}
export function asKeysSequence<K>(keyedIterable: {
keys(): Iterable<K>;
}): Sequence<K>;
export function asKeysSequence<K extends PropertyKey>(obj: Record<K, any>): Sequence<K>;
export function asKeysSequence<K>(a: any): Sequence<K> {
if (typeof a !== "object") {
throw new TypeError("Cannot create keys sequence for non-object input");
}
if (a === null) {
throw new TypeError("Cannot create keys sequence for input: null");
}
const gen = typeof a.keys === "function" ? a.keys.bind(a) : function* () {
for (const key in a) {
if (a.hasOwnProperty(key)) {
yield key as K;
}
}
};
return asSequence(gen());
}
export function asValuesSequence<V>(keyedIterable: {
values(): Iterable<V>;
}): Sequence<V>;
export function asValuesSequence<V>(obj: Record<any, V>): Sequence<V>;
export function asValuesSequence<V>(a: any): Sequence<V> {
if (typeof a !== "object") {
throw new TypeError("Cannot create values sequence for non-object input");
}
if (a === null) {
throw new TypeError("Cannot create values sequence for input: null");
}
const gen = typeof a.values === "function" ? a.values.bind(a) : function* () {
for (const key in a) {
if (a.hasOwnProperty(key)) {
yield a[key];
}
}
};
return asSequence(gen());
}