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

Support for before #153

Closed
haejunejung opened this issue Jul 10, 2024 · 2 comments · Fixed by #315
Closed

Support for before #153

haejunejung opened this issue Jul 10, 2024 · 2 comments · Fixed by #315

Comments

@haejunejung
Copy link
Contributor

Hello, @raon0211!
Can I work before of #91 ?

If it's okay to work on it, before and after seem very similar. Is it okay if I dug up a new issue for after and work on it together?

@raon0211
Copy link
Collaborator

Sure!

@haejunejung
Copy link
Contributor Author

I've implemented the before function as below.

export const before = <P, F extends (...args: any[]) => P>(n: number, func: F): F => {
  if (!Number.isInteger(n) || n <= 1) {
    throw new Error(`n must be an integer greater than one.`);
  }

  let counter = 1;
  return ((...args: Parameters<F>) => {
    if (counter >= n) {
      throw new Error(`Function has been called ${n - 1} times already, which is the limit.`);
    }

    counter++;
    return func(...args);
  }) as F;
};

If it looks good, I proceed with enhancing documentation and then I submit a pull request!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants