feat: add tap function - #350
Conversation
Benchmark Results
Performance regressions of 30% or more should be investigated, unless they were anticipated. Smaller regressions may be due to normal variability, as we don't use dedicated CI infrastructure. |
|
Hey @nusohiro, thanks for the PR! I appreciate you helping out. 👍 Of all the utility libraries I track, only 1 has this |
|
@aleclarson You’re right that tap is often used for logging, but another use case is avoiding explicit For example, without tap: let obj = { a: 1 };
if (someCondition) {
obj.a += 1;
} else {
obj.a -= 1;
}With tap: const obj = _.tap({ a: 1 }, (o) => {
if (somecondition) {
o.a += 1;
} else {
o.a -= 1;
}
}); |
|
Hey @nusohiro 👋 I've thought about it some more, and I don't think I really do appreciate your suggestions. I look forward to any future contributions you make! 🤝 |
Summary
This PR adds the
tapfunction.The function executes a given function with a provided value, allowing for side effects or mutations, and then returns the original value.
Related issue, if any:
N/A
For any code change,
Does this PR introduce a breaking change?
No
Bundle impact
src/function/tap.tsFootnotes
Function size includes the
importdependencies of the function. ↩