Skip to content

Commit 0c539fa

Browse files
committed
test: add common.getArrayBufferViews(buf)
PR-URL: #12223 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent faa447b commit 0c539fa

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

test/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,12 @@ The expected error should be [subclassed by the `internal/errors` module](https:
220220

221221
Tests whether `name` and `expected` are part of a raised warning.
222222

223+
## getArrayBufferViews(buf)
224+
* `buf` [&lt;Buffer>](https://nodejs.org/api/buffer.html#buffer_class_buffer)
225+
* return [&lt;ArrayBufferView&#91;&#93;>](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView)
226+
227+
Returns an instance of all possible `ArrayBufferView`s of the provided Buffer.
228+
223229
### hasCrypto
224230
* return [&lt;Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)
225231

test/common.js

+26
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,29 @@ exports.skipIfInspectorDisabled = function skipIfInspectorDisabled() {
654654
process.exit(0);
655655
}
656656
};
657+
658+
const arrayBufferViews = [
659+
Int8Array,
660+
Uint8Array,
661+
Uint8ClampedArray,
662+
Int16Array,
663+
Uint16Array,
664+
Int32Array,
665+
Uint32Array,
666+
Float32Array,
667+
Float64Array,
668+
DataView
669+
];
670+
671+
exports.getArrayBufferViews = function getArrayBufferViews(buf) {
672+
const { buffer, byteOffset, byteLength } = buf;
673+
674+
const out = [];
675+
for (const type of arrayBufferViews) {
676+
const { BYTES_PER_ELEMENT = 1 } = type;
677+
if (byteLength % BYTES_PER_ELEMENT === 0) {
678+
out.push(new type(buffer, byteOffset, byteLength / BYTES_PER_ELEMENT));
679+
}
680+
}
681+
return out;
682+
};

0 commit comments

Comments
 (0)