From 0c539faac39644fbeefd9497e548efab04cf478c Mon Sep 17 00:00:00 2001 From: Timothy Gu Date: Mon, 3 Apr 2017 18:29:41 -0700 Subject: [PATCH] test: add common.getArrayBufferViews(buf) PR-URL: https://github.com/nodejs/node/pull/12223 Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Anna Henningsen --- test/README.md | 6 ++++++ test/common.js | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/test/README.md b/test/README.md index a9ea434a23..9a3edd067a 100644 --- a/test/README.md +++ b/test/README.md @@ -220,6 +220,12 @@ The expected error should be [subclassed by the `internal/errors` module](https: Tests whether `name` and `expected` are part of a raised warning. +## getArrayBufferViews(buf) +* `buf` [<Buffer>](https://nodejs.org/api/buffer.html#buffer_class_buffer) +* return [<ArrayBufferView[]>](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView) + +Returns an instance of all possible `ArrayBufferView`s of the provided Buffer. + ### hasCrypto * return [<Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) diff --git a/test/common.js b/test/common.js index e77ee100cb..ba377bebd1 100644 --- a/test/common.js +++ b/test/common.js @@ -654,3 +654,29 @@ exports.skipIfInspectorDisabled = function skipIfInspectorDisabled() { process.exit(0); } }; + +const arrayBufferViews = [ + Int8Array, + Uint8Array, + Uint8ClampedArray, + Int16Array, + Uint16Array, + Int32Array, + Uint32Array, + Float32Array, + Float64Array, + DataView +]; + +exports.getArrayBufferViews = function getArrayBufferViews(buf) { + const { buffer, byteOffset, byteLength } = buf; + + const out = []; + for (const type of arrayBufferViews) { + const { BYTES_PER_ELEMENT = 1 } = type; + if (byteLength % BYTES_PER_ELEMENT === 0) { + out.push(new type(buffer, byteOffset, byteLength / BYTES_PER_ELEMENT)); + } + } + return out; +};