Skip to content

Commit f48e8b9

Browse files
authored
feat(function): tap utility (#1)
1 parent 3c75555 commit f48e8b9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/function.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { tap } from './function'
2+
3+
it('tap', () => {
4+
expect(tap(1, value => value++)).toEqual(1)
5+
expect(tap({ a: 1 }, obj => obj.a++)).toEqual({ a: 2 })
6+
})

src/function.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,20 @@ export function batchInvoke(functions: Nullable<Fn>[]) {
1313
export function invoke(fn: Fn) {
1414
return fn()
1515
}
16+
17+
/**
18+
* Pass the value through the callback, and return the value.
19+
*
20+
* @example
21+
* ```
22+
* function createUser(name: string): User {
23+
* return tap(new User, user => {
24+
* user.name = name
25+
* })
26+
* }
27+
* ```
28+
*/
29+
export function tap<T>(value: T, callback: (value: T) => void): T {
30+
callback(value)
31+
return value
32+
}

0 commit comments

Comments
 (0)