FxTS icon indicating copy to clipboard operation
FxTS copied to clipboard

function: `spreadArgs` (maybe not the best name)

Open 2chanhaeng opened this issue 3 months ago • 2 comments

Suggestion

Yeah, I don't think the name is good too 🤣. If you want to add this function to the library, please suggest a better name.

⭐ Suggestion

const fn = (a: A, b: B, ...) => ...
spreadArgs(fn)([a as A, b as B, ...])

Implement

const spreadArgs = <F extends Arrow>(fn: F):
(args: Parameters<typeof fn> | readonly [...Parameters<typeof fn>]) =>
ReturnType<typeof fn> =>
(args) => fn(...args);

💻 Use Cases

It's useful with map:

import { writeFile } from "node:fs/promises";

const contentByPath = {
  "a.txt": "Hello, world",
  "b.json", "{ \"hello\": \"world\" }"
}

await pipe(
  contentByPath,
  entries,
  map(spreadArgs(writeFile)),
  toAsync,
)

2chanhaeng avatar Sep 13 '25 05:09 2chanhaeng