From 022dbd5d9115992722c50043b930b2dbafa94b43 Mon Sep 17 00:00:00 2001 From: FUJI Goro Date: Fri, 7 Jun 2019 08:11:59 +0900 Subject: [PATCH] @types/node: add doc to Buffer#slice and Buffer#subarray (and fix the type of Buffer#subarray) (#35998) * add doc to Buffer#slice and Buffer#subarray * add tests for Buffer#subarray() --- types/node/globals.d.ts | 20 ++++++++++++++++++-- types/node/test/buffer.ts | 2 ++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/types/node/globals.d.ts b/types/node/globals.d.ts index f2ac48fc26..d53ebd9d8f 100644 --- a/types/node/globals.d.ts +++ b/types/node/globals.d.ts @@ -244,8 +244,24 @@ interface Buffer extends Uint8Array { equals(otherBuffer: Uint8Array): boolean; compare(otherBuffer: Uint8Array, targetStart?: number, targetEnd?: number, sourceStart?: number, sourceEnd?: number): number; copy(targetBuffer: Uint8Array, targetStart?: number, sourceStart?: number, sourceEnd?: number): number; - slice(start?: number, end?: number): Buffer; - subarray(begin: number, end?: number): Buffer; + /** + * Returns a new `Buffer` that references **the same memory as the original**, but offset and cropped by the start and end indices. + * + * This method is incompatible with `Uint8Array#slice()`, which returns a copy of the original memory. + * + * @param begin Where the new `Buffer` will start. Default: `0`. + * @param end Where the new `Buffer` will end (not inclusive). Default: `buf.length`. + */ + slice(begin?: number, end?: number): Buffer; + /** + * Returns a new `Buffer` that references **the same memory as the original**, but offset and cropped by the start and end indices. + * + * This method is compatible with `Uint8Array#subarray()`. + * + * @param begin Where the new `Buffer` will start. Default: `0`. + * @param end Where the new `Buffer` will end (not inclusive). Default: `buf.length`. + */ + subarray(begin?: number, end?: number): Buffer; writeUIntLE(value: number, offset: number, byteLength: number): number; writeUIntBE(value: number, offset: number, byteLength: number): number; writeIntLE(value: number, offset: number, byteLength: number): number; diff --git a/types/node/test/buffer.ts b/types/node/test/buffer.ts index dd3329ba24..3509fe46a8 100644 --- a/types/node/test/buffer.ts +++ b/types/node/test/buffer.ts @@ -207,7 +207,9 @@ b.fill('a').fill('b'); { const b = Buffer.from('asd'); let res: Buffer = b.reverse(); + res = b.subarray(); res = b.subarray(1); + res = b.subarray(1, 2); } // Buffer module, transcode function