Skip to content

Commit

Permalink
v1.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
cheprasov committed Oct 25, 2023
1 parent 600a823 commit 32e09f3
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)

@cheprasov/jsbt (v1.2.1)
@cheprasov/jsbt (v1.2.3)
=========

JSBT is a library for serializing structured JavaScript data. The library is JavaScript oriented and tries to resolve JS needs for better data serialization.
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cheprasov/jsbt",
"version": "1.2.1",
"version": "1.2.3",
"author": "Alexander Cheprasov",
"description": "JSBT is a library for serializing structured JavaScript data. The library is JavaScript oriented and tries to resolve JS needs for better data serialization.",
"keywords": ["JSBT", "serialization"],
Expand Down
2 changes: 1 addition & 1 deletion specification.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Specification JSBT v1.2.1
# Specification JSBT v1.2.3
JavaScript Byte Translation


Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

export const MAX_7_BYTES_INTEGER = 0x1FFFFFFFFFFFFF; // Number.MAX_SAFE_NUMBER;
export const MAX_7_BYTES_INTEGER = 0x1F_FF_FF_FF_FF_FF_FF; // Number.MAX_SAFE_NUMBER;
export const MAX_DATE_INTEGER = 8640000000000000;
15 changes: 12 additions & 3 deletions src/converter/bytesToInteger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { bytesToBigInt } from './bytesToBigInt'

export const bytesToInteger = (bytes: Uint8Array | number[]): number => {
return Number(bytesToBigInt(bytes));
const len = Array.isArray(bytes) ? bytes.length : bytes.byteLength;
if (len === 0) {
return 0;
}

let int = 0;
for (let i = 0; i < len; i += 1) {
const byte = bytes[i];
int = (byte * (256**i)) + int
}

return int;
}
19 changes: 5 additions & 14 deletions src/converter/integerToBytes.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MAX_7_BYTES_INTEGER } from '../constants';
import { integerToBytes } from './integerToBytes';

describe('integerToBytes', () => {
Expand All @@ -9,14 +10,6 @@ describe('integerToBytes', () => {
expect(integerToBytes(4370)).toEqual([0x12, 0x11]);
});

it('should return bytes for negative Integer', () => {
expect(integerToBytes(-1)).toEqual([0xFF, 0xFF, 0xFF, 0xFF]);
expect(integerToBytes(-2)).toEqual([0xFE, 0xFF, 0xFF, 0xFF]);
expect(integerToBytes(-255)).toEqual([0x01, 0xFF, 0xFF, 0xFF]);
expect(integerToBytes(-256)).toEqual([0x00, 0xFF, 0xFF, 0xFF]);
expect(integerToBytes(-257)).toEqual([0xFF, 0xFE, 0xFF, 0xFF]);
});

it('should return correct bytes at Big-Endian order', () => {
expect(integerToBytes(1, 1, true)).toEqual([0x01]);
expect(integerToBytes(255, 1, true)).toEqual([0xFF]);
Expand All @@ -29,8 +22,6 @@ describe('integerToBytes', () => {
expect(integerToBytes(4370, 2, true)).toEqual([0x11, 0x12]);
expect(integerToBytes(4370, 3, true)).toEqual([0x00, 0x11, 0x12]);

expect(integerToBytes(-1, 2, true)).toEqual([0xFF, 0xFF]);
expect(integerToBytes(-2, 2, true)).toEqual([0xFF, 0xFE]);
});

it('should return correct bytes at Little-Endian order', () => {
Expand All @@ -44,15 +35,15 @@ describe('integerToBytes', () => {
expect(integerToBytes(4370, 1)).toEqual([0x12]);
expect(integerToBytes(4370, 2)).toEqual([0x12, 0x11]);
expect(integerToBytes(4370, 3)).toEqual([0x12, 0x11, 0x00]);

expect(integerToBytes(-2, 2)).toEqual([0xFE, 0xFF]);
});

it('should return bytes for large Integer', () => {
expect(integerToBytes(0xFF_FF_FF_FE)).toEqual([0xFE, 0xFF, 0xFF, 0xFF]);
expect(integerToBytes(0xFF_FF_FF_FA)).toEqual([0xFA, 0xFF, 0xFF, 0xFF]);
expect(integerToBytes(0xFF_FF_FF_FF)).toEqual([0xFF, 0xFF, 0xFF, 0xFF]);
expect(integerToBytes(-0xFF_FF_FF_FF)).toEqual([0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF]);
expect(integerToBytes(-0xFF_FF_FF, 4)).toEqual([0x01, 0x00, 0x00, 0xFF]);
expect(integerToBytes(0xFA_FD_FC_FE)).toEqual([0xFE, 0xFC, 0xFD, 0xFA]);
expect(integerToBytes(0x11_FF_FF_FF_22)).toEqual([0x22, 0xFF, 0xFF, 0xFF, 0x11]);
expect(integerToBytes(0xFA_FB_FC_FD_FE_FF)).toEqual([0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA]);
//expect(integerToBytes(0x1F_FA_FB_FC_FD_FE_FF)).toEqual([0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0x1F]);
});
});
19 changes: 7 additions & 12 deletions src/converter/integerToBytes.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { bigIntToBytes } from '../converter/bigIntToBytes';

export const integerToBytes = (int: number, byteSize: number = 0, bigEndianOrder: boolean = false): number[] => {
if (int > 0xFF_FF_FF_FF || int <= -0xFF_FF_FF_FF) {
// Because JS uses 4 bytes for bitwise operations :(
return bigIntToBytes(
BigInt(int),
int < 0 && !byteSize ? 8 : byteSize,
bigEndianOrder,
).map((bint) => Number(bint));
if (int < 0) {
throw new Error('integerToBytes does not support negative integers');
}
const bytes: number[] = [];
let num = int;
let num = Math.abs(int);
for (let i = 1; byteSize ? i <= byteSize : num; i += 1) {
bytes.push(num & 0xFF);
num = num >>> 8;
const n = num % 256;
bytes.push(n);
num = num - n;
num = Math.floor(num / 256);
}
return bigEndianOrder ? bytes.reverse() : bytes;
}
49 changes: 35 additions & 14 deletions src/decoder/decodeTypedArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,41 @@ export type TTypedArrayConstructor = {
new (init: number): TTypedArray;
};

export const typedArrayConstructorByType: Record<number, TTypedArrayConstructor> = {
[ETypedArrayByteCode.ArrayBuffer]: Uint8Array,
[ETypedArrayByteCode.Int8Array]: Int8Array,
[ETypedArrayByteCode.Uint8Array]: Uint8Array,
[ETypedArrayByteCode.Uint8ClampedArray]: Uint8ClampedArray,
[ETypedArrayByteCode.Int16Array]: Int16Array,
[ETypedArrayByteCode.Uint16Array]: Uint16Array,
[ETypedArrayByteCode.Int32Array]: Int32Array,
[ETypedArrayByteCode.Uint32Array]: Uint32Array,
[ETypedArrayByteCode.Float32Array]: Float32Array,
[ETypedArrayByteCode.Float64Array]: Float64Array,
[ETypedArrayByteCode.BigInt64Array]: BigInt64Array,
[ETypedArrayByteCode.BigUint64Array]: BigInt64Array,
};
export const typedArrayConstructorByType: Record<number, TTypedArrayConstructor> = {};
if (typeof(Uint8Array) !== 'undefined') {
typedArrayConstructorByType[ETypedArrayByteCode.ArrayBuffer] = Uint8Array;
typedArrayConstructorByType[ETypedArrayByteCode.Uint8Array] = Uint8Array;
}
if (typeof(Int8Array) !== 'undefined') {
typedArrayConstructorByType[ETypedArrayByteCode.Int8Array] = Int8Array;
}
if (typeof(Uint8ClampedArray) !== 'undefined') {
typedArrayConstructorByType[ETypedArrayByteCode.Uint8ClampedArray] = Uint8ClampedArray;
}
if (typeof(Int16Array) !== 'undefined') {
typedArrayConstructorByType[ETypedArrayByteCode.Int16Array] = Int16Array;
}
if (typeof(Uint16Array) !== 'undefined') {
typedArrayConstructorByType[ETypedArrayByteCode.Uint16Array] = Uint16Array;
}
if (typeof(Int32Array) !== 'undefined') {
typedArrayConstructorByType[ETypedArrayByteCode.Int32Array] = Int32Array;
}
if (typeof(Uint32Array) !== 'undefined') {
typedArrayConstructorByType[ETypedArrayByteCode.Uint32Array] = Uint32Array;
}
if (typeof(Float32Array) !== 'undefined') {
typedArrayConstructorByType[ETypedArrayByteCode.Float32Array] = Float32Array;
}
if (typeof(Float64Array) !== 'undefined') {
typedArrayConstructorByType[ETypedArrayByteCode.Float64Array] = Float64Array;
}
if (typeof(BigInt64Array) !== 'undefined') {
typedArrayConstructorByType[ETypedArrayByteCode.BigInt64Array] = BigInt64Array;
}
if (typeof(BigUint64Array) !== 'undefined') {
typedArrayConstructorByType[ETypedArrayByteCode.BigUint64Array] = BigUint64Array;
}

export const dataViewGetter: Record<number, TDataViewGetter> = {
[ETypedArrayByteCode.ArrayBuffer]: 'getUint8',
Expand Down
24 changes: 12 additions & 12 deletions src/utils/vars/isTypedArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { TTypedArray } from '../../types/TTypedArray';

export const isTypedArray = (value: any): value is TTypedArray => {
return (
value instanceof ArrayBuffer ||
value instanceof Int8Array ||
value instanceof Uint8Array ||
value instanceof Uint8ClampedArray ||
value instanceof Int16Array ||
value instanceof Uint16Array ||
value instanceof Int32Array ||
value instanceof Uint32Array ||
value instanceof Float32Array ||
value instanceof Float64Array ||
value instanceof BigInt64Array ||
value instanceof BigUint64Array
typeof(ArrayBuffer) !== 'undefined' && value instanceof ArrayBuffer ||
typeof(Int8Array) !== 'undefined' && value instanceof Int8Array ||
typeof(Uint8Array) !== 'undefined' && value instanceof Uint8Array ||
typeof(Uint8ClampedArray) !== 'undefined' && value instanceof Uint8ClampedArray ||
typeof(Int16Array) !== 'undefined' && value instanceof Int16Array ||
typeof(Uint16Array) !== 'undefined' && value instanceof Uint16Array ||
typeof(Int32Array) !== 'undefined' && value instanceof Int32Array ||
typeof(Uint32Array) !== 'undefined' && value instanceof Uint32Array ||
typeof(Float32Array) !== 'undefined' && value instanceof Float32Array ||
typeof(Float64Array) !== 'undefined' && value instanceof Float64Array ||
typeof(BigInt64Array) !== 'undefined' && value instanceof BigInt64Array ||
typeof(BigUint64Array) !== 'undefined' && value instanceof BigUint64Array
);
};

0 comments on commit 32e09f3

Please sign in to comment.