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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/bytes.test.ts
Expand Up @@ -7,6 +7,7 @@ import {
bytesToSignedBigInt,
bytesToString,
concatBytes,
createDataView,
hexToBytes,
isBytes,
numberToBytes,
Expand Down Expand Up @@ -414,3 +415,83 @@ describe('concatBytes', () => {
).toStrictEqual(Uint8Array.from([1, 2, 3, 52, 5]));
});
});

describe('createDataView', () => {
describe('Uint8Array', () => {
it('returns a DataView from a byte array', () => {
const dataView = createDataView(new Uint8Array([1, 2, 3]));

expect(dataView).toBeInstanceOf(DataView);
expect(dataView.getUint8(0)).toBe(1);
expect(dataView.getUint8(1)).toBe(2);
expect(dataView.getUint8(2)).toBe(3);
});

it('returns a DataView from a subarray of a byte array', () => {
const original = new Uint8Array([1, 2, 3, 4, 5]);
const subset = original.subarray(1, 4);

const dataView = createDataView(subset);

expect(dataView).toBeInstanceOf(DataView);
expect(dataView.byteOffset).toBe(1);
expect(dataView.byteLength).toBe(3);
expect(dataView.getUint8(0)).toBe(2);
expect(dataView.getUint8(1)).toBe(3);
expect(dataView.getUint8(2)).toBe(4);
});

it('returns a DataView from a slice of a byte array', () => {
const original = new Uint8Array([1, 2, 3, 4, 5]);
const subset = original.slice(1, 4);

const dataView = createDataView(subset);

expect(dataView).toBeInstanceOf(DataView);
expect(dataView.byteOffset).toBe(0);
expect(dataView.byteLength).toBe(3);
expect(dataView.getUint8(0)).toBe(2);
expect(dataView.getUint8(1)).toBe(3);
expect(dataView.getUint8(2)).toBe(4);
});
});

describe('Node.js Buffer', () => {
it('returns a DataView from a byte array', () => {
const dataView = createDataView(Buffer.from([1, 2, 3]));

expect(dataView).toBeInstanceOf(DataView);
expect(dataView.getUint8(0)).toBe(1);
expect(dataView.getUint8(1)).toBe(2);
expect(dataView.getUint8(2)).toBe(3);
});

it('returns a DataView from a subarray of a byte array', () => {
const original = Buffer.from([1, 2, 3, 4, 5]);
const subset = original.subarray(1, 4);

const dataView = createDataView(subset);

expect(dataView).toBeInstanceOf(DataView);
expect(dataView.byteOffset).toBe(0);
expect(dataView.byteLength).toBe(3);
expect(dataView.getUint8(0)).toBe(2);
expect(dataView.getUint8(1)).toBe(3);
expect(dataView.getUint8(2)).toBe(4);
});

it('returns a DataView from a slice of a byte array', () => {
const original = Buffer.from([1, 2, 3, 4, 5]);
const subset = original.slice(1, 4);

const dataView = createDataView(subset);

expect(dataView).toBeInstanceOf(DataView);
expect(dataView.byteOffset).toBe(0);
expect(dataView.byteLength).toBe(3);
expect(dataView.getUint8(0)).toBe(2);
expect(dataView.getUint8(1)).toBe(3);
expect(dataView.getUint8(2)).toBe(4);
});
});
});
35 changes: 35 additions & 0 deletions src/bytes.ts
Expand Up @@ -393,3 +393,38 @@ export function concatBytes(values: Bytes[]): Uint8Array {

return bytes;
}

/**
* Create a {@link DataView} from a {@link Uint8Array}. This is a convenience
* function that avoids having to create a {@link DataView} manually, which
* requires passing the `byteOffset` and `byteLength` parameters every time.
*
* Not passing the `byteOffset` and `byteLength` parameters can result in
* unexpected behavior when the {@link Uint8Array} is a view of a larger
* {@link ArrayBuffer}, e.g., when using {@link Uint8Array.subarray}.
*
* This function also supports Node.js {@link Buffer}s.
*
* @example
* ```typescript
* const bytes = new Uint8Array([1, 2, 3]);
*
* // This is equivalent to:
* // const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
* const dataView = createDataView(bytes);
* ```
* @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

if (typeof Buffer !== 'undefined' && bytes instanceof Buffer) {
const buffer = bytes.buffer.slice(
bytes.byteOffset,
bytes.byteOffset + bytes.byteLength,
);

return new DataView(buffer);
}

return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
}