Skip to content

Commit

Permalink
Merge branch 'master' into impr/biome
Browse files Browse the repository at this point in the history
  • Loading branch information
iwoplaza committed Jul 20, 2024
2 parents 377ceb4 + 16d8c23 commit 6c01d97
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typed-binary",
"version": "4.0.0",
"version": "4.0.1",
"description": "Describe binary structures with full TypeScript support. Encode and decode into pure JavaScript objects.",
"packageManager": "[email protected]+sha256.691fe176eea9a8a80df20e4976f3dfb44a04841ceb885638fe2a26174f81e65e",
"main": "dist/cjs/index.js",
Expand All @@ -10,7 +10,7 @@
"check": "biome check --write .",
"build": "rollup -c",
"prepublishOnly": "pnpm lint && pnpm build",
"dryPublish": "npm publish --dry-run",
"dryPublish": "pnpm publish --dry-run",
"test": "vitest run",
"dev:test": "vitest",
"ts-version": "tsc -v",
Expand Down
21 changes: 9 additions & 12 deletions src/io/bufferIOBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export type BufferIOOptions = {
/**
* @default 0
*/
byteOffset: number;
byteOffset?: number;
/**
* @default 'system'
*/
endianness: Endianness | 'system';
endianness?: Endianness | 'system';
};

export class BufferIOBase {
Expand All @@ -25,7 +25,13 @@ export class BufferIOBase {
public readonly endianness: Endianness;

constructor(buffer: ArrayBufferLike, options?: BufferIOOptions) {
this.byteOffset = options?.byteOffset ?? 0;
const { byteOffset = 0, endianness = 'system' } = options ?? {};

this.byteOffset = byteOffset;

const systemEndianness = getSystemEndianness();
this.endianness = endianness === 'system' ? systemEndianness : endianness;
this.switchEndianness = this.endianness !== systemEndianness;

let innerBuffer = buffer;
if (typeof Buffer !== 'undefined' && innerBuffer instanceof Buffer) {
Expand All @@ -41,15 +47,6 @@ export class BufferIOBase {
this.helperUint32View = new Uint32Array(helperBuffer);
this.helperFloatView = new Float32Array(helperBuffer);
this.helperByteView = new Uint8Array(helperBuffer);

const systemEndianness = getSystemEndianness();

this.endianness =
!options || options.endianness === 'system'
? systemEndianness
: options.endianness;

this.switchEndianness = this.endianness !== systemEndianness;
}

get currentByteOffset() {
Expand Down
21 changes: 21 additions & 0 deletions src/io/bufferReaderWriter.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
import { describe, expect, it } from 'vitest';
import { randBetween, randIntBetween } from '../test/random';
import { getSystemEndianness } from '../util';
import { BufferReader } from './bufferReader';
import { BufferWriter } from './bufferWriter';

describe('BufferWriter/BufferReader', () => {
it('parses options correctly', () => {
const buffer = Buffer.alloc(16);

const noOptions = new BufferWriter(buffer);
expect(noOptions.endianness).toEqual(getSystemEndianness());
expect(noOptions.currentByteOffset).toEqual(0);

const withByteOffset = new BufferWriter(buffer, { byteOffset: 16 });
expect(withByteOffset.endianness).toEqual(getSystemEndianness());
expect(withByteOffset.currentByteOffset).toEqual(16);

const withBigEndian = new BufferWriter(buffer, { endianness: 'big' });
expect(withBigEndian.endianness).toEqual('big');
expect(withBigEndian.currentByteOffset).toEqual(0);

const withLittleEndian = new BufferWriter(buffer, { endianness: 'little' });
expect(withLittleEndian.endianness).toEqual('little');
expect(withLittleEndian.currentByteOffset).toEqual(0);
});

it('should encode and decode int sequence', () => {
// Generating random int sequence
const intList = [];
Expand Down

0 comments on commit 6c01d97

Please sign in to comment.