compact-encoding
Advanced tools
Comparing version 2.11.0 to 2.12.0
123
index.js
@@ -361,14 +361,2 @@ const b4a = require('b4a') | ||
exports.none = { | ||
preencode (state, m) { | ||
// do nothing | ||
}, | ||
encode (state, m) { | ||
// do nothing | ||
}, | ||
decode (state) { | ||
return null | ||
} | ||
} | ||
exports.array = function array (enc) { | ||
@@ -418,2 +406,113 @@ return { | ||
// simple helper for when you want to just express nothing | ||
exports.none = { | ||
preencode (state, n) { | ||
// do nothing | ||
}, | ||
encode (state, n) { | ||
// do nothing | ||
}, | ||
decode (state) { | ||
return null | ||
} | ||
} | ||
// "any" encoders here for helping just structure any object without schematising it | ||
const anyArray = { | ||
preencode (state, arr) { | ||
uint.preencode(state, arr.length) | ||
for (let i = 0; i < arr.length; i++) { | ||
any.preencode(state, arr[i]) | ||
} | ||
}, | ||
encode (state, arr) { | ||
uint.encode(state, arr.length) | ||
for (let i = 0; i < arr.length; i++) { | ||
any.encode(state, arr[i]) | ||
} | ||
}, | ||
decode (state) { | ||
const arr = [] | ||
let len = uint.decode(state) | ||
while (len-- > 0) { | ||
arr.push(any.decode(state)) | ||
} | ||
return arr | ||
} | ||
} | ||
const anyObject = { | ||
preencode (state, o) { | ||
const keys = Object.keys(o) | ||
uint.preencode(state, keys.length) | ||
for (const key of keys) { | ||
utf8.preencode(state, key) | ||
any.preencode(state, o[key]) | ||
} | ||
}, | ||
encode (state, o) { | ||
const keys = Object.keys(o) | ||
uint.encode(state, keys.length) | ||
for (const key of keys) { | ||
utf8.encode(state, key) | ||
any.encode(state, o[key]) | ||
} | ||
}, | ||
decode (state) { | ||
let len = uint.decode(state) | ||
const o = {} | ||
while (len-- > 0) { | ||
const key = utf8.decode(state) | ||
o[key] = any.decode(state) | ||
} | ||
return o | ||
} | ||
} | ||
const anyTypes = [ | ||
exports.none, | ||
exports.bool, | ||
exports.string, | ||
exports.buffer, | ||
exports.uint, | ||
exports.int, | ||
exports.float64, | ||
anyArray, | ||
anyObject | ||
] | ||
const any = exports.any = { | ||
preencode (state, o) { | ||
const t = getType(o) | ||
uint.preencode(state, t) | ||
anyTypes[t].preencode(state, o) | ||
}, | ||
encode (state, o) { | ||
const t = getType(o) | ||
uint.encode(state, t) | ||
anyTypes[t].encode(state, o) | ||
}, | ||
decode (state) { | ||
const t = uint.decode(state) | ||
if (t >= anyTypes.length) throw new Error('Unknown type: ' + t) | ||
return anyTypes[t].decode(state) | ||
} | ||
} | ||
function getType (o) { | ||
if (o === null || o === undefined) return 0 | ||
if (typeof o === 'boolean') return 1 | ||
if (typeof o === 'string') return 2 | ||
if (b4a.isBuffer(o)) return 3 | ||
if (typeof o === 'number') { | ||
if (Number.isInteger(o)) return o >= 0 ? 4 : 5 | ||
return 6 | ||
} | ||
if (Array.isArray(o)) return 7 | ||
if (typeof o === 'object') return 8 | ||
throw new Error('Unsupported type for ' + o) | ||
} | ||
exports.from = function from (enc) { | ||
@@ -420,0 +519,0 @@ if (typeof enc === 'string') return fromNamed(enc) |
{ | ||
"name": "compact-encoding", | ||
"version": "2.11.0", | ||
"version": "2.12.0", | ||
"description": "A series of compact encoding schemes for building small and fast parsers and serializers", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"brittle": "^1.5.1", | ||
"brittle": "^3.0.0", | ||
"standard": "^16.0.3" | ||
@@ -22,3 +22,3 @@ }, | ||
"author": "Mathias Buus (@mafintosh)", | ||
"license": "MIT", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
@@ -25,0 +25,0 @@ "url": "https://github.com/compact-encoding/compact-encoding/issues" |
@@ -143,2 +143,3 @@ # compact-encoding | ||
* `cenc.raw.ndjson` - Encodes a JSON value as newline delimited utf-8 without a length prefixed. | ||
* `cenc.any` - Encodes any JSON representable value into a self described buffer. Like JSON + buffer, but using compact types. Useful for schemaless codecs. | ||
* `cenc.from(enc)` - Makes a compact encoder from a [codec](https://github.com/mafintosh/codecs) or [abstract-encoding](https://github.com/mafintosh/abstract-encoding). | ||
@@ -148,2 +149,2 @@ | ||
MIT | ||
Apache 2.0 |
17
test.js
@@ -657,1 +657,18 @@ const enc = require('./') | ||
}) | ||
test('any', function (t) { | ||
const o = { | ||
hello: 'world', | ||
num: 42, | ||
neg: -42, | ||
arr: [{ yes: 1 }, { no: false }], | ||
nest: {}, | ||
float: 0.54 | ||
} | ||
t.alike(enc.decode(enc.any, enc.encode(enc.any, o)), o) | ||
const arr = new Array(3) | ||
t.alike(enc.decode(enc.any, enc.encode(enc.any, arr)), [null, null, null]) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
61339
10
1304
149