Comparing version 2.0.2 to 2.0.3
@@ -5,2 +5,4 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
/* eslint-env mocha */ | ||
@@ -159,4 +161,4 @@ var assert = require('assert'); | ||
it('serializes', function () { | ||
assertSerializer(type, 0); | ||
assertSerializer(type, 255); | ||
assertSerializer(type, 0, '00'); | ||
assertSerializer(type, 255, 'ff'); | ||
throws(function () { | ||
@@ -185,4 +187,4 @@ return assertSerializer(type, 256); | ||
assertSerializer(type, '18446744073709551615'); | ||
assertSerializer(type, '0'); | ||
assertSerializer(type, '18446744073709551615', 'ffffffffffffffff'); | ||
assertSerializer(type, '0', '0000000000000000'); | ||
throws(function () { | ||
@@ -197,9 +199,32 @@ return assertSerializer(type, '18446744073709551616'); | ||
it('int', function () { | ||
it('uint128', function () { | ||
var _Types10 = Types(), | ||
int8 = _Types10.int8; | ||
uint128 = _Types10.uint128; | ||
var type = uint128(); | ||
assertSerializer(type, '340282366920938463463374607431768211455', // (2^128)-1 | ||
'ffffffffffffffffffffffffffffffff'); | ||
assertSerializer(type, '0', '00000000000000000000000000000000'); | ||
throws(function () { | ||
return assertSerializer(type, '340282366920938463463374607431768211456'); | ||
}, /Overflow/); | ||
throws(function () { | ||
return assertSerializer(type, '-1'); | ||
}, /format/); | ||
assertRequired(type); | ||
}); | ||
it('int', function () { | ||
var _Types11 = Types(), | ||
int8 = _Types11.int8; | ||
var type = int8(); | ||
assertSerializer(type, -128); | ||
assertSerializer(type, 127); | ||
assertSerializer(type, -128, '80'); | ||
assertSerializer(type, 127, '7f'); | ||
var serializerType = _typeof(assertSerializer(type, 0)); | ||
assert.equal(serializerType, 'number', 'expecting number type when bits <= 53'); | ||
throws(function () { | ||
@@ -215,9 +240,13 @@ return assertSerializer(type, -129); | ||
it('int64', function () { | ||
var _Types11 = Types(), | ||
int64 = _Types11.int64; | ||
var _Types12 = Types(), | ||
int64 = _Types12.int64; | ||
var type = int64(); | ||
assertSerializer(type, '9223372036854775807'); | ||
assertSerializer(type, '-9223372036854775808'); | ||
assertSerializer(type, '9223372036854775807', 'ffffffffffffff7f'); | ||
assertSerializer(type, '-9223372036854775808', '0000000000000080'); | ||
var serializerType = _typeof(assertSerializer(type, '0')); | ||
assert.equal(serializerType, 'string', 'expecting string type when bits > 53'); | ||
throws(function () { | ||
@@ -232,7 +261,25 @@ return assertSerializer(type, '9223372036854775808'); | ||
it('int128', function () { | ||
var _Types13 = Types(), | ||
int128 = _Types13.int128; | ||
var type = int128(); | ||
assertSerializer(type, '0', '00000000000000000000000000000000'); | ||
assertSerializer(type, '170141183460469231731687303715884105727', 'ffffffffffffffffffffffffffffff7f'); | ||
assertSerializer(type, '-170141183460469231731687303715884105728', '00000000000000000000000000000080'); | ||
throws(function () { | ||
return assertSerializer(type, '170141183460469231731687303715884105728'); | ||
}, /Overflow/); | ||
throws(function () { | ||
return assertSerializer(type, '-170141183460469231731687303715884105729'); | ||
}, /Overflow/); | ||
assertRequired(type); | ||
}); | ||
describe('struct', function () { | ||
var _Types12 = Types(), | ||
vector = _Types12.vector, | ||
uint16 = _Types12.uint16, | ||
fixed_bytes33 = _Types12.fixed_bytes33; | ||
var _Types14 = Types(), | ||
vector = _Types14.vector, | ||
uint16 = _Types14.uint16, | ||
fixed_bytes33 = _Types14.fixed_bytes33; | ||
@@ -552,5 +599,8 @@ var KeyPermissionWeight = Struct('KeyPermissionWeight'); | ||
function assertSerializer(type, value) { | ||
function assertSerializer(type, value, bufferHex) { | ||
var obj = type.fromObject(value); // tests fromObject | ||
var buf = Fcbuffer.toBuffer(type, obj); // tests appendByteBuffer | ||
if (bufferHex != null) { | ||
assert.equal(buf.toString('hex'), bufferHex); | ||
} | ||
var obj2 = Fcbuffer.fromBuffer(type, buf); // tests fromByteBuffer | ||
@@ -560,2 +610,3 @@ var obj3 = type.toObject(obj); // tests toObject | ||
deepEqual(obj3, obj2, 'serialize buffer'); | ||
return value; | ||
} | ||
@@ -562,0 +613,0 @@ |
@@ -64,30 +64,52 @@ 'use strict'; | ||
uint8: function uint8() { | ||
return [intbuf, { bits: 8 }]; | ||
return [bnbuf, { bits: 8 }]; | ||
}, | ||
uint16: function uint16() { | ||
return [intbuf, { bits: 16 }]; | ||
return [bnbuf, { bits: 16 }]; | ||
}, | ||
uint32: function uint32() { | ||
return [intbuf, { bits: 32 }]; | ||
return [bnbuf, { bits: 32 }]; | ||
}, | ||
uint64: function uint64() { | ||
return [intbuf, { bits: 64 }]; | ||
return [bnbuf, { bits: 64 }]; | ||
}, | ||
// ,128,224,256,512 TODO | ||
uint128: function uint128() { | ||
return [bnbuf, { bits: 128 }]; | ||
}, | ||
uint224: function uint224() { | ||
return [bnbuf, { bits: 224 }]; | ||
}, | ||
uint256: function uint256() { | ||
return [bnbuf, { bits: 256 }]; | ||
}, | ||
uint512: function uint512() { | ||
return [bnbuf, { bits: 512 }]; | ||
}, | ||
int8: function int8() { | ||
return [intbuf, { signed: true, bits: 8 }]; | ||
return [bnbuf, { signed: true, bits: 8 }]; | ||
}, | ||
int16: function int16() { | ||
return [intbuf, { signed: true, bits: 16 }]; | ||
return [bnbuf, { signed: true, bits: 16 }]; | ||
}, | ||
int32: function int32() { | ||
return [intbuf, { signed: true, bits: 32 }]; | ||
return [bnbuf, { signed: true, bits: 32 }]; | ||
}, | ||
int64: function int64() { | ||
return [intbuf, { signed: true, bits: 64 }]; | ||
return [bnbuf, { signed: true, bits: 64 }]; | ||
}, | ||
int128: function int128() { | ||
return [bnbuf, { signed: true, bits: 128 }]; | ||
}, | ||
int224: function int224() { | ||
return [bnbuf, { signed: true, bits: 224 }]; | ||
}, | ||
int256: function int256() { | ||
return [bnbuf, { signed: true, bits: 256 }]; | ||
}, | ||
int512: function int512() { | ||
return [bnbuf, { signed: true, bits: 512 }]; | ||
} | ||
// ,128,224,256,512 TODO | ||
// VarInt32: ()=> [intbuf, {signed: true, bits: 32}], | ||
// VarInt32: ()=> [bnbuf, {signed: true, bits: 32}], | ||
@@ -443,2 +465,47 @@ | ||
/** Big Numbers (> 64 bits) */ | ||
var bnbuf = function bnbuf(validation) { | ||
var _validation$signed = validation.signed, | ||
signed = _validation$signed === undefined ? false : _validation$signed, | ||
bits = validation.bits; | ||
var size = bits / 8; | ||
return { | ||
fromByteBuffer: function fromByteBuffer(b) { | ||
var bcopy = b.copy(b.offset, b.offset + size); | ||
b.skip(size); | ||
var bn = new BN(bcopy.toBuffer()); | ||
var buf = bn.toArrayLike(Buffer, 'le', size); | ||
bn = new BN(buf); | ||
if (signed) { | ||
bn = bn.fromTwos(bits); | ||
} | ||
var value = bn.toString(); | ||
validateInt(value, validation); | ||
return bits > 53 ? value : bn.toNumber(); | ||
}, | ||
appendByteBuffer: function appendByteBuffer(b, value) { | ||
validateInt(value, validation); | ||
var bn = new BN(value); | ||
if (signed) { | ||
bn = bn.toTwos(bits); | ||
} | ||
var buf = bn.toArrayLike(Buffer, 'le', size); | ||
b.append(buf.toString('binary'), 'binary'); | ||
}, | ||
fromObject: function fromObject(value) { | ||
validateInt(value, validation); | ||
return value; | ||
}, | ||
toObject: function toObject(value) { | ||
if (validation.defaults && value == null) { | ||
return validation.bits > 53 ? '0' : 0; | ||
} | ||
validateInt(value, validation); | ||
return value; | ||
} | ||
}; | ||
}; | ||
var bytebuf = function bytebuf(validation) { | ||
@@ -614,4 +681,4 @@ var _bytebuf = { | ||
} | ||
var _validation$signed = validation.signed, | ||
signed = _validation$signed === undefined ? false : _validation$signed, | ||
var _validation$signed2 = validation.signed, | ||
signed = _validation$signed2 === undefined ? false : _validation$signed2, | ||
_validation$bits = validation.bits, | ||
@@ -618,0 +685,0 @@ bits = _validation$bits === undefined ? 54 : _validation$bits; |
{ | ||
"name": "fcbuffer", | ||
"description": "Serialization library geared towards immutable data storage such as blockchains.", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"main": "lib/index.js", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66305
1700