Outsource functions
parent
589d5d830c
commit
dc95721546
@ -0,0 +1,21 @@
|
||||
export function deepen(object: Object): any {
|
||||
let newObject = {};
|
||||
for (const prop in object) {
|
||||
if (prop.includes('.')) {
|
||||
const splittedProp = prop.split('.');
|
||||
const outerProp = splittedProp[0];
|
||||
const innerProp = splittedProp.slice(1).join('.');
|
||||
if (!newObject[outerProp]) {
|
||||
newObject[outerProp] = {};
|
||||
}
|
||||
newObject[outerProp][innerProp] = object[prop];
|
||||
} else {
|
||||
newObject[prop] = object[prop];
|
||||
}
|
||||
}
|
||||
for (const prop in newObject) {
|
||||
if (typeof newObject[prop] === 'object' && newObject[prop] !== null)
|
||||
newObject[prop] = deepen(newObject[prop]);
|
||||
}
|
||||
return newObject;
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
export function flatten(object: Object, prefix: string = ''): any {
|
||||
let newObject = {};
|
||||
for (const prop in object) {
|
||||
let propName = prefix + prop;
|
||||
if (typeof object[prop] === 'object' && object[prop] !== null) {
|
||||
const flattenedObject = flatten(object[prop], propName + '.');
|
||||
for (const flattenedProperty in flattenedObject) {
|
||||
newObject[flattenedProperty] =
|
||||
flattenedObject[flattenedProperty];
|
||||
}
|
||||
} else if(!prop.includes('_', 0)) {
|
||||
newObject[propName] = object[prop];
|
||||
}
|
||||
}
|
||||
return newObject;
|
||||
}
|
Loading…
Reference in New Issue