Closed Terris closed 8 months ago
I love that it also mimics the behavior of built-in map. I'll try it out & hopefully fold this into the next release. Thank you!
On Thu, Jan 18, 2024 at 12:55 PM Terris Kremer @.***> wrote:
I found myself wanting asyncMap to provide an index in asyncTransform props.
In my case I was updating an order column on a list of records and found it convenient to have an index while mapping over them and building a return. For instance:
// Update the order of all courseLessons to keepawait asyncMap(courseLessons, (cm, index) => { return ctx.db.patch(cm._id, { order: index + 1 });});
So, this PR adds an index to the asyncTransform params on asyncMap! Might be a rare use case, but I thought I'd put it out there anyhow.
/* asyncMap returns the results of applying an async function over an list. @param list - Iterable object of items, e.g. an Array, Set, Object.keys @param asyncTransform @returns */export async function asyncMap<FromType, ToType>( list: Iterable
, asyncTransform: (item: FromType, index: number) => Promise // add index and type to param): Promise<ToType[]> { const promises: Promise [] = []; let index = 0; // init an index var for (const item of list) { promises.push(asyncTransform(item, index)); // return the index with the item } return Promise.all(promises);} Thanks for considering :)
You can view, comment on, or merge this pull request online at:
https://github.com/get-convex/convex-helpers/pull/56 Commit Summary
- 5070c71 https://github.com/get-convex/convex-helpers/pull/56/commits/5070c71afc7bca4f5fac1fdde132eb5cf51b79b6 Add index to async map asyncTransform props
File Changes
(1 file https://github.com/get-convex/convex-helpers/pull/56/files)
- M packages/convex-helpers/index.ts https://github.com/get-convex/convex-helpers/pull/56/files#diff-aebb942ebd3f61226c36440ad532d8456887173ec683092a76477e0794654cad (5)
Patch Links:
- https://github.com/get-convex/convex-helpers/pull/56.patch
- https://github.com/get-convex/convex-helpers/pull/56.diff
— Reply to this email directly, view it on GitHub https://github.com/get-convex/convex-helpers/pull/56, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACZQW5KQGJ7C2JOGBC2R23YPGD27AVCNFSM6AAAAABCA5JXNGVHI2DSMVQWIX3LMV43ASLTON2WKOZSGA4DSMBTGQZTMNY . You are receiving this because you are subscribed to this thread.Message ID: @.***>
I added incrementing the index :P
Many thanks, Ian!
Released in 0.1.13
On Sun, Jan 21, 2024 at 3:11 PM Terris Kremer @.***> wrote:
Many thanks, Ian!
— Reply to this email directly, view it on GitHub https://github.com/get-convex/convex-helpers/pull/56#issuecomment-1902797996, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACZQW2C4J7ZUCZGVWMGU3TYPWOCZAVCNFSM6AAAAABCA5JXNGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMBSG44TOOJZGY . You are receiving this because you modified the open/close state.Message ID: @.***>
I found myself wanting
asyncMap
to provide an index inasyncTransform
props.In my case I was updating an order column on a list of records and found it convenient to have an index while mapping over them and building a return. For instance:
So, this PR adds an index to the asyncTransform params on asyncMap! Might be a rare use case, but I thought I'd put it out there anyhow.
Thanks for considering :)