-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e2ce80a
commit f8458f5
Showing
5 changed files
with
90 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2021-present the Fonction authors. All rights reserved. MIT license. | ||
import { slice } from '../deps.ts' | ||
|
||
/** | ||
* Infer the tail types. | ||
* @typeParam T - `string` or any `array` | ||
* | ||
* @example | ||
* ```ts | ||
* // String | ||
* Tail<string> // string | ||
* Tail<''> // '' | ||
* Tail<'hello'> // 'ello' | ||
* ``` | ||
* | ||
* @example | ||
* ```ts | ||
* // Array | ||
* Tail<[] | never[] | readonly [] | readonly never[]> // [] | ||
* Tail<['hello', 'world']> // 'world' | ||
* ``` | ||
* | ||
* @category `Array` `String` | ||
* | ||
* @see Related to {@link Init} | ||
* | ||
* @beta | ||
*/ | ||
type Tail<T extends string | readonly unknown[]> = T extends string | ||
? T extends `${string}${infer R}` | ||
? R | ||
: T extends '' | ||
? '' | ||
: string | ||
: T extends readonly never[] | [] | ||
? [] | ||
: T extends readonly [unknown, ...infer R] | ||
? R | ||
: T | ||
/** | ||
* Returns all but the first element of the given list or string. | ||
* | ||
* @param val - string or any array object | ||
* @returns The result of `val.slice(1, Infinity)` | ||
* | ||
* @example | ||
* ```ts | ||
* // String | ||
* tail('hello') // 'ello' | ||
* tail('h') // '' | ||
* tail('') // '' | ||
* ``` | ||
* | ||
* @example | ||
* ```ts | ||
* tail([1, 2, 3]) // [2, 3] | ||
* tail(['hello', 'world']) // ['world'] | ||
* tail(['hello']) // [] | ||
* tail([]) // [] | ||
* ``` | ||
* | ||
* @category `Array` `String` | ||
* | ||
* @see Related to {@link head} | ||
* | ||
* @public | ||
*/ | ||
const tail = <T extends string | readonly unknown[]>(val: T): Tail<T> => | ||
slice(1, Infinity, val) as Tail<T> | ||
|
||
export { tail } | ||
export type { Tail } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.