Add a merge function
This commit is contained in:
@@ -28,3 +28,21 @@ export const firstNonEmpty = (ss: (string | undefined)[]) => {
|
||||
for (const s of ss) if (s && s.length > 0) return s;
|
||||
return undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* Merge the given array of {@link Uint8Array}s in order into a single
|
||||
* {@link Uint8Array}.
|
||||
*
|
||||
* @param as An array of {@link Uint8Array}.
|
||||
*/
|
||||
export const mergeUint8Arrays = (as: Uint8Array[]) => {
|
||||
// A longer but better performing replacement of
|
||||
//
|
||||
// new Uint8Array(as.reduce((acc, x) => acc.concat(...x), []))
|
||||
//
|
||||
|
||||
const len = as.reduce((len, xs) => len + xs.length, 0);
|
||||
const result = new Uint8Array(len);
|
||||
as.reduce((n, xs) => (result.set(xs, n), n + xs.length), 0);
|
||||
return result;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user