Created
July 13, 2020 16:38
-
-
Save alaindet/bd31154be9b5634aa58a12226a7177c6 to your computer and use it in GitHub Desktop.
This file contains 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 toBoolean = (context: any, flags: string[]) => { | |
for (const flag of flags) { | |
const propValue = context[flag]; | |
const propType = typeof propValue; | |
if ('boolean' === propType) { | |
context[flag] = propValue; | |
} | |
else if ('string' == propType) { | |
if (propValue === 'false' || propValue === 'no') { | |
context[flag] = false; | |
} else { | |
context[flag] = true; | |
} | |
} | |
else { | |
context[flag] = !!propValue; | |
} | |
} | |
}; | |
interface ICommon { | |
booleanish: string | boolean; | |
} | |
class MyClass { | |
flag: ICommon['booleanish']; | |
constructor(flag: ICommon['booleanish']) { | |
this.flag = flag; | |
toBoolean(this, ['flag']); | |
} | |
} | |
const obj = new MyClass('no'); | |
console.log(obj.flag); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment