get-convex / convex-helpers

A collection of useful code to complement the official packages.
MIT License
94 stars 16 forks source link

Add index to async map asyncTransform params #56

Closed Terris closed 8 months ago

Terris commented 8 months ago

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 keep
await 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<FromType>,
  asyncTransform: (item: FromType, index: number) => Promise<ToType> // add index and type to param
): Promise<ToType[]> {
  const promises: Promise<ToType>[] = [];
  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 :)

ianmacartney commented 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

File Changes

(1 file https://github.com/get-convex/convex-helpers/pull/56/files)

Patch Links:

— 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: @.***>

ianmacartney commented 8 months ago

I added incrementing the index :P

Terris commented 8 months ago

Many thanks, Ian!

ianmacartney commented 8 months ago

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: @.***>