mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 09:31:22 -05:00
feat(std/node): Add Buffer.allocUnsafe (#6533)
This commit is contained in:
parent
2da0840583
commit
d779053dc6
2 changed files with 23 additions and 2 deletions
|
@ -60,8 +60,9 @@ export default class Buffer extends Uint8Array {
|
|||
if (typeof fill === "string" && fill.length === 1 && encoding === "utf8")
|
||||
buf.fill(fill.charCodeAt(0));
|
||||
else bufFill = Buffer.from(fill, encoding);
|
||||
} else if (typeof fill === "number") buf.fill(fill);
|
||||
else if (fill instanceof Uint8Array) {
|
||||
} else if (typeof fill === "number") {
|
||||
buf.fill(fill);
|
||||
} else if (fill instanceof Uint8Array) {
|
||||
if (fill.length === 0) {
|
||||
throw new TypeError(
|
||||
`The argument "value" is invalid. Received ${fill.constructor.name} []`
|
||||
|
@ -89,6 +90,10 @@ export default class Buffer extends Uint8Array {
|
|||
return buf;
|
||||
}
|
||||
|
||||
static allocUnsafe(size: number): Buffer {
|
||||
return new Buffer(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the byte length of a string when encoded. This is not the same as
|
||||
* String.prototype.length, which does not account for the encoding that is
|
||||
|
|
|
@ -96,6 +96,22 @@ Deno.test({
|
|||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "allocUnsafe allocates a buffer with the expected size",
|
||||
fn() {
|
||||
const buffer: Buffer = Buffer.allocUnsafe(1);
|
||||
assertEquals(buffer.length, 1, "Buffer size should be 1");
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "allocUnsafe(0) creates an empty buffer",
|
||||
fn() {
|
||||
const buffer: Buffer = Buffer.allocUnsafe(0);
|
||||
assertEquals(buffer.length, 0, "Buffer size should be 0");
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "alloc filled correctly with integer",
|
||||
fn() {
|
||||
|
|
Loading…
Add table
Reference in a new issue