https://github.com/devlop/tap
A JavaScript implementation of Laravel's tap helper.
https://github.com/devlop/tap
typescript
Last synced: 27 days ago
JSON representation
A JavaScript implementation of Laravel's tap helper.
- Host: GitHub
- URL: https://github.com/devlop/tap
- Owner: devlop
- License: mit
- Created: 2021-02-26T08:03:00.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-11-23T05:23:51.000Z (over 4 years ago)
- Last Synced: 2025-10-27T18:47:24.386Z (7 months ago)
- Topics: typescript
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# tap
A JavaScript implementation of Laravel's [tap helper](https://laravel.com/docs/8.x/helpers#method-tap).
# Installing
using npm
```bash
npm install @devlop/tap
```
# Usage
```js
import tap from '@devlop/tap';
```
`tap` accepts two arguments, a value and a callback.
The `callback` will get the `value` as the first argument, any return value of the callback will be ignored and the `original value` will be returned.
## Examples
```js
let someVariable = true;
let anotherValue = tap(someVariable, function () {
console.log(someVariable); // true
});
console.log(anotherValue); // true
```
```js
let someVariable = true;
let anotherValue = tap(someVariable, function () {
console.log(someVariable); // true
// re-assignments have no effect
someVariable = false;
// return values are ignored
return false;
});
console.log(anotherValue); // still true
```
```js
let someObject = {
team: 'Mora IK',
};
tap(someVariable, function () {
// objects are passed by reference in JavaScript and can be modified
someObject.name = 'Chicago Blackhawks';
});
console.log(someObject.name); // Chicago Blackhawks
```