Skip to content

function: spreadArgs (maybe not the best name) #347

@2chanhaeng

Description

@2chanhaeng

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,
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions