-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate.ts
243 lines (221 loc) · 6.97 KB
/
create.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
/*
* @nevware21/ts-utils
* https://github.com/nevware21/ts-utils
*
* Copyright (c) 2022 NevWare21 Solutions LLC
* Licensed under the MIT license.
*/
import { objDefine } from "../object/define";
import { getKnownSymbol } from "../symbol/symbol";
import { WellKnownSymbols } from "../symbol/well_known";
/**
* The context used to manage how the {@link createIterator} returns and moves to the next item,
* and provides to the current value `v`.
* @since 0.4.2
* @group Iterator
*/
export interface CreateIteratorContext<T> {
/**
* A function that returns a boolean to indicate whether it was able to produce
* the next value in the sequence. It should return `true` when the sequence is done.
* @param args - Optional additional arguments that where passed to the iterator `next` function.
* @return `false` if a new value was produced and assigned to the `v` of the context, otherwise
* `true` to indicate that the sequence is done.
*/
n: (...args: any) => boolean;
/**
* The current value to be assigned to the returned iterator result, the next `n`
* function should assign this value to the context as part of incrementing to
* the next value.
*/
v?: T;
/**
* Optional function that accepts zero or one argument. This function is called via the
* iterator `return` function when the iterator caller does not intend to make any more
* `next()` calls so the implementation and can perform any cleanup actions.
* @return [Optional] value to be included in the final iteration result
*/
r?: (value?: T) => T | undefined;
/**
* A function that accepts zero or one argument. The function is called via the iterator
* `throw` function when that the iterator caller detects an error condition, and e is
* typically an Error instance.
* @return [Optional] value to be included in the final iteration result
*/
t?: (e?: any) => T | undefined;
}
/**
* Create an iterable which conforms to the `Iterable` protocol, it uses the provided `ctx` to
* create an `Iterator` via {@link createIterator}.
*
* @see [Iterable protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol)
* @since 0.4.2
* @group Iterator
* @typeParam T - Identifies the type that will be returned by the iterator
* @param ctx - The context used to manage the iteration over the items.
* @returns A new Iterable instance
* @example
* ```ts
* let current = 0;
* let next = 1;
* let done = false;
* let fibCtx: CreateIteratorContext<number> = {
* n: function() {
* fibCtx.v = current;
* current = next;
* next = fibCtx.v + next;
*
* // Return not done
* return false;
* },
* r: function(value) {
* done = true;
* return value;
* }
* };
*
* let values: number[] = [];
* iterForOf(createIterable(fibCtx), (value) => {
* values.push(value);
* if (values.length === 10) {
* return -1;
* }
* });
*
* // Done is true
* // done === true
* // Values: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
* ```
*/
/*#__NO_SIDE_EFFECTS__*/
export function createIterable<T>(ctx: CreateIteratorContext<T>): Iterable<T> {
return makeIterable({} as Iterable<T>, ctx);
}
/**
* Adds or replaces an iterable implementation that conforms to the `Iterable` protocol to the target instance, it
* uses the provided `ctx` to create an `Iterator` via {@link createIterator}.
*
* @see [Iterable protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol)
* @since 0.4.2
* @group Iterator
* @typeParam T - Identifies the target type
* @typeParam I - Identifies the type that will be returned by the iterator
* @param ctx - The context used to manage the iteration over the items.
* @returns A new Iterable instance
* @example
* ```ts
* let current = 0;
* let next = 1;
* let done = false;
* let fibCtx: CreateIteratorContext<number> = {
* n: function() {
* fibCtx.v = current;
* current = next;
* next = fibCtx.v + next;
*
* // Return not done, so it will just continue
* return false;
* }
* };
*
* let values: number[] = [];
* let theIterable: Iterable<T> = makeIterable({}, fibCtx);
*
* iterForOf(theIterable, (value) => {
* values.push(value);
* if (values.length === 10) {
* return -1;
* }
* });
*
* // Values: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
* ```
*/
export function makeIterable<T, I>(target: T, ctx: CreateIteratorContext<I>): T & Iterable<I> {
let itSymbol = getKnownSymbol(WellKnownSymbols.iterator);
function _createIterator() {
return createIterator(ctx);
}
target[itSymbol] = _createIterator;
return target as T & Iterable<I>;
}
/**
* Create an iterator which conforms to the `Iterator` protocol, it uses the provided `ctx` to
* managed moving to the `next`.
*
* @see [Iterator protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol)
* @since 0.4.2
* @group Iterator
* @typeParam T - Identifies the type that will be returned by the iterator
* @param ctx - The context used to manage the iteration over the items.
* @returns A new Iterator instance
* @example
* ```ts
* let idx = -1;
* let theValues = [ 5, 10, 15, 20, 25, 30 ];
*
* function getNextFn() {
* idx++;
* let isDone = idx >= theValues.length;
* if (!isDone) {
* // this is passed as the current iterator
* // so you can directly assign the next "value" that will be returned
* this.v = theValues[idx];
* }
*
* return isDone;
* }
*
* let theIterator = createIterator<number>({ n: getNextFn });
*
* let values: number[] = [];
* iterForOf(theIterator, (value) => {
* values.push(value);
* });
*
* // Values: [5, 10, 15, 20, 25, 30 ]
* ```
*/
/*#__NO_SIDE_EFFECTS__*/
export function createIterator<T>(ctx: CreateIteratorContext<T>): Iterator<T> {
let isDone = false;
function _value(): T {
return ctx.v;
}
function _next(): IteratorResult<T> {
if (!isDone) {
isDone = (ctx.n ? ctx.n(arguments) : true);
}
let result = {
done: isDone
};
if (!isDone) {
objDefine<IteratorResult<T>>(result as any, "value", { g: _value });
}
return result as IteratorResult<T>;
}
function _return(value?: T): IteratorReturnResult<T> {
isDone = true;
return {
done: true,
value: ctx.r && ctx.r(value)
};
}
function _throw(e?: any): IteratorResult<T> {
isDone = true;
return {
done: true,
value: ctx.t && ctx.t(e)
};
}
let theIterator: Iterator<T> = {
next: _next
};
if (ctx.r) {
theIterator.return = _return;
}
if (ctx.t) {
theIterator.throw = _throw;
}
return theIterator;
}