mirror of
https://github.com/denoland/std.git
synced 2024-11-22 04:59:05 +00:00
BREAKING(bytes): remove spread concat()
overload (#3854)
This commit is contained in:
parent
f685e27364
commit
e21c1fc903
@ -13,62 +13,16 @@
|
||||
* concat([a, b]); // Uint8Array(6) [ 0, 1, 2, 3, 4, 5 ]
|
||||
* ```
|
||||
*/
|
||||
export function concat(buf: Uint8Array[]): Uint8Array;
|
||||
/**
|
||||
* @deprecated (will be removed in 0.209.0) Pass in an array instead of a
|
||||
* spread of arguments.
|
||||
*
|
||||
* Concatenate an array of {@linkcode Uint8Array}s.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { concat } from "https://deno.land/std@$STD_VERSION/bytes/concat.ts";
|
||||
*
|
||||
* const a = new Uint8Array([0, 1, 2]);
|
||||
* const b = new Uint8Array([3, 4, 5]);
|
||||
* concat(a, b); // Uint8Array(6) [ 0, 1, 2, 3, 4, 5 ]
|
||||
* ```
|
||||
*/
|
||||
export function concat(...buf: Uint8Array[]): Uint8Array;
|
||||
export function concat(...buf: (Uint8Array | Uint8Array[])[]): Uint8Array {
|
||||
/**
|
||||
* @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];
|
||||
}
|
||||
}
|
||||
|
||||
export function concat(buf: Uint8Array[]): Uint8Array {
|
||||
let length = 0;
|
||||
for (const b of buf) {
|
||||
if (Array.isArray(b)) {
|
||||
for (const b1 of b) {
|
||||
length += b1.length;
|
||||
}
|
||||
} else {
|
||||
length += b.length;
|
||||
}
|
||||
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;
|
||||
}
|
||||
output.set(b, index);
|
||||
index += b.length;
|
||||
}
|
||||
|
||||
return output;
|
||||
|
@ -45,38 +45,3 @@ Deno.test("[bytes] concat an array of Uint8Array", () => {
|
||||
const expected = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||
assertEquals(joined, expected);
|
||||
});
|
||||
|
||||
Deno.test("[bytes] concat multiple arrays of Uint8Array using spread operator", () => {
|
||||
const a = [new Uint8Array([0, 1, 2, 3]), new Uint8Array([4, 5, 6, 7, 8, 9])];
|
||||
const b = [
|
||||
new Uint8Array([10, 11]),
|
||||
new Uint8Array([12, 13]),
|
||||
new Uint8Array([14, 15]),
|
||||
new Uint8Array([16]),
|
||||
new Uint8Array([17, 18, 19]),
|
||||
];
|
||||
const joined = concat(...a, ...b);
|
||||
const expected = new Uint8Array([
|
||||
0,
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8,
|
||||
9,
|
||||
10,
|
||||
11,
|
||||
12,
|
||||
13,
|
||||
14,
|
||||
15,
|
||||
16,
|
||||
17,
|
||||
18,
|
||||
19,
|
||||
]);
|
||||
assertEquals(joined, expected);
|
||||
});
|
||||
|
@ -235,7 +235,7 @@ export class DelimiterStream extends TransformStream<Uint8Array, Uint8Array> {
|
||||
// Concat not needed when a single buffer is passed.
|
||||
controller.enqueue(bufs[0]);
|
||||
} else {
|
||||
controller.enqueue(concat(...bufs));
|
||||
controller.enqueue(concat(bufs));
|
||||
}
|
||||
// Drop all previous chunks.
|
||||
bufs.length = 0;
|
||||
@ -249,7 +249,7 @@ export class DelimiterStream extends TransformStream<Uint8Array, Uint8Array> {
|
||||
} else if (delimitedChunkEnd > 0 && bufs.length > 0) {
|
||||
// Previous chunks and current chunk together form a delimited chunk.
|
||||
const chunkSliced = chunk.subarray(chunkStart, delimitedChunkEnd);
|
||||
const result = concat(...bufs, chunkSliced);
|
||||
const result = concat([...bufs, chunkSliced]);
|
||||
bufs.length = 0;
|
||||
chunkStart = disposition === "prefix"
|
||||
? delimitedChunkEnd
|
||||
|
Loading…
Reference in New Issue
Block a user