-
-
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
3379416
commit 1df1aa4
Showing
5 changed files
with
114 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<!-- Do not edit this file. It is automatically generated by API Documenter. --> | ||
|
||
|
||
|
||
## head variable | ||
|
||
> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment. | ||
> | ||
Returns all but the last element of the given list or string. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
head: { | ||
(val: string): string; | ||
<T extends unknown[]>(val: T): T; | ||
} | ||
``` | ||
## Example 1 | ||
```ts | ||
// String | ||
head('hello') // 'hell' | ||
head('h') // '' | ||
head('') // '' | ||
|
||
``` | ||
## Example 2 | ||
```ts | ||
head([1, 2, 3]) // [1, 2] | ||
head(['hello', 'world']) // ['hello'] | ||
head(['hello']) // [] | ||
head([]) // [] | ||
|
||
``` | ||
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 last element of the given list or string. | ||
* | ||
* @param val - string or any array object | ||
* @returns The result of `val.slice(0, -1)` | ||
* | ||
* @example | ||
* ```ts | ||
* // String | ||
* head('hello') // 'hell' | ||
* head('h') // '' | ||
* head('') // '' | ||
* ``` | ||
* | ||
* @example | ||
* ```ts | ||
* head([1, 2, 3]) // [1, 2] | ||
* head(['hello', 'world']) // ['hello'] | ||
* head(['hello']) // [] | ||
* head([]) // [] | ||
* ``` | ||
* | ||
* @beta | ||
*/ | ||
const head: { | ||
(val: string): string | ||
<T extends unknown[]>(val: T): T | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} = (val: any) => val.slice(0, -1) | ||
|
||
export { head } |
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,39 @@ | ||
import { head } from '@/head' | ||
|
||
describe('head', () => { | ||
const tableString: [string, string][] = [ | ||
['', ''], | ||
['a', ''], | ||
['ab', 'a'], | ||
['abc', 'ab'] | ||
] | ||
|
||
it.each(tableString)('head(%s) -> %s', (val, expected) => { | ||
expect(head(val)).toEqual(expected) | ||
}) | ||
|
||
const tableArray: [unknown[], unknown[]][] = [ | ||
[[], []], | ||
[[''], []], | ||
[[undefined], []], | ||
[[null], []], | ||
[[0], []], | ||
[['', ''], ['']], | ||
[[0, 0], [0]], | ||
[[0, ''], [0]], | ||
[['hello', 'world'], ['hello']], | ||
[ | ||
['hello', 'new', 'world'], | ||
['hello', 'new'] | ||
], | ||
[ | ||
[undefined, null, 'hello', 'world'], | ||
[undefined, null, 'hello'] | ||
], | ||
[[['hello', 'world']], []] | ||
] | ||
|
||
it.each(tableArray)('head(%s) -> %s', (val, expected) => { | ||
expect(head(val)).toEqual(expected) | ||
}) | ||
}) |