UNPKG

remeda

Version:

A utility library for JavaScript and Typescript.

1 lines 3.15 kB
{"version":3,"file":"mapWithFeedback.cjs","names":["purryFromLazy"],"sources":["../src/mapWithFeedback.ts"],"sourcesContent":["import { purryFromLazy } from \"./internal/purryFromLazy\";\nimport type { IterableContainer } from \"./internal/types/IterableContainer\";\nimport type { LazyEvaluator } from \"./internal/types/LazyEvaluator\";\nimport type { Mapped } from \"./internal/types/Mapped\";\n\n/**\n * Applies a function on each element of the array, using the result of the\n * previous application, and returns an array of the successively computed\n * values.\n *\n * @param data - The array to map over.\n * @param callbackfn - The callback function that receives the previous value,\n * the current element.\n * @param initialValue - The initial value to start the computation with.\n * @returns An array of successively computed values from the left side of the\n * array.\n * @signature\n * R.mapWithFeedback(data, callbackfn, initialValue);\n * @example\n * R.mapWithFeedback(\n * [1, 2, 3, 4, 5],\n * (prev, x) => prev + x,\n * 100,\n * ); // => [101, 103, 106, 110, 115]\n * @dataFirst\n * @lazy\n * @category Array\n */\nexport function mapWithFeedback<T extends IterableContainer, U>(\n data: T,\n callbackfn: (\n previousValue: U,\n currentValue: T[number],\n currentIndex: number,\n data: T,\n ) => U,\n initialValue: U,\n): Mapped<T, U>;\n\n/**\n * Applies a function on each element of the array, using the result of the\n * previous application, and returns an array of the successively computed\n * values.\n *\n * @param callbackfn - The callback function that receives the previous value,\n * the current element.\n * @param initialValue - The initial value to start the computation with.\n * @returns An array of successively computed values from the left side of the\n * array.\n * @signature\n * R.mapWithFeedback(callbackfn, initialValue)(data);\n * @example\n * R.pipe(\n * [1, 2, 3, 4, 5],\n * R.mapWithFeedback((prev, x) => prev + x, 100),\n * ); // => [101, 103, 106, 110, 115]\n * @dataLast\n * @lazy\n * @category Array\n */\nexport function mapWithFeedback<T extends IterableContainer, U>(\n callbackfn: (\n previousValue: U,\n currentValue: T[number],\n currentIndex: number,\n data: T,\n ) => U,\n initialValue: U,\n): (data: T) => Mapped<T, U>;\n\nexport function mapWithFeedback(...args: ReadonlyArray<unknown>): unknown {\n return purryFromLazy(lazyImplementation, args);\n}\n\nconst lazyImplementation = <T, U>(\n reducer: (\n previousValue: U,\n currentValue: T,\n index: number,\n data: ReadonlyArray<T>,\n ) => U,\n initialValue: U,\n): LazyEvaluator<T, U> => {\n let previousValue = initialValue;\n return (currentValue, index, data) => {\n previousValue = reducer(previousValue, currentValue, index, data);\n return { done: false, hasNext: true, next: previousValue };\n };\n};\n"],"mappings":"gDAsEA,SAAgB,EAAgB,GAAG,EAAuC,CACxE,OAAOA,EAAAA,EAAc,EAAoB,EAAK,CAGhD,MAAM,GACJ,EAMA,IACwB,CACxB,IAAI,EAAgB,EACpB,OAAQ,EAAc,EAAO,KAC3B,EAAgB,EAAQ,EAAe,EAAc,EAAO,EAAK,CAC1D,CAAE,KAAM,GAAO,QAAS,GAAM,KAAM,EAAe"}