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

Commit f8458f5

Browse files
committed
✨ Add types of Tail
1 parent e2ce80a commit f8458f5

File tree

5 files changed

+90
-39
lines changed

5 files changed

+90
-39
lines changed

common/mod.ts

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export type { Init } from './init.ts'
55
export { init } from './init.ts'
66
export type { Last } from './last.ts'
77
export { last } from './last.ts'
8+
export type { Tail } from './tail.ts'
9+
export { tail } from './tail.ts'

common/tail.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license.
2+
import { slice } from '../deps.ts'
3+
4+
/**
5+
* Infer the tail types.
6+
* @typeParam T - `string` or any `array`
7+
*
8+
* @example
9+
* ```ts
10+
* // String
11+
* Tail<string> // string
12+
* Tail<''> // ''
13+
* Tail<'hello'> // 'ello'
14+
* ```
15+
*
16+
* @example
17+
* ```ts
18+
* // Array
19+
* Tail<[] | never[] | readonly [] | readonly never[]> // []
20+
* Tail<['hello', 'world']> // 'world'
21+
* ```
22+
*
23+
* @category `Array` `String`
24+
*
25+
* @see Related to {@link Init}
26+
*
27+
* @beta
28+
*/
29+
type Tail<T extends string | readonly unknown[]> = T extends string
30+
? T extends `${string}${infer R}`
31+
? R
32+
: T extends ''
33+
? ''
34+
: string
35+
: T extends readonly never[] | []
36+
? []
37+
: T extends readonly [unknown, ...infer R]
38+
? R
39+
: T
40+
/**
41+
* Returns all but the first element of the given list or string.
42+
*
43+
* @param val - string or any array object
44+
* @returns The result of `val.slice(1, Infinity)`
45+
*
46+
* @example
47+
* ```ts
48+
* // String
49+
* tail('hello') // 'ello'
50+
* tail('h') // ''
51+
* tail('') // ''
52+
* ```
53+
*
54+
* @example
55+
* ```ts
56+
* tail([1, 2, 3]) // [2, 3]
57+
* tail(['hello', 'world']) // ['world']
58+
* tail(['hello']) // []
59+
* tail([]) // []
60+
* ```
61+
*
62+
* @category `Array` `String`
63+
*
64+
* @see Related to {@link head}
65+
*
66+
* @public
67+
*/
68+
const tail = <T extends string | readonly unknown[]>(val: T): Tail<T> =>
69+
slice(1, Infinity, val) as Tail<T>
70+
71+
export { tail }
72+
export type { Tail }

test/tail.test.ts renamed to common/tail_test.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license.
2-
import { assertEquals } from '../dev_deps.ts'
3-
import { tail } from '../src/tail.ts'
2+
import { assertEquals, assertEqualsType } from '../dev_deps.ts'
3+
import { Tail, tail } from './tail.ts'
44

55
Deno.test('tail', () => {
66
const tableString: [string, string][] = [
@@ -37,4 +37,18 @@ Deno.test('tail', () => {
3737
tableArray.forEach(([val, expected]) => {
3838
assertEquals(tail(val), expected, `tail(${val}) -> ${expected}`)
3939
})
40+
41+
assertEqualsType<string, Tail<string>>()
42+
assertEqualsType<'', Tail<''>>()
43+
assertEqualsType<'', Tail<'a'>>()
44+
assertEqualsType<'b', Tail<'ab'>>()
45+
assertEqualsType<'bcdef', Tail<'abcdef'>>()
46+
assertEqualsType<[], Tail<[]>>()
47+
assertEqualsType<[], Tail<[1]>>()
48+
assertEqualsType<[], Tail<readonly [1]>>()
49+
assertEqualsType<[2], Tail<[1, 2]>>()
50+
assertEqualsType<[2, 3, 4, 5], Tail<[1, 2, 3, 4, 5]>>()
51+
assertEqualsType<[2, 3, 4, 5], Tail<readonly [1, 2, 3, 4, 5]>>()
52+
assertEqualsType<string[], Tail<string[]>>()
53+
assertEqualsType<readonly string[], Tail<readonly string[]>>()
4054
})

mod.ts

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export { props } from './src/props.ts'
3737
export { subtract } from './src/subtract.ts'
3838
export { sum } from './src/sum.ts'
3939
export { T } from './src/T.ts'
40-
export { tail } from './src/tail.ts'
4140
export { take } from './src/take.ts'
4241
export { takeLast } from './src/takeLast.ts'
4342
export { tap } from './src/tap.ts'

src/tail.ts

-36
This file was deleted.

0 commit comments

Comments
 (0)
0