Skip to content

Commit

Permalink
add more helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc committed Jan 10, 2024
1 parent b947621 commit f007323
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
46 changes: 46 additions & 0 deletions src/tests/utils/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
areDataViewsEqual,
nodeBufferToDataView,
dataViewToNodeBuffer,
nodeBufferToArrayBuffer,
arrayBufferToNodeBuffer
} from '../../utils/helpers';

test('nodeBufferToDataView() and dataViewToNodeBuffer()', () =>
{
const array = new Uint8Array(
[
0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08
]
);

const view1 = new DataView(
array.buffer,
array.byteOffset,
array.byteLength
);

const nodeBuffer = dataViewToNodeBuffer(view1);
const view2 = nodeBufferToDataView(nodeBuffer);

expect(areDataViewsEqual(view1, view2)).toBe(true);
});

test('nodeBufferToArrayBuffer() and arrayBufferToNodeBuffer()', () =>
{
const array = new Uint8Array(
[
0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08
]
);

const arrayBuffer1 = array.buffer;
const nodeBuffer = arrayBufferToNodeBuffer(arrayBuffer1);
const arrayBuffer2 = nodeBufferToArrayBuffer(nodeBuffer);
const view1 = new DataView(arrayBuffer1);
const view2 = new DataView(arrayBuffer2);

expect(areDataViewsEqual(view1, view2)).toBe(true);
});
40 changes: 34 additions & 6 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,23 @@ export function areDataViewsEqual(view1: DataView, view2: DataView)
* @remarks
* - Just for Node.js.
*/
export function nodeBufferToDataView(buffer: Buffer): DataView
export function nodeBufferToDataView(nodeBuffer: Buffer): DataView
{
return new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
return new DataView(
nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.byteLength
);
}

/**
* Convert DataView to Node.js Buffer using the same underlying ArrayBuffer.
*
* @category Utils
* @remarks
* - Just for Node.js.
*/
export function dataViewToNodeBuffer(view: DataView): Buffer
{
return Buffer.from(view.buffer, view.byteOffset, view.byteLength);
}

/**
Expand All @@ -146,9 +160,23 @@ export function nodeBufferToDataView(buffer: Buffer): DataView
* @remarks
* - Just for Node.js.
*/
export function nodeBufferToArrayBuffer(buffer: Buffer): ArrayBuffer
export function nodeBufferToArrayBuffer(nodeBuffer: Buffer): ArrayBuffer
{
return nodeBuffer.buffer.slice(
nodeBuffer.byteOffset, nodeBuffer.byteOffset + nodeBuffer.byteLength
);
}

/**
* Convert ArrayBuffer to Node.js Buffer.
*
* @category Utils
* @remarks
* - Just for Node.js.
*/
export function arrayBufferToNodeBuffer(arrayBuffer: ArrayBuffer): Buffer
{
return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
return Buffer.from(arrayBuffer, 0, arrayBuffer.byteLength);
}

/**
Expand Down Expand Up @@ -205,11 +233,11 @@ export function dataViewToString(view: DataView): string
*
* @category Utils
*/
export function arrayBufferToString(buffer: ArrayBuffer): string
export function arrayBufferToString(arrayBuffer: ArrayBuffer): string
{
const decoder = new TextDecoder();

return decoder.decode(buffer);
return decoder.decode(arrayBuffer);
}

/**
Expand Down

0 comments on commit f007323

Please sign in to comment.