feat: handle null and undefined

This commit is contained in:
cha0s 2019-04-20 01:55:16 -05:00
parent 7226ca8979
commit 7825e647b0

View File

@ -5,16 +5,18 @@ const PACKER_STEP_OP_ADD = 0;
const PACKER_STEP_OP_REPLACE = 1;
const PACKER_STEP_OP_REMOVE = 2;
const PACKER_STEP_VALUE_BOOLEAN = 0;
const PACKER_STEP_VALUE_BYTE = 1;
const PACKER_STEP_VALUE_UBYTE = 2;
const PACKER_STEP_VALUE_SHORT = 3;
const PACKER_STEP_VALUE_USHORT = 4;
const PACKER_STEP_VALUE_INT = 5;
const PACKER_STEP_VALUE_UINT = 6;
const PACKER_STEP_VALUE_FLOAT = 7;
const PACKER_STEP_VALUE_STRING = 8;
const PACKER_STEP_VALUE_JSON = 9;
const PACKER_STEP_VALUE_BOOLEAN = 0;
const PACKER_STEP_VALUE_NULL = 1;
const PACKER_STEP_VALUE_UNDEFINED = 2;
const PACKER_STEP_VALUE_BYTE = 3;
const PACKER_STEP_VALUE_UBYTE = 4;
const PACKER_STEP_VALUE_SHORT = 5;
const PACKER_STEP_VALUE_USHORT = 6;
const PACKER_STEP_VALUE_INT = 7;
const PACKER_STEP_VALUE_UINT = 8;
const PACKER_STEP_VALUE_FLOAT = 9;
const PACKER_STEP_VALUE_STRING = 10;
const PACKER_STEP_VALUE_JSON = 11;
export class Packer {
@ -125,7 +127,9 @@ export class Packer {
caret += 1;
}
break;
case PACKER_STEP_VALUE_NULL:
case PACKER_STEP_VALUE_UNDEFINED:
break;
}
});
return packedSteps;
@ -157,6 +161,16 @@ export class Packer {
}
static prepareValue(value) {
if (undefined === value) {
return {
type: PACKER_STEP_VALUE_UNDEFINED,
};
}
if (null === value) {
return {
type: PACKER_STEP_VALUE_NULL,
};
}
if ('boolean' === typeof value) {
return {
type: PACKER_STEP_VALUE_BOOLEAN,
@ -211,9 +225,6 @@ export class Packer {
data: Buffer.from(value),
};
}
if (!value.toJS) {
console.log(value);
}
return {
type: PACKER_STEP_VALUE_JSON,
data: msgpack.encode(value.toJS()),
@ -222,6 +233,9 @@ export class Packer {
static packedValueLength({type, data}) {
switch (type) {
case PACKER_STEP_VALUE_NULL:
case PACKER_STEP_VALUE_UNDEFINED:
return 0;
case PACKER_STEP_VALUE_BOOLEAN:
case PACKER_STEP_VALUE_BYTE:
case PACKER_STEP_VALUE_UBYTE:
@ -317,6 +331,12 @@ export class Unpacker {
caret += 1;
}
break;
case PACKER_STEP_VALUE_NULL:
value = null;
break;
case PACKER_STEP_VALUE_UNDEFINED:
value = undefined;
break;
}
if (PACKER_STEP_VALUE_STRING === valueType) {
value = value.toString();