{"version":3,"file":"reduce.cjs","names":[],"sources":["../../src/promise/reduce.ts"],"sourcesContent":["/**\n * Iterates over an array and reduces it to a single value using an async reducer function.\n *\n * @template T - The type of the array elements\n * @template U - The type of the accumulator and final result\n * @param array - The array to reduce\n * @param reducer - The function to reduce each element (can be async or return a promise)\n * @param initialValue - The initial value for the accumulator\n * @returns Returns a promise that resolves with the final accumulated value\n *\n * @example\n * const sum = await reduce(\n *   [1, 2, 3, 4],\n *   (acc, value) => Promise.resolve(acc + value),\n *   0\n * ); // => 10\n *\n * @example\n * const users = await reduce(\n *   [1, 2, 3],\n *   async (acc, id) => {\n *     const user = await fetchUser(id);\n *     return [...acc, user];\n *   },\n *   []\n * );\n */\nexport function reduce<T, U>(\n  array: T[],\n  reducer: (accumulator: U, value: T, index: number) => Promise<U> | U,\n  initialValue: U,\n): Promise<U> {\n  if (!Array.isArray(array) || array.length === 0) {\n    return Promise.resolve(initialValue);\n  }\n\n  return array.reduce(\n    (promise, value, index) => promise.then((accumulator) => Promise.resolve(reducer(accumulator, value, index))),\n    Promise.resolve(initialValue),\n  );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BA,SAAgB,OACd,OACA,SACA,cACY;CACZ,IAAI,CAAC,MAAM,QAAQ,KAAK,KAAK,MAAM,WAAW,GAC5C,OAAO,QAAQ,QAAQ,YAAY;CAGrC,OAAO,MAAM,QACV,SAAS,OAAO,UAAU,QAAQ,MAAM,gBAAgB,QAAQ,QAAQ,QAAQ,aAAa,OAAO,KAAK,CAAC,CAAC,GAC5G,QAAQ,QAAQ,YAAY,CAC9B;AACF"}