From 5d7534dd4483841e39cb87a7c0c0766dfe08890a Mon Sep 17 00:00:00 2001 From: SEK1RO Date: Tue, 5 Aug 2025 20:08:01 +0300 Subject: [PATCH] feat(Boolean) --- src/index.js | 14 ++++++++++++++ test/index.test.js | 25 ++++++++++++++++++++++--- types/index.d.ts | 2 +- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index db82769..5198741 100644 --- a/src/index.js +++ b/src/index.js @@ -12,6 +12,13 @@ export { limits, memcpy, Type, Int, ConstString, ConstArray, ConstDataView, Stru export function serialize(dv, src, ...types) { const [type, ...inner_types] = types + if (type == Boolean && typeof src == 'boolean') { + if (dv.byteLength < 1) { + throw new Error('too small buffer') + } + dv.setUint8(0, src ? 1 : 0) + return + } if (type == Number && typeof src == 'number') { if (dv.byteLength < 8) { throw new Error('too small buffer') @@ -68,6 +75,7 @@ export function serialize(dv, src, ...types) { const frame = new DataView(dv.buffer, dv.byteOffset + 4) memcpy(frame, src) + return } if (type instanceof Type) { type.serialize(dv, src, ...inner_types) @@ -78,6 +86,9 @@ export function serialize(dv, src, ...types) { export function parse(dv, ...types) { const [type, ...inner_types] = types + if (type == Boolean) { + return !!dv.getUint8(0) + } if (type == Number) { return dv.getFloat64(0) } @@ -138,6 +149,9 @@ export function sizeofHead(...args) { export function sizeof(...args) { const [arg, ...inner_args] = args + if (arg == Boolean || typeof arg == 'boolean') { + return 1 + } if (arg == Number || typeof arg == 'number') { return 8 } diff --git a/test/index.test.js b/test/index.test.js index 784cd08..2fe8c86 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -3,6 +3,16 @@ import { expectDataViewEqual, filledDataView, sizedDataView } from '.' import { parse, serialize, sizeof, sizeofHead } from '../src' describe('serialize', () => { + test('Boolean', () => { + const expected = filledDataView([0x01]) + let actual = sizedDataView(0) + expect(() => serialize(actual, true, Boolean)).toThrow() + + actual = sizedDataView(1) + + serialize(actual, true, Boolean) + expectDataViewEqual(actual, expected) + }) test('Number', () => { const expected = filledDataView([0x40, 0x97, 0xF2, 0x80, 0x00, 0x00, 0x00, 0x00]) let dv = sizedDataView(7) @@ -61,11 +71,15 @@ describe('serialize', () => { }) describe('parse', () => { + test('Boolean', () => { + expect(parse(filledDataView([0x00]), Boolean)).toBeFalsy() + expect(parse(filledDataView([0x01]), Boolean)).toBeTruthy() + }) test('Number', () => { const expected = 1532.625 const dv = filledDataView([0x40, 0x97, 0xF2, 0x80, 0x00, 0x00, 0x00, 0x00]) const actual = parse(dv, Number) - expectDataViewEqual(actual, expected) + expect(actual).toEqual(expected) }) test('String', () => { const expected = 'hello' @@ -74,7 +88,7 @@ describe('parse', () => { 0x68, 0x65, 0x6C, 0x6C, 0x6F ]) const actual = parse(dv, String) - expectDataViewEqual(actual, expected) + expect(actual).toEqual(expected) }) test('Array, Number', () => { const expected = [1, 2] @@ -84,7 +98,7 @@ describe('parse', () => { 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ]) const actual = parse(dv, Array, Number) - expectDataViewEqual(actual, expected) + expect(actual).toEqual(expected) }) test('DataView', () => { const expected = filledDataView([ @@ -112,11 +126,16 @@ describe('sizeofHead', () => { expect(sizeofHead(Array, Number)).toEqual(4) expect(sizeofHead([1], Array, Number)).toEqual(4) }) + test('DataView', () => { + expect(sizeofHead(DataView)).toEqual(4) + expect(sizeofHead(filledDataView([0x01]), DataView)).toEqual(4) + }) }) describe('sizeof', () => { test('unknown', () => { expect(() => sizeof(String)).toThrow() expect(() => sizeof(Array, Number)).toThrow() + expect(() => sizeof(DataView)).toThrow() }) }) diff --git a/types/index.d.ts b/types/index.d.ts index e0fbae4..30106e9 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -9,7 +9,7 @@ import { Struct } from "./Struct"; export { memcpy, limits, Type, Int, ConstString, ConstArray, ConstDataView, Struct } -export type SerializableType = NumberConstructor | StringConstructor | ArrayConstructor | DataViewConstructor | Type +export type SerializableType = BooleanConstructor | NumberConstructor | StringConstructor | ArrayConstructor | DataViewConstructor | Type export type Serializable = unknown /**