compact-encoding 2.15.1 → 2.16.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.
- package/README.md +1 -0
- package/index.js +15 -1
- package/package.json +1 -1
- package/test.js +7 -0
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
|
@@ -492,6 +492,18 @@ exports.frame = function frame (enc) {
|
|
|
492
492
|
}
|
|
493
493
|
}
|
|
494
494
|
|
|
495
|
+
exports.date = {
|
|
496
|
+
preencode (state, d) {
|
|
497
|
+
uint.preencode(state, d.getTime())
|
|
498
|
+
},
|
|
499
|
+
encode (state, d) {
|
|
500
|
+
uint.encode(state, d.getTime())
|
|
501
|
+
},
|
|
502
|
+
decode (state, d) {
|
|
503
|
+
return new Date(uint.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
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
|
|