We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 3c75555 commit f48e8b9Copy full SHA for f48e8b9
src/function.test.ts
@@ -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
@@ -13,3 +13,20 @@ export function batchInvoke(functions: Nullable<Fn>[]) {
13
export function invoke(fn: Fn) {
14
return fn()
15
}
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