-
Notifications
You must be signed in to change notification settings - Fork 7
/
dropLast.ts
39 lines (32 loc) · 1.35 KB
/
dropLast.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
31
32
33
34
35
36
37
38
39
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import { dispatch } from './utils/dispatch.ts';
import curryN from './utils/curry_n.ts';
import type { InferType, PH } from './utils/types.ts';
import { take } from './take.ts';
import DropLastTransformer from './utils/Transformers/dropLast.ts';
// @types
type DropLast_2 = <L extends unknown[] | string>(list: L) => InferType<L>;
type DropLast_1<L extends unknown[] | string> = (n: number) => InferType<L>;
type DropLast =
& ((n: number) => DropLast_2)
& (<L extends unknown[] | string>(n: PH, list: L) => DropLast_1<L>)
& (<L extends unknown[] | string>(n: number, list: L) => InferType<L>);
function _dropLast<L extends T[] | string, T>(n: number, list: L) {
return take(n < list.length ? list.length - n : 0, list);
}
const dispatchedDropLast = dispatch(
DropLastTransformer,
_dropLast,
);
/**
* Returns all but last `n` elements of given list.
*
* Acts as a transducer if a transformer is passed in place of `list`
*
* Fae.dropLast(1, ['foo', 'bar', 'baz']); //=> ['foo', 'bar']
* Fae.dropLast(2, ['foo', 'bar', 'baz']); //=> ['foo']
* Fae.dropLast(3, ['foo', 'bar', 'baz']); //=> []
* Fae.dropLast(4, ['foo', 'bar', 'baz']); //=> []
* Fae.dropLast(3, 'foobar'); //=> 'foo'
*/
export const dropLast: DropLast = curryN(2, dispatchedDropLast);