2

I have a function that returns another function. I need to pass arguments to both functions. When I try to do that, I can only pass one argument and not two. How do I pass two arguments to validatorRegex?

export const validatorRegex = (regex: RegExp | RegExp[]) => (value: string) =>
  Array.isArray(regex) ? regex.every((rx) => rx.test(value)) : regex.test(value);
2

1 Answer 1

4

validatorRegex is what is called a curried function. Instead of taking two arguments, it takes one argument and returns a function that takes the other argument. You can call it like this:

validatorRegex(/foo/)('bar')

This is short for:

const f = validatorRegex(/foo/)
f('bar')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.