Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: add common.getArrayBufferViews(buf)
  • Loading branch information
TimothyGu committed Apr 4, 2017
commit dae94ffb7187b3c307f3bcc9b7c4410181492d13
6 changes: 6 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,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)

Expand Down
26 changes: 26 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (Number.isInteger(byteLength % BYTES_PER_ELEMENT)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean byteLength % BYTES_PER_ELEMENT === 0, or Number.isInteger(byteLength / BYTES_PER_ELEMENT)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhh… that's embarrassing. Good catch.

out.push(new type(buffer, byteOffset, byteLength / BYTES_PER_ELEMENT));
}
}
return out;
};