Browse Source

test: add common.getArrayBufferViews(buf)

PR-URL: https://github.com/nodejs/node/pull/12223
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
v6
Timothy Gu 8 years ago
parent
commit
0c539faac3
  1. 6
      test/README.md
  2. 26
      test/common.js

6
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. Tests whether `name` and `expected` are part of a raised warning.
## getArrayBufferViews(buf)
* `buf` [&lt;Buffer>](https://nodejs.org/api/buffer.html#buffer_class_buffer)
* return [&lt;ArrayBufferView&#91;&#93;>](https://developer.mozilla.org/en-US/docs/Web/API/ArrayBufferView)
Returns an instance of all possible `ArrayBufferView`s of the provided Buffer.
### hasCrypto ### hasCrypto
* return [&lt;Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type) * return [&lt;Boolean>](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type)

26
test/common.js

@ -654,3 +654,29 @@ exports.skipIfInspectorDisabled = function skipIfInspectorDisabled() {
process.exit(0); 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;
};

Loading…
Cancel
Save