Skip to content

Commit 92580ac

Browse files
committed
docs(array): document firstOrSelf
1 parent 9e0e0b2 commit 92580ac

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/array.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
import type { Arrayable } from 'type-fest';
22
import type { Choice, Maybe, Optional, ReadonlyArrayStrict } from './types.js';
33

4-
export function firstOrSelf<T>(value?: Maybe<Arrayable<T>>): Optional<T> {
5-
if (!value) return undefined;
4+
/**
5+
* Returns the first element of an array or the value itself if it's not an array.
6+
*
7+
* ```ts
8+
* import { firstOrSelf } from '@jagaad/utils';
9+
*
10+
* const singleValue = firstOrSelf('Hello'); // 'Hello'
11+
* const arrayValue = firstOrSelf(['Hello', 'World']); // 'Hello'
12+
* const emptyArray = firstOrSelf([]); // undefined
13+
* const nullValue = firstOrSelf(null); // undefined
14+
* const undefinedValue = firstOrSelf(undefined); // undefined
15+
* const numberValue = firstOrSelf(42); // 42
16+
* const emptyString = firstOrSelf(''); // ''
17+
* const booleanValue = firstOrSelf(false); // false
18+
* ```
19+
*/
20+
export function firstOrSelf<T>(value: Maybe<Arrayable<T>>): Optional<T> {
21+
if (value == null) return undefined;
622

723
if (Array.isArray(value)) {
824
return value.at(0);

0 commit comments

Comments
 (0)