Skip to content

Commit

Permalink
✅ test: Nested arrays are YArrays in objects
Browse files Browse the repository at this point in the history
  • Loading branch information
joebobmiles committed Jul 9, 2021
1 parent 1c8d513 commit 70f6db0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/utility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,20 @@ describe("objectToYMap", () =>
.get("bar"))
.toBe(2);
});

it("Converts nested arrays into nested YArrays.", () =>
{
const ydoc = new Y.Doc();
const ymap = ydoc.getMap("tmp");

ymap.set("map", objectToYMap({ "foo": [ 1, 2 ], }));

expect(ymap.get("map").toJSON()).toEqual({ "foo": [ 1, 2 ], });
expect(ymap.get("map").get("foo")
.get(0)).toBe(1);
expect(ymap.get("map").get("foo")
.get(1)).toBe(2);
});
});

describe("yMapToObject", () =>
Expand Down
5 changes: 4 additions & 1 deletion src/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export const objectToYMap = (object: any): Y.Map<any> =>

Object.entries(object).forEach(([ property, value ]) =>
{
if (value instanceof Object)
if (value instanceof Array)
ymap.set(property, arrayToYArray(value));

else if (value instanceof Object)
ymap.set(property, objectToYMap(value));

else
Expand Down

0 comments on commit 70f6db0

Please sign in to comment.