Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the toJS method to the ObjectProxy's handler #449

Merged
merged 2 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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