compact-encoding
Advanced tools
+31
-0
@@ -861,2 +861,33 @@ const b4a = require('b4a') | ||
| const record = (exports.record = function (keyEncoding, valueEncoding) { | ||
| return { | ||
| preencode(state, v) { | ||
| const keys = Object.keys(v) | ||
| uint.preencode(state, keys.length) | ||
| for (const k of keys) { | ||
| keyEncoding.preencode(state, k) | ||
| valueEncoding.preencode(state, v[k]) | ||
| } | ||
| }, | ||
| encode(state, v) { | ||
| const keys = Object.keys(v) | ||
| uint.encode(state, keys.length) | ||
| for (const k of keys) { | ||
| keyEncoding.encode(state, k) | ||
| valueEncoding.encode(state, v[k]) | ||
| } | ||
| }, | ||
| decode(state) { | ||
| const out = Object.create(null) | ||
| const keys = uint.decode(state) | ||
| for (let i = 0; i < keys; i++) { | ||
| out[keyEncoding.decode(state)] = valueEncoding.decode(state) | ||
| } | ||
| return out | ||
| } | ||
| } | ||
| }) | ||
| exports.stringRecord = record(utf8, utf8) | ||
| function getType(o) { | ||
@@ -863,0 +894,0 @@ if (o === null || o === undefined) return 0 |
+1
-1
| { | ||
| "name": "compact-encoding", | ||
| "version": "2.18.0", | ||
| "version": "2.19.0", | ||
| "description": "A series of compact encoding schemes for building small and fast parsers and serializers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+43
-0
@@ -1142,1 +1142,44 @@ const enc = require('./') | ||
| }) | ||
| test('record', function (t) { | ||
| const encoding = enc.record(enc.string, enc.string) | ||
| t.alike( | ||
| enc.decode(encoding, enc.encode(encoding, { a: 'hello', b: 'world' })), | ||
| Object.assign(Object.create(null), { | ||
| a: 'hello', | ||
| b: 'world' | ||
| }) | ||
| ) | ||
| }) | ||
| test('record - nested', function (t) { | ||
| const encoding = enc.record(enc.string, enc.record(enc.string, enc.string)) | ||
| t.alike( | ||
| enc.decode( | ||
| encoding, | ||
| enc.encode(encoding, { | ||
| a: { b: 'record' }, | ||
| c: { d: 'nested', e: 'test' } | ||
| }) | ||
| ), | ||
| Object.assign(Object.create(null), { | ||
| a: Object.assign(Object.create(null), { b: 'record' }), | ||
| c: Object.assign(Object.create(null), { d: 'nested', e: 'test' }) | ||
| }) | ||
| ) | ||
| }) | ||
| test('stringRecord', function (t) { | ||
| t.alike( | ||
| enc.decode( | ||
| enc.stringRecord, | ||
| enc.encode(enc.stringRecord, { a: 'hello', b: 'world' }) | ||
| ), | ||
| Object.assign(Object.create(null), { | ||
| a: 'hello', | ||
| b: 'world' | ||
| }) | ||
| ) | ||
| }) |
82714
2.22%2143
3.23%