{"version":3,"file":"map.cjs","names":[],"sources":["../../src/promise/map.ts"],"sourcesContent":["/**\n * Iterates over an array and returns a promise that resolves with an array of mapped values.\n * Supports concurrency limiting.\n *\n * @template T - The type of the input array elements\n * @template U - The type of the mapped values\n * @param array - The array to iterate over\n * @param mapper - The function to apply to each element (can be async or return a promise)\n * @param concurrency - The maximum number of concurrent operations (default: Infinity)\n * @returns Returns a promise that resolves with an array of mapped values in the same order\n *\n * @example\n * const ids = [1, 2, 3];\n * const users = await map(ids, (id) => fetchUser(id), 2); // Max 2 concurrent requests\n *\n * @example\n * const numbers = [1, 2, 3];\n * const doubled = await map(numbers, (n) => Promise.resolve(n * 2));\n */\nexport function map<T, U>(\n  array: T[],\n  mapper: (value: T, index: number) => Promise<U> | U,\n  concurrency = Infinity,\n): Promise<U[]> {\n  if (!Array.isArray(array) || array.length === 0) {\n    return Promise.resolve([]);\n  }\n\n  return new Promise((resolve, reject) => {\n    const results: U[] = new Array(array.length);\n    let completed = 0;\n    let inProgress = 0;\n    let index = 0;\n    let hasError = false;\n\n    function execute() {\n      while (index < array.length && inProgress < concurrency && !hasError) {\n        const currentIndex = index++;\n        inProgress++;\n\n        Promise.resolve(mapper(array[currentIndex], currentIndex)).then(\n          (result) => {\n            if (!hasError) {\n              results[currentIndex] = result;\n              completed++;\n              inProgress--;\n\n              if (completed === array.length) {\n                resolve(results);\n              } else {\n                execute();\n              }\n            }\n          },\n          (error) => {\n            if (!hasError) {\n              hasError = true;\n              reject(error);\n            }\n          },\n        );\n      }\n    }\n\n    execute();\n  });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAmBA,SAAgB,IACd,OACA,QACA,cAAc,UACA;CACd,IAAI,CAAC,MAAM,QAAQ,KAAK,KAAK,MAAM,WAAW,GAC5C,OAAO,QAAQ,QAAQ,CAAC,CAAC;CAG3B,OAAO,IAAI,SAAS,SAAS,WAAW;EACtC,MAAM,UAAe,IAAI,MAAM,MAAM,MAAM;EAC3C,IAAI,YAAY;EAChB,IAAI,aAAa;EACjB,IAAI,QAAQ;EACZ,IAAI,WAAW;EAEf,SAAS,UAAU;GACjB,OAAO,QAAQ,MAAM,UAAU,aAAa,eAAe,CAAC,UAAU;IACpE,MAAM,eAAe;IACrB;IAEA,QAAQ,QAAQ,OAAO,MAAM,eAAe,YAAY,CAAC,CAAC,CAAC,MACxD,WAAW;KACV,IAAI,CAAC,UAAU;MACb,QAAQ,gBAAgB;MACxB;MACA;MAEA,IAAI,cAAc,MAAM,QACtB,QAAQ,OAAO;WAEf,QAAQ;KAEZ;IACF,IACC,UAAU;KACT,IAAI,CAAC,UAAU;MACb,WAAW;MACX,OAAO,KAAK;KACd;IACF,CACF;GACF;EACF;EAEA,QAAQ;CACV,CAAC;AACH"}