-
Notifications
You must be signed in to change notification settings - Fork 72
/
iterablex.ts
181 lines (166 loc) · 5.31 KB
/
iterablex.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
import { OperatorFunction } from '../interfaces';
import { bindCallback } from '../internal/bindcallback';
import { identity } from '../internal/identity';
import { toLength } from '../internal/tolength';
import { isArrayLike, isIterable } from '../internal/isiterable';
/**
* This clas serves as the base for all operations which support [Symbol.iterator].
*/
export abstract class IterableX<T> implements Iterable<T> {
abstract [Symbol.iterator](): Iterator<T>;
forEach(projection: (value: T, index: number) => void, thisArg?: any): void {
const fn = bindCallback(projection, thisArg, 2);
let i = 0;
for (let item of this) {
fn(item, i++);
}
}
pipe<R>(...operations: OperatorFunction<T, R>[]): IterableX<R> {
if (operations.length === 0) {
return this as any;
}
const piped = (input: Iterable<T>): IterableX<R> => {
return operations.reduce((prev: any, fn: OperatorFunction<T, R>) => fn(prev), input as any);
};
return piped(this);
}
static as(source: string): IterableX<string>;
static as<T>(source: Iterable<T>): IterableX<T>;
static as<T>(source: ArrayLike<T>): IterableX<T>;
static as<T>(source: T): IterableX<T>;
static as(source: any) {
/* tslint:disable */
if (typeof source === 'string') {
return new OfIterable([source]);
}
if (isIterable(source)) {
return new FromIterable(source, identity);
}
if (isArrayLike(source)) {
return new FromIterable(source, identity);
}
return new OfIterable([source]);
/* tslint:enable */
}
static from<TSource, TResult = TSource>(
source: Iterable<TSource> | ArrayLike<TSource>,
selector: (value: TSource, index: number) => TResult = identity,
thisArg?: any
): IterableX<TResult> {
const fn = bindCallback(selector, thisArg, 2);
/* tslint:disable */
if (isIterable(source)) {
return new FromIterable<TSource, TResult>(source, fn);
}
if (isArrayLike(source)) {
return new FromIterable<TSource, TResult>(source, fn);
}
throw new TypeError('Input type not supported');
/* tslint:enable */
}
static of<TSource>(...args: TSource[]): IterableX<TSource> {
//tslint:disable-next-line
return new OfIterable<TSource>(args);
}
}
class FromIterable<TSource, TResult = TSource> extends IterableX<TResult> {
private _source: Iterable<TSource> | ArrayLike<TSource>;
private _fn: (value: TSource, index: number) => TResult;
constructor(
source: Iterable<TSource> | ArrayLike<TSource>,
fn: (value: TSource, index: number) => TResult
) {
super();
this._source = source;
this._fn = fn;
}
*[Symbol.iterator]() {
const iterable = isIterable(this._source);
let i = 0;
if (iterable) {
for (let item of <Iterable<TSource>>this._source) {
yield this._fn(item, i++);
}
} else {
let length = toLength((<ArrayLike<TSource>>this._source).length);
while (i < length) {
let val = (<ArrayLike<TSource>>this._source)[i];
yield this._fn(val, i++);
}
}
}
}
class OfIterable<TSource> extends IterableX<TSource> {
private _args: TSource[];
constructor(args: TSource[]) {
super();
this._args = args;
}
*[Symbol.iterator]() {
yield* this._args;
}
}
declare module '../iterable/iterablex' {
interface IterableX<T> extends Iterable<T> {
pipe(): IterableX<T>;
pipe<A>(op1: OperatorFunction<T, A>): IterableX<A>;
pipe<A, B>(op1: OperatorFunction<T, A>, op2: OperatorFunction<A, B>): IterableX<B>;
pipe<A, B, C>(
op1: OperatorFunction<T, A>,
op2: OperatorFunction<A, B>,
op3: OperatorFunction<B, C>
): IterableX<C>;
pipe<A, B, C, D>(
op1: OperatorFunction<T, A>,
op2: OperatorFunction<A, B>,
op3: OperatorFunction<B, C>,
op4: OperatorFunction<C, D>
): IterableX<D>;
pipe<A, B, C, D, E>(
op1: OperatorFunction<T, A>,
op2: OperatorFunction<A, B>,
op3: OperatorFunction<B, C>,
op4: OperatorFunction<C, D>,
op5: OperatorFunction<D, E>
): IterableX<E>;
pipe<A, B, C, D, E, F>(
op1: OperatorFunction<T, A>,
op2: OperatorFunction<A, B>,
op3: OperatorFunction<B, C>,
op4: OperatorFunction<C, D>,
op5: OperatorFunction<D, E>,
op6: OperatorFunction<E, F>
): IterableX<F>;
pipe<A, B, C, D, E, F, G>(
op1: OperatorFunction<T, A>,
op2: OperatorFunction<A, B>,
op3: OperatorFunction<B, C>,
op4: OperatorFunction<C, D>,
op5: OperatorFunction<D, E>,
op6: OperatorFunction<E, F>,
op7: OperatorFunction<F, G>
): IterableX<G>;
pipe<A, B, C, D, E, F, G, H>(
op1: OperatorFunction<T, A>,
op2: OperatorFunction<A, B>,
op3: OperatorFunction<B, C>,
op4: OperatorFunction<C, D>,
op5: OperatorFunction<D, E>,
op6: OperatorFunction<E, F>,
op7: OperatorFunction<F, G>,
op8: OperatorFunction<G, H>
): IterableX<H>;
pipe<A, B, C, D, E, F, G, H, I>(
op1: OperatorFunction<T, A>,
op2: OperatorFunction<A, B>,
op3: OperatorFunction<B, C>,
op4: OperatorFunction<C, D>,
op5: OperatorFunction<D, E>,
op6: OperatorFunction<E, F>,
op7: OperatorFunction<F, G>,
op8: OperatorFunction<G, H>,
op9: OperatorFunction<H, I>
): IterableX<I>;
pipe<R>(...operations: OperatorFunction<T, R>[]): IterableX<R>;
}
}