compact-encoding 2.18.0 → 2.19.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 (3) hide show
  1. package/index.js +31 -0
  2. package/package.json +1 -1
  3. package/test.js +43 -0
package/index.js CHANGED
@@ -858,6 +858,37 @@ exports.ipAddress = {
858
858
  }
859
859
  }
860
860
 
861
+ const record = (exports.record = function (keyEncoding, valueEncoding) {
862
+ return {
863
+ preencode(state, v) {
864
+ const keys = Object.keys(v)
865
+ uint.preencode(state, keys.length)
866
+ for (const k of keys) {
867
+ keyEncoding.preencode(state, k)
868
+ valueEncoding.preencode(state, v[k])
869
+ }
870
+ },
871
+ encode(state, v) {
872
+ const keys = Object.keys(v)
873
+ uint.encode(state, keys.length)
874
+ for (const k of keys) {
875
+ keyEncoding.encode(state, k)
876
+ valueEncoding.encode(state, v[k])
877
+ }
878
+ },
879
+ decode(state) {
880
+ const out = Object.create(null)
881
+ const keys = uint.decode(state)
882
+ for (let i = 0; i < keys; i++) {
883
+ out[keyEncoding.decode(state)] = valueEncoding.decode(state)
884
+ }
885
+ return out
886
+ }
887
+ }
888
+ })
889
+
890
+ exports.stringRecord = record(utf8, utf8)
891
+
861
892
  function getType(o) {
862
893
  if (o === null || o === undefined) return 0
863
894
  if (typeof o === 'boolean') return 1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "2.18.0",
3
+ "version": "2.19.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
@@ -1139,3 +1139,46 @@ test('dual ip + port', function (t) {
1139
1139
  )
1140
1140
  }
1141
1141
  })
1142
+
1143
+ test('record', function (t) {
1144
+ const encoding = enc.record(enc.string, enc.string)
1145
+
1146
+ t.alike(
1147
+ enc.decode(encoding, enc.encode(encoding, { a: 'hello', b: 'world' })),
1148
+ Object.assign(Object.create(null), {
1149
+ a: 'hello',
1150
+ b: 'world'
1151
+ })
1152
+ )
1153
+ })
1154
+
1155
+ test('record - nested', function (t) {
1156
+ const encoding = enc.record(enc.string, enc.record(enc.string, enc.string))
1157
+
1158
+ t.alike(
1159
+ enc.decode(
1160
+ encoding,
1161
+ enc.encode(encoding, {
1162
+ a: { b: 'record' },
1163
+ c: { d: 'nested', e: 'test' }
1164
+ })
1165
+ ),
1166
+ Object.assign(Object.create(null), {
1167
+ a: Object.assign(Object.create(null), { b: 'record' }),
1168
+ c: Object.assign(Object.create(null), { d: 'nested', e: 'test' })
1169
+ })
1170
+ )
1171
+ })
1172
+
1173
+ test('stringRecord', function (t) {
1174
+ t.alike(
1175
+ enc.decode(
1176
+ enc.stringRecord,
1177
+ enc.encode(enc.stringRecord, { a: 'hello', b: 'world' })
1178
+ ),
1179
+ Object.assign(Object.create(null), {
1180
+ a: 'hello',
1181
+ b: 'world'
1182
+ })
1183
+ )
1184
+ })