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 support for array.every #52

Merged
merged 2 commits into from
Feb 22, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/core/src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ function arrayImplementation<T>(arr: Y.Array<T>) {
return [].forEach.apply(slice.apply(this), arguments);
} as T[]["forEach"],

every: function () {
return [].every.apply(slice.apply(this), arguments);
},

filter: function () {
return [].filter.apply(slice.apply(this), arguments);
} as T[]["filter"],
Expand Down Expand Up @@ -142,9 +146,8 @@ export function crdtArray<T>(initializer: T[], arr = new Y.Array<T>()) {
if (typeof p !== "number") {
throw new Error();
}
throw new Error("array assignment is not implemented / supported");
// TODO map.set(p, smartValue(value));
return true;
throw new Error("array assignment is not implemented / supported");
},
get: (target, pArg, receiver) => {
const p = propertyToNumber(pArg);
Expand Down
9 changes: 9 additions & 0 deletions packages/core/test/crdt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ describe("SyncedStore", () => {
expect(store1.arr).toEqual([0, 1]);
});

it("every() for array", () => {
let store1 = syncedStore({ map: {} as any }).map;
store1.arr = [0, 1, 2, 3];
const areAllNumbersSmallerThan10 = store1.arr.every((n) => n < 10);
expect(areAllNumbersSmallerThan10).toEqual(true);
const areAllNumbersSmallersThan2 = store1.arr.every((n) => n < 2);
expect(areAllNumbersSmallersThan2).toEqual(false);
});

it("move already inserted object to different location in document (nested)", () => {
let store1 = syncedStore({ map: {} as any }).map;
store1.mymap = {};
Expand Down