Skip to content

Commit 99b28c5

Browse files
committed
fix types & add support for third parameter of map "callback" (the list itself)
1 parent 51a376e commit 99b28c5

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

typescript/packages/common-builder/src/opaque-ref.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ export function opaqueRef<T>(value?: Opaque<T> | T): OpaqueRef<T> {
7878
},
7979
map: <S>(
8080
fn: (
81-
value: Opaque<Required<T extends Array<infer U> ? U : T>>,
81+
element: Opaque<Required<T extends Array<infer U> ? U : T>>,
8282
index: Opaque<number>,
83+
array: T,
8384
) => Opaque<S>,
8485
) => {
8586
// Create the factory if it doesn't exist. Doing it here to avoid
@@ -90,8 +91,10 @@ export function opaqueRef<T>(value?: Opaque<T> | T): OpaqueRef<T> {
9091
});
9192
return mapFactory({
9293
list: proxy,
93-
op: recipe("mapping function", ({ item, index }: Opaque<any>) =>
94-
fn(item, index),
94+
op: recipe(
95+
"mapping function",
96+
({ element, index, array }: Opaque<any>) =>
97+
fn(element, index, array),
9598
),
9699
});
97100
},

typescript/packages/common-builder/src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ export type OpaqueRefMethods<T> = {
4646
unsafe_bindToRecipeAndPath(recipe: Recipe, path: PropertyKey[]): void;
4747
unsafe_getExternal(): OpaqueRef<T>;
4848
map<S>(
49-
fn: (value: T extends Array<infer U> ? Opaque<U> : Opaque<T>) => Opaque<S>,
49+
fn: (
50+
element: T extends Array<infer U> ? Opaque<U> : Opaque<T>,
51+
index: Opaque<number>,
52+
array: T,
53+
) => Opaque<S>,
5054
): Opaque<S[]>;
5155
[Symbol.iterator](): Iterator<T>;
5256
[Symbol.toPrimitive](hint: string): T;

typescript/packages/common-runner/src/builtins/map.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export function map(
120120
if (!value.cell.entityId) value.cell.generateEntityId();
121121
const resultCell = cell();
122122
resultCell.generateEntityId({ map: value.cell.entityId });
123-
run(op, { item: value, index }, resultCell);
123+
run(op, { element: value, index, array: list }, resultCell);
124124
resultCell.sourceCell!.sourceCell = parentCell;
125125
if (Array.isArray(parentCell.get())) debugger;
126126

typescript/packages/common-runner/test/recipes.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ describe("Recipe Runner", () => {
7676
const multipliedArray = recipe<{ values: { x: number }[] }>(
7777
"Multiply numbers",
7878
({ values }) => {
79-
const multiplied = values.map(({ x }, index) => {
80-
const multiply = lift<number>(x => x * (index + 1));
79+
const multiplied = values.map(({ x }, index, array) => {
80+
const multiply = lift<number>(x => x * (index + 1) * array.length);
8181
return { multiplied: multiply(x) };
8282
});
8383
return { multiplied };
@@ -91,7 +91,7 @@ describe("Recipe Runner", () => {
9191
await idle();
9292

9393
expect(result.getAsQueryResult()).toMatchObject({
94-
multiplied: [{ multiplied: 1 }, { multiplied: 4 }, { multiplied: 9 }],
94+
multiplied: [{ multiplied: 3 }, { multiplied: 12 }, { multiplied: 27 }],
9595
});
9696
});
9797

0 commit comments

Comments
 (0)