fix(core): fix decorator defalut values
This commit is contained in:
parent
3993279527
commit
bd1dcb5f11
@ -297,21 +297,20 @@ export function makeDecorator(
|
||||
}
|
||||
|
||||
function makeMetadataCtor(props: ([string, any] | {[key: string]: any})[]): any {
|
||||
function ctor(...args: any[]) {
|
||||
return function ctor(...args: any[]) {
|
||||
props.forEach((prop, i) => {
|
||||
const argVal = args[i];
|
||||
if (Array.isArray(prop)) {
|
||||
// plain parameter
|
||||
this[prop[0]] = !argVal || argVal === undefined ? prop[1] : argVal;
|
||||
this[prop[0]] = argVal === undefined ? prop[1] : argVal;
|
||||
} else {
|
||||
for (let propName in prop) {
|
||||
for (const propName in prop) {
|
||||
this[propName] =
|
||||
!argVal || argVal[propName] === undefined ? prop[propName] : argVal[propName];
|
||||
argVal && argVal.hasOwnProperty(propName) ? argVal[propName] : prop[propName];
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return ctor;
|
||||
};
|
||||
}
|
||||
|
||||
export function makeParamDecorator(
|
||||
|
@ -35,6 +35,18 @@ export function main() {
|
||||
const p = reflector.propMetadata(TestClass);
|
||||
expect(p['watch']).toEqual([new Prop('firefox!')]);
|
||||
});
|
||||
|
||||
it('should work with any default plain values', () => {
|
||||
const Default = makePropDecorator('Default', [['value', 5]]);
|
||||
expect(new Default(0)['value']).toEqual(0);
|
||||
});
|
||||
|
||||
it('should work with any object values', () => {
|
||||
// make sure we don't walk up the prototype chain
|
||||
const Default = makePropDecorator('Default', [{value: 5}]);
|
||||
const value = Object.create({value: 10});
|
||||
expect(new Default(value)['value']).toEqual(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe('decorators', () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user