compact-encoding 2.7.0 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +2 -0
  2. package/index.js +25 -1
  3. package/package.json +1 -1
  4. package/test.js +23 -0
package/README.md CHANGED
@@ -115,6 +115,8 @@ to build others on top. Feel free to PR more that are missing.
115
115
  * `cenc.fixed64` - Encodes a fixed 64 byte buffer.
116
116
  * `cenc.fixed(n)` - Makes a fixed sized encoder.
117
117
  * `cenc.array(enc)` - Makes an array encoder from another encoder. Arrays are uint prefixed with their length.
118
+ * `cenc.json` - Encodes a JSON value as utf-8.
119
+ * `cenc.ndjson` - Encodes a JSON value as newline delimited utf-8.
118
120
  * `cenc.from(enc)` - Makes a compact encoder from a [codec](https://github.com/mafintosh/codecs) or [abstract-encoding](https://github.com/mafintosh/abstract-encoding).
119
121
 
120
122
  ## License
package/index.js CHANGED
@@ -307,7 +307,7 @@ function string (encoding) {
307
307
  }
308
308
  }
309
309
 
310
- exports.string = exports.utf8 = string('utf-8')
310
+ const utf8 = exports.string = exports.utf8 = string('utf-8')
311
311
  exports.ascii = string('ascii')
312
312
  exports.hex = string('hex')
313
313
  exports.base64 = string('base64')
@@ -377,6 +377,30 @@ exports.array = function array (enc) {
377
377
  }
378
378
  }
379
379
 
380
+ exports.json = {
381
+ preencode (state, v) {
382
+ utf8.preencode(state, JSON.stringify(v))
383
+ },
384
+ encode (state, v) {
385
+ utf8.encode(state, JSON.stringify(v))
386
+ },
387
+ decode (state) {
388
+ return JSON.parse(utf8.decode(state))
389
+ }
390
+ }
391
+
392
+ exports.ndjson = {
393
+ preencode (state, v) {
394
+ utf8.preencode(state, JSON.stringify(v) + '\n')
395
+ },
396
+ encode (state, v) {
397
+ utf8.encode(state, JSON.stringify(v) + '\n')
398
+ },
399
+ decode (state) {
400
+ return JSON.parse(utf8.decode(state))
401
+ }
402
+ }
403
+
380
404
  exports.from = function from (enc) {
381
405
  if (enc.preencode) return enc
382
406
  if (enc.encodingLength) return fromAbstractEncoder(enc)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "2.7.0",
3
+ "version": "2.8.0",
4
4
  "description": "A series of compact encoding schemes for building small and fast parsers and serializers",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/test.js CHANGED
@@ -408,6 +408,29 @@ tape('array', function (t) {
408
408
  t.exception(() => arr.decode(state))
409
409
  })
410
410
 
411
+ tape('json', function (t) {
412
+ const state = enc.state()
413
+
414
+ enc.json.preencode(state, { a: 1, b: 2 })
415
+ t.alike(state, { start: 0, end: 14, buffer: null })
416
+
417
+ state.buffer = Buffer.alloc(state.end)
418
+ enc.json.encode(state, { a: 1, b: 2 })
419
+ t.alike(state, {
420
+ start: 14,
421
+ end: 14,
422
+ buffer: Buffer.concat([
423
+ Buffer.from([13]),
424
+ Buffer.from('{"a":1,"b":2}')
425
+ ])
426
+ })
427
+
428
+ state.start = 0
429
+ t.alike(enc.json.decode(state), { a: 1, b: 2 })
430
+
431
+ t.exception(() => enc.json.decode(state))
432
+ })
433
+
411
434
  tape('lexint: big numbers', function (t) {
412
435
  t.plan(1)
413
436