Skip to content

Commit c7688cd

Browse files
committed
chore: add docs
1 parent 94ad1f6 commit c7688cd

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

src/arrays/chunk.ts

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* @param {T[]} arr - The array to be chunked into smaller arrays.
77
* @param {number} size - The size of each smaller array. Must be a positive integer.
88
* @returns {T[][]} A two-dimensional array where each sub-array has a maximum length of `size`.
9-
* @throws {Error} Throws an error if `size` is not a positive integer.
109
* @example
1110
* // Splits an array of numbers into sub-arrays of length 2
1211
* chunk([1, 2, 3, 4, 5], 2);

src/arrays/list.ts

+73-2
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,63 @@ export class List<T> extends Array<T> {
9898
return lastIndex(this);
9999
}
100100

101+
/**
102+
* Get a random value with `Math.random()`
103+
* @template T
104+
* @returns {T} A random element from the array.
105+
*/
101106
pick(): T {
102107
return pick(this);
103108
}
104109

110+
/**
111+
* Returns a sample element array of a specified `size`.
112+
*
113+
* This function takes an array and a number, and returns an array containing the sampled elements using Floyd's algorithm.
114+
*
115+
* {@link https://www.nowherenearithaca.com/2013/05/robert-floyds-tiny-and-beautiful.html Floyd's algoritm}
116+
* @template T
117+
* @param {number} size - The number of elements to sample.
118+
* @returns {T[]} An array containing the sampled elements.
119+
*/
105120
sample(size: number): List<T> {
106121
return List.fromArray(sample(this, size));
107122
}
108123

124+
/**
125+
* Creates a duplicate-free version of an array.
126+
*
127+
* This function takes an array and returns a new array containing only the unique values from the original array, preserving the order of first occurrence.
128+
* @template T - The type of elements in the array.
129+
* @returns {T[]} A new array with only unique values from the original array.
130+
*/
109131
uniq(): List<T> {
110132
return List.fromArray(uniq(this));
111133
}
112134

135+
/**
136+
* Creates an array of unique values from all given arrays.
137+
*
138+
* This function takes two arrays, merges them into a single array, and returns a new array
139+
* containing only the unique values from the merged array.
140+
* @template T - The type of elements in the array.
141+
* @param {List<T>} other - The second array to merge and filter for unique values.
142+
* @returns {List<T>} A new array of unique values.
143+
*/
113144
union(other: List<T>): List<T> {
114145
return List.fromArray(union(this, other));
115146
}
116147

148+
/**
149+
* Returns the intersection of two arrays.
150+
*
151+
* This function takes two arrays and returns a new array containing the elements that are
152+
* present in both arrays. It effectively filters out any elements from the first array that
153+
* are not found in the second array.
154+
* @template T
155+
* @param {T[]} others - The arrays to intersect. Each array is compared to the other arrays
156+
* @returns {T[]} A new array containing the elements that are present in both arrays.
157+
*/
117158
intersection(...others: Array<List<T>>): List<T> {
118159
return List.fromArray(intersection(this, ...others));
119160
}
@@ -122,8 +163,23 @@ export class List<T> extends Array<T> {
122163
return List.fromArray(xor(this, other));
123164
}
124165

125-
difference(...others: T[][]): List<T> {
126-
return List.fromArray(difference(this, ...others));
166+
/**
167+
* Computes the difference the current list and another array.
168+
*
169+
* This function takes two arrays and returns a new array containing the elements
170+
* that are present in the current list but not in the second array. It effectively filters out any elements from the current list that also appear in the second array.
171+
* @template T
172+
* @param {T[]} diffs - The array containing elements to be excluded from the current list.
173+
* Each element in this array will be checked against the current list, and if a match is found, that element will be excluded from the result.
174+
* @returns {T[]} A new array containing the elements that are present in the current list but not in the second array.
175+
* @example
176+
* const list = new List(...[1, 2, 3, 4, 5]);
177+
* const array2 = [2, 4];
178+
* const result = list.difference(array2);
179+
* // result will be [1, 3, 5] since 2 and 4 are in both arrays and are excluded from the result.
180+
*/
181+
difference(...diffs: T[][]): List<T> {
182+
return List.fromArray(difference(this, ...diffs));
127183
}
128184

129185
select<K>(
@@ -133,6 +189,13 @@ export class List<T> extends Array<T> {
133189
return List.fromArray(select(this, mapper, condition));
134190
}
135191

192+
/**
193+
* Sorts a list of items into groups. The return value is a map where the keys are the group ids the given getGroupId function produced and the value is a list of each item in that group.
194+
* @template T
195+
* @template Key
196+
* @param {(item: T) => Key} getGroupId - A function that returns the group id for each item.
197+
* @returns {Partial<{ [key in Key]: T[] }>} A map where the keys are the group ids and the value is an array of each item in that group.
198+
*/
136199
group<Key extends string | number | symbol>(
137200
getGroupId: (item: T) => Key,
138201
): Partial<{ [key in Key]: T[] }> {
@@ -162,6 +225,14 @@ export class List<T> extends Array<T> {
162225
return countOccurrences(this, value);
163226
}
164227

228+
/**
229+
* Creates an array of elements split into groups the length of size.
230+
* If collection can’t be split evenly, the
231+
* final chunk will be the remaining elements.
232+
* @template T The type of elements in the array.
233+
* @param {number} size - The size of each smaller array. Must be a positive integer.
234+
* @returns {List<T[]>} A two-dimensional array where each sub-array has a maximum length of `size`.
235+
*/
165236
chunk(size: number): List<T[]> {
166237
return List.fromArray(chunk(this, size));
167238
}

0 commit comments

Comments
 (0)