compact-encoding
Advanced tools
Comparing version 2.3.0 to 2.4.0
17
index.js
@@ -85,2 +85,19 @@ const LE = (new Uint8Array(new Uint16Array([255]).buffer))[0] === 0xff | ||
exports.float64 = { | ||
preencode (state, n) { | ||
state.end += 8 | ||
}, | ||
encode (state, n) { | ||
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8) | ||
view.setFloat64(0, n, true) // little endian | ||
state.start += 8 | ||
}, | ||
decode (state) { | ||
const view = new DataView(state.buffer.buffer, state.start + state.buffer.byteOffset, 8) | ||
const float = view.getFloat64(0, true) // little endian | ||
state.start += 8 | ||
return float | ||
} | ||
} | ||
exports.buffer = { | ||
@@ -87,0 +104,0 @@ preencode (state, b) { |
{ | ||
"name": "compact-encoding", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "A series of compact encoding schemes for building small and fast parsers and serializers", | ||
@@ -16,3 +16,3 @@ "main": "index.js", | ||
"type": "git", | ||
"url": "https://github.com/mafintosh/compact-encoding.git" | ||
"url": "https://github.com/compact-encoding/compact-encoding.git" | ||
}, | ||
@@ -22,5 +22,5 @@ "author": "Mathias Buus (@mafintosh)", | ||
"bugs": { | ||
"url": "https://github.com/mafintosh/compact-encoding/issues" | ||
"url": "https://github.com/compact-encoding/compact-encoding/issues" | ||
}, | ||
"homepage": "https://github.com/mafintosh/compact-encoding" | ||
"homepage": "https://github.com/compact-encoding/compact-encoding" | ||
} |
97
test.js
@@ -57,2 +57,99 @@ const enc = require('./') | ||
tape('float64', function (t) { | ||
const state = enc.state() | ||
enc.float64.preencode(state, 162.2377294) | ||
t.same(state, { start: 0, end: 8, buffer: null }) | ||
state.buffer = Buffer.alloc(state.end) | ||
t.same(state, { start: 0, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]) }) | ||
enc.float64.encode(state, 162.2377294) | ||
t.same(state, { start: 8, end: 8, buffer: Buffer.from([0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) }) | ||
state.start = 0 | ||
t.same(enc.float64.decode(state), 162.2377294) | ||
t.same(state.start, state.end) | ||
t.throws(() => enc.float64.decode(state)) | ||
// alignement | ||
state.start = 0 | ||
state.end = 0 | ||
state.buffer = null | ||
enc.int.preencode(state, 0) | ||
enc.float64.preencode(state, 162.2377294) | ||
t.same(state, { start: 0, end: 9, buffer: null }) | ||
state.buffer = Buffer.alloc(state.end) | ||
t.same(state, { start: 0, end: 9, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]) }) | ||
enc.int.encode(state, 0) | ||
enc.float64.encode(state, 162.2377294) | ||
t.same(state, { start: 9, end: 9, buffer: Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) }) | ||
state.start = 0 | ||
t.same(enc.int.decode(state), 0) | ||
t.same(enc.float64.decode(state), 162.2377294) | ||
t.same(state.start, state.end) | ||
// subarray | ||
const buf = Buffer.alloc(10) | ||
state.start = 0 | ||
state.buffer = buf.subarray(1) | ||
t.same(state, { start: 0, end: 9, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0]) }) | ||
enc.int.encode(state, 0) | ||
enc.float64.encode(state, 162.2377294) | ||
t.same(state, { start: 9, end: 9, buffer: Buffer.from([0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40]) }) | ||
t.same(buf, Buffer.from([0, 0, 0x87, 0xc9, 0xaf, 0x7a, 0x9b, 0x47, 0x64, 0x40])) | ||
state.start = 0 | ||
t.same(enc.int.decode(state), 0) | ||
t.same(enc.float64.decode(state), 162.2377294) | ||
t.same(state.start, state.end) | ||
// 0 | ||
state.start = 0 | ||
state.end = 0 | ||
state.buffer = null | ||
enc.float64.preencode(state, 162.2377294) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.float64.encode(state, 0) | ||
t.same(state, { start: 8, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0, 0]) }) | ||
state.start = 0 | ||
t.same(enc.float64.decode(state), 0) | ||
t.same(state.start, state.end) | ||
// Infinity | ||
state.start = 0 | ||
state.end = 0 | ||
state.buffer = null | ||
enc.float64.preencode(state, Infinity) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.float64.encode(state, Infinity) | ||
t.same(state, { start: 8, end: 8, buffer: Buffer.from([0, 0, 0, 0, 0, 0, 0xf0, 0x7f]) }) | ||
state.start = 0 | ||
t.same(enc.float64.decode(state), Infinity) | ||
t.same(state.start, state.end) | ||
// Edge cases | ||
state.start = 0 | ||
state.end = 0 | ||
state.buffer = null | ||
enc.float64.preencode(state, 0.1 + 0.2) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.float64.encode(state, 0.1 + 0.2) | ||
t.same(state, { start: 8, end: 8, buffer: Buffer.from([0x34, 0x33, 0x33, 0x33, 0x33, 0x33, 0xd3, 0x3f]) }) | ||
state.start = 0 | ||
t.same(enc.float64.decode(state), 0.1 + 0.2) | ||
t.same(state.start, state.end) | ||
t.end() | ||
}) | ||
tape('buffer', function (t) { | ||
@@ -59,0 +156,0 @@ const state = enc.state() |
27355
647