Trial Script to map any object/array to an structure object for easier interface mapping
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
console.log("structure", this.fetchObjectStructure([ | |
{ | |
attributes: [ 1, 2, 3 ] | |
}, | |
{ | |
attributes: [ "a", "b" ] | |
} | |
])); | |
fetchObjectStructure(data: any) { | |
if(data === null) { | |
return "null"; | |
} | |
switch(data.__proto__.constructor.name) { | |
case 'Object': { | |
const structure: { [prop: string]: any, [prop: number]: any} = {}; | |
for(let key in data) { | |
const temp = this.fetchObjectStructure(data[key]); | |
if(structure[key] === undefined || structure[key] === "null") { | |
structure[key] = temp; | |
if(structure[key].__proto__.constructor.name==="String") { | |
structure[key] = structure[key].toLowerCase() + ";"; | |
} | |
return; | |
} | |
if(structure[key].__proto__.constructor.name === temp.__proto__.constructor.name) { | |
if(structure[key].__proto__.constructor.name === "Object" && temp.__proto__.constructor.name === "Object") { | |
Object.assign(structure[key], temp); | |
} else if(structure[key].__proto__.constructor.name === 'Array' && temp.__proto__.constructor.name === 'Array') { | |
Object.assign(structure[key][0], temp[0]); | |
} | |
} else { | |
structure[key] = JSON.stringify(structure[key])+" | "+JSON.stringify(temp); | |
} | |
} | |
return structure; | |
} | |
case 'Array': { | |
let structure: any; | |
for(let item of data) { | |
const subStructure = this.fetchObjectStructure(item); | |
if(subStructure.__proto__.constructor.name === 'Object') { | |
for(let key in subStructure) { | |
if(structure[key] === undefined || structure[key] === "null") { | |
structure[key] = subStructure[key]; | |
if(structure[key].__proto__.constructor.name==="String") | |
structure[key] = structure[key].toLowerCase() + ";"; | |
} else if(structure[key].__proto__.constructor.name === subStructure[key].__proto__.constructor.name) { | |
if(structure[key].__proto__.constructor.name === "Object" && subStructure[key].__proto__.constructor.name === "Object") { | |
Object.assign(structure[key], subStructure[key]); | |
} else if(structure[key].__proto__.constructor.name === 'Array' && subStructure[key].__proto__.constructor.name === 'Array') { | |
Object.assign(structure[key][0], subStructure[key][0]); | |
} | |
} else { | |
structure[key] = JSON.stringify(structure[key])+" | "+JSON.stringify(subStructure[key]); | |
} | |
} | |
} else if(subStructure.__proto__.constructor.name === 'Array') { | |
} else { | |
if(structure === null || structure === undefined || structure === 'null') { | |
structure = subStructure; | |
} else if(structure !== subStructure) { | |
structure += structure+" | "+subStructure; | |
} | |
} | |
} | |
return [structure]; | |
} | |
default: | |
return data.__proto__.constructor.name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Script fails and returns a "circular" object with the current temp input.