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) {
|
export function serialize(dv, src, ...types) {
|
||||||
const [type, ...inner_types] = 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 (type == Number && typeof src == 'number') {
|
||||||
if (dv.byteLength < 8) {
|
if (dv.byteLength < 8) {
|
||||||
throw new Error('too small buffer')
|
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)
|
const frame = new DataView(dv.buffer, dv.byteOffset + 4)
|
||||||
memcpy(frame, src)
|
memcpy(frame, src)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if (type instanceof Type) {
|
if (type instanceof Type) {
|
||||||
type.serialize(dv, src, ...inner_types)
|
type.serialize(dv, src, ...inner_types)
|
||||||
@ -78,6 +86,9 @@ export function serialize(dv, src, ...types) {
|
|||||||
export function parse(dv, ...types) {
|
export function parse(dv, ...types) {
|
||||||
const [type, ...inner_types] = types
|
const [type, ...inner_types] = types
|
||||||
|
|
||||||
|
if (type == Boolean) {
|
||||||
|
return !!dv.getUint8(0)
|
||||||
|
}
|
||||||
if (type == Number) {
|
if (type == Number) {
|
||||||
return dv.getFloat64(0)
|
return dv.getFloat64(0)
|
||||||
}
|
}
|
||||||
@ -138,6 +149,9 @@ export function sizeofHead(...args) {
|
|||||||
export function sizeof(...args) {
|
export function sizeof(...args) {
|
||||||
const [arg, ...inner_args] = args
|
const [arg, ...inner_args] = args
|
||||||
|
|
||||||
|
if (arg == Boolean || typeof arg == 'boolean') {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
if (arg == Number || typeof arg == 'number') {
|
if (arg == Number || typeof arg == 'number') {
|
||||||
return 8
|
return 8
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,6 +3,16 @@ import { expectDataViewEqual, filledDataView, sizedDataView } from '.'
|
|||||||
import { parse, serialize, sizeof, sizeofHead } from '../src'
|
import { parse, serialize, sizeof, sizeofHead } from '../src'
|
||||||
|
|
||||||
describe('serialize', () => {
|
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', () => {
|
test('Number', () => {
|
||||||
const expected = filledDataView([0x40, 0x97, 0xF2, 0x80, 0x00, 0x00, 0x00, 0x00])
|
const expected = filledDataView([0x40, 0x97, 0xF2, 0x80, 0x00, 0x00, 0x00, 0x00])
|
||||||
let dv = sizedDataView(7)
|
let dv = sizedDataView(7)
|
||||||
@ -61,11 +71,15 @@ describe('serialize', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('parse', () => {
|
describe('parse', () => {
|
||||||
|
test('Boolean', () => {
|
||||||
|
expect(parse(filledDataView([0x00]), Boolean)).toBeFalsy()
|
||||||
|
expect(parse(filledDataView([0x01]), Boolean)).toBeTruthy()
|
||||||
|
})
|
||||||
test('Number', () => {
|
test('Number', () => {
|
||||||
const expected = 1532.625
|
const expected = 1532.625
|
||||||
const dv = filledDataView([0x40, 0x97, 0xF2, 0x80, 0x00, 0x00, 0x00, 0x00])
|
const dv = filledDataView([0x40, 0x97, 0xF2, 0x80, 0x00, 0x00, 0x00, 0x00])
|
||||||
const actual = parse(dv, Number)
|
const actual = parse(dv, Number)
|
||||||
expectDataViewEqual(actual, expected)
|
expect(actual).toEqual(expected)
|
||||||
})
|
})
|
||||||
test('String', () => {
|
test('String', () => {
|
||||||
const expected = 'hello'
|
const expected = 'hello'
|
||||||
@ -74,7 +88,7 @@ describe('parse', () => {
|
|||||||
0x68, 0x65, 0x6C, 0x6C, 0x6F
|
0x68, 0x65, 0x6C, 0x6C, 0x6F
|
||||||
])
|
])
|
||||||
const actual = parse(dv, String)
|
const actual = parse(dv, String)
|
||||||
expectDataViewEqual(actual, expected)
|
expect(actual).toEqual(expected)
|
||||||
})
|
})
|
||||||
test('Array, Number', () => {
|
test('Array, Number', () => {
|
||||||
const expected = [1, 2]
|
const expected = [1, 2]
|
||||||
@ -84,7 +98,7 @@ describe('parse', () => {
|
|||||||
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
])
|
])
|
||||||
const actual = parse(dv, Array, Number)
|
const actual = parse(dv, Array, Number)
|
||||||
expectDataViewEqual(actual, expected)
|
expect(actual).toEqual(expected)
|
||||||
})
|
})
|
||||||
test('DataView', () => {
|
test('DataView', () => {
|
||||||
const expected = filledDataView([
|
const expected = filledDataView([
|
||||||
@ -112,11 +126,16 @@ describe('sizeofHead', () => {
|
|||||||
expect(sizeofHead(Array, Number)).toEqual(4)
|
expect(sizeofHead(Array, Number)).toEqual(4)
|
||||||
expect(sizeofHead([1], 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', () => {
|
describe('sizeof', () => {
|
||||||
test('unknown', () => {
|
test('unknown', () => {
|
||||||
expect(() => sizeof(String)).toThrow()
|
expect(() => sizeof(String)).toThrow()
|
||||||
expect(() => sizeof(Array, Number)).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 { 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
|
export type Serializable = unknown
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user