8000 :sparkles: Add tap function and types of Arity1Fn · TomokiMiyauci/fonction@d4d7080 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit d4d7080

Browse files
committed
✨ Add tap function and types of Arity1Fn
Closes #80
1 parent 7d9fefc commit d4d7080

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ const api: Api = {
301301
// splitEvery: {
302302
// fonction: undefined
303303
// },
304+
tap: ['lodash', 'rambda', 'ramda'],
304305
take: ALL_MODULES,
305306
takeLast: {
306307
ramda: 'takeLast',

mod.ts

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export { T } from './src/T.ts'
7474
export { tail } from './src/tail.ts'
7575
export { take } from './src/take.ts'
7676
export { takeLast } from './src/takeLast.ts'
77+
export { tap } from './src/tap.ts'
7778
export type { Trim } from './src/trim.ts'
7879
export { trim } from './src/trim.ts'
7980
export type { TrimLeft } from './src/trimLeft.ts'
@@ -83,6 +84,7 @@ export { trimRight } from './src/trimRight.ts'
8384
export { tryCatch } from './src/tryCatch.ts'
8485
export type {
8586
AnyFn,
87+
Arity1Fn,
8688
Empty,
8789
Falsy,
8890
FalsyLike,

src/tap.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license.
2+
import { Arity1Fn } from './types/index.ts'
3+
4+
/**
5+
* Runs the given function with the supplied value, then returns the value.
6+
*
7+
* @param fn - The function to call with `val`. The return value of fn will be thrown away.
8+
* @returns The result of `(val) => fn(val)`
9+
*
10+
* @example
11+
* ```ts
12+
* tap(console.log)('hello') // hello
13+
* // log: hello
14+
* ```
15+
*
16+
* @beta
17+
*/
18+
const tap =
19+
<T>(fn: Arity1Fn<T>) =>
20+
<R extends T>(val: R): R => {
21+
fn(val)
22+
return val
23+
}
24+
25+
export { tap }

src/types/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
*/
1010
export type AnyFn<T = any, U = unknown> = (...args: T[]) => U
1111

12+
/**
13+
* Type of arity 1 function.
14+
*
15+
* @beta
16+
*
17+
* @typeParam T - Argument types
18+
*/
19+
export type Arity1Fn<T = any, U = unknown> = (args: T) => U
20+
1221
/**
1322
* Alias for Primitive values types.
1423
*

test/tag.test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license.
2+
import { assertEquals } from '../dev_deps.ts'
3+
import { tap } from '../src/tap.ts'
4+
import { Arity1Fn } from '../src/types/index.ts'
5+
import { assertEqual } from './asserts.ts'
6+
7+
Deno.test('tap', () => {
8+
const table: [Arity1Fn, unknown][] = [
9+
[console.log, 'hello'],
10+
[() => console.log(1), ''],
11+
[(a: string) => console.log(a), ''],
12+
[(a: unknown[]) => console.log(a), ['hello', 'world']]
13+
]
14+
15+
table.forEach(([val, expected]) => {
16+
assertEquals(tap(val)(expected), expected, `tap(${val}) -> ${expected}`)
17+
})
18+
19+
assertEqual<''>(tap(console.log)(''))
20+
assertEqual<''>(tap((_: string) => true)(''))
21+
assertEqual<''>(tap(() => true)(''))
22+
assertEqual<string[]>(tap((val: unknown[]) => val)(['hello']))
23+
assertEqual<string[]>(tap((val: unknown[]) => val)(['hello']))
24+
assertEqual<readonly ['hello']>(
25+
tap((val: readonly unknown[]) => val)(['hello'] as const)
26+
)
27+
})

0 commit comments

Comments
 (0)
0