compact-encoding
Advanced tools
Comparing version 1.0.1 to 1.1.0
22
index.js
const LE = (new Uint8Array(new Uint16Array([255]).buffer))[0] === 0xff | ||
const BE = !LE | ||
exports.state = function () { | ||
return { start: 0, end: 0, buffer: null } | ||
} | ||
const uint = exports.uint = { | ||
@@ -36,11 +40,2 @@ preencode (state, n) { | ||
function zigzagDecode (n) { | ||
return n === 0 ? n : (n & 1) === 0 ? n / 2 : -(n + 1) / 2 | ||
} | ||
function zigzagEncode (n) { | ||
// 0, -1, 1, -2, 2, ... | ||
return n < 0 ? (2 * -n) - 1 : n === 0 ? 0 : 2 * n | ||
} | ||
exports.buffer = { | ||
@@ -337,1 +332,10 @@ preencode (state, b) { | ||
} | ||
function zigzagDecode (n) { | ||
return n === 0 ? n : (n & 1) === 0 ? n / 2 : -(n + 1) / 2 | ||
} | ||
function zigzagEncode (n) { | ||
// 0, -1, 1, -2, 2, ... | ||
return n < 0 ? (2 * -n) - 1 : n === 0 ? 0 : 2 * n | ||
} |
{ | ||
"name": "compact-encoding", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "A series of compact encoding schemes for building small and fast parsers and serializers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -39,4 +39,6 @@ # compact-encoding | ||
Should be an object that looks like this `{ start, end, buffer }` | ||
Should be an object that looks like this `{ start, end, buffer }`. | ||
You can also get a blank state object using `cenc.state()`. | ||
#### `enc.preencode(state, val)` | ||
@@ -63,3 +65,3 @@ | ||
* `cenc.uint` - Encodes a uint using [compact-uint](https://github.com/mafintosh/compact-uint) | ||
* `cenc.int` - Encodes an int using [compact-uint](https://github.com/mafintosh/compact-uint) as a signed int. | ||
* `cenc.int` - Encodes an int using [compact-uint](https://github.com/mafintosh/compact-uint) as a signed int using ZigZag encoding. | ||
* `cenc.buffer` - Encodes a buffer with it's length uint prefixed. When decoding an empty buf, null is returned. | ||
@@ -66,0 +68,0 @@ * `cenc.raw` - Pass through encodes a buffer - ie a basic copy. |
20
test.js
@@ -5,3 +5,3 @@ const enc = require('./') | ||
tape('uint', function (t) { | ||
const state = { start: 0, end: 0, buffer: null } | ||
const state = enc.state() | ||
@@ -35,3 +35,3 @@ enc.uint.preencode(state, 42) | ||
tape('int', function (t) { | ||
const state = { start: 0, end: 0, buffer: null } | ||
const state = enc.state() | ||
@@ -60,3 +60,3 @@ enc.int.preencode(state, 42) | ||
tape('buffer', function (t) { | ||
const state = { start: 0, end: 0, buffer: null } | ||
const state = enc.state() | ||
@@ -93,3 +93,3 @@ enc.buffer.preencode(state, Buffer.from('hi')) | ||
tape('raw', function (t) { | ||
const state = { start: 0, end: 0, buffer: null } | ||
const state = enc.state() | ||
@@ -111,3 +111,3 @@ enc.raw.preencode(state, Buffer.from('hi')) | ||
tape('uint32array', function (t) { | ||
const state = { start: 0, end: 0, buffer: null } | ||
const state = enc.state() | ||
@@ -153,3 +153,3 @@ enc.uint32array.preencode(state, new Uint32Array([1])) | ||
tape('string', function (t) { | ||
const state = { start: 0, end: 0, buffer: null } | ||
const state = enc.state() | ||
@@ -178,3 +178,3 @@ enc.string.preencode(state, 'hi') | ||
tape('fixed32', function (t) { | ||
const state = { start: 0, end: 0, buffer: null } | ||
const state = enc.state() | ||
@@ -203,3 +203,3 @@ enc.fixed32.preencode(state, Buffer.alloc(32).fill('a')) | ||
tape('fixed64', function (t) { | ||
const state = { start: 0, end: 0, buffer: null } | ||
const state = enc.state() | ||
@@ -228,3 +228,3 @@ enc.fixed64.preencode(state, Buffer.alloc(64).fill('a')) | ||
tape('fixed n', function (t) { | ||
const state = { start: 0, end: 0, buffer: null } | ||
const state = enc.state() | ||
const fixed = enc.fixed(3) | ||
@@ -256,3 +256,3 @@ | ||
tape('array', function (t) { | ||
const state = { start: 0, end: 0, buffer: null } | ||
const state = enc.state() | ||
const arr = enc.array(enc.bool) | ||
@@ -259,0 +259,0 @@ |
512
79
22506