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

feat(function): tap utility #1

Merged
merged 4 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/function.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { tap } from './function'

it('tap', () => {
expect(tap(1, value => value++)).toEqual(1)
expect(tap({ a: 1 }, obj => obj.a++)).toEqual({ a: 2 })
})
17 changes: 17 additions & 0 deletions src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@ export function batchInvoke(functions: Nullable<Fn>[]) {
export function invoke(fn: Fn) {
return fn()
}

/**
* Pass the value through the callback, and return the value.
*
* @example
* ```
* function createUser(name: string): User {
* return tap(new User, user => {
* user.name = name
* })
* }
* ```
*/
export function tap<T>(value: T, callback: (value: T) => void): T {
callback(value)
return value
}