@deno/kv
Version:
A Deno KV client library optimized for Node.js.
46 lines (45 loc) • 1.27 kB
JavaScript
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
export function concat(...buf) {
/**
* @todo(iuioiua): Revert to the old implementation upon removal of the
* spread signatures.
*
* @see {@link https://github.com/denoland/deno_std/blob/e6c61ba64d547b60076422bbc1f6ad33184cc10a/bytes/concat.ts}
*/
// No need to concatenate if there is only one element in array or sub-array
if (buf.length === 1) {
if (!Array.isArray(buf[0])) {
return buf[0];
}
else if (buf[0].length === 1) {
return buf[0][0];
}
}
let length = 0;
for (const b of buf) {
if (Array.isArray(b)) {
for (const b1 of b) {
length += b1.length;
}
}
else {
length += b.length;
}
}
const output = new Uint8Array(length);
let index = 0;
for (const b of buf) {
if (Array.isArray(b)) {
for (const b1 of b) {
output.set(b1, index);
index += b1.length;
}
}
else {
output.set(b, index);
index += b.length;
}
}
return output;
}