remeda
Version:
A utility library for JavaScript and Typescript.
1 lines • 2.05 kB
Source Map (JSON)
{"version":3,"file":"tap.cjs","names":["purry"],"sources":["../src/tap.ts"],"sourcesContent":["import { purry } from \"./purry\";\n\n/**\n * Calls the given function with the given value, then returns the given value.\n * The return value of the provided function is ignored.\n *\n * This allows \"tapping into\" a function sequence in a pipe, to perform side\n * effects on intermediate results.\n *\n * @param value - The value to pass into the function.\n * @param fn - The function to call.\n * @signature\n * R.tap(value, fn)\n * @example\n * R.tap(\"foo\", console.log) // => \"foo\"\n * @dataFirst\n * @category Other\n */\nexport function tap<T>(value: T, fn: (value: T) => void): T;\n\n/**\n * Calls the given function with the given value, then returns the given value.\n * The return value of the provided function is ignored.\n *\n * This allows \"tapping into\" a function sequence in a pipe, to perform side\n * effects on intermediate results.\n *\n * @param fn - The function to call.\n * @signature\n * R.tap(fn)(value)\n * @example\n * R.pipe(\n * [-5, -1, 2, 3],\n * R.filter(n => n > 0),\n * R.tap(console.log), // prints [2, 3]\n * R.map(n => n * 2)\n * ) // => [4, 6]\n * @dataLast\n * @category Other\n */\nexport function tap<\n T,\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters -- TODO: This is solvable by inlining F and wrapping the T parameter with `NoInfer` (e.g. `(value: NoInfer<T>) => unknown`); to prevent typescript from inferring it as `unknown`. This is only available in TS 5.4, which is above what we currently support (5.1).\n F extends (value: T) => unknown,\n>(fn: F): (value: T) => T;\n\nexport function tap(...args: ReadonlyArray<unknown>): unknown {\n return purry(tapImplementation, args);\n}\n\nfunction tapImplementation<T>(value: T, fn: (value: T) => void): T {\n fn(value);\n return value;\n}\n"],"mappings":"wCA8CA,SAAgB,EAAI,GAAG,EAAuC,CAC5D,OAAOA,EAAAA,EAAM,EAAmB,EAAK,CAGvC,SAAS,EAAqB,EAAU,EAA2B,CAEjE,OADA,EAAG,EAAM,CACF"}