Skip to content

Commit

Permalink
Merge branch 'release/0.1.28'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmy Huang committed Nov 29, 2019
2 parents 5c93e78 + aca7b8f commit 34cc394
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions lib/write-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,29 @@ function getWriteType(options) {
token[type](encoder, value);
}

function isInt(n){
return Number(n) === n && n % 1 === 0;
}

function number(encoder, value) {
var type;
if (isInt(value)) {
if (-0x20 <= value && value <= 0x7F) {
// positive fixint -- 0x00 - 0x7f
// negative fixint -- 0xe0 - 0xff
type = value & 0xFF;
} else if (0 <= value) {
// uint 8 -- 0xcc
// uint 16 -- 0xcd
// uint 32 -- 0xce
type = (value <= 0xFF) ? 0xcc : (value <= 0xFFFF) ? 0xcd : 0xce;
} else {
// int 8 -- 0xd0
// int 16 -- 0xd1
// int 32 -- 0xd2
type = (-0x80 <= value) ? 0xd0 : (-0x8000 <= value) ? 0xd1 : 0xd2;
}
token[type](encoder, value);
} else {
if (Math.floor(value) != value) {
// float 64 -- 0xcb
type = 0xcb;
token[type](encoder, value);
return;
} else if (-0x20 <= value && value <= 0x7F) {
// positive fixint -- 0x00 - 0x7f
// negative fixint -- 0xe0 - 0xff
type = value & 0xFF;
} else if (0 <= value) {
// uint 8 -- 0xcc
// uint 16 -- 0xcd
// uint 32 -- 0xce
type = (value <= 0xFF) ? 0xcc : (value <= 0xFFFF) ? 0xcd : (value <= 0xFFFFFFFF) ? 0xce : 0xcf;
} else {
// int 8 -- 0xd0
// int 16 -- 0xd1
// int 32 -- 0xd2
type = (-0x80 <= value) ? 0xd0 : (-0x8000 <= value) ? 0xd1 : (-0x80000000 <= value) ? 0xd2 : 0xd3;
}
token[type](encoder, value);
}

// uint 64 -- 0xcf
Expand Down

0 comments on commit 34cc394

Please sign in to comment.