@bemoje/array 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts ADDED
@@ -0,0 +1,368 @@
1
+ /**
2
+ * Calculates the average of an array of numbers.
3
+ * @returns The average of all numbers in the array.
4
+ * @throws an error if the input array is empty.
5
+ * @param array The array of numbers.
6
+ * @example ```ts
7
+ * const numbers = [1, 2, 3, 4, 5];
8
+ * arrAverage(numbers);
9
+ * //=> 3
10
+ * ```
11
+ */
12
+ declare function arrAverage(array: number[]): number;
13
+
14
+ /**
15
+ * Coerce each element of an array to string.
16
+ * @template T - The type of elements in the input array.
17
+ * @returns A new array where each element is the string representation of the corresponding element in the input array.
18
+ * @param array The array to iterate over.
19
+ * @example ```ts
20
+ * const numbers = [1, 2, 3];
21
+ * arrEachToString(numbers);
22
+ * //=> ['1', '2', '3']
23
+ * ```
24
+ */
25
+ declare function arrEachToString<T>(array: T[]): string[];
26
+
27
+ /**
28
+ * Returns an array of indices where the predicate function returns true for the corresponding element in the input array.
29
+ * @param input - The array to search.
30
+ * @param predicate - The function to test each element of the array.
31
+ * @returns An array of indices where the predicate function returns true.
32
+ */
33
+ declare function arrFindIndicesOf<T>(input: Array<T>, predicate: (value: T) => boolean): number[];
34
+
35
+ /**
36
+ * Get array element at index or create it using factory function if it doesn't exist.
37
+ */
38
+ declare function arrGetOrDefault<V>(array: V[], index: number, factory: (index: number) => V): V;
39
+
40
+ /**
41
+ * Checks if an array has any duplicate elements.
42
+ * @param arr - The array to check for duplicates.
43
+ * @returns A boolean indicating whether the array has duplicates.
44
+ * @typeParam T - The type of elements in the array.
45
+ */
46
+ declare function arrHasDuplicates<T>(arr: T[]): boolean;
47
+
48
+ /**
49
+ * Returns all indexes at which an element is found.
50
+ * @param input The array to search
51
+ * @template T - The type of elements in the input array.
52
+ * @returns An array of indices where the specified element can be found.
53
+ * @param element The element to find
54
+ * @example ```ts
55
+ * const inputArray = [1, 2, 3, 2, 4, 2, 5];
56
+ * const elementToFind = 2;
57
+ * arrIndicesOf(inputArray, elementToFind);
58
+ * //=> [1, 3, 5]
59
+ * ```
60
+ */
61
+ declare function arrIndicesOf<T>(input: Array<T>, element: T): number[];
62
+
63
+ /**
64
+ * Returns the last element of an array.
65
+ * Throws an error if the array is empty.
66
+ * @template T The type of elements in the array.
67
+ * @param array The array to get the last element from.
68
+ * @returns The last element of the array.
69
+ * @throws If the array is empty.
70
+ * @example const numbers = [1, 2, 3, 4, 5];
71
+ * const lastNumber = arrLast(numbers);
72
+ * //=> 5
73
+ */
74
+ declare function arrLast<T>(array: T[]): T;
75
+
76
+ /**
77
+ * This function takes an array and a callback function as arguments. It applies the callback function to each element of the array, mutating the original array in the process.
78
+ * @template T The type of elements in the input array.
79
+ * @param input The array to be mapped over.
80
+ * @param f The callback function to be applied to each element of the array. This function takes three arguments: the current element, its index, and the original array.
81
+ * @returns The original array, mutated by the callback function.
82
+ * @example ```ts
83
+ * arrMapMutable([1, 2, 3], (value: number) => value * 2);
84
+ * //=> [2, 4, 6]
85
+ * ```
86
+ */
87
+ declare function arrMapMutable<T>(input: Array<T>, f: (value: T, index: number, array: T[]) => T): Array<T>;
88
+
89
+ /**
90
+ * Convert an array of objects to a two-dimensional table.
91
+ * @param objects The array of objects to convert to a table.
92
+ * @template T - The type of the values in the objects.
93
+ * @param options.headers An optional array of strings specifying the headers (property names) to use. If not provided, the function will use all unique keys found in the objects.
94
+ * @param options.emptyCell An optional value to use for empty cells. If not provided, the function will use `undefined`.
95
+ * @returns A 2D array (table) where each row represents an object and each column represents a property of the object.
96
+ * @param options The options for converting the objects to a table.
97
+ * @example ```ts
98
+ * arrObjectsToTable(
99
+ * [
100
+ * { a: 1, b: 2 },
101
+ * { a: 3, b: 4, c: 5 },
102
+ * ],
103
+ * { emptyCell:1 },
104
+ * ) //=> [ [ 'a', 'b', 'c' ], [ 1, 2,1 ], [ 3, 4, 5 ] ]
105
+ * ```
106
+ */
107
+ declare function arrObjectsToTable<T, E>(
108
+ objects: Record<string, T | undefined>[],
109
+ options?: {
110
+ headers?: string[];
111
+ emptyCell?: E;
112
+ },
113
+ ): Array<Array<string | T | E>>;
114
+
115
+ /**
116
+ * Returns an array of all unique object keys found in an array of objects.
117
+ * @template T - The type of values in the input objects.
118
+ * @returns An array of unique keys present in the input objects.
119
+ * @param objects The array of objects.
120
+ * @example ```ts
121
+ * const objects = [
122
+ * { name: 'John', age: 25 },
123
+ * { name: 'Jane', gender: 'female' },
124
+ * { name: 'Bob', age: 30, gender: 'male' },
125
+ * ];
126
+ * arrObjectsUniqueKeys(objects);
127
+ * //=> ['name', 'age', 'gender']
128
+ * ```
129
+ */
130
+ declare function arrObjectsUniqueKeys<T>(objects: Record<string, T>[]): string[];
131
+
132
+ /**
133
+ * Remove a given element from a copy of a given array and return the resulting array.
134
+ */
135
+ declare function arrRemove<T>(arr: T[], elementToRemove: T): T[];
136
+
137
+ /**
138
+ * Remove duplicates from an array
139
+ * @remarks This function uses the JavaScript Set object to remove duplicate values from an array.
140
+ * @typeparam T - The type of elements in the array.
141
+ * @returns The new array with duplicates removed.
142
+ * @param array The array from which to remove duplicates.
143
+ * @example ```ts
144
+ * const array = [1, 2, 2, 3, 4, 4, 5];
145
+ * arrRemoveDuplicates(array);
146
+ * //=> [1, 2, 3, 4, 5]
147
+ * ```
148
+ */
149
+ declare function arrRemoveDuplicates<T>(array: T[]): T[];
150
+
151
+ /**
152
+ * Remove elements in-place from an array.
153
+ */
154
+ declare function arrRemoveMutable<T>(arr: T[], elementToRemove: T): void;
155
+
156
+ /**
157
+ * Shuffle items in an array in-place. Guarantees changes.
158
+ * @remarks This function does not guarantee that the order of the elements will be different after shuffling.
159
+ * @typeparam T - The type of the elements in the input array.
160
+ * @returns The same array, but shuffled.
161
+ * @param input The array to shuffle.
162
+ * @example ```ts
163
+ * const input = [1, 2, 3, 4, 5];
164
+ * arrShuffle(input);
165
+ * //=> [3, 1, 5, 2, 4]
166
+ * ```
167
+ */
168
+ declare function arrShuffle<T>(input: Array<T>): Array<T>;
169
+
170
+ /**
171
+ * Sorts an array of numbers, bigints, or booleans in ascending order.
172
+ * @returns The sorted array.
173
+ * @remarks This function uses the JavaScript `Array.prototype.sort()` method, which sorts elements in place.
174
+ * Therefore, the original array will be modified.
175
+ * @throws If any element in the input array is not a number, bigint, or boolean.
176
+ * @param input The array to be sorted.
177
+ * @example ```ts
178
+ * const input = [5, 2n, true, 10, false];
179
+ * arrSortNumeric(input);
180
+ * //=> [false, true, 2n, 5, 10]
181
+ * ```
182
+ */
183
+ declare function arrSortNumeric(input: Array<number | bigint | boolean>): Array<number | bigint | boolean>;
184
+
185
+ /**
186
+ * Returns an index in the sorted array where the specified value could be inserted while maintaining the sorted order of the array.
187
+ * If the element is already in the array, returns the index after the last instance of the element.
188
+ * @param array - The sorted array to search.
189
+ * @param value - The value to locate in the array.
190
+ * @param comparator - A function that defines the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.
191
+ * @returns The index at which the value could be inserted into array to maintain the array's sorted order.
192
+ * @example ```ts
193
+ * const array = [1, 2, 3, 5, 6];
194
+ * const value = 4;
195
+ * const comparator = (a, b) => a - b;
196
+ * const index = arrSortedLowerBound(array, value, comparator);
197
+ * console.log(index); // Output: 3
198
+ * ```
199
+ */
200
+ declare function arrSortedInsertionIndex<T>(
201
+ array: readonly T[],
202
+ value: T,
203
+ comparator: (a: T, b: T) => number,
204
+ ): number;
205
+
206
+ /**
207
+ * Calculates the sum of an array of numbers.
208
+ * @returns The sum of all numbers in the array.
209
+ * @param array The array of numbers to sum.
210
+ * @example ```ts
211
+ * const numbers = [1, 2, 3, 4, 5];
212
+ * arrSum(numbers);
213
+ * //=> 15
214
+ * ```
215
+ */
216
+ declare function arrSum(array: number[]): number;
217
+
218
+ /**
219
+ * Swaps two elements in an array. This function takes an input array and swaps the elements at the specified indices.
220
+ * @param to The index of the element to swap to.
221
+ * @param from The index of the element to swap from.
222
+ * @template T - The type of elements in the array.
223
+ * @returns The modified array with swapped elements.
224
+ * @throws Will throw an error if 'from' or 'to' is not a valid index in the array.
225
+ * @param input The input array.
226
+ * @example ```ts
227
+ * const arr = [1, 2, 3, 4, 5]
228
+ * arrSwap(arr, 1, 3) //=> [1, 4, 3, 2, 5]
229
+ * ```
230
+ */
231
+ declare function arrSwap<T>(input: Array<T>, from: number, to: number): Array<T>;
232
+
233
+ /**
234
+ * Asserts that all rows in a 2D array have the same length.
235
+ * @param - Optional array of headers to compare the row length against.
236
+ * @throws If any row in the array has a different length than the others.
237
+ * @param headers Optional. An array of headers. If provided, each row must have the same length as this array.
238
+ * @typeparam T - The type of elements in the rows.
239
+ * @param rows The 2D array to check.
240
+ * @example ```ts
241
+ * const rows = [
242
+ * [1, 2, 3],
243
+ * [4, 5, 6],
244
+ * [7, 8, 9],
245
+ * ];
246
+ * arrTableAssertRowsSameLength(rows);
247
+ * //=> undefined
248
+ * const rowsWithDifferentLength = [
249
+ * [1, 2, 3],
250
+ * [4, 5],
251
+ * [7, 8, 9],
252
+ * ];
253
+ * arrTableAssertRowsSameLength(rowsWithDifferentLength);
254
+ * //=> Error: Expected 3 columns, got 2
255
+ * ```
256
+ */
257
+ declare function arrTableAssertRowsSameLength<T>(rows: T[][], headers?: string[]): void;
258
+
259
+ /**
260
+ * Coerce each value of a 2D array table to string.
261
+ * @template T - The type of the elements in the input array.
262
+ * @returns The converted 2D array where each element is a string.
263
+ * @param table The 2D array to convert.
264
+ * @example ```ts
265
+ * const input: number[][] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
266
+ * arrTableEachToString(input);
267
+ * //=> [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]
268
+ * ```
269
+ */
270
+ declare function arrTableEachToString<T>(table: T[][]): string[][];
271
+
272
+ /**
273
+ * Generator that iterates through a 2D array table, yielding objects with header keys and row values.
274
+ */
275
+ declare function arrTableIterateAsObjects<T>(
276
+ rows: T[][],
277
+ headers: string[],
278
+ ignoreHeaders?: Set<string>,
279
+ ): Generator<Record<string, T>, void, unknown>;
280
+
281
+ /**
282
+ * Removes specified columns from a 2D array table.
283
+ * @param table - The 2D array (table) from which columns will be removed. The first row of the table is assumed to contain column names.
284
+ * @param removeColumnNames - The names of the columns to be removed. These should match the entries in the first row of the table.
285
+ * @returns A new 2D array (table) with the specified columns removed.
286
+ */
287
+ declare function arrTableRemoveColumns(table: string[][], ...removeColumnNames: string[]): string[][];
288
+
289
+ /**
290
+ * Converts a 2D array to a CSV string.
291
+ * @param input The input 2D array.
292
+ * @remarks This function is useful for exporting data to CSV format.
293
+ * @param replaceLinebreakWith The character used to replace line breaks in the CSV string. Defaults to '|'.
294
+ * @typeparam T - The type of the elements in the input array.
295
+ * @returns The CSV string representation of the input array.
296
+ * @param delimiter The delimiter to use for separating values in the CSV string.
297
+ * @example ```ts
298
+ * const input = [
299
+ * ['Name', 'Age', 'Country'],
300
+ * ['John', '25', 'USA'],
301
+ * ['Alice', '30', 'Canada'],
302
+ * ['Bob', '35', 'UK'],
303
+ * ];
304
+ * arrTableToCsv(input);
305
+ * //=> "Name;Age;Country\nJohn;25;USA\nAlice;30;Canada\nBob;35;UK"
306
+ * ```
307
+ */
308
+ declare function arrTableToCsv<T>(input: T[][], delimiter?: string, replaceLinebreakWith?: string): string;
309
+
310
+ /**
311
+ * Converts a 2D array representing a table into an array of objects.
312
+ * @param rows The 2D array representing the table.
313
+ * @template T - The type of the elements in the rows.
314
+ * @param headers The headers to use as keys for the objects. If not provided, the first row of the table is used as headers.
315
+ * @returns An array of objects, where each object represents a row in the table.
316
+ * @throws Throws an error if the headers are not provided and the table is empty or only contains one row.
317
+ * @param headers Optional array of headers for the table.
318
+ * @example ```ts
319
+ * const table = [
320
+ * ['Name', 'Age', 'Country'],
321
+ * ['John', 25, 'USA'],
322
+ * ['Jane', 30, 'Canada'],
323
+ * ];
324
+ * const headers = ['Name', 'Age', 'Country'];
325
+ * arrTableToObjects(table, headers) //=> [
326
+ * // { Name: 'John', Age: 25, Country: 'USA' },
327
+ * // { Name: 'Jane', Age: 30, Country: 'Canada' },
328
+ * // ]
329
+ * ```
330
+ */
331
+ declare function arrTableToObjects<T>(
332
+ rows: T[][],
333
+ headers?: string[],
334
+ ignoreKeys?: Set<string>,
335
+ ): Record<string, T>[];
336
+
337
+ /**
338
+ * Short and condensed string representation of an array, easy to read for error outputs or similar.
339
+ */
340
+ declare function arrayToString<T>(array: T[]): string;
341
+
342
+ export {
343
+ arrAverage,
344
+ arrEachToString,
345
+ arrFindIndicesOf,
346
+ arrGetOrDefault,
347
+ arrHasDuplicates,
348
+ arrIndicesOf,
349
+ arrLast,
350
+ arrMapMutable,
351
+ arrObjectsToTable,
352
+ arrObjectsUniqueKeys,
353
+ arrRemove,
354
+ arrRemoveDuplicates,
355
+ arrRemoveMutable,
356
+ arrShuffle,
357
+ arrSortNumeric,
358
+ arrSortedInsertionIndex,
359
+ arrSum,
360
+ arrSwap,
361
+ arrTableAssertRowsSameLength,
362
+ arrTableEachToString,
363
+ arrTableIterateAsObjects,
364
+ arrTableRemoveColumns,
365
+ arrTableToCsv,
366
+ arrTableToObjects,
367
+ arrayToString,
368
+ };