I have a typescript
class with various methods for checking variable types. How can I determine which method to use at the beginning of the doProcess()
for processing the input?
class MyClass {
public static arr : any[] = [];
// main method
public static doProcess(object : object , justForObjects : boolean = false){
if (justForObjects){
// specify the required checking method to use at this point
} else {
}
for (let key in object){
if (object.hasOwnProperty(key)){
if (/* specify checking method here */){
MyClass.doProcess(object[key] , justObjects)
} else {
MyClass.arr.push(object[key])
}
}
}
return MyClass.arr;
}
// methods for checking variable types
private static is_object(value : any){
return !!(typeof value === 'object' && !(value instanceof Array) && value);
}
private static is_object_or_array(value : any){
return !!(typeof value === 'object' && value);
}
}
let object = {
'main color' : 'black',
'other colors' : {
'front color' : 'purple',
'back color' : 'yellow',
'inside colors' : {
'top color' : 'red',
'bottom color' : 'green'
}
}
}
MyClass.doProcess(object , true);
Although it can be achieved in the same for loop (as shown below), I'd prefer to explore alternative ways first.
for (let key in object){
if(object.hasOwnProperty(key)){
if (justForObjects){
if (MyClass.is_object(object[key])){
// execute certain logic
}
} else {
if (MyClass.is_object_or_array(object[key])){
// execute different logic
}
}
}
}
Thank you for your guidance