diff --git a/src/index.ts b/src/index.ts index d995879..d4538e1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, @@ -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 @@ -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; diff --git a/src/utils/object-tree-node.ts b/src/utils/object-tree-node.ts index bd96a6d..49855e2 100644 --- a/src/utils/object-tree-node.ts +++ b/src/utils/object-tree-node.ts @@ -130,10 +130,6 @@ class ObjectTreeNode implements ProxyHandler { return changes; } - - toJSON() { - return JSON.parse(JSON.stringify(this.unwrap())); - } } export { ObjectTreeNode }; diff --git a/test/index.test.ts b/test/index.test.ts index 423f314..9328b98 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -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' }]); }); @@ -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' } ]); }); @@ -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' } ]); @@ -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' } ]); @@ -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' } ]); @@ -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' @@ -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' @@ -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',