Created
May 29, 2019 19:24
-
-
Save phenomnomnominal/a6bc59eea954176e1a90e11ab61a18d9 to your computer and use it in GitHub Desktop.
A decorator that fixes boolean input values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const VALUE_KEY = '__value_'; | |
export function isBool (): PropertyDecorator { | |
return (target: any, propertyKey: string) => { | |
Object.defineProperty(target, propertyKey, { | |
get: function () { | |
return this[`${VALUE_KEY}${propertyKey}`]; | |
}, | |
set: function (value) { | |
this[`${VALUE_KEY}${propertyKey}`] = value != null && `${value}` !== 'false'; | |
} | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment