2020-01-29 18:14:33 +01:00
|
|
|
import { cloneDeep } from 'lodash'
|
2018-04-05 22:15:08 +02:00
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default getObjStructure
|
2018-04-05 22:15:08 +02:00
|
|
|
|
|
|
|
// This will create an object that represents the structure of the given object
|
|
|
|
// it replaces all values with the result of their type
|
|
|
|
|
|
|
|
// {
|
|
|
|
// "data": {
|
|
|
|
// "CurrencyController": {
|
|
|
|
// "conversionDate": "number",
|
|
|
|
// "conversionRate": "number",
|
|
|
|
// "currentCurrency": "string"
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2018-04-19 17:38:56 +02:00
|
|
|
/**
|
|
|
|
* Creates an object that represents the structure of the given object. It replaces all values with the result of their
|
|
|
|
* type.
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {Object} obj - The object for which a 'structure' will be returned. Usually a plain object and not a class.
|
|
|
|
* @returns {Object} - The "mapped" version of a deep clone of the passed object, with each non-object property value
|
2018-04-19 17:38:56 +02:00
|
|
|
* replaced with the javascript type of that value.
|
|
|
|
*
|
|
|
|
*/
|
2018-07-03 00:49:33 +02:00
|
|
|
function getObjStructure (obj) {
|
2020-01-29 18:14:33 +01:00
|
|
|
const structure = cloneDeep(obj)
|
2018-04-05 22:15:08 +02:00
|
|
|
return deepMap(structure, (value) => {
|
|
|
|
return value === null ? 'null' : typeof value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:38:56 +02:00
|
|
|
/**
|
|
|
|
* Modifies all the properties and deeply nested of a passed object. Iterates recursively over all nested objects and
|
|
|
|
* their properties, and covers the entire depth of the object. At each property value which is not an object is modified.
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {Object} target - The object to modify
|
|
|
|
* @param {Function} visit - The modifier to apply to each non-object property value
|
|
|
|
* @returns {Object} - The modified object
|
2018-04-19 17:38:56 +02:00
|
|
|
*/
|
2018-07-03 00:49:33 +02:00
|
|
|
function deepMap (target = {}, visit) {
|
2018-04-05 22:15:08 +02:00
|
|
|
Object.entries(target).forEach(([key, value]) => {
|
|
|
|
if (typeof value === 'object' && value !== null) {
|
|
|
|
target[key] = deepMap(value, visit)
|
|
|
|
} else {
|
|
|
|
target[key] = visit(value)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return target
|
|
|
|
}
|