compact-encoding 2.15.1 → 2.16.1

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.
package/README.md CHANGED
@@ -143,6 +143,7 @@ to build others on top. Feel free to PR more that are missing.
143
143
  * `cenc.fixed32` - Encodes a fixed 32 byte buffer.
144
144
  * `cenc.fixed64` - Encodes a fixed 64 byte buffer.
145
145
  * `cenc.fixed(n)` - Makes a fixed sized encoder.
146
+ * `cenc.date(d)` - Encodes a date object.
146
147
  * `cenc.array(enc)` - Makes an array encoder from another encoder. Arrays are uint prefixed with their length.
147
148
  * `cenc.raw.array(enc)` - Makes an array encoder from another encoder, without a length prefixed.
148
149
  * `cenc.json` - Encodes a JSON value as utf-8.
package/index.js CHANGED
@@ -172,7 +172,7 @@ const uint64 = exports.uint64 = {
172
172
  }
173
173
  }
174
174
 
175
- exports.int = zigZagInt(uint)
175
+ const int = exports.int = zigZagInt(uint)
176
176
  exports.int8 = zigZagInt(uint8)
177
177
  exports.int16 = zigZagInt(uint16)
178
178
  exports.int24 = zigZagInt(uint24)
@@ -492,6 +492,18 @@ exports.frame = function frame (enc) {
492
492
  }
493
493
  }
494
494
 
495
+ exports.date = {
496
+ preencode (state, d) {
497
+ int.preencode(state, d.getTime())
498
+ },
499
+ encode (state, d) {
500
+ int.encode(state, d.getTime())
501
+ },
502
+ decode (state, d) {
503
+ return new Date(int.decode(state))
504
+ }
505
+ }
506
+
495
507
  exports.json = {
496
508
  preencode (state, v) {
497
509
  utf8.preencode(state, JSON.stringify(v))
@@ -591,7 +603,8 @@ const anyTypes = [
591
603
  exports.int,
592
604
  exports.float64,
593
605
  anyArray,
594
- anyObject
606
+ anyObject,
607
+ exports.date
595
608
  ]
596
609
 
597
610
  const any = exports.any = {
@@ -622,6 +635,7 @@ function getType (o) {
622
635
  return 6
623
636
  }
624
637
  if (Array.isArray(o)) return 7
638
+ if (o instanceof Date) return 9
625
639
  if (typeof o === 'object') return 8
626
640
 
627
641
  throw new Error('Unsupported type for ' + o)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compact-encoding",
3
- "version": "2.15.1",
3
+ "version": "2.16.1",
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
@@ -810,6 +810,12 @@ test('lexint: unpack', function (t) {
810
810
  }
811
811
  })
812
812
 
813
+ test('date', function (t) {
814
+ const d = new Date()
815
+
816
+ t.alike(enc.decode(enc.date, enc.encode(enc.date, d)), d)
817
+ })
818
+
813
819
  test('any', function (t) {
814
820
  const o = {
815
821
  hello: 'world',
@@ -817,6 +823,7 @@ test('any', function (t) {
817
823
  neg: -42,
818
824
  arr: [{ yes: 1 }, { no: false }],
819
825
  nest: {},
826
+ today: new Date(),
820
827
  float: 0.54
821
828
  }
822
829