Skip to content

Commit

Permalink
✨ Add getting first element of array and string function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Apr 19, 2021
1 parent 3b67787 commit cc59f96
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export { F } from '@/F'
export { first } from '@/first'
export { gt } from '@/gt'
export { gte } from '@/gte'
export { has } from '@/has'
export { hasPath } from '@/hasPath'
export { inc } from '@/inc'
export { isBigint } from '@/isBigint'
export { isBoolean } from '@/isBoolean'
Expand All @@ -32,6 +34,7 @@ export { startsWith } from '@/startsWith'
export { subtract } from '@/subtract'
export { sum } from '@/sum'
export { T } from '@/T'
export { tail } from '@/tail'
export { trim } from '@/trim'
export type { AnyFn, Ord, Primitive } from '@/types'
export { upperCase } from '@/upperCase'
Expand Down
31 changes: 31 additions & 0 deletions src/tail.ts
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 }
32 changes: 32 additions & 0 deletions test/tail.spec.ts
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)
})
})

0 comments on commit cc59f96

Please sign in to comment.