File tree 3 files changed +31
-1
lines changed
3 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " crdtmap" ,
3
- "version" : " 0.1.2 " ,
3
+ "version" : " 0.1.3 " ,
4
4
"license" : " MIT" ,
5
5
"main" : " ./dist/crdtmap.cjs" ,
6
6
"scripts" : {
Original file line number Diff line number Diff line change @@ -188,6 +188,17 @@ function CrdtMap (options) {
188
188
} )
189
189
190
190
return results
191
+ } ,
192
+ toJSON : function ( ) {
193
+ const obj = { }
194
+
195
+ map . forEach ( ( value , key ) => {
196
+ if ( value . data !== null && key . startsWith ( prefix ) ) {
197
+ obj [ key . substr ( prefix . length ) ] = value . data
198
+ }
199
+ } )
200
+
201
+ return obj
191
202
}
192
203
}
193
204
subMaps . set ( name , subMap )
Original file line number Diff line number Diff line change @@ -622,6 +622,25 @@ describe('subdocs', () => {
622
622
expect ( subMap2 . entries ( ) ) . toEqual ( [ [ 'key3' , 'data3' ] ] )
623
623
} )
624
624
625
+ test ( 'subdoc toJSON()' , ( ) => {
626
+ const doc = CrdtMap ( )
627
+ const subMap1 = doc . getMap ( 'sub1' )
628
+ const subMap2 = doc . getMap ( 'sub2' )
629
+
630
+ doc . set ( 'key1' , 'data1' )
631
+ subMap1 . set ( 'key2' , 'data2' )
632
+ subMap2 . set ( 'key3' , 'data3' )
633
+ subMap1 . set ( 'key2-2' , 'data2-2' )
634
+ subMap1 . set ( 'removed-key' , 'data' , 1000 )
635
+ subMap1 . delete ( 'removed-key' , 1001 )
636
+
637
+ expect ( subMap1 . toJSON ( ) ) . toEqual ( {
638
+ key2 : 'data2' ,
639
+ 'key2-2' : 'data2-2'
640
+ } )
641
+ expect ( subMap2 . toJSON ( ) ) . toEqual ( { key3 : 'data3' } )
642
+ } )
643
+
625
644
test ( 'getting same subdoc multiple times should result in same subdoc object' , ( ) => {
626
645
const doc = CrdtMap ( )
627
646
const subMap1 = doc . getMap ( 'sub1' )
You can’t perform that action at this time.
0 commit comments