Skip to content

Commit

Permalink
Add the toJS method to the ObjectProxy's handler (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
chacha912 authored Jan 31, 2023
1 parent ecc8fb3 commit 98d42f9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/document/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export class Document<T> implements Observable<DocEvent> {
/**
* `getRoot` returns a new proxy of cloned root.
*/
public getRoot(): T {
public getRoot(): JSONObject<T> {
this.ensureClone();

const context = ChangeContext.create(this.changeID.next(), this.clone!);
Expand Down Expand Up @@ -401,14 +401,14 @@ export class Document<T> implements Observable<DocEvent> {
}

/**
* `toJSON` returns the JSON encoding of this array.
* `toJSON` returns the JSON encoding of this document.
*/
public toJSON(): string {
return this.root.toJSON();
}

/**
* `toJSON` returns the sorted JSON encoding of this array.
* `toSortedJSON` returns the sorted JSON encoding of this document.
*/
public toSortedJSON(): string {
return this.root.toSortedJSON();
Expand Down
9 changes: 9 additions & 0 deletions src/document/json/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export type JSONObject<T> = {
* `toJSON` returns the JSON encoding of this object.
*/
toJSON?(): string;

/**
* `toJS` returns the JSON object of this object.
*/
toJS?(): T;
} & T;

/**
Expand Down Expand Up @@ -96,6 +101,10 @@ export class ObjectProxy {
return (): string => {
return target.toJSON();
};
} else if (keyOrMethod === 'toJS') {
return (): object => {
return target.toJS();
};
}

return toJSONElement(context, target.get(keyOrMethod));
Expand Down
17 changes: 17 additions & 0 deletions test/integration/object_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { assert } from 'chai';
import { JSONObject } from '@yorkie-js-sdk/src/yorkie';
import { DocEventType, Document } from '@yorkie-js-sdk/src/document/document';
import { withTwoClientsAndDocuments } from '@yorkie-js-sdk/test/integration/integration_helper';

Expand Down Expand Up @@ -76,6 +77,22 @@ describe('Object', function () {
assert.equal('{"k1":{"k1-2":"v2","k1-3":"v4"}}', doc.toSortedJSON());
});

it('should support toJS and toJSON methods', function () {
const doc = Document.create<{
content: JSONObject<{ a: number; b: number; c: number }>;
}>('test-doc');
doc.update((root) => {
root.content = { a: 1, b: 2, c: 3 };
}, 'set a, b, c');
assert.equal(doc.toSortedJSON(), '{"content":{"a":1,"b":2,"c":3}}');

const root = doc.getRoot();
assert.equal(root.toJSON!(), '{"content":{"a":1,"b":2,"c":3}}');
assert.deepEqual(root.toJS!(), { content: { a: 1, b: 2, c: 3 } });
assert.equal(root.content.toJSON!(), '{"a":1,"b":2,"c":3}');
assert.deepEqual(root.content.toJS!(), { a: 1, b: 2, c: 3 });
});

it('Object.keys, Object.values and Object.entries test', function () {
const doc = Document.create<{
content: { a: number; b: number; c: number };
Expand Down

0 comments on commit 98d42f9

Please sign in to comment.