ohif-viewer/Packages/ohif-core/both/lib/object.js
2017-10-11 08:55:10 -03:00

53 lines
1.7 KiB
JavaScript

import { OHIF } from 'meteor/ohif:core';
OHIF.object = {};
// Transforms a shallow object with keys separated by "." into a nested object
OHIF.object.getNestedObject = shallowObject => {
const nestedObject = {};
for (let key in shallowObject) {
if (!shallowObject.hasOwnProperty(key)) continue;
const value = shallowObject[key];
const propertyArray = key.split('.');
let currentObject = nestedObject;
while (propertyArray.length) {
const currentProperty = propertyArray.shift();
if (!propertyArray.length) {
currentObject[currentProperty] = value;
} else {
if (!currentObject[currentProperty]) {
currentObject[currentProperty] = {};
}
currentObject = currentObject[currentProperty];
}
}
}
return nestedObject;
};
// Transforms a nested object into a shallowObject merging its keys with "." character
OHIF.object.getShallowObject = nestedObject => {
const shallowObject = {};
const putValues = (baseKey, nestedObject, resultObject) => {
for (let key in nestedObject) {
if (!nestedObject.hasOwnProperty(key)) continue;
let currentKey = baseKey ? `${baseKey}.${key}` : key;
const currentValue = nestedObject[key];
if (typeof currentValue === 'object') {
if (currentValue instanceof Array) {
currentKey += '[]';
}
putValues(currentKey, currentValue, resultObject);
} else {
resultObject[currentKey] = currentValue;
}
}
};
putValues('', nestedObject, shallowObject);
return shallowObject;
};