Skip to content

Commit

Permalink
Refactor: no unwrap for arrays (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer authored Feb 25, 2021
1 parent 9c68413 commit 0eef8e0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
36 changes: 20 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import take from './utils/take';
import mergeDeep from './utils/merge-deep';
import setDeep from './utils/set-deep';
import getDeep, { getSubObject } from './utils/get-deep';
import { objectToArray, arrayToObject } from './utils/array-object';

import {
Changes,
Expand Down Expand Up @@ -966,22 +967,6 @@ export class BufferedChangeset implements IChangeset {
} else if (typeof result !== 'undefined') {
return result;
}

// TODO: make more obvious we are dealing with arrays with branches above
const baseContent = this.safeGet(content, baseKey);
if (Array.isArray(baseContent)) {
const subChanges = getSubObject(changes, key);

if (!subChanges) {
return this.getDeep(baseContent, remaining.join('.'));
}

// give back an object that can further retrieve changes and/or content
// TODO: consider different construct to handle arrays. Arrays don't fit right with ObjectTreeNode
const tree = new ObjectTreeNode(subChanges, baseContent, this.getDeep, this.isObject);

return tree;
}
}

// this comes after the isObject check to ensure we don't lose remaining keys
Expand All @@ -1007,6 +992,25 @@ export class BufferedChangeset implements IChangeset {
// may still access a value on the changes or content objects
const tree = new ObjectTreeNode(subChanges, subContent, this.getDeep, this.isObject);
return tree.proxy;
} else if (Array.isArray(subContent)) {
let subChanges = this.getDeep(changes, key);
if (!subChanges) {
// return array of contents. Dont need to worry about further access sibling keys in array case
return subContent;
}

if (isObject(subChanges)) {
if (isObject(subContent)) {
subChanges = normalizeObject(subChanges, this.isObject);
return { ...subContent, ...subChanges };
} else if (Array.isArray(subContent)) {
subChanges = normalizeObject(subChanges, this.isObject);

return objectToArray(mergeDeep(arrayToObject(subContent), subChanges));
}
}

return subChanges;
}

return subContent;
Expand Down
4 changes: 0 additions & 4 deletions src/utils/object-tree-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ class ObjectTreeNode implements ProxyHandler {

return changes;
}

toJSON() {
return JSON.parse(JSON.stringify(this.unwrap()));
}
}

export { ObjectTreeNode };
16 changes: 8 additions & 8 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ describe('Unit | Utility | changeset', () => {
changeset.set('emails.0.primary', 'fun@email.com');

expect(changeset.get('emails.0.primary')).toEqual('fun@email.com');
expect(changeset.get('emails').unwrap()).toEqual([{ primary: 'fun@email.com' }]);
expect(changeset.get('emails')).toEqual([{ primary: 'fun@email.com' }]);
expect(changeset.changes).toEqual([{ key: 'emails.0.primary', value: 'fun@email.com' }]);
});

Expand All @@ -948,7 +948,7 @@ describe('Unit | Utility | changeset', () => {

expect(changeset.get('emails.0.funEmail')).toEqual('fun@email.com');
expect(changeset.changes).toEqual([{ key: 'emails.0.funEmail', value: 'fun@email.com' }]);
expect(changeset.get('emails').unwrap()).toEqual([
expect(changeset.get('emails')).toEqual([
{ primary: 'bob@email.com', funEmail: 'fun@email.com' }
]);
});
Expand All @@ -961,7 +961,7 @@ describe('Unit | Utility | changeset', () => {

expect(changeset.get('emails.1.funEmail')).toEqual('fun@email.com');
expect(changeset.get('emails.1.primary')).toEqual('primary@email.com');
expect(changeset.get('emails').unwrap()).toEqual([
expect(changeset.get('emails')).toEqual([
{ primary: 'bob@email.com' },
{ primary: 'primary@email.com', funEmail: 'fun@email.com' }
]);
Expand All @@ -981,7 +981,7 @@ describe('Unit | Utility | changeset', () => {

expect(changeset.get('emails.1.funEmail')).toEqual('fun@email.com');
expect(changeset.get('emails.1.primary')).toEqual('primary@email.com');
expect(changeset.get('emails').unwrap()).toEqual([
expect(changeset.get('emails')).toEqual([
{ primary: 'bob@email.com' },
{ primary: 'primary@email.com', funEmail: 'fun@email.com' }
]);
Expand All @@ -1004,7 +1004,7 @@ describe('Unit | Utility | changeset', () => {
}
}
]);
expect(changeset.get('emails').unwrap()).toEqual([
expect(changeset.get('emails')).toEqual([
{ primary: 'bob@email.com' },
{ primary: 'primary2@email.com', funEmail: 'fun@email.com' }
]);
Expand All @@ -1028,7 +1028,7 @@ describe('Unit | Utility | changeset', () => {

expect(changeset.get('emails.0.fun')).toEqual('fun0@email.com');
expect(changeset.get('emails.0.primary')).toEqual('primary0@email.com');
expect(changeset.get('emails').unwrap()).toEqual([
expect(changeset.get('emails')).toEqual([
{
fun: 'fun0@email.com',
primary: 'primary0@email.com'
Expand All @@ -1047,7 +1047,7 @@ describe('Unit | Utility | changeset', () => {
primary: 'brandNewPrimary@email.com'
});

expect(changeset.get('emails').unwrap()).toEqual([
expect(changeset.get('emails')).toEqual([
{
fun: 'fun0@email.com',
primary: 'primary0@email.com'
Expand Down Expand Up @@ -1086,7 +1086,7 @@ describe('Unit | Utility | changeset', () => {

changeset.set('emails.1', null);

expect(changeset.get('emails').unwrap()).toEqual([
expect(changeset.get('emails')).toEqual([
{
fun: 'fun0@email.com',
primary: 'primary0@email.com',
Expand Down

0 comments on commit 0eef8e0

Please sign in to comment.