@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 +368 -0
- package/index.mjs +354 -0
- package/package.json +11 -14
- package/README.md +0 -3
- package/dist/arrAverage.d.ts +0 -12
- package/dist/arrEachToString.d.ts +0 -12
- package/dist/arrFindIndicesOf.d.ts +0 -7
- package/dist/arrGetOrDefault.d.ts +0 -4
- package/dist/arrHasDuplicates.d.ts +0 -7
- package/dist/arrIndicesOf.d.ts +0 -14
- package/dist/arrLast.d.ts +0 -12
- package/dist/arrMapMutable.d.ts +0 -12
- package/dist/arrObjectsToTable.d.ts +0 -22
- package/dist/arrObjectsUniqueKeys.d.ts +0 -16
- package/dist/arrRemove.d.ts +0 -4
- package/dist/arrRemoveDuplicates.d.ts +0 -13
- package/dist/arrRemoveMutable.d.ts +0 -4
- package/dist/arrShuffle.d.ts +0 -13
- package/dist/arrSortNumeric.d.ts +0 -14
- package/dist/arrSortedInsertionIndex.d.ts +0 -16
- package/dist/arrSum.d.ts +0 -11
- package/dist/arrSwap.d.ts +0 -14
- package/dist/arrTableAssertRowsSameLength.d.ts +0 -25
- package/dist/arrTableEachToString.d.ts +0 -12
- package/dist/arrTableIterateAsObjects.d.ts +0 -4
- package/dist/arrTableRemoveColumns.d.ts +0 -7
- package/dist/arrTableToCsv.d.ts +0 -20
- package/dist/arrTableToObjects.d.ts +0 -22
- package/dist/arrayToString.d.ts +0 -4
- package/dist/index.d.ts +0 -56
- package/dist/index.mjs +0 -442
- package/dist/index.mjs.map +0 -7
package/index.mjs
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => {
|
|
3
|
+
return __defProp(target, 'name', { value, configurable: true });
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
// src/lib/arrSum.ts
|
|
7
|
+
function arrSum(array) {
|
|
8
|
+
return array.reduce((acc, cur) => {
|
|
9
|
+
return acc + cur;
|
|
10
|
+
}, 0);
|
|
11
|
+
}
|
|
12
|
+
__name(arrSum, 'arrSum');
|
|
13
|
+
|
|
14
|
+
// src/lib/arrAverage.ts
|
|
15
|
+
function arrAverage(array) {
|
|
16
|
+
if (!array.length) {
|
|
17
|
+
throw new Error('Cannot take an average of zero values.');
|
|
18
|
+
}
|
|
19
|
+
return arrSum(array) / array.length;
|
|
20
|
+
}
|
|
21
|
+
__name(arrAverage, 'arrAverage');
|
|
22
|
+
|
|
23
|
+
// src/lib/arrEachToString.ts
|
|
24
|
+
function arrEachToString(array) {
|
|
25
|
+
return array.map((element) => {
|
|
26
|
+
return `${element}`;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
__name(arrEachToString, 'arrEachToString');
|
|
30
|
+
|
|
31
|
+
// src/lib/arrFindIndicesOf.ts
|
|
32
|
+
function arrFindIndicesOf(input, predicate) {
|
|
33
|
+
const result = [];
|
|
34
|
+
for (let i = 0; i < input.length; i++) {
|
|
35
|
+
if (predicate(input[i])) {
|
|
36
|
+
result.push(i);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
__name(arrFindIndicesOf, 'arrFindIndicesOf');
|
|
42
|
+
|
|
43
|
+
// src/lib/arrGetOrDefault.ts
|
|
44
|
+
function arrGetOrDefault(array, index, factory) {
|
|
45
|
+
const value = array[index];
|
|
46
|
+
if (value !== void 0 || Object.hasOwn(array, index)) {
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
return (array[index] = factory(index));
|
|
50
|
+
}
|
|
51
|
+
__name(arrGetOrDefault, 'arrGetOrDefault');
|
|
52
|
+
|
|
53
|
+
// src/lib/arrHasDuplicates.ts
|
|
54
|
+
function arrHasDuplicates(arr) {
|
|
55
|
+
const seen = /* @__PURE__ */ new Set();
|
|
56
|
+
for (const e of arr) {
|
|
57
|
+
if (seen.has(e)) {
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
seen.add(e);
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
__name(arrHasDuplicates, 'arrHasDuplicates');
|
|
65
|
+
|
|
66
|
+
// src/lib/arrIndicesOf.ts
|
|
67
|
+
function arrIndicesOf(input, element) {
|
|
68
|
+
const result = [];
|
|
69
|
+
for (let i = 0; i < input.length; i++) {
|
|
70
|
+
if (element === input[i]) {
|
|
71
|
+
result.push(i);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
__name(arrIndicesOf, 'arrIndicesOf');
|
|
77
|
+
|
|
78
|
+
// src/lib/arrLast.ts
|
|
79
|
+
function arrLast(array) {
|
|
80
|
+
if (!array.length) {
|
|
81
|
+
throw new Error('Cannot get last element of empty array.');
|
|
82
|
+
}
|
|
83
|
+
return array[array.length - 1];
|
|
84
|
+
}
|
|
85
|
+
__name(arrLast, 'arrLast');
|
|
86
|
+
|
|
87
|
+
// src/lib/arrMapMutable.ts
|
|
88
|
+
function arrMapMutable(input, f) {
|
|
89
|
+
for (let i = 0; i < input.length; i++) {
|
|
90
|
+
input[i] = f(input[i], i, input);
|
|
91
|
+
}
|
|
92
|
+
return input;
|
|
93
|
+
}
|
|
94
|
+
__name(arrMapMutable, 'arrMapMutable');
|
|
95
|
+
|
|
96
|
+
// src/lib/arrObjectsUniqueKeys.ts
|
|
97
|
+
function arrObjectsUniqueKeys(objects) {
|
|
98
|
+
const keys = /* @__PURE__ */ new Set();
|
|
99
|
+
for (const o of objects) {
|
|
100
|
+
for (const key of Object.keys(o)) {
|
|
101
|
+
keys.add(key);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return Array.from(keys);
|
|
105
|
+
}
|
|
106
|
+
__name(arrObjectsUniqueKeys, 'arrObjectsUniqueKeys');
|
|
107
|
+
|
|
108
|
+
// src/lib/arrObjectsToTable.ts
|
|
109
|
+
function arrObjectsToTable(objects, options = {}) {
|
|
110
|
+
const headers = options?.headers?.slice() || arrObjectsUniqueKeys(objects);
|
|
111
|
+
const table = [headers];
|
|
112
|
+
for (const o of objects) {
|
|
113
|
+
const row = headers.map((header) => {
|
|
114
|
+
const value = o[header];
|
|
115
|
+
return value !== void 0 ? value : options.emptyCell;
|
|
116
|
+
});
|
|
117
|
+
table.push(row);
|
|
118
|
+
}
|
|
119
|
+
return table;
|
|
120
|
+
}
|
|
121
|
+
__name(arrObjectsToTable, 'arrObjectsToTable');
|
|
122
|
+
|
|
123
|
+
// src/lib/arrRemove.ts
|
|
124
|
+
function arrRemove(arr, elementToRemove) {
|
|
125
|
+
return arr.filter((element) => {
|
|
126
|
+
return element !== elementToRemove;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
__name(arrRemove, 'arrRemove');
|
|
130
|
+
|
|
131
|
+
// src/lib/arrRemoveDuplicates.ts
|
|
132
|
+
function arrRemoveDuplicates(array) {
|
|
133
|
+
return Array.from(new Set(array));
|
|
134
|
+
}
|
|
135
|
+
__name(arrRemoveDuplicates, 'arrRemoveDuplicates');
|
|
136
|
+
|
|
137
|
+
// src/lib/arrRemoveMutable.ts
|
|
138
|
+
function arrRemoveMutable(arr, elementToRemove) {
|
|
139
|
+
let index = arr.indexOf(elementToRemove);
|
|
140
|
+
while (index !== -1) {
|
|
141
|
+
arr.splice(index, 1);
|
|
142
|
+
index = arr.indexOf(elementToRemove);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
__name(arrRemoveMutable, 'arrRemoveMutable');
|
|
146
|
+
|
|
147
|
+
// src/lib/arrSwap.ts
|
|
148
|
+
function arrSwap(input, from, to) {
|
|
149
|
+
if (from === to) {
|
|
150
|
+
return input;
|
|
151
|
+
}
|
|
152
|
+
[input[from], input[to]] = [input[to], input[from]];
|
|
153
|
+
return input;
|
|
154
|
+
}
|
|
155
|
+
__name(arrSwap, 'arrSwap');
|
|
156
|
+
|
|
157
|
+
// src/lib/arrShuffle.ts
|
|
158
|
+
function arrShuffle(input) {
|
|
159
|
+
if (input.length <= 1) {
|
|
160
|
+
return input;
|
|
161
|
+
}
|
|
162
|
+
const original = input.slice();
|
|
163
|
+
let equal = true;
|
|
164
|
+
while (equal) {
|
|
165
|
+
for (let i = 0; i < input.length; i++) {
|
|
166
|
+
const newIndex = Math.floor(Math.random() * input.length);
|
|
167
|
+
arrSwap(input, i, newIndex);
|
|
168
|
+
}
|
|
169
|
+
equal = input.join(',') === original.join(',');
|
|
170
|
+
}
|
|
171
|
+
return input;
|
|
172
|
+
}
|
|
173
|
+
__name(arrShuffle, 'arrShuffle');
|
|
174
|
+
|
|
175
|
+
// src/lib/arrSortNumeric.ts
|
|
176
|
+
function arrSortNumeric(input) {
|
|
177
|
+
return input.sort((a, b) => {
|
|
178
|
+
if (a < b) {
|
|
179
|
+
return -1;
|
|
180
|
+
}
|
|
181
|
+
if (a > b) {
|
|
182
|
+
return 1;
|
|
183
|
+
}
|
|
184
|
+
return 0;
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
__name(arrSortNumeric, 'arrSortNumeric');
|
|
188
|
+
|
|
189
|
+
// src/lib/arrSortedInsertionIndex.ts
|
|
190
|
+
function arrSortedInsertionIndex(array, value, comparator) {
|
|
191
|
+
let first = 0;
|
|
192
|
+
let count = array.length;
|
|
193
|
+
while (count > 0) {
|
|
194
|
+
const step = Math.trunc(count / 2);
|
|
195
|
+
let it = first + step;
|
|
196
|
+
if (comparator(array[it], value) <= 0) {
|
|
197
|
+
it++;
|
|
198
|
+
first = it;
|
|
199
|
+
count -= step + 1;
|
|
200
|
+
} else {
|
|
201
|
+
count = step;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return first;
|
|
205
|
+
}
|
|
206
|
+
__name(arrSortedInsertionIndex, 'arrSortedInsertionIndex');
|
|
207
|
+
|
|
208
|
+
// src/lib/arrTableAssertRowsSameLength.ts
|
|
209
|
+
function arrTableAssertRowsSameLength(rows, headers) {
|
|
210
|
+
const numHeaders = (headers || rows[0]).length;
|
|
211
|
+
for (const row of rows) {
|
|
212
|
+
if (row.length !== numHeaders) {
|
|
213
|
+
throw new Error(`Expected ${numHeaders} columns, got ${row.length}`);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
__name(arrTableAssertRowsSameLength, 'arrTableAssertRowsSameLength');
|
|
218
|
+
|
|
219
|
+
// src/lib/arrTableEachToString.ts
|
|
220
|
+
function arrTableEachToString(table) {
|
|
221
|
+
return table.map(arrEachToString);
|
|
222
|
+
}
|
|
223
|
+
__name(arrTableEachToString, 'arrTableEachToString');
|
|
224
|
+
|
|
225
|
+
// src/lib/arrTableIterateAsObjects.ts
|
|
226
|
+
function* arrTableIterateAsObjects(rows, headers, ignoreHeaders = /* @__PURE__ */ new Set()) {
|
|
227
|
+
if (!headers.length) {
|
|
228
|
+
throw new Error('No headers provided');
|
|
229
|
+
}
|
|
230
|
+
ignoreHeaders.forEach((h) => {
|
|
231
|
+
if (!headers.includes(h)) {
|
|
232
|
+
throw new Error(`Header "${h}" not found in headers: ${headers}`);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
if (new Set(headers).size === ignoreHeaders.size) {
|
|
236
|
+
throw new Error('All headers are ignored');
|
|
237
|
+
}
|
|
238
|
+
for (let r = 0; r < rows.length; r++) {
|
|
239
|
+
if (rows[r].length !== headers.length) {
|
|
240
|
+
throw new Error(`Row ${r} has ${rows[r].length} columns, but expected ${headers.length}`);
|
|
241
|
+
}
|
|
242
|
+
const o = {};
|
|
243
|
+
for (let c = 0; c < headers.length; c++) {
|
|
244
|
+
if (ignoreHeaders.has(headers[c])) {
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
o[headers[c]] = rows[r][c];
|
|
248
|
+
}
|
|
249
|
+
yield o;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
__name(arrTableIterateAsObjects, 'arrTableIterateAsObjects');
|
|
253
|
+
|
|
254
|
+
// src/lib/arrTableRemoveColumns.ts
|
|
255
|
+
function arrTableRemoveColumns(table, ...removeColumnNames) {
|
|
256
|
+
if (!removeColumnNames.length || !table.length) {
|
|
257
|
+
return table;
|
|
258
|
+
}
|
|
259
|
+
const set = new Set(
|
|
260
|
+
removeColumnNames
|
|
261
|
+
.map((col) => {
|
|
262
|
+
return arrIndicesOf(table[0], col);
|
|
263
|
+
})
|
|
264
|
+
.flat(),
|
|
265
|
+
);
|
|
266
|
+
return table.map((row) => {
|
|
267
|
+
return row.filter((_, i) => {
|
|
268
|
+
return !set.has(i);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
__name(arrTableRemoveColumns, 'arrTableRemoveColumns');
|
|
273
|
+
|
|
274
|
+
// src/lib/arrTableToCsv.ts
|
|
275
|
+
function arrTableToCsv(input, delimiter = ';', replaceLinebreakWith = '<br>') {
|
|
276
|
+
return input
|
|
277
|
+
.map((row) => {
|
|
278
|
+
return row
|
|
279
|
+
.map((item) => {
|
|
280
|
+
return String(item).replace(new RegExp(';', 'g'), '').replace(/\r*\n/g, replaceLinebreakWith);
|
|
281
|
+
})
|
|
282
|
+
.join(delimiter);
|
|
283
|
+
})
|
|
284
|
+
.join('\n');
|
|
285
|
+
}
|
|
286
|
+
__name(arrTableToCsv, 'arrTableToCsv');
|
|
287
|
+
|
|
288
|
+
// src/lib/arrTableToObjects.ts
|
|
289
|
+
function arrTableToObjects(rows, headers, ignoreKeys) {
|
|
290
|
+
if (headers) {
|
|
291
|
+
if (!rows.length) {
|
|
292
|
+
return [];
|
|
293
|
+
}
|
|
294
|
+
} else {
|
|
295
|
+
if (rows.length <= 1) {
|
|
296
|
+
return [];
|
|
297
|
+
}
|
|
298
|
+
headers = rows[0].map((header) => {
|
|
299
|
+
return header === null || header === void 0 ? '' : String(header);
|
|
300
|
+
});
|
|
301
|
+
rows = rows.slice(1);
|
|
302
|
+
}
|
|
303
|
+
const _headers = headers;
|
|
304
|
+
return rows.map((row) => {
|
|
305
|
+
const o = {};
|
|
306
|
+
for (let i = 0; i < _headers.length; i++) {
|
|
307
|
+
const header = _headers[i];
|
|
308
|
+
if (ignoreKeys && ignoreKeys.has(header)) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
o[header] = row[i];
|
|
312
|
+
}
|
|
313
|
+
return o;
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
__name(arrTableToObjects, 'arrTableToObjects');
|
|
317
|
+
|
|
318
|
+
// src/lib/arrayToString.ts
|
|
319
|
+
function arrayToString(array) {
|
|
320
|
+
return `[${array
|
|
321
|
+
.map((item) => {
|
|
322
|
+
return item == null ? String(item) : Array.isArray(item) ? arrayToString(item) : item.toString();
|
|
323
|
+
})
|
|
324
|
+
.join(',')}]`;
|
|
325
|
+
}
|
|
326
|
+
__name(arrayToString, 'arrayToString');
|
|
327
|
+
|
|
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
|
+
};
|
package/package.json
CHANGED
|
@@ -1,35 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bemoje/array",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Array utils",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
|
-
"
|
|
7
|
+
"keywords": [
|
|
8
|
+
"array"
|
|
9
|
+
],
|
|
8
10
|
"exports": {
|
|
9
11
|
".": {
|
|
10
|
-
"types": "./
|
|
11
|
-
"import": "./
|
|
12
|
-
"default": "./
|
|
12
|
+
"types": "./index.d.ts",
|
|
13
|
+
"import": "./index.mjs",
|
|
14
|
+
"default": "./index.mjs"
|
|
13
15
|
}
|
|
14
16
|
},
|
|
15
|
-
"
|
|
16
|
-
"dist/"
|
|
17
|
-
],
|
|
17
|
+
"dependencies": {},
|
|
18
18
|
"publishConfig": {
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
|
-
"scripts": {
|
|
22
|
-
"build": "node esbuild.mjs"
|
|
23
|
-
},
|
|
24
21
|
"license": "MIT",
|
|
25
22
|
"author": {
|
|
26
|
-
"name": "Benjamin
|
|
23
|
+
"name": "Benjamin Møller Jensen",
|
|
27
24
|
"email": "bemoje@bemoje.net",
|
|
28
25
|
"url": "https://github.com/bemoje/"
|
|
29
26
|
},
|
|
30
27
|
"repository": {
|
|
31
28
|
"type": "git",
|
|
32
|
-
"url": "https://github.com/bemoje/mono.git",
|
|
29
|
+
"url": "git+https://github.com/bemoje/mono.git",
|
|
33
30
|
"directory": "libs/array"
|
|
34
31
|
}
|
|
35
32
|
}
|
package/README.md
DELETED
package/dist/arrAverage.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
export declare function arrAverage(array: number[]): number;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Coerce each element of an array to string.
|
|
3
|
-
* @template T - The type of elements in the input array.
|
|
4
|
-
* @returns A new array where each element is the string representation of the corresponding element in the input array.
|
|
5
|
-
* @param array The array to iterate over.
|
|
6
|
-
* @example ```ts
|
|
7
|
-
* const numbers = [1, 2, 3];
|
|
8
|
-
* arrEachToString(numbers);
|
|
9
|
-
* //=> ['1', '2', '3']
|
|
10
|
-
* ```
|
|
11
|
-
*/
|
|
12
|
-
export declare function arrEachToString<T>(array: T[]): string[];
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns an array of indices where the predicate function returns true for the corresponding element in the input array.
|
|
3
|
-
* @param input - The array to search.
|
|
4
|
-
* @param predicate - The function to test each element of the array.
|
|
5
|
-
* @returns An array of indices where the predicate function returns true.
|
|
6
|
-
*/
|
|
7
|
-
export declare function arrFindIndicesOf<T>(input: Array<T>, predicate: (value: T) => boolean): number[];
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks if an array has any duplicate elements.
|
|
3
|
-
* @param arr - The array to check for duplicates.
|
|
4
|
-
* @returns A boolean indicating whether the array has duplicates.
|
|
5
|
-
* @typeParam T - The type of elements in the array.
|
|
6
|
-
*/
|
|
7
|
-
export declare function arrHasDuplicates<T>(arr: T[]): boolean;
|
package/dist/arrIndicesOf.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns all indexes at which an element is found.
|
|
3
|
-
* @param input The array to search
|
|
4
|
-
* @template T - The type of elements in the input array.
|
|
5
|
-
* @returns An array of indices where the specified element can be found.
|
|
6
|
-
* @param element The element to find
|
|
7
|
-
* @example ```ts
|
|
8
|
-
* const inputArray = [1, 2, 3, 2, 4, 2, 5];
|
|
9
|
-
* const elementToFind = 2;
|
|
10
|
-
* arrIndicesOf(inputArray, elementToFind);
|
|
11
|
-
* //=> [1, 3, 5]
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
|
-
export declare function arrIndicesOf<T>(input: Array<T>, element: T): number[];
|
package/dist/arrLast.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns the last element of an array.
|
|
3
|
-
* Throws an error if the array is empty.
|
|
4
|
-
* @template T The type of elements in the array.
|
|
5
|
-
* @param array The array to get the last element from.
|
|
6
|
-
* @returns The last element of the array.
|
|
7
|
-
* @throws If the array is empty.
|
|
8
|
-
* @example const numbers = [1, 2, 3, 4, 5];
|
|
9
|
-
* const lastNumber = arrLast(numbers);
|
|
10
|
-
* //=> 5
|
|
11
|
-
*/
|
|
12
|
-
export declare function arrLast<T>(array: T[]): T;
|
package/dist/arrMapMutable.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 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.
|
|
3
|
-
* @template T The type of elements in the input array.
|
|
4
|
-
* @param input The array to be mapped over.
|
|
5
|
-
* @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.
|
|
6
|
-
* @returns The original array, mutated by the callback function.
|
|
7
|
-
* @example ```ts
|
|
8
|
-
* arrMapMutable([1, 2, 3], (value: number) => value * 2);
|
|
9
|
-
* //=> [2, 4, 6]
|
|
10
|
-
* ```
|
|
11
|
-
*/
|
|
12
|
-
export declare function arrMapMutable<T>(input: Array<T>, f: (value: T, index: number, array: T[]) => T): Array<T>;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Convert an array of objects to a two-dimensional table.
|
|
3
|
-
* @param objects The array of objects to convert to a table.
|
|
4
|
-
* @template T - The type of the values in the objects.
|
|
5
|
-
* @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.
|
|
6
|
-
* @param options.emptyCell An optional value to use for empty cells. If not provided, the function will use `undefined`.
|
|
7
|
-
* @returns A 2D array (table) where each row represents an object and each column represents a property of the object.
|
|
8
|
-
* @param options The options for converting the objects to a table.
|
|
9
|
-
* @example ```ts
|
|
10
|
-
* arrObjectsToTable(
|
|
11
|
-
* [
|
|
12
|
-
* { a: 1, b: 2 },
|
|
13
|
-
* { a: 3, b: 4, c: 5 },
|
|
14
|
-
* ],
|
|
15
|
-
* { emptyCell:1 },
|
|
16
|
-
* ) //=> [ [ 'a', 'b', 'c' ], [ 1, 2,1 ], [ 3, 4, 5 ] ]
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
export declare function arrObjectsToTable<T, E>(objects: Record<string, T | undefined>[], options?: {
|
|
20
|
-
headers?: string[];
|
|
21
|
-
emptyCell?: E;
|
|
22
|
-
}): Array<Array<string | T | E>>;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns an array of all unique object keys found in an array of objects.
|
|
3
|
-
* @template T - The type of values in the input objects.
|
|
4
|
-
* @returns An array of unique keys present in the input objects.
|
|
5
|
-
* @param objects The array of objects.
|
|
6
|
-
* @example ```ts
|
|
7
|
-
* const objects = [
|
|
8
|
-
* { name: 'John', age: 25 },
|
|
9
|
-
* { name: 'Jane', gender: 'female' },
|
|
10
|
-
* { name: 'Bob', age: 30, gender: 'male' },
|
|
11
|
-
* ];
|
|
12
|
-
* arrObjectsUniqueKeys(objects);
|
|
13
|
-
* //=> ['name', 'age', 'gender']
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
export declare function arrObjectsUniqueKeys<T>(objects: Record<string, T>[]): string[];
|
package/dist/arrRemove.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Remove duplicates from an array
|
|
3
|
-
* @remarks This function uses the JavaScript Set object to remove duplicate values from an array.
|
|
4
|
-
* @typeparam T - The type of elements in the array.
|
|
5
|
-
* @returns The new array with duplicates removed.
|
|
6
|
-
* @param array The array from which to remove duplicates.
|
|
7
|
-
* @example ```ts
|
|
8
|
-
* const array = [1, 2, 2, 3, 4, 4, 5];
|
|
9
|
-
* arrRemoveDuplicates(array);
|
|
10
|
-
* //=> [1, 2, 3, 4, 5]
|
|
11
|
-
* ```
|
|
12
|
-
*/
|
|
13
|
-
export declare function arrRemoveDuplicates<T>(array: T[]): T[];
|
package/dist/arrShuffle.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shuffle items in an array in-place. Guarantees changes.
|
|
3
|
-
* @remarks This function does not guarantee that the order of the elements will be different after shuffling.
|
|
4
|
-
* @typeparam T - The type of the elements in the input array.
|
|
5
|
-
* @returns The same array, but shuffled.
|
|
6
|
-
* @param input The array to shuffle.
|
|
7
|
-
* @example ```ts
|
|
8
|
-
* const input = [1, 2, 3, 4, 5];
|
|
9
|
-
* arrShuffle(input);
|
|
10
|
-
* //=> [3, 1, 5, 2, 4]
|
|
11
|
-
* ```
|
|
12
|
-
*/
|
|
13
|
-
export declare function arrShuffle<T>(input: Array<T>): Array<T>;
|
package/dist/arrSortNumeric.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sorts an array of numbers, bigints, or booleans in ascending order.
|
|
3
|
-
* @returns The sorted array.
|
|
4
|
-
* @remarks This function uses the JavaScript `Array.prototype.sort()` method, which sorts elements in place.
|
|
5
|
-
* Therefore, the original array will be modified.
|
|
6
|
-
* @throws If any element in the input array is not a number, bigint, or boolean.
|
|
7
|
-
* @param input The array to be sorted.
|
|
8
|
-
* @example ```ts
|
|
9
|
-
* const input = [5, 2n, true, 10, false];
|
|
10
|
-
* arrSortNumeric(input);
|
|
11
|
-
* //=> [false, true, 2n, 5, 10]
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
|
-
export declare function arrSortNumeric(input: Array<number | bigint | boolean>): Array<number | bigint | boolean>;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns an index in the sorted array where the specified value could be inserted while maintaining the sorted order of the array.
|
|
3
|
-
* If the element is already in the array, returns the index after the last instance of the element.
|
|
4
|
-
* @param array - The sorted array to search.
|
|
5
|
-
* @param value - The value to locate in the array.
|
|
6
|
-
* @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.
|
|
7
|
-
* @returns The index at which the value could be inserted into array to maintain the array's sorted order.
|
|
8
|
-
* @example ```ts
|
|
9
|
-
* const array = [1, 2, 3, 5, 6];
|
|
10
|
-
* const value = 4;
|
|
11
|
-
* const comparator = (a, b) => a - b;
|
|
12
|
-
* const index = arrSortedLowerBound(array, value, comparator);
|
|
13
|
-
* console.log(index); // Output: 3
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
export declare function arrSortedInsertionIndex<T>(array: readonly T[], value: T, comparator: (a: T, b: T) => number): number;
|
package/dist/arrSum.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Calculates the sum of an array of numbers.
|
|
3
|
-
* @returns The sum of all numbers in the array.
|
|
4
|
-
* @param array The array of numbers to sum.
|
|
5
|
-
* @example ```ts
|
|
6
|
-
* const numbers = [1, 2, 3, 4, 5];
|
|
7
|
-
* arrSum(numbers);
|
|
8
|
-
* //=> 15
|
|
9
|
-
* ```
|
|
10
|
-
*/
|
|
11
|
-
export declare function arrSum(array: number[]): number;
|
package/dist/arrSwap.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Swaps two elements in an array. This function takes an input array and swaps the elements at the specified indices.
|
|
3
|
-
* @param to The index of the element to swap to.
|
|
4
|
-
* @param from The index of the element to swap from.
|
|
5
|
-
* @template T - The type of elements in the array.
|
|
6
|
-
* @returns The modified array with swapped elements.
|
|
7
|
-
* @throws Will throw an error if 'from' or 'to' is not a valid index in the array.
|
|
8
|
-
* @param input The input array.
|
|
9
|
-
* @example ```ts
|
|
10
|
-
* const arr = [1, 2, 3, 4, 5]
|
|
11
|
-
* arrSwap(arr, 1, 3) //=> [1, 4, 3, 2, 5]
|
|
12
|
-
* ```
|
|
13
|
-
*/
|
|
14
|
-
export declare function arrSwap<T>(input: Array<T>, from: number, to: number): Array<T>;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Asserts that all rows in a 2D array have the same length.
|
|
3
|
-
* @param - Optional array of headers to compare the row length against.
|
|
4
|
-
* @throws If any row in the array has a different length than the others.
|
|
5
|
-
* @param headers Optional. An array of headers. If provided, each row must have the same length as this array.
|
|
6
|
-
* @typeparam T - The type of elements in the rows.
|
|
7
|
-
* @param rows The 2D array to check.
|
|
8
|
-
* @example ```ts
|
|
9
|
-
* const rows = [
|
|
10
|
-
* [1, 2, 3],
|
|
11
|
-
* [4, 5, 6],
|
|
12
|
-
* [7, 8, 9],
|
|
13
|
-
* ];
|
|
14
|
-
* arrTableAssertRowsSameLength(rows);
|
|
15
|
-
* //=> undefined
|
|
16
|
-
* const rowsWithDifferentLength = [
|
|
17
|
-
* [1, 2, 3],
|
|
18
|
-
* [4, 5],
|
|
19
|
-
* [7, 8, 9],
|
|
20
|
-
* ];
|
|
21
|
-
* arrTableAssertRowsSameLength(rowsWithDifferentLength);
|
|
22
|
-
* //=> Error: Expected 3 columns, got 2
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
export declare function arrTableAssertRowsSameLength<T>(rows: T[][], headers?: string[]): void;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Coerce each value of a 2D array table to string.
|
|
3
|
-
* @template T - The type of the elements in the input array.
|
|
4
|
-
* @returns The converted 2D array where each element is a string.
|
|
5
|
-
* @param table The 2D array to convert.
|
|
6
|
-
* @example ```ts
|
|
7
|
-
* const input: number[][] = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
|
|
8
|
-
* arrTableEachToString(input);
|
|
9
|
-
* //=> [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]
|
|
10
|
-
* ```
|
|
11
|
-
*/
|
|
12
|
-
export declare function arrTableEachToString<T>(table: T[][]): string[][];
|