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

Sakota issue fixes #39

Merged
merged 3 commits into from
Apr 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2022-04-06 - v2.4.6

- bug fixes on mergeChanges and unwrap methods

# 2021-10-27 - v2.4.5

- Fix the ownKeys function type issue.
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@creately/sakota",
"version": "2.4.5",
"version": "2.4.6",
"description": "Proxies js objects and records all changes made on an object without modifying the object.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
53 changes: 52 additions & 1 deletion src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,47 @@ describe('Sakota', () => {
wrapped.__sakota__.mergeChanges(wrapped1.__sakota__.getChanges());
expect(wrapped).toEqual(target as any);
});

it('should merge only upto 2 levels', () => {
const entity = {
data: { d1: 'data1' },
};
const wrapped = Sakota.create(entity);
const modifier = { $set: { 'links.l1': { id: 'l1' } } };
spyOn(console, 'warn');
wrapped.__sakota__.mergeChanges(modifier);
expect(wrapped).toEqual({
...entity,
links: {
l1: {
id: 'l1',
},
},
} as any);
expect(console.warn).toHaveBeenCalled();
});

it('throw if the modifier is incorrect', () => {
const entity = {
data: { d1: 'data1' },
};
const wrapped = Sakota.create(entity);
const modifier = { $set: { 'links.l1.data': 'data1' } };
spyOn(console, 'warn');
expect(() => wrapped.__sakota__.mergeChanges(modifier)).toThrow();
expect(console.warn).toHaveBeenCalled();
});

it('throw if the modifier is incorrect(2)', () => {
const entity = {
data: { d1: 'data1' },
};
const wrapped = Sakota.create(entity);
const modifier = { $unset: { 'links.l1': true } };
spyOn(console, 'warn');
expect(() => wrapped.__sakota__.mergeChanges(modifier)).toThrow();
expect(console.warn).toHaveBeenCalled();
});
});

// Test for unwrap
Expand Down Expand Up @@ -797,13 +838,23 @@ describe('Sakota', () => {
a: [{ b: 234 }],
};
const wrapped = Sakota.create(obj);
delete obj.a[0].b;
delete wrapped.a[0].b;

const unwrapped = wrapped.__sakota__.unwrap(true);
expect(unwrapped === obj).toBeTruthy();
expect(unwrapped).toEqual({ a: [{}] });
expect(Sakota.hasSakota(unwrapped)).toBeFalsy();
});

it('should preserve the object type information after unwrap', () => {
// class APoint extends Point {}
const obj: any = new Point();
const wrapped = Sakota.create(obj);
wrapped.p = { x: 2, y: 3 };

const unwrapped = wrapped.__sakota__.unwrap();
expect(unwrapped).toEqual(new Point(2, 3));
});
});

describe('mergeChanges + unwrap', () => {
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ export class Sakota<T extends object> implements ProxyHandler<T> {
} else {
console.warn('unexpected modifier', { path: k, modifier: changes });
const skeys = Object.keys(kidChanges[k].$set);
const ukeys = Object.keys(kidChanges[k].$set);
const ukeys = Object.keys(kidChanges[k].$unset);
if (skeys.length === 0 || ukeys.length > 0 || skeys.some(k => k.includes('.'))) {
throw new Error('Invalid modifier'); // this scenario is not expected.
} else {
Expand Down Expand Up @@ -459,7 +459,12 @@ export class Sakota<T extends object> implements ProxyHandler<T> {
val = inplace ? this.target : this.target.slice();
Object.keys($set).forEach(k => (val[k] = $set[k]));
} else {
val = inplace ? Object.assign(this.target, $set) : Object.assign({}, this.target, $set);
if (inplace) {
val = Object.assign(this.target, $set);
} else {
val = Object.assign({}, this.target, $set);
Object.setPrototypeOf(val, Object.getPrototypeOf(this.target));
}
}
$unset.forEach(k => delete val[k]);
return val;
Expand Down