Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add flip function #35

Merged
merged 5 commits into from
Jul 6, 2024
Merged

feat: add flip function #35

merged 5 commits into from
Jul 6, 2024

Conversation

aleclarson
Copy link
Member

Tip

The owner of this PR can publish a preview release by commenting /publish in this PR. Afterwards, anyone can try it out by running pnpm add radashi@pr<PR_NUMBER>.

Description

The flip function takes a function and swaps its two arguments. It's most helpful for reversing the order of a comparator (i.e. going from ascending order to descending, or vice versa).

Checklist

  • Changes are covered by tests if behavior has been changed or added
  • Tests have 100% coverage
  • If code changes were made, the documentation (in the /docs directory) has been updated

Resolves

@aleclarson aleclarson added the new feature This PR adds a new function or extends an existing one label Jun 28, 2024
@localusercamp
Copy link
Contributor

Is there any way to make it work with any number of parameters?

@aleclarson
Copy link
Member Author

aleclarson commented Jun 28, 2024

Is there any way to make it work with any number of parameters?

If we can keep it performant for the two-argument case, I'd say sure.

Maybe something like this?

export function flip(fn, length = fn.length) {
  return length === 2
    ? (a: any, b: any) => fn(b, a)
    : length !== fn.length
      ? (...args: any[]) =>
          fn(...args.slice(0, length).reverse(), ...args.slice(length))
      : (...args: any[]) => fn(...args.reverse())
}

Note: The length property of a function is 0 when only rest arguments are used.

@aleclarson
Copy link
Member Author

Perhaps it might be best to keep it simple (only two arguments) until the demand for more complex use cases is certain.

@aleclarson aleclarson force-pushed the main branch 4 times, most recently from 35fc67a to 04bad7b Compare July 1, 2024 19:17
@aleclarson
Copy link
Member Author

Keeping it just the first two arguments, to align with Ramda (see here)

@aleclarson aleclarson marked this pull request as ready for review July 5, 2024 22:57
@aleclarson aleclarson merged commit 66a71b0 into main Jul 6, 2024
4 checks passed
@aleclarson aleclarson deleted the feat/flip-fn branch July 6, 2024 21:42
Copy link

github-actions bot commented Jul 7, 2024

A new beta version 12.2.0-beta.c59abfd has been published to NPM. 🚀

To install:

pnpm add radashi@12.2.0-beta.c59abfd

The radashi@beta tag also includes this PR.

See the changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This PR adds a new function or extends an existing one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants