Skip to content

Commit bd51b00

Browse files
committed
add types for iterator helpers proposal
1 parent cbae6cf commit bd51b00

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

src/compiler/commandLineParser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ const libEntries: [string, string][] = [
236236
["esnext.object", "lib.esnext.object.d.ts"],
237237
["esnext.array", "lib.esnext.array.d.ts"],
238238
["esnext.regexp", "lib.esnext.regexp.d.ts"],
239+
["esnext.iterator", "lib.esnext.iterator.d.ts"],
239240
["decorators", "lib.decorators.d.ts"],
240241
["decorators.legacy", "lib.decorators.legacy.d.ts"],
241242
];

src/lib/esnext.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
/// <reference lib="esnext.collection" />
88
/// <reference lib="esnext.array" />
99
/// <reference lib="esnext.regexp" />
10+
/// <reference lib="esnext.iterator" />

src/lib/esnext.iterator.d.ts

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
interface NativeIterator<T, TReturn = void, TNext = undefined> extends Iterator<T, TReturn, TNext> {
2+
/**
3+
* Returns this iterator.
4+
*/
5+
[Symbol.iterator](): NativeIterator<T, TReturn, TNext>;
6+
7+
/**
8+
* Creates an iterator whose values are the result of applying the callback to the values from this iterator.
9+
* @param callbackfn A function that accepts up to two arguments to be used to transform values from the underlying iterator.
10+
*/
11+
map<U>(callbackfn: (value: T, index: number) => U): NativeIterator<U>;
12+
13+
/**
14+
* Creates an iterator whose values are those from this iterator for which the provided predicate returns true.
15+
* @param predicate A function that accepts up to two arguments to be used to test values from the underlying iterator.
16+
*/
17+
filter<S extends T>(predicate: (value: T, index: number) => value is S): NativeIterator<S>;
18+
19+
/**
20+
* Creates an iterator whose values are those from this iterator for which the provided predicate returns true.
21+
* @param predicate A function that accepts up to two arguments to be used to test values from the underlying iterator.
22+
*/
23+
filter(predicate: (value: T, index: number) => unknown): NativeIterator<T>;
24+
25+
/**
26+
* Creates an iterator whose values are the values from this iterator, stopping once the provided limit is reached.
27+
* @param limit The maximum number of values to yield.
28+
*/
29+
take(limit: number): NativeIterator<T>;
30+
31+
/**
32+
* Creates an iterator whose values are the values from this iterator after skipping the provided count.
33+
* @param count The number of values to drop.
34+
*/
35+
drop(count: number): NativeIterator<T>;
36+
37+
/**
38+
* Creates an iterator whose values are the result of applying the callback to the values from this iterator and then flattening the resulting iterators or iterables.
39+
* @param callback A function that accepts up to two arguments to be used to transform values from the underlying iterator into new iterators or iterables to be flattened into the result.
40+
*/
41+
flatMap<U>(callback: (value: T, index: number) => (Iterator<U> | Iterable<U>)): NativeIterator<U>;
42+
43+
/**
44+
* Calls the specified callback function for all the elements in this iterator. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
45+
* @param callbackfn A function that accepts up to three arguments. The reduce method calls the callbackfn function one time for each element in the iterator.
46+
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of a value from the iterator.
47+
*/
48+
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number) => T): T;
49+
reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number) => T, initialValue: T): T;
50+
51+
/**
52+
* Calls the specified callback function for all the elements in this iterator. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
53+
* @param callbackfn A function that accepts up to three arguments. The reduce method calls the callbackfn function one time for each element in the iterator.
54+
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of a value from the iterator.
55+
*/
56+
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number) => U, initialValue: U): U;
57+
58+
/**
59+
* Creates a new array from the values yielded by this iterator.
60+
*/
61+
toArray(): Array<T>;
62+
63+
/**
64+
* Performs the specified action for each element in the iterator.
65+
* @param callbackfn A function that accepts up to two arguments. forEach calls the callbackfn function one time for each element in the iterator.
66+
*/
67+
forEach(callbackfn: (value: T, index: number) => void): void;
68+
69+
/**
70+
* Determines whether the specified callback function returns true for any element of this iterator.
71+
* @param predicate A function that accepts up to two arguments. The some method calls
72+
* the predicate function for each element in this iterator until the predicate returns a value
73+
* true, or until the end of the iterator.
74+
*/
75+
some(predicate: (value: T, index: number) => unknown): boolean;
76+
77+
/**
78+
* Determines whether all the members of this iterator satisfy the specified test.
79+
* @param predicate A function that accepts up to two arguments. The every method calls
80+
* the predicate function for each element in this iterator until the predicate returns
81+
* false, or until the end of this iterator.
82+
*/
83+
every(predicate: (value: T, index: number) => unknown): boolean;
84+
85+
/**
86+
* Returns the value of the first element in this iterator where predicate is true, and undefined
87+
* otherwise.
88+
* @param predicate find calls predicate once for each element of this iterator, in
89+
* order, until it finds one where predicate returns true. If such an element is found, find
90+
* immediately returns that element value. Otherwise, find returns undefined.
91+
*/
92+
find<S extends T>(predicate: (value: T, index: number) => value is S): S | undefined;
93+
find(predicate: (value: T, index: number) => unknown): T | undefined;
94+
95+
readonly [Symbol.toStringTag]: "Iterator";
96+
}
97+
98+
interface IteratorConstructor {
99+
/**
100+
* Creates a native iterator from an iterator or iterable object.
101+
* Returns its input if the input already inherits from the built-in Iterator class.
102+
* @param value An iterator or iterable object to convert a native iterator.
103+
*/
104+
from<T>(value: Iterator<T> | Iterable<T>): NativeIterator<T>;
105+
}
106+
107+
declare var Iterator: (abstract new <T>() => NativeIterator<T>) & IteratorConstructor;
108+
109+
110+
// TODO BEFORE MERGING: update all existing IterableIterator-return methods to return NativeIterator
111+
// I have only done a couple so far, for illustration
112+
113+
interface Generator<T = unknown, TReturn = any, TNext = unknown> extends NativeIterator<T, TReturn, TNext> {
114+
}
115+
116+
interface Array<T> {
117+
/** Iterator */
118+
[Symbol.iterator](): NativeIterator<T>;
119+
120+
/**
121+
* Returns an iterable iterator of key, value pairs for every entry in the array
122+
*/
123+
entries(): NativeIterator<[number, T]>;
124+
125+
/**
126+
* Returns an iterable iterator of keys in the array
127+
*/
128+
keys(): NativeIterator<number>;
129+
130+
/**
131+
* Returns an iterable iterator of values in the array
132+
*/
133+
values(): NativeIterator<T>;
134+
}

src/lib/libs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"esnext.collection",
8080
"esnext.array",
8181
"esnext.regexp",
82+
"esnext.iterator",
8283
"decorators",
8384
"decorators.legacy",
8485
// Default libraries
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @target: esnext
2+
3+
const iterator = Iterator.from([0, 1, 2]);
4+
5+
const mapped = iterator.map(String);
6+
7+
const filtered = iterator.filter(x => x > 0);
8+
9+
function isZero(x: number): x is 0 {
10+
return x === 0;
11+
}
12+
const zero = iterator.filter(isZero);
13+
14+
const iteratorFromBare = Iterator.from({
15+
next() {
16+
return {
17+
done: Math.random() < .5,
18+
value: "a string",
19+
};
20+
},
21+
});
22+
23+
24+
function* gen() {
25+
yield 0;
26+
}
27+
28+
const mappedGen = gen().map(x => x === 0 ? "zero" : "other");
29+
30+
const mappedValues = [0, 1, 2].values().map(x => x === 0 ? "zero" : "other");

0 commit comments

Comments
 (0)