Skip to content

Commit

Permalink
Added RealmObject.entries() (#5927)
Browse files Browse the repository at this point in the history
Co-authored-by: LJ <81748770+elle-j@users.noreply.github.com>
  • Loading branch information
papafe and elle-j committed Jun 23, 2023
1 parent 1c922d0 commit d357faf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
40 changes: 40 additions & 0 deletions integration-tests/tests/src/tests/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,46 @@ describe("Realm.Object", () => {
obj.getPropertyType("foo");
}).throws("Property 'foo' does not exist on 'AllTypesObject' objects");
});

it("returns entries correctly", function (this: Mocha.Context & RealmContext) {
const obj = this.realm.write(() => {
return this.realm.create<IPrimaryString>(PrimaryStringSchema.name, {
pk: "one",
value: 1,
});
});

let entries = obj.entries();
expect(entries).to.have.length(2);
expect(entries).to.have.deep.members([
["pk", "one"],
["value", 1],
]);

entries = Object.entries(obj);
expect(entries).to.have.length(2);
expect(entries).to.have.deep.members([
["pk", "one"],
["value", 1],
]);
});

it("returns keys correctly", function (this: Mocha.Context & RealmContext) {
const obj = this.realm.write(() => {
return this.realm.create<IPrimaryString>(PrimaryStringSchema.name, {
pk: "one",
value: 1,
});
});

let keys = obj.keys();
expect(keys).to.have.length(2);
expect(keys).to.have.deep.members(["pk", "value"]);

keys = Object.keys(obj);
expect(keys).to.have.length(2);
expect(keys).to.have.deep.members(["pk", "value"]);
});
});

describe("linktype properties", () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/realm/src/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,15 @@ export class RealmObject<T = DefaultObject> {
*/
private declare readonly [KEY_SET]: ReadonlySet<string>;

/** @deprecated Please use {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys | Object.keys()} */
keys(): string[] {
// copying to prevent caller from modifying the static array.
return [...this[KEY_ARRAY]];
}

/** @deprecated Please use {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries | Object.entries()} */
entries(): [string, unknown][] {
throw new Error("Not yet implemented");
return Object.entries(this);
}

/**
Expand Down

0 comments on commit d357faf

Please sign in to comment.