fcbuffer
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "fcbuffer", | ||
"description": "Serialization library geared towards immutable data storage such as blockchains.", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -20,3 +20,2 @@ [![Build Status](https://travis-ci.org/jcalfee/fcbuffer.svg?branch=master)](https://travis-ci.org/jcalfee/fcbuffer) | ||
- Unit testing and code coverage | ||
- Deterministic sorting | ||
@@ -23,0 +22,0 @@ # Non Features |
@@ -87,7 +87,6 @@ const BN = require('bn.js') | ||
} | ||
return sortDefinition(result, validation.type) | ||
return result | ||
}, | ||
appendByteBuffer (b, value) { | ||
validate(value, validation) | ||
value = sortDefinition(value, validation.type) | ||
b.writeVarint32(value.length) | ||
@@ -100,3 +99,2 @@ for (const o of value) { | ||
validate(value, validation) | ||
value = sortDefinition(value, validation.type) | ||
const result = [] | ||
@@ -113,3 +111,2 @@ for (const o of value) { | ||
validate(value, validation) | ||
value = sortDefinition(value, validation.type) | ||
@@ -353,20 +350,1 @@ const result = [] | ||
const minSigned = bits => new BN(1).ishln(bits - 1).ineg() | ||
const strCmp = (a, b) => a > b ? 1 : a < b ? -1 : 0 | ||
const firstEl = el => Array.isArray(el) ? el[0] : el | ||
const sortDefinition = (array, stDefinition) => { | ||
if (!Array.isArray(array)) { throw new TypeError('Expecting array') } | ||
// console.log('definition.nosort', stDefinition.nosort) | ||
return stDefinition.nosort ? array | ||
: stDefinition.compare | ||
? array.sort((a, b) => stDefinition.compare(firstEl(a), firstEl(b))) // custom compare definition | ||
: array.sort((a, b) => | ||
typeof firstEl(a) === 'number' && typeof firstEl(b) === 'number' ? firstEl(a) - firstEl(b) | ||
// A binary string compare does not work. Performanance is very good so HEX is used.. localeCompare is another option. | ||
: Buffer.isBuffer(firstEl(a)) && Buffer.isBuffer(firstEl(b)) | ||
? strCmp(firstEl(a).toString('hex'), firstEl(b).toString('hex')) | ||
: strCmp(firstEl(a).toString(), firstEl(b).toString()) | ||
) | ||
} |
63037
892
101