feat(Boolean)
This commit is contained in:
14
src/index.js
14
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
|
||||
}
|
||||
|
||||
@ -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()
|
||||
})
|
||||
})
|
||||
|
||||
2
types/index.d.ts
vendored
2
types/index.d.ts
vendored
@ -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
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user