mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 18:28:02 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @name arrayFlatten
|
||||
* @summary Merge T[][] into T[]
|
||||
* @description
|
||||
* Returns a new array with all arrays merged into one
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { arrayFlatten } from '@pezkuwi/util';
|
||||
*
|
||||
* arrayFlatten([[1, 2], [3, 4], [5]]); // [1, 2, 3, 4, 5]
|
||||
* ```
|
||||
*/
|
||||
export function arrayFlatten(arrays) {
|
||||
const num = arrays.length;
|
||||
// shortcuts for the empty & single-entry case
|
||||
if (num === 0) {
|
||||
return [];
|
||||
}
|
||||
else if (num === 1) {
|
||||
return arrays[0];
|
||||
}
|
||||
// pre-allocate based on the combined size
|
||||
let size = 0;
|
||||
for (let i = 0; i < num; i++) {
|
||||
size += arrays[i].length;
|
||||
}
|
||||
const output = new Array(size);
|
||||
let i = -1;
|
||||
for (let j = 0; j < num; j++) {
|
||||
const a = arrays[j];
|
||||
// instead of pushing, we just set the entries
|
||||
for (let e = 0, count = a.length; e < count; e++) {
|
||||
output[++i] = a[e];
|
||||
}
|
||||
}
|
||||
return output;
|
||||
}
|
||||
Reference in New Issue
Block a user