[js][bstruct]add DateHandler
This commit is contained in:
parent
9d6b5b911e
commit
c350abc5f6
@ -98,100 +98,113 @@ describe('binary-struct', () => {
|
||||
let ans = new Uint8Array([0x02, 0x03]);
|
||||
assert.ok(areArrayBuffersEqual(res, ans));
|
||||
});
|
||||
|
||||
it('type Date', () => {
|
||||
let res = serializeToBinary(new Date("2007-04-08"), BASIC_TYPES.DateTime);
|
||||
let ans = new Uint8Array([0x80, 0x30, 0x18, 0x46, 0, 0, 0, 0]);
|
||||
assert.ok(areArrayBuffersEqual(res, ans));
|
||||
});
|
||||
});
|
||||
|
||||
describe('deserializeFromBinary', () => {
|
||||
it('type i8', () => {
|
||||
let binaryData = new Uint8Array([0x85]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.i8);
|
||||
let binary_data = new Uint8Array([0x85]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), BASIC_TYPES.i8);
|
||||
let ans = -123;
|
||||
assert.equal(res, ans);
|
||||
});
|
||||
|
||||
it('type i16', () => {
|
||||
let binaryData = new Uint8Array([0xc7, 0xcf]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.i16);
|
||||
let binary_data = new Uint8Array([0xc7, 0xcf]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), BASIC_TYPES.i16);
|
||||
let ans = -12345;
|
||||
assert.equal(res, ans);
|
||||
});
|
||||
|
||||
it('type i32', () => {
|
||||
let binaryData = new Uint8Array([0x2e, 0xfd, 0x69, 0xb6]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.i32);
|
||||
let binary_data = new Uint8Array([0x2e, 0xfd, 0x69, 0xb6]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), BASIC_TYPES.i32);
|
||||
let ans = -1234567890;
|
||||
assert.equal(res, ans);
|
||||
});
|
||||
|
||||
it('type i64', () => {
|
||||
let binaryData = new Uint8Array([0xeb, 0x7e, 0x16, 0x82, 0x0b, 0xef, 0xdd, 0xee]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.i64);
|
||||
let binary_data = new Uint8Array([0xeb, 0x7e, 0x16, 0x82, 0x0b, 0xef, 0xdd, 0xee]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), BASIC_TYPES.i64);
|
||||
let ans = BigInt('-1234567890123456789');
|
||||
assert.equal(res, ans);
|
||||
});
|
||||
|
||||
it('type u8', () => {
|
||||
let binaryData = new Uint8Array([0x3c]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.u8);
|
||||
let binary_data = new Uint8Array([0x3c]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), BASIC_TYPES.u8);
|
||||
let ans = 60;
|
||||
assert.equal(res, ans);
|
||||
});
|
||||
|
||||
it('type u16', () => {
|
||||
let binaryData = new Uint8Array([0xd5, 0xdd]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.u16);
|
||||
let binary_data = new Uint8Array([0xd5, 0xdd]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), BASIC_TYPES.u16);
|
||||
let ans = 56789;
|
||||
assert.equal(res, ans);
|
||||
});
|
||||
|
||||
it('type u32', () => {
|
||||
let binaryData = new Uint8Array([0xd2, 0x02, 0x96, 0x49]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.u32);
|
||||
let binary_data = new Uint8Array([0xd2, 0x02, 0x96, 0x49]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), BASIC_TYPES.u32);
|
||||
let ans = 1234567890;
|
||||
assert.equal(res, ans);
|
||||
});
|
||||
|
||||
it('type u64', () => {
|
||||
let binaryData = new Uint8Array([0x3f, 0x46, 0x0b, 0xa6, 0x4b, 0x9b, 0xb6, 0x01]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.u64);
|
||||
let binary_data = new Uint8Array([0x3f, 0x46, 0x0b, 0xa6, 0x4b, 0x9b, 0xb6, 0x01]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), BASIC_TYPES.u64);
|
||||
let ans = BigInt('123456789009876543');
|
||||
assert.equal(res, ans);
|
||||
});
|
||||
|
||||
it('type uuid', () => {
|
||||
let binaryData = new Uint8Array([0xf5, 0xb3, 0xad, 0x32, 0x2a, 0x77, 0x4f, 0x2e, 0xa3, 0xb8, 0x3e, 0xb8, 0xa8, 0x54, 0x6f, 0x2b]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.uuid);
|
||||
let binary_data = new Uint8Array([0xf5, 0xb3, 0xad, 0x32, 0x2a, 0x77, 0x4f, 0x2e, 0xa3, 0xb8, 0x3e, 0xb8, 0xa8, 0x54, 0x6f, 0x2b]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), BASIC_TYPES.uuid);
|
||||
let ans = 'f5b3ad32-2a77-4f2e-a3b8-3eb8a8546f2b';
|
||||
assert.equal(res.toString(), ans);
|
||||
});
|
||||
|
||||
it('type string', () => {
|
||||
let binaryData = new Uint8Array([0x0b, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binaryData), BASIC_TYPES.str);
|
||||
let binary_data = new Uint8Array([0x0b, 0x00, 0x00, 0x00, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), BASIC_TYPES.str);
|
||||
let ans = 'hello world';
|
||||
assert.equal(res, ans);
|
||||
});
|
||||
|
||||
it('type u16[]', () => {
|
||||
let binaryData = new Uint8Array([0x03, 0x00, 0x00, 0x00, 0x70, 0x17, 0x3a, 0x3c, 0x12, 0x90]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binaryData), new DynamicArrayHandler(BASIC_TYPES.u16));
|
||||
let binary_data = new Uint8Array([0x03, 0x00, 0x00, 0x00, 0x70, 0x17, 0x3a, 0x3c, 0x12, 0x90]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), new DynamicArrayHandler(BASIC_TYPES.u16));
|
||||
let ans = [0x1770, 0x3c3a, 0x9012];
|
||||
assert.deepEqual(res, ans);
|
||||
});
|
||||
|
||||
it('type u16[3]', () => {
|
||||
let binaryData = new Uint8Array([0x70, 0x17, 0x3a, 0x3c, 0x12, 0x90]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binaryData), new FixedArrayHandler(3, BASIC_TYPES.u16));
|
||||
let binary_data = new Uint8Array([0x70, 0x17, 0x3a, 0x3c, 0x12, 0x90]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), new FixedArrayHandler(3, BASIC_TYPES.u16));
|
||||
let ans = [0x1770, 0x3c3a, 0x9012];
|
||||
assert.deepEqual(res, ans);
|
||||
});
|
||||
|
||||
it('type Point', () => {
|
||||
let binaryData = new Uint8Array([0x02, 0x03]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binaryData), Point);
|
||||
let binary_data = new Uint8Array([0x02, 0x03]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), Point);
|
||||
let ans = new Point(2, 3);
|
||||
assert.equal(res.x, ans.x);
|
||||
assert.equal(res.y, ans.y);
|
||||
assert.equal(res.foo(), ans.foo())
|
||||
});
|
||||
|
||||
it('type Date', () => {
|
||||
let binary_data = new Uint8Array([0x80, 0x30, 0x18, 0x46, 0, 0, 0, 0]).buffer;
|
||||
let res = deserializeFromBinary(new DataView(binary_data), BASIC_TYPES.DateTime);
|
||||
let ans = new Date("2007-04-08");
|
||||
assert.equal(ans.toUTCString(), res.toUTCString());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -475,6 +475,45 @@ export class BoolHandler extends BaseTypeHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles boolean values (bool).
|
||||
* @extends {BaseTypeHandler}
|
||||
* @class
|
||||
*/
|
||||
export class DateHandler extends BaseTypeHandler {
|
||||
/**
|
||||
* Gets the size of the serialized Date value in bytes (always 8).
|
||||
* @param {Date} _value - The bool value.
|
||||
* @returns {number} - The size of the serialized bool value in bytes.
|
||||
*/
|
||||
sizeof(_value) {
|
||||
return 8;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes the Date value and writes it to the DataView at the specified offset.
|
||||
* @param {DataView} view - The DataView to write to.
|
||||
* @param {number} offset - The offset to start writing at.
|
||||
* @param {Date} value - The Date value to be serialized.
|
||||
* @returns {number} - The new offset after serialization.
|
||||
*/
|
||||
serialize(view, offset, value) {
|
||||
view.setBigUint64(offset, BigInt(Math.floor(value.getTime() / 1000)), true);
|
||||
return offset + 8;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserializes the Date value from the DataView at the specified offset.
|
||||
* @param {DataView} view - The DataView to read from.
|
||||
* @param {number} offset - The offset to start reading from.
|
||||
* @returns {DeserializedResult} - The deserialized result containing the Date value and a new offset.
|
||||
*/
|
||||
deserialize(view, offset) {
|
||||
const timestamp = parseInt(view.getBigUint64(offset, true));
|
||||
return new DeserializedResult(new Date(timestamp * 1000), offset + 8);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles void values (void).
|
||||
* @extends {BaseTypeHandler}
|
||||
@ -868,4 +907,5 @@ export const BASIC_TYPES = {
|
||||
void: new VoidHandler(),
|
||||
uuid: new UUIDHandler(),
|
||||
str: new StringHandler(),
|
||||
DateTime: new DateHandler(),
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user