From 91767501e127a0de40d34d38bf769702fcb7c942 Mon Sep 17 00:00:00 2001 From: Marcos Casagrande Date: Mon, 6 Jul 2020 00:01:36 +0200 Subject: feat(std/node): add buf.equals method (#6640) --- std/node/buffer.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'std/node/buffer.ts') diff --git a/std/node/buffer.ts b/std/node/buffer.ts index 9ec7c04ff..0fc8be6f1 100644 --- a/std/node/buffer.ts +++ b/std/node/buffer.ts @@ -200,6 +200,26 @@ export default class Buffer extends Uint8Array { return sourceBuffer.length; } + /* + * Returns true if both buf and otherBuffer have exactly the same bytes, false otherwise. + */ + equals(otherBuffer: Uint8Array | Buffer): boolean { + if (!(otherBuffer instanceof Uint8Array)) { + throw new TypeError( + `The "otherBuffer" argument must be an instance of Buffer or Uint8Array. Received type ${typeof otherBuffer}` + ); + } + + if (this === otherBuffer) return true; + if (this.byteLength !== otherBuffer.byteLength) return false; + + for (let i = 0; i < this.length; i++) { + if (this[i] !== otherBuffer[i]) return false; + } + + return true; + } + readBigInt64BE(offset = 0): bigint { return new DataView( this.buffer, -- cgit v1.2.3