Skip to content

Commit

Permalink
feat(core): take account of enumerable getter on clone (#1637)
Browse files Browse the repository at this point in the history
  • Loading branch information
aitboudad committed Jun 26, 2019
1 parent 7015bbe commit 66239d9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/core/src/lib/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,14 @@ describe('clone', () => {
expect(clone(d)).toEqual(d);
expect(clone(d) === d).toBeFalsy();
});

it('Enumerable getter', () => {
const d = {};
Object.defineProperty(d, 'a', { get: () => 'test', enumerable: true });
const value = clone(d);

const propDescriptor = Object.getOwnPropertyDescriptor(value, 'a');
expect(propDescriptor.get).toBeDefined();
expect(propDescriptor.enumerable).toBeTruthy();
});
});
13 changes: 10 additions & 3 deletions src/core/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,17 @@ export function clone(value: any): any {
return value.slice(0).map(v => clone(v));
}

value = Object.assign({}, value);
Object.keys(value).forEach(k => value[k] = clone(value[k]));
return Object.keys(value).reduce((newVal, prop) => {
const propDescriptor = Object.getOwnPropertyDescriptor(value, prop);

return value;
if (propDescriptor.get) {
Object.defineProperty(newVal, prop, { ...propDescriptor, get: () => clone(value[prop]) });
} else {
newVal[prop] = clone(value[prop]);
}

return newVal;
}, {});
}

export function defineHiddenProp(field, prop, defaultValue) {
Expand Down
2 changes: 1 addition & 1 deletion src/material/form-field/src/form-field.wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class FormlyWrapperFormField extends FieldWrapper<MatFormlyFieldConfig> i
defineHiddenProp(this.field, '__formField__', this.formField);

const fieldComponent = this.formlyField['_componentFactory'];
if (!(fieldComponent.componentRef.instance instanceof FieldType)) {
if (fieldComponent && !(fieldComponent.componentRef.instance instanceof FieldType)) {
console.warn(`Component '${fieldComponent.component.prototype.constructor.name}' must extend 'FieldType' from '@ngx-formly/material'.`);
}

Expand Down

0 comments on commit 66239d9

Please sign in to comment.