-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_last.ts
More file actions
33 lines (31 loc) · 1.08 KB
/
init_last.ts
File metadata and controls
33 lines (31 loc) · 1.08 KB
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
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.
import type { Sequence } from "./types.ts";
import type { LastChar } from "./last.ts";
import type { InitString } from "./init.ts";
/** Split the sequence into init and last.
*
* @example
* ```ts
* import { initLast } from "https://deno.land/x/seqtools/init_last.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* assertEquals(initLast([1, 2, 3]), [[1, 2], 3]);
* assertEquals(initLast("abc"), ["ab", "c"]);
* assertEquals(initLast([]), [[], undefined]);
* ```
*/
export function initLast<const T extends readonly unknown[], const U>(
array: readonly [...T, U],
): [init: T, last: U];
export function initLast<const T extends string>(
string: `${T}`,
): [init: InitString<T>, last: LastChar<T>];
export function initLast<T, U = T>(
seq: Readonly<Sequence<T, U>>,
): [init: U, last: T | undefined];
export function initLast<T, U = T>(
seq: Readonly<Sequence<T, U>>,
): [init: U, last: T | undefined] {
return [seq.slice(0, -1), seq.at(-1)];
}