Last active
January 22, 2021 14:34
-
-
Save Gummiees/7f4be1233c952de6145e8f4f6cb9d664 to your computer and use it in GitHub Desktop.
Checks if an object is has all the properties defined.
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
/** | |
* Checks if an object is has all the properties defined. | |
* | |
* Attention: It does not work recursively. | |
* @param obj Object to scan. | |
* @param type Object type. | |
* @returns Boolean, depending on if all the properties are defined or not. | |
*/ | |
defined<T>(obj: T, type: new () => T): boolean { | |
if (!obj) { | |
return false; | |
} | |
let isDefined: boolean = true; | |
const backupObj: T = new type(); | |
const keys: any[] = Object.keys(backupObj); | |
keys.forEach((key: string) => { | |
if (obj[key] === null || obj[key] === undefined || obj[key] === "") { | |
isDefined = false; | |
} | |
}); | |
return isDefined; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment