Skip to content

Commit 341d972

Browse files
committedNov 19, 2021
Add .toJSON() to subdocs
1 parent c94725d commit 341d972

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "crdtmap",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"license": "MIT",
55
"main": "./dist/crdtmap.cjs",
66
"scripts": {

‎src/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ function CrdtMap (options) {
188188
})
189189

190190
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
191202
}
192203
}
193204
subMaps.set(name, subMap)

‎src/index.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,25 @@ describe('subdocs', () => {
622622
expect(subMap2.entries()).toEqual([['key3', 'data3']])
623623
})
624624

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+
625644
test('getting same subdoc multiple times should result in same subdoc object', () => {
626645
const doc = CrdtMap()
627646
const subMap1 = doc.getMap('sub1')

0 commit comments

Comments
 (0)