-
-
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
3da9310
commit 816c978
Showing
5 changed files
with
137 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,36 @@ | ||
import { IsString } from '@/isString' | ||
import { InferArray } from '@/types' | ||
|
||
type first<T extends string | unknown[]> = IsString<T> extends true | ||
? string | ||
: T extends never[] | ||
? undefined | ||
: InferArray<T> | ||
|
||
/** | ||
* Returns the first element of the given list or string. | ||
* | ||
* @param val - string or any array object | ||
* @returns The first element of the `val` | ||
* | ||
* @example | ||
* String | ||
* first('hello') // 'h' | ||
* | ||
* @example | ||
* Array | ||
* first('hello', 'new', 'world') // 'hello' | ||
* first([]) // undefined | ||
* first(['one', 2, 3, 4]) // 'one' | ||
*/ | ||
const first = <T extends string | unknown[]>(val: T): first<T> => { | ||
if (Array.isArray(val)) { | ||
return val.length | ||
? (val.slice(0, 1)[0] as first<T>) | ||
: (undefined as first<T>) | ||
} else { | ||
return (val as string).slice(0, 1) as first<T> | ||
} | ||
} | ||
|
||
export { first } |
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,34 @@ | ||
import { IsString } from '@/isString' | ||
import { InferArray } from '@/types' | ||
|
||
type Last<T extends string | unknown[]> = IsString<T> extends true | ||
? string | ||
: T extends never[] | ||
? undefined | ||
: InferArray<T> | ||
|
||
/** | ||
* Returns the last element of the given list or string. | ||
* | ||
* @param val - string or any array object | ||
* @returns The last element of the `val` | ||
* | ||
* @example | ||
* String | ||
* last('hello') // 'o' | ||
* | ||
* @example | ||
* Array | ||
* last('hello', 'new', 'world') // 'world' | ||
* last([]) // undefined | ||
* last(['one', 2, 3, 4]) // 4 | ||
*/ | ||
const last = <T extends string | unknown[]>(val: T): Last<T> => { | ||
if (Array.isArray(val)) { | ||
return val.length ? (val.slice(-1)[0] as Last<T>) : (undefined as Last<T>) | ||
} else { | ||
return (val as string).slice(-1) as Last<T> | ||
} | ||
} | ||
|
||
export { last } |
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,34 @@ | ||
import { first } from '@/first' | ||
|
||
describe('first', () => { | ||
const table: [string | unknown[], unknown][] = [ | ||
['', ''], | ||
['a', 'a'], | ||
[' ab', ' '], | ||
['abcd', 'a'], | ||
['abcde ', 'a'], | ||
['abcd_', 'a'], | ||
[[], undefined], | ||
[[''], ''], | ||
[['hello', 'world'], 'hello'], | ||
[['hello', 'new', 'world'], 'hello'], | ||
[ | ||
[['hello', 'new', 'world'], 'test'], | ||
['hello', 'new', 'world'] | ||
], | ||
[ | ||
[ | ||
['hello', 'new', 'world'], | ||
['hello2', 'world'] | ||
], | ||
['hello', 'new', 'world'] | ||
], | ||
[[0], 0], | ||
[[0, 3, 6, 9], 0], | ||
[[{}], {}] | ||
] | ||
|
||
it.each(table)('first(%s) -> %s', (val, expected) => { | ||
expect(first(val)).toEqual(expected) | ||
}) | ||
}) |
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 @@ | ||
import { last } from '@/last' | ||
|
||
describe('last', () => { | ||
const table: [string | unknown[], unknown][] = [ | ||
['', ''], | ||
['a', 'a'], | ||
[' ab', 'b'], | ||
['abcd', 'd'], | ||
['abcde ', ' '], | ||
['abcd_', '_'], | ||
[[], undefined], | ||
[[''], ''], | ||
[['hello', 'world'], 'world'], | ||
[['hello', 'new', 'world'], 'world'], | ||
[[['hello', 'new', 'world'], 'test'], 'test'], | ||
[ | ||
[ | ||
['hello', 'new', 'world'], | ||
['hello2', 'world'] | ||
], | ||
['hello2', 'world'] | ||
], | ||
[[0], 0], | ||
[[0, 3, 6, 9], 9], | ||
[[{}], {}] | ||
] | ||
|
||
it.each(table)('last(%s) -> %s', (val, expected) => { | ||
expect(last(val)).toEqual(expected) | ||
}) | ||
}) |