Last active
October 15, 2020 13:08
-
-
Save faisalmuhammad/4e129b7c10a1ef9fb1a6b03cb2a66587 to your computer and use it in GitHub Desktop.
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
export class Mapping { | |
/** | |
* Checks if the given json object is type of a given instance (class/interface) type. | |
* @param jsonObject Object to check. | |
* @param instanceType The type to check for the object. | |
* @returns true if object is of the given instance type; false otherwise. | |
*/ | |
public static isTypeOf<T>(jsonObject: Object, instanceType: { new(): T; }): boolean { | |
// Check that all the properties of the JSON Object are also available in the Class. | |
const instanceObject = new instanceType(); | |
for (let propertyName in instanceObject) { | |
// If any property in instance object is missing then we have a mismatch. | |
if (!jsonObject.hasOwnProperty(propertyName)) { | |
return false; | |
} | |
} | |
// All the properties are matching between object and the instance type. | |
return true; | |
}; | |
/** | |
* Checks if the given json object is type of a given instance (class/interface) type. | |
* @param jsonObject Object to check. | |
* @param instanceType The type to check for the object. | |
* @returns true if object is of the given instance type; false otherwise. | |
*/ | |
public static isCollectionTypeOf<T>(jsonObjectCollection: any[], instanceType: { new(): T; }): boolean { | |
// Check that all the properties of the JSON Object are also available in the Class. | |
const instanceObject = new instanceType(); | |
for (let jsonObject of jsonObjectCollection) { | |
for (let propertyName in instanceObject) { | |
// If any property in instance object is missing then we have a mismatch. | |
if (!jsonObject.hasOwnProperty(propertyName)) { | |
return false; | |
} | |
} | |
} | |
// All the properties are matching between object and the instance type. | |
return true; | |
}; | |
}; // End of class: Mapping |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment