Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Commit a86a288

Browse files
authored
Add Function.tap
Decided not to include a `debug` helper function `input => { debugger input; return input; }` due to lack of library precedent and overlap with `tap`. Closes #8.
1 parent 921d0e8 commit a86a288

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,48 @@ Precedents include:
299299

300300
[lodash.identity]: https://www.npmjs.com/package/lodash.identity
301301

302+
## Function.tap
303+
The `Function.tap` static method creates a new unary function
304+
that applies some callback to its argument before returning the original argument.
305+
306+
```js
307+
Function.tap(callback)(input);
308+
309+
const { tap } = Function;
310+
311+
tap(console.log)(5); // Prints 5 before returning 5.
312+
313+
arr.map(tap(console.log)).map(f); // Prints each item from `arr` before passing them to `f`.
314+
315+
const data = await Promise.resolve('intro.txt')
316+
.then(Deno.open)
317+
.then(Deno.readAll)
318+
.then(tap(console.log))
319+
.then(data => new TextDecoder('utf-8').decode(data));
320+
321+
// From [email protected]/src/constructor.js
322+
var fakeConstructorFromNames = (funcNames) => {
323+
return _.tap(tdFunction('(unnamed constructor)'), (fakeConstructor) => {
324+
_.each(funcNames, (funcName) => {
325+
fakeConstructor.prototype[funcName] = tdFunction(`#${String(funcName)}`)
326+
})
327+
})
328+
}
329+
330+
// From <https://github.com/rendrjs/rendr/blob/1.1.4/test/shared/fetcher.test.js>
331+
function getModelResponse(version, id, addJsonKey) {
332+
if (addJsonKey) {
333+
return _.tap({}, function(obj) {
334+
obj.listing = resp;
335+
});
336+
}
337+
}
338+
```
339+
340+
Precedents include:
341+
* [lodash][]: `_.tap`
342+
* [Ramda][]: `import { tap } from 'ramda/src/tap';`
343+
302344
[lodash]: https://lodash.com/docs/4.17.15
303345
[stdlib]: https://github.com/stdlib-js/stdlib
304346
[RxJS]: https://rxjs.dev

0 commit comments

Comments
 (0)