-
-
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.
✨ Add getting first element of array and string function
- Loading branch information
1 parent
3b67787
commit cc59f96
Showing
3 changed files
with
66 additions
and
0 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,31 @@ | ||
/** | ||
* 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([]) // [] | ||
* ``` | ||
* | ||
* @beta | ||
*/ | ||
const tail: { | ||
(val: string): string | ||
<T extends unknown[]>(val: T): T | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} = (val: any) => val.slice(1, Infinity) | ||
|
||
export { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { tail } from '@/tail' | ||
|
||
describe('tail', () => { | ||
const table: [string | unknown[], string | unknown[]][] = [ | ||
['', ''], | ||
['a', ''], | ||
['ab', 'b'], | ||
['abc', 'bc'], | ||
[[], []], | ||
[[''], []], | ||
[[undefined], []], | ||
[[null], []], | ||
[[0], []], | ||
[['', ''], ['']], | ||
[[0, 0], [0]], | ||
[[0, ''], ['']], | ||
[['hello', 'world'], ['world']], | ||
[ | ||
['hello', 'new', 'world'], | ||
['new', 'world'] | ||
], | ||
[ | ||
[undefined, null, 'hello', 'world'], | ||
[null, 'hello', 'world'] | ||
], | ||
[[['hello', 'world']], []] | ||
] | ||
|
||
it.each(table)('tail(%s) -> %s', (val, expected) => { | ||
expect(tail(val)).toEqual(expected) | ||
}) | ||
}) |