compact-encoding
Advanced tools
Comparing version 2.7.0 to 2.8.0
26
index.js
@@ -310,3 +310,3 @@ const b4a = require('b4a') | ||
exports.string = exports.utf8 = string('utf-8') | ||
const utf8 = exports.string = exports.utf8 = string('utf-8') | ||
exports.ascii = string('ascii') | ||
@@ -381,2 +381,26 @@ exports.hex = string('hex') | ||
exports.json = { | ||
preencode (state, v) { | ||
utf8.preencode(state, JSON.stringify(v)) | ||
}, | ||
encode (state, v) { | ||
utf8.encode(state, JSON.stringify(v)) | ||
}, | ||
decode (state) { | ||
return JSON.parse(utf8.decode(state)) | ||
} | ||
} | ||
exports.ndjson = { | ||
preencode (state, v) { | ||
utf8.preencode(state, JSON.stringify(v) + '\n') | ||
}, | ||
encode (state, v) { | ||
utf8.encode(state, JSON.stringify(v) + '\n') | ||
}, | ||
decode (state) { | ||
return JSON.parse(utf8.decode(state)) | ||
} | ||
} | ||
exports.from = function from (enc) { | ||
@@ -383,0 +407,0 @@ if (enc.preencode) return enc |
{ | ||
"name": "compact-encoding", | ||
"version": "2.7.0", | ||
"version": "2.8.0", | ||
"description": "A series of compact encoding schemes for building small and fast parsers and serializers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -118,2 +118,4 @@ # compact-encoding | ||
* `cenc.array(enc)` - Makes an array encoder from another encoder. Arrays are uint prefixed with their length. | ||
* `cenc.json` - Encodes a JSON value as utf-8. | ||
* `cenc.ndjson` - Encodes a JSON value as newline delimited utf-8. | ||
* `cenc.from(enc)` - Makes a compact encoder from a [codec](https://github.com/mafintosh/codecs) or [abstract-encoding](https://github.com/mafintosh/abstract-encoding). | ||
@@ -120,0 +122,0 @@ |
23
test.js
@@ -411,2 +411,25 @@ const enc = require('./') | ||
tape('json', function (t) { | ||
const state = enc.state() | ||
enc.json.preencode(state, { a: 1, b: 2 }) | ||
t.alike(state, { start: 0, end: 14, buffer: null }) | ||
state.buffer = Buffer.alloc(state.end) | ||
enc.json.encode(state, { a: 1, b: 2 }) | ||
t.alike(state, { | ||
start: 14, | ||
end: 14, | ||
buffer: Buffer.concat([ | ||
Buffer.from([13]), | ||
Buffer.from('{"a":1,"b":2}') | ||
]) | ||
}) | ||
state.start = 0 | ||
t.alike(enc.json.decode(state), { a: 1, b: 2 }) | ||
t.exception(() => enc.json.decode(state)) | ||
}) | ||
tape('lexint: big numbers', function (t) { | ||
@@ -413,0 +436,0 @@ t.plan(1) |
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
40707
989
125