remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 3.29 kB
Source Map (JSON)
{"version":3,"file":"forEach.cjs","names":["purry"],"sources":["../src/forEach.ts"],"sourcesContent":["import type { Writable } from \"type-fest\";\nimport type { IterableContainer } from \"./internal/types/IterableContainer\";\nimport type { LazyEvaluator } from \"./internal/types/LazyEvaluator\";\nimport { purry } from \"./purry\";\n\n/**\n * Executes a provided function once for each array element. Equivalent to\n * `Array.prototype.forEach`.\n *\n * The dataLast version returns the original array (instead of not returning\n * anything (`void`)) to allow using it in a pipe. When not used in a `pipe` the\n * returned array is equal to the input array (by reference), and not a shallow\n * copy of it!\n *\n * @param data - The values that would be iterated on.\n * @param callbackfn - A function to execute for each element in the array.\n * @signature\n * R.forEach(data, callbackfn)\n * @example\n * R.forEach([1, 2, 3], x => {\n * console.log(x)\n * });\n * @dataFirst\n * @lazy\n * @category Array\n */\nexport function forEach<T extends IterableContainer>(\n data: T,\n callbackfn: (value: T[number], index: number, data: T) => void,\n): void;\n\n/**\n * Executes a provided function once for each array element. Equivalent to\n * `Array.prototype.forEach`.\n *\n * The dataLast version returns the original array (instead of not returning\n * anything (`void`)) to allow using it in a pipe. The returned array is the\n * same reference as the input array, and not a shallow copy of it!\n *\n * @param callbackfn - A function to execute for each element in the array.\n * @returns The original array (the ref itself, not a shallow copy of it).\n * @signature\n * R.forEach(callbackfn)(data)\n * @example\n * R.pipe(\n * [1, 2, 3],\n * R.forEach(x => {\n * console.log(x)\n * })\n * ) // => [1, 2, 3]\n * @dataLast\n * @lazy\n * @category Array\n */\nexport function forEach<T extends IterableContainer>(\n callbackfn: (value: T[number], index: number, data: T) => void,\n): (data: T) => Writable<T>;\n\nexport function forEach(...args: readonly unknown[]): unknown {\n return purry(forEachImplementation, args, lazyImplementation);\n}\n\nfunction forEachImplementation<T>(\n data: readonly T[],\n callbackfn: (value: T, index: number, data: readonly T[]) => void,\n): T[] {\n // eslint-disable-next-line unicorn/no-array-for-each -- We are intentionally proxying the built in forEach, it's up to the user to decide if they want to use a for loop instead.\n data.forEach(callbackfn);\n // @ts-expect-error [ts4104] - Because the dataFirst signature returns void this is only a problem when the dataLast function is used **outside** of a pipe; for these cases we warn the user that this is happening.\n return data;\n}\n\nconst lazyImplementation =\n <T>(\n callbackfn: (value: T, index: number, data: readonly T[]) => void,\n ): LazyEvaluator<T> =>\n (value, index, data) => {\n callbackfn(value, index, data);\n return { done: false, hasNext: true, next: value };\n };\n"],"mappings":"wCA0DA,SAAgB,EAAQ,GAAG,EAAmC,CAC5D,OAAOA,EAAAA,EAAM,EAAuB,EAAM,EAAmB,CAG/D,SAAS,EACP,EACA,EACK,CAIL,OAFA,EAAK,QAAQ,EAAW,CAEjB,EAGT,MAAM,EAEF,IAED,EAAO,EAAO,KACb,EAAW,EAAO,EAAO,EAAK,CACvB,CAAE,KAAM,GAAO,QAAS,GAAM,KAAM,EAAO"}