Skip to content

Commit 0620b74

Browse files
committed
docs(array): document all
1 parent 92580ac commit 0620b74

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

src/array.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import type { Choice, Maybe, Optional, ReadonlyArrayStrict } from './types.js';
55
* Returns the first element of an array or the value itself if it's not an array.
66
*
77
* ```ts
8-
* import { firstOrSelf } from '@jagaad/utils';
9-
*
108
* const singleValue = firstOrSelf('Hello'); // 'Hello'
119
* const arrayValue = firstOrSelf(['Hello', 'World']); // 'Hello'
1210
* const emptyArray = firstOrSelf([]); // undefined
1311
* const nullValue = firstOrSelf(null); // undefined
1412
* const undefinedValue = firstOrSelf(undefined); // undefined
15-
* const numberValue = firstOrSelf(42); // 42
13+
* const numberValue = firstOrSelf(0); // 0
1614
* const emptyString = firstOrSelf(''); // ''
1715
* const booleanValue = firstOrSelf(false); // false
1816
* ```
@@ -27,16 +25,48 @@ export function firstOrSelf<T>(value: Maybe<Arrayable<T>>): Optional<T> {
2725
return value;
2826
}
2927

28+
/**
29+
* Inserts a separator between each element of the array.
30+
*
31+
* ```ts
32+
* import { intersperse } from '@jagaad/utils';
33+
*
34+
* const result = intersperse(['a', 'b', 'c'], '-'); // ['a', '-', 'b', '-', 'c']
35+
*/
3036
export function intersperse<T>(array: ReadonlyArray<T>, separator: T): T[] {
3137
return [...Array(2 * array.length - 1)].map((_, i) =>
3238
i % 2 ? separator : (array[i / 2] as T),
3339
);
3440
}
3541

42+
/**
43+
* Returns a random item from the given array.
44+
*
45+
* ```ts
46+
* const items = ['apple', 'banana', 'cherry'];
47+
* const random = randomItem(items); // Could be 'apple', 'banana', or 'cherry'
48+
* ```
49+
*
50+
* @deprecated Use `sample` from `es-toolkit` instead.
51+
*/
3652
export function randomItem<T>(arr: ReadonlyArray<T>): T {
3753
return arr[Math.floor(Math.random() * arr.length)] as T;
3854
}
3955

56+
/**
57+
* Returns metadata about the index of an item in a list.
58+
*
59+
* ```ts
60+
* const list = ['apple', 'banana', 'cherry'];
61+
* const meta = getIndexMeta(1, list);
62+
* console.log(meta.count); // 3
63+
* console.log(meta.current); // 2
64+
* console.log(meta.first); // false
65+
* console.log(meta.last); // false
66+
* console.log(meta.odd); // true
67+
* console.log(meta.even); // false
68+
* ```
69+
*/
4070
export function getIndexMeta(
4171
index: number,
4272
list: ReadonlyArrayStrict<unknown>,
@@ -81,6 +111,18 @@ export function getIndexMeta(
81111
};
82112
}
83113

114+
/**
115+
* Converts an array of choices into a record where the keys are the choice IDs and the values are the choice names.
116+
*
117+
* ```ts
118+
* const choices = [
119+
* { id: '1', name: 'Choice 1' },
120+
* { id: '2', name: 'Choice 2' },
121+
* ];
122+
* const record = choicesToRecord(choices);
123+
* console.log(record); // { '1': 'Choice 1', '2': 'Choice 2' }
124+
* ```
125+
*/
84126
export function choicesToRecord(
85127
choices: Maybe<ReadonlyArrayStrict<Choice>>,
86128
): Record<string, string> {

0 commit comments

Comments
 (0)