Skip to content

Commit

Permalink
Merge pull request #39 from creately/sakota-issue-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
chandika authored Apr 6, 2022
2 parents e16b3aa + b55de19 commit 3e45b61
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 5 deletions.
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

0 comments on commit 3e45b61

Please sign in to comment.