Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience function for creating a DataView #45

Merged
merged 2 commits into from
Oct 14, 2022

Conversation

Mrtenz
Copy link
Member

@Mrtenz Mrtenz commented Oct 13, 2022

DataView is great for reading bytes of certain lengths from a byte array. One quirk of it is that if you're working with subarrays of Uint8Arrays, the original ArrayBuffer still refers to the full Uint8Array, so the DataView will operate on the entire byte array. To solve this I added a small convenience function, which always takes the byteOffset and byteLength of the Uint8Array into account.

@Mrtenz Mrtenz requested a review from a team as a code owner October 13, 2022 16:13
* @param bytes - The bytes to create the {@link DataView} from.
* @returns The {@link DataView}.
*/
export function createDataView(bytes: Uint8Array): DataView {
Copy link
Member

Choose a reason for hiding this comment

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

Should the type be Uint8Array | Buffer?

Copy link
Member Author

Choose a reason for hiding this comment

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

Buffer extends Uint8Array

@Mrtenz Mrtenz merged commit 0e252ee into main Oct 14, 2022
@Mrtenz Mrtenz deleted the mrtenz/create-data-view branch October 14, 2022 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants