-
Notifications
You must be signed in to change notification settings - Fork 0
/
head.ts
23 lines (21 loc) · 803 Bytes
/
head.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Copyright © 2023 Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.
import type { Indexable } from "./types.ts";
/** Extract the first element of a sequence.
*
* @example
* ```ts
* import { head } from "https://deno.land/x/seqtools/head.ts";
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* assertEquals(head([1, 2, 3]), 1);
* assertEquals(head("abc"), "a");
* assertEquals(head([]), undefined);
* ```
*/
export function head<const T>(array: readonly [T, ...readonly unknown[]]): T;
export function head<const T extends string>(string: `${T}${string}`): T;
export function head<T>(seq: Readonly<Indexable<T>>): T | undefined;
export function head<T>(seq: Readonly<Indexable<T>>): T | undefined {
return seq.at(0);
}