-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtakeLast.ts
30 lines (23 loc) · 946 Bytes
/
takeLast.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import { drop } from './drop.ts';
import curryN from './utils/curry_n.ts';
import type { InferType, PH } from './utils/types.ts';
// TODO: write transformer
// @types
type TakeLast_2 = <F extends unknown[] | string>(functor: F) => InferType<F>;
type TakeLast_1<F extends unknown[] | string> = (n: number) => InferType<F>;
type TakeLast =
& ((n: number) => TakeLast_2)
& (<F extends unknown[] | string>(
n: PH,
functor: F,
) => TakeLast_1<InferType<F>>)
& (<F extends unknown[] | string>(n: number, functor: F) => InferType<F>);
/**
* Returns last `n` elements of the array or string.
* If `n > functor.length` or `n` is negative, a copy of `functor` is returned.
*/
function _takeLast<F extends unknown[] | string>(n: number, functor: F) {
return drop(n >= 0 ? functor.length - n : 0, functor);
}
export const takeLast = curryN(2, _takeLast) as TakeLast;