Skip to content

Commit

Permalink
[Feat]: isObject for unwrap on object tree node (#98)
Browse files Browse the repository at this point in the history
plan to handle ember-data objects
  • Loading branch information
snewcomer authored Jan 29, 2021
1 parent 0a9d591 commit da68651
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ export class BufferedChangeset implements IChangeset {
const subContent = this.getDeep(baseContent, remaining.join('.'));
const subChanges = getSubObject(changes, key);
// give back and object that can further retrieve changes and/or content
const tree = new ObjectTreeNode(subChanges, subContent, this.getDeep);
const tree = new ObjectTreeNode(subChanges, subContent, this.getDeep, this.isObject);
return tree.proxy;
} else if (typeof result !== 'undefined') {
return result;
Expand Down Expand Up @@ -982,7 +982,7 @@ export class BufferedChangeset implements IChangeset {
}

// may still access a value on the changes or content objects
const tree = new ObjectTreeNode(subChanges, subContent, this.getDeep);
const tree = new ObjectTreeNode(subChanges, subContent, this.getDeep, this.isObject);
return tree.proxy;
}

Expand Down
9 changes: 6 additions & 3 deletions src/utils/normalize-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ import isObject from './is-object';
* @param {Object} target
* @return {Object}
*/
export default function normalizeObject<T extends { [key: string]: any }>(target: T): T {
if (!target || !isObject(target)) {
export default function normalizeObject<T extends { [key: string]: any }>(
target: T,
isObj: Function = isObject
): T {
if (!target || !isObj(target)) {
return target;
}

Expand All @@ -35,7 +38,7 @@ export default function normalizeObject<T extends { [key: string]: any }>(target

for (let key in obj) {
const next: any = obj[key];
if (next && isObject(next)) {
if (next && isObj(next)) {
if (Object.prototype.hasOwnProperty.call(next, 'value') && next instanceof Change) {
obj[key] = next.value;
} else {
Expand Down
7 changes: 4 additions & 3 deletions src/utils/object-tree-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ class ObjectTreeNode implements ProxyHandler {
constructor(
changes: Record<string, any> = {},
content: Content = {},
public safeGet: Function = defaultSafeGet
public safeGet: Function = defaultSafeGet,
public isObject: Function = isObject
) {
this.changes = changes;
this.content = content;
Expand All @@ -111,11 +112,11 @@ class ObjectTreeNode implements ProxyHandler {
let changes = this.changes;

if (isObject(changes)) {
changes = normalizeObject(changes);
changes = normalizeObject(changes, this.isObject);

const content = this.content;
if (isObject(content)) {
changes = normalizeObject(changes);
changes = normalizeObject(changes, this.isObject);
return { ...content, ...changes };
}
}
Expand Down
12 changes: 12 additions & 0 deletions test/utils/object-tree-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ describe('Unit | Utility | object tree node', () => {
expect(result.content).toEqual({ name: 'c' });
});

it('can pass custom isObject', () => {
function isObject() {
return true;
}
const result = new ObjectTreeNode({ name: 'z' }, { name: 'c' }, undefined, isObject);

expect(result.changes).toEqual({ name: 'z' });
expect(result.proxy.name).toBe('z');
expect(result.content).toEqual({ name: 'c' });
expect(result.unwrap()).toEqual({ name: 'z' });
});

it('it returns nested children', () => {
const initialVal = { details: { name: 'z' } };
const result = new ObjectTreeNode(initialVal, { details: { name: 'c' } });
Expand Down

0 comments on commit da68651

Please sign in to comment.