@bemoje/array 1.0.3 → 1.0.4
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 +6 -47
- package/index.mjs +53 -86
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -104,13 +104,10 @@ declare function arrMapMutable<T>(input: Array<T>, f: (value: T, index: number,
|
|
|
104
104
|
* ) //=> [ [ 'a', 'b', 'c' ], [ 1, 2,1 ], [ 3, 4, 5 ] ]
|
|
105
105
|
* ```
|
|
106
106
|
*/
|
|
107
|
-
declare function arrObjectsToTable<T, E>(
|
|
108
|
-
objects: Record<string, T | undefined>[],
|
|
109
|
-
options?: {
|
|
107
|
+
declare function arrObjectsToTable<T, E>(objects: Record<string, T | undefined>[], options?: {
|
|
110
108
|
headers?: string[];
|
|
111
109
|
emptyCell?: E;
|
|
112
|
-
|
|
113
|
-
): Array<Array<string | T | E>>;
|
|
110
|
+
}): Array<Array<string | T | E>>;
|
|
114
111
|
|
|
115
112
|
/**
|
|
116
113
|
* Returns an array of all unique object keys found in an array of objects.
|
|
@@ -197,11 +194,7 @@ declare function arrSortNumeric(input: Array<number | bigint | boolean>): Array<
|
|
|
197
194
|
* console.log(index); // Output: 3
|
|
198
195
|
* ```
|
|
199
196
|
*/
|
|
200
|
-
declare function arrSortedInsertionIndex<T>(
|
|
201
|
-
array: readonly T[],
|
|
202
|
-
value: T,
|
|
203
|
-
comparator: (a: T, b: T) => number,
|
|
204
|
-
): number;
|
|
197
|
+
declare function arrSortedInsertionIndex<T>(array: readonly T[], value: T, comparator: (a: T, b: T) => number): number;
|
|
205
198
|
|
|
206
199
|
/**
|
|
207
200
|
* Calculates the sum of an array of numbers.
|
|
@@ -272,11 +265,7 @@ declare function arrTableEachToString<T>(table: T[][]): string[][];
|
|
|
272
265
|
/**
|
|
273
266
|
* Generator that iterates through a 2D array table, yielding objects with header keys and row values.
|
|
274
267
|
*/
|
|
275
|
-
declare function arrTableIterateAsObjects<T>(
|
|
276
|
-
rows: T[][],
|
|
277
|
-
headers: string[],
|
|
278
|
-
ignoreHeaders?: Set<string>,
|
|
279
|
-
): Generator<Record<string, T>, void, unknown>;
|
|
268
|
+
declare function arrTableIterateAsObjects<T>(rows: T[][], headers: string[], ignoreHeaders?: Set<string>): Generator<Record<string, T>, void, unknown>;
|
|
280
269
|
|
|
281
270
|
/**
|
|
282
271
|
* Removes specified columns from a 2D array table.
|
|
@@ -328,41 +317,11 @@ declare function arrTableToCsv<T>(input: T[][], delimiter?: string, replaceLineb
|
|
|
328
317
|
* // ]
|
|
329
318
|
* ```
|
|
330
319
|
*/
|
|
331
|
-
declare function arrTableToObjects<T>(
|
|
332
|
-
rows: T[][],
|
|
333
|
-
headers?: string[],
|
|
334
|
-
ignoreKeys?: Set<string>,
|
|
335
|
-
): Record<string, T>[];
|
|
320
|
+
declare function arrTableToObjects<T>(rows: T[][], headers?: string[], ignoreKeys?: Set<string>): Record<string, T>[];
|
|
336
321
|
|
|
337
322
|
/**
|
|
338
323
|
* Short and condensed string representation of an array, easy to read for error outputs or similar.
|
|
339
324
|
*/
|
|
340
325
|
declare function arrayToString<T>(array: T[]): string;
|
|
341
326
|
|
|
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
|
-
};
|
|
327
|
+
export { arrAverage, arrEachToString, arrFindIndicesOf, arrGetOrDefault, arrHasDuplicates, arrIndicesOf, arrLast, arrMapMutable, arrObjectsToTable, arrObjectsUniqueKeys, arrRemove, arrRemoveDuplicates, arrRemoveMutable, arrShuffle, arrSortNumeric, arrSortedInsertionIndex, arrSum, arrSwap, arrTableAssertRowsSameLength, arrTableEachToString, arrTableIterateAsObjects, arrTableRemoveColumns, arrTableToCsv, arrTableToObjects, arrayToString };
|
package/index.mjs
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => {
|
|
3
|
-
return __defProp(target, 'name', { value, configurable: true });
|
|
4
|
-
};
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
3
|
|
|
6
4
|
// src/lib/arrSum.ts
|
|
7
5
|
function arrSum(array) {
|
|
@@ -9,16 +7,16 @@ function arrSum(array) {
|
|
|
9
7
|
return acc + cur;
|
|
10
8
|
}, 0);
|
|
11
9
|
}
|
|
12
|
-
__name(arrSum,
|
|
10
|
+
__name(arrSum, "arrSum");
|
|
13
11
|
|
|
14
12
|
// src/lib/arrAverage.ts
|
|
15
13
|
function arrAverage(array) {
|
|
16
14
|
if (!array.length) {
|
|
17
|
-
throw new Error(
|
|
15
|
+
throw new Error("Cannot take an average of zero values.");
|
|
18
16
|
}
|
|
19
17
|
return arrSum(array) / array.length;
|
|
20
18
|
}
|
|
21
|
-
__name(arrAverage,
|
|
19
|
+
__name(arrAverage, "arrAverage");
|
|
22
20
|
|
|
23
21
|
// src/lib/arrEachToString.ts
|
|
24
22
|
function arrEachToString(array) {
|
|
@@ -26,7 +24,7 @@ function arrEachToString(array) {
|
|
|
26
24
|
return `${element}`;
|
|
27
25
|
});
|
|
28
26
|
}
|
|
29
|
-
__name(arrEachToString,
|
|
27
|
+
__name(arrEachToString, "arrEachToString");
|
|
30
28
|
|
|
31
29
|
// src/lib/arrFindIndicesOf.ts
|
|
32
30
|
function arrFindIndicesOf(input, predicate) {
|
|
@@ -38,7 +36,7 @@ function arrFindIndicesOf(input, predicate) {
|
|
|
38
36
|
}
|
|
39
37
|
return result;
|
|
40
38
|
}
|
|
41
|
-
__name(arrFindIndicesOf,
|
|
39
|
+
__name(arrFindIndicesOf, "arrFindIndicesOf");
|
|
42
40
|
|
|
43
41
|
// src/lib/arrGetOrDefault.ts
|
|
44
42
|
function arrGetOrDefault(array, index, factory) {
|
|
@@ -46,9 +44,9 @@ function arrGetOrDefault(array, index, factory) {
|
|
|
46
44
|
if (value !== void 0 || Object.hasOwn(array, index)) {
|
|
47
45
|
return value;
|
|
48
46
|
}
|
|
49
|
-
return
|
|
47
|
+
return array[index] = factory(index);
|
|
50
48
|
}
|
|
51
|
-
__name(arrGetOrDefault,
|
|
49
|
+
__name(arrGetOrDefault, "arrGetOrDefault");
|
|
52
50
|
|
|
53
51
|
// src/lib/arrHasDuplicates.ts
|
|
54
52
|
function arrHasDuplicates(arr) {
|
|
@@ -61,7 +59,7 @@ function arrHasDuplicates(arr) {
|
|
|
61
59
|
}
|
|
62
60
|
return false;
|
|
63
61
|
}
|
|
64
|
-
__name(arrHasDuplicates,
|
|
62
|
+
__name(arrHasDuplicates, "arrHasDuplicates");
|
|
65
63
|
|
|
66
64
|
// src/lib/arrIndicesOf.ts
|
|
67
65
|
function arrIndicesOf(input, element) {
|
|
@@ -73,16 +71,16 @@ function arrIndicesOf(input, element) {
|
|
|
73
71
|
}
|
|
74
72
|
return result;
|
|
75
73
|
}
|
|
76
|
-
__name(arrIndicesOf,
|
|
74
|
+
__name(arrIndicesOf, "arrIndicesOf");
|
|
77
75
|
|
|
78
76
|
// src/lib/arrLast.ts
|
|
79
77
|
function arrLast(array) {
|
|
80
78
|
if (!array.length) {
|
|
81
|
-
throw new Error(
|
|
79
|
+
throw new Error("Cannot get last element of empty array.");
|
|
82
80
|
}
|
|
83
81
|
return array[array.length - 1];
|
|
84
82
|
}
|
|
85
|
-
__name(arrLast,
|
|
83
|
+
__name(arrLast, "arrLast");
|
|
86
84
|
|
|
87
85
|
// src/lib/arrMapMutable.ts
|
|
88
86
|
function arrMapMutable(input, f) {
|
|
@@ -91,7 +89,7 @@ function arrMapMutable(input, f) {
|
|
|
91
89
|
}
|
|
92
90
|
return input;
|
|
93
91
|
}
|
|
94
|
-
__name(arrMapMutable,
|
|
92
|
+
__name(arrMapMutable, "arrMapMutable");
|
|
95
93
|
|
|
96
94
|
// src/lib/arrObjectsUniqueKeys.ts
|
|
97
95
|
function arrObjectsUniqueKeys(objects) {
|
|
@@ -103,12 +101,14 @@ function arrObjectsUniqueKeys(objects) {
|
|
|
103
101
|
}
|
|
104
102
|
return Array.from(keys);
|
|
105
103
|
}
|
|
106
|
-
__name(arrObjectsUniqueKeys,
|
|
104
|
+
__name(arrObjectsUniqueKeys, "arrObjectsUniqueKeys");
|
|
107
105
|
|
|
108
106
|
// src/lib/arrObjectsToTable.ts
|
|
109
107
|
function arrObjectsToTable(objects, options = {}) {
|
|
110
108
|
const headers = options?.headers?.slice() || arrObjectsUniqueKeys(objects);
|
|
111
|
-
const table = [
|
|
109
|
+
const table = [
|
|
110
|
+
headers
|
|
111
|
+
];
|
|
112
112
|
for (const o of objects) {
|
|
113
113
|
const row = headers.map((header) => {
|
|
114
114
|
const value = o[header];
|
|
@@ -118,7 +118,7 @@ function arrObjectsToTable(objects, options = {}) {
|
|
|
118
118
|
}
|
|
119
119
|
return table;
|
|
120
120
|
}
|
|
121
|
-
__name(arrObjectsToTable,
|
|
121
|
+
__name(arrObjectsToTable, "arrObjectsToTable");
|
|
122
122
|
|
|
123
123
|
// src/lib/arrRemove.ts
|
|
124
124
|
function arrRemove(arr, elementToRemove) {
|
|
@@ -126,13 +126,13 @@ function arrRemove(arr, elementToRemove) {
|
|
|
126
126
|
return element !== elementToRemove;
|
|
127
127
|
});
|
|
128
128
|
}
|
|
129
|
-
__name(arrRemove,
|
|
129
|
+
__name(arrRemove, "arrRemove");
|
|
130
130
|
|
|
131
131
|
// src/lib/arrRemoveDuplicates.ts
|
|
132
132
|
function arrRemoveDuplicates(array) {
|
|
133
133
|
return Array.from(new Set(array));
|
|
134
134
|
}
|
|
135
|
-
__name(arrRemoveDuplicates,
|
|
135
|
+
__name(arrRemoveDuplicates, "arrRemoveDuplicates");
|
|
136
136
|
|
|
137
137
|
// src/lib/arrRemoveMutable.ts
|
|
138
138
|
function arrRemoveMutable(arr, elementToRemove) {
|
|
@@ -142,17 +142,20 @@ function arrRemoveMutable(arr, elementToRemove) {
|
|
|
142
142
|
index = arr.indexOf(elementToRemove);
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
|
-
__name(arrRemoveMutable,
|
|
145
|
+
__name(arrRemoveMutable, "arrRemoveMutable");
|
|
146
146
|
|
|
147
147
|
// src/lib/arrSwap.ts
|
|
148
148
|
function arrSwap(input, from, to) {
|
|
149
149
|
if (from === to) {
|
|
150
150
|
return input;
|
|
151
151
|
}
|
|
152
|
-
[input[from], input[to]] = [
|
|
152
|
+
[input[from], input[to]] = [
|
|
153
|
+
input[to],
|
|
154
|
+
input[from]
|
|
155
|
+
];
|
|
153
156
|
return input;
|
|
154
157
|
}
|
|
155
|
-
__name(arrSwap,
|
|
158
|
+
__name(arrSwap, "arrSwap");
|
|
156
159
|
|
|
157
160
|
// src/lib/arrShuffle.ts
|
|
158
161
|
function arrShuffle(input) {
|
|
@@ -166,11 +169,11 @@ function arrShuffle(input) {
|
|
|
166
169
|
const newIndex = Math.floor(Math.random() * input.length);
|
|
167
170
|
arrSwap(input, i, newIndex);
|
|
168
171
|
}
|
|
169
|
-
equal = input.join(
|
|
172
|
+
equal = input.join(",") === original.join(",");
|
|
170
173
|
}
|
|
171
174
|
return input;
|
|
172
175
|
}
|
|
173
|
-
__name(arrShuffle,
|
|
176
|
+
__name(arrShuffle, "arrShuffle");
|
|
174
177
|
|
|
175
178
|
// src/lib/arrSortNumeric.ts
|
|
176
179
|
function arrSortNumeric(input) {
|
|
@@ -184,7 +187,7 @@ function arrSortNumeric(input) {
|
|
|
184
187
|
return 0;
|
|
185
188
|
});
|
|
186
189
|
}
|
|
187
|
-
__name(arrSortNumeric,
|
|
190
|
+
__name(arrSortNumeric, "arrSortNumeric");
|
|
188
191
|
|
|
189
192
|
// src/lib/arrSortedInsertionIndex.ts
|
|
190
193
|
function arrSortedInsertionIndex(array, value, comparator) {
|
|
@@ -203,7 +206,7 @@ function arrSortedInsertionIndex(array, value, comparator) {
|
|
|
203
206
|
}
|
|
204
207
|
return first;
|
|
205
208
|
}
|
|
206
|
-
__name(arrSortedInsertionIndex,
|
|
209
|
+
__name(arrSortedInsertionIndex, "arrSortedInsertionIndex");
|
|
207
210
|
|
|
208
211
|
// src/lib/arrTableAssertRowsSameLength.ts
|
|
209
212
|
function arrTableAssertRowsSameLength(rows, headers) {
|
|
@@ -214,18 +217,18 @@ function arrTableAssertRowsSameLength(rows, headers) {
|
|
|
214
217
|
}
|
|
215
218
|
}
|
|
216
219
|
}
|
|
217
|
-
__name(arrTableAssertRowsSameLength,
|
|
220
|
+
__name(arrTableAssertRowsSameLength, "arrTableAssertRowsSameLength");
|
|
218
221
|
|
|
219
222
|
// src/lib/arrTableEachToString.ts
|
|
220
223
|
function arrTableEachToString(table) {
|
|
221
224
|
return table.map(arrEachToString);
|
|
222
225
|
}
|
|
223
|
-
__name(arrTableEachToString,
|
|
226
|
+
__name(arrTableEachToString, "arrTableEachToString");
|
|
224
227
|
|
|
225
228
|
// src/lib/arrTableIterateAsObjects.ts
|
|
226
229
|
function* arrTableIterateAsObjects(rows, headers, ignoreHeaders = /* @__PURE__ */ new Set()) {
|
|
227
230
|
if (!headers.length) {
|
|
228
|
-
throw new Error(
|
|
231
|
+
throw new Error("No headers provided");
|
|
229
232
|
}
|
|
230
233
|
ignoreHeaders.forEach((h) => {
|
|
231
234
|
if (!headers.includes(h)) {
|
|
@@ -233,7 +236,7 @@ function* arrTableIterateAsObjects(rows, headers, ignoreHeaders = /* @__PURE__ *
|
|
|
233
236
|
}
|
|
234
237
|
});
|
|
235
238
|
if (new Set(headers).size === ignoreHeaders.size) {
|
|
236
|
-
throw new Error(
|
|
239
|
+
throw new Error("All headers are ignored");
|
|
237
240
|
}
|
|
238
241
|
for (let r = 0; r < rows.length; r++) {
|
|
239
242
|
if (rows[r].length !== headers.length) {
|
|
@@ -249,41 +252,33 @@ function* arrTableIterateAsObjects(rows, headers, ignoreHeaders = /* @__PURE__ *
|
|
|
249
252
|
yield o;
|
|
250
253
|
}
|
|
251
254
|
}
|
|
252
|
-
__name(arrTableIterateAsObjects,
|
|
255
|
+
__name(arrTableIterateAsObjects, "arrTableIterateAsObjects");
|
|
253
256
|
|
|
254
257
|
// src/lib/arrTableRemoveColumns.ts
|
|
255
258
|
function arrTableRemoveColumns(table, ...removeColumnNames) {
|
|
256
259
|
if (!removeColumnNames.length || !table.length) {
|
|
257
260
|
return table;
|
|
258
261
|
}
|
|
259
|
-
const set = new Set(
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
return arrIndicesOf(table[0], col);
|
|
263
|
-
})
|
|
264
|
-
.flat(),
|
|
265
|
-
);
|
|
262
|
+
const set = new Set(removeColumnNames.map((col) => {
|
|
263
|
+
return arrIndicesOf(table[0], col);
|
|
264
|
+
}).flat());
|
|
266
265
|
return table.map((row) => {
|
|
267
266
|
return row.filter((_, i) => {
|
|
268
267
|
return !set.has(i);
|
|
269
268
|
});
|
|
270
269
|
});
|
|
271
270
|
}
|
|
272
|
-
__name(arrTableRemoveColumns,
|
|
271
|
+
__name(arrTableRemoveColumns, "arrTableRemoveColumns");
|
|
273
272
|
|
|
274
273
|
// src/lib/arrTableToCsv.ts
|
|
275
|
-
function arrTableToCsv(input, delimiter =
|
|
276
|
-
return input
|
|
277
|
-
.map((
|
|
278
|
-
return
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
})
|
|
282
|
-
.join(delimiter);
|
|
283
|
-
})
|
|
284
|
-
.join('\n');
|
|
274
|
+
function arrTableToCsv(input, delimiter = ";", replaceLinebreakWith = "<br>") {
|
|
275
|
+
return input.map((row) => {
|
|
276
|
+
return row.map((item) => {
|
|
277
|
+
return String(item).replace(new RegExp(";", "g"), "").replace(/\r*\n/g, replaceLinebreakWith);
|
|
278
|
+
}).join(delimiter);
|
|
279
|
+
}).join("\n");
|
|
285
280
|
}
|
|
286
|
-
__name(arrTableToCsv,
|
|
281
|
+
__name(arrTableToCsv, "arrTableToCsv");
|
|
287
282
|
|
|
288
283
|
// src/lib/arrTableToObjects.ts
|
|
289
284
|
function arrTableToObjects(rows, headers, ignoreKeys) {
|
|
@@ -296,7 +291,7 @@ function arrTableToObjects(rows, headers, ignoreKeys) {
|
|
|
296
291
|
return [];
|
|
297
292
|
}
|
|
298
293
|
headers = rows[0].map((header) => {
|
|
299
|
-
return header === null || header === void 0 ?
|
|
294
|
+
return header === null || header === void 0 ? "" : String(header);
|
|
300
295
|
});
|
|
301
296
|
rows = rows.slice(1);
|
|
302
297
|
}
|
|
@@ -313,42 +308,14 @@ function arrTableToObjects(rows, headers, ignoreKeys) {
|
|
|
313
308
|
return o;
|
|
314
309
|
});
|
|
315
310
|
}
|
|
316
|
-
__name(arrTableToObjects,
|
|
311
|
+
__name(arrTableToObjects, "arrTableToObjects");
|
|
317
312
|
|
|
318
313
|
// src/lib/arrayToString.ts
|
|
319
314
|
function arrayToString(array) {
|
|
320
|
-
return `[${array
|
|
321
|
-
.
|
|
322
|
-
|
|
323
|
-
})
|
|
324
|
-
.join(',')}]`;
|
|
315
|
+
return `[${array.map((item) => {
|
|
316
|
+
return item == null ? String(item) : Array.isArray(item) ? arrayToString(item) : item.toString();
|
|
317
|
+
}).join(",")}]`;
|
|
325
318
|
}
|
|
326
|
-
__name(arrayToString,
|
|
319
|
+
__name(arrayToString, "arrayToString");
|
|
327
320
|
|
|
328
|
-
export {
|
|
329
|
-
arrAverage,
|
|
330
|
-
arrEachToString,
|
|
331
|
-
arrFindIndicesOf,
|
|
332
|
-
arrGetOrDefault,
|
|
333
|
-
arrHasDuplicates,
|
|
334
|
-
arrIndicesOf,
|
|
335
|
-
arrLast,
|
|
336
|
-
arrMapMutable,
|
|
337
|
-
arrObjectsToTable,
|
|
338
|
-
arrObjectsUniqueKeys,
|
|
339
|
-
arrRemove,
|
|
340
|
-
arrRemoveDuplicates,
|
|
341
|
-
arrRemoveMutable,
|
|
342
|
-
arrShuffle,
|
|
343
|
-
arrSortNumeric,
|
|
344
|
-
arrSortedInsertionIndex,
|
|
345
|
-
arrSum,
|
|
346
|
-
arrSwap,
|
|
347
|
-
arrTableAssertRowsSameLength,
|
|
348
|
-
arrTableEachToString,
|
|
349
|
-
arrTableIterateAsObjects,
|
|
350
|
-
arrTableRemoveColumns,
|
|
351
|
-
arrTableToCsv,
|
|
352
|
-
arrTableToObjects,
|
|
353
|
-
arrayToString,
|
|
354
|
-
};
|
|
321
|
+
export { arrAverage, arrEachToString, arrFindIndicesOf, arrGetOrDefault, arrHasDuplicates, arrIndicesOf, arrLast, arrMapMutable, arrObjectsToTable, arrObjectsUniqueKeys, arrRemove, arrRemoveDuplicates, arrRemoveMutable, arrShuffle, arrSortNumeric, arrSortedInsertionIndex, arrSum, arrSwap, arrTableAssertRowsSameLength, arrTableEachToString, arrTableIterateAsObjects, arrTableRemoveColumns, arrTableToCsv, arrTableToObjects, arrayToString };
|