mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 07:44:48 -05:00
fix(node/buffer): make slice be the same as subarray (#19481)
This commit is contained in:
parent
7e81d3c876
commit
92e7287f4a
3 changed files with 14 additions and 25 deletions
|
@ -47,6 +47,7 @@ util::unit_test_factory!(
|
|||
_fs_watch_test = _fs / _fs_watch_test,
|
||||
_fs_write_test = _fs / _fs_write_test,
|
||||
async_hooks_test,
|
||||
buffer_test,
|
||||
child_process_test,
|
||||
crypto_cipher_test = crypto / crypto_cipher_test,
|
||||
crypto_hash_test = crypto / crypto_hash_test,
|
||||
|
|
12
cli/tests/unit_node/buffer_test.ts
Normal file
12
cli/tests/unit_node/buffer_test.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
import { Buffer } from "node:buffer";
|
||||
import { assertEquals } from "../../../test_util/std/testing/asserts.ts";
|
||||
|
||||
Deno.test({
|
||||
name: "[node/buffer] slice with infinity returns empty buffer",
|
||||
fn() {
|
||||
const buf = Buffer.from([1, 2, 3, 4, 5]);
|
||||
assertEquals(buf.slice(Infinity).length, 0);
|
||||
},
|
||||
});
|
|
@ -860,31 +860,7 @@ function _hexSlice(buf, start, end) {
|
|||
}
|
||||
|
||||
Buffer.prototype.slice = function slice(start, end) {
|
||||
const len = this.length;
|
||||
start = ~~start;
|
||||
end = end === void 0 ? len : ~~end;
|
||||
if (start < 0) {
|
||||
start += len;
|
||||
if (start < 0) {
|
||||
start = 0;
|
||||
}
|
||||
} else if (start > len) {
|
||||
start = len;
|
||||
}
|
||||
if (end < 0) {
|
||||
end += len;
|
||||
if (end < 0) {
|
||||
end = 0;
|
||||
}
|
||||
} else if (end > len) {
|
||||
end = len;
|
||||
}
|
||||
if (end < start) {
|
||||
end = start;
|
||||
}
|
||||
const newBuf = this.subarray(start, end);
|
||||
Object.setPrototypeOf(newBuf, Buffer.prototype);
|
||||
return newBuf;
|
||||
return this.subarray(start, end);
|
||||
};
|
||||
|
||||
Buffer.prototype.readUintLE = Buffer.prototype.readUIntLE = function readUIntLE(
|
||||
|
|
Loading…
Reference in a new issue