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

[PR proposal] Custom function.length #13

Closed
ehmicky opened this issue Feb 27, 2019 · 5 comments · Fixed by #23
Closed

[PR proposal] Custom function.length #13

ehmicky opened this issue Feb 27, 2019 · 5 comments · Fixed by #23

Comments

@ehmicky
Copy link
Collaborator

ehmicky commented Feb 27, 2019

Some users might want to copy all function properties except length. For example:

const newFunc = func.bind(null, true)
// newFunc.length should be one fewer than func.length
mimicFn(newFunc)

A length option could be added to solve this:

const newFunc = func.bind(null, true)
// funcLength is func.length
// If the return value of length() is negative, newFunc.length will be 0
mimicFn(newFunc, { length: funcLength => funcLength - 1 })

This could be useful for any function wrapper that modifies function arity through binding, currying, etc.

I can submit a PR if you think it is a good idea.

@sindresorhus
Copy link
Owner

Sure, PR welcome :)

Make sure to mention the use-cases for it when you document it.

@ehmicky
Copy link
Collaborator Author

ehmicky commented Mar 15, 2019

Done in #18

@sindresorhus
Copy link
Owner

Hmm, I've been thinking about this. First, it seems like an obscure use-case, as no one should really depend on Function#length, that's an anti-pattern. Second, wouldn't it be simpler to just have a {length: false} (to not copy length) option?

@ehmicky
Copy link
Collaborator Author

ehmicky commented Jun 11, 2019

Not copying length is a much better idea indeed. Actually I don't think there needs to be an option, Function#length should just never be copied, as I don't see a reason why anyone would want this.

It is an obscure use-case indeed, but we could make this lightweight by only doing this: when iterating over properties, skip length. What do you think?

@ehmicky
Copy link
Collaborator Author

ehmicky commented Jun 11, 2019

Done in #23

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