Skip to content

Commit e30a2a2

Browse files
committed
.map(value, counter), as per tc39/proposal-iterator-helpers#211
1 parent 3f97f82 commit e30a2a2

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/operations/async/map.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { Arguments, AsyncOperation, Name } from "../operation";
22

33
export interface MapFn<T, O> {
4-
(value: T): O | Promise<O>;
4+
(value: T, counter: number): O | Promise<O>;
55
}
66

77
export function map<T, O>(callbackFn: MapFn<T, O>) {
88
const fn: AsyncOperation<T, AsyncIterable<O>> = async function *map(iterable) {
9+
let counter: number = -1;
910
for await (const value of async(iterable)) {
10-
yield callbackFn(value);
11+
counter += 1;
12+
yield callbackFn(value, counter);
1113
}
1214
async function *async(value: AsyncIterable<T>) {
1315
yield * value;

src/operations/sync/map.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ import { Arguments, AsyncFn, GetAsync, Name, SyncOperation } from "../operation"
22
import { isAsyncIterable, isIterable } from "../../async-like";
33
import * as Async from "../async";
44

5-
export type MapFn<T, O> = (value: T) => O;
5+
export type MapFn<T, O> = (value: T, counter: number) => O;
66

77
export function map<T, O>(callbackFn: MapFn<T, O>) {
88
const fn: SyncOperation<T, Iterable<O>> = function *map(iterable) {
99
if (isAsyncIterable(iterable) && !isIterable(iterable)) throw new Async.ExpectedAsyncOperationError(
1010
fn[GetAsync]()
1111
);
12+
let counter = -1;
1213
for (const value of iterable) {
13-
yield callbackFn(value);
14+
counter += 1;
15+
yield callbackFn(value, counter);
1416
}
1517
};
1618
fn[Name] = "map";

0 commit comments

Comments
 (0)