fabric8-planner
Version:
A planner front-end for Fabric8.
118 lines • 4.04 kB
JavaScript
var modelService = /** @class */ (function () {
function modelService() {
}
return modelService;
}());
export { modelService };
export function switchModel(input, mapTree) {
var output = {};
for (var i = 0; i < mapTree.length; i++) {
var fromPath = mapTree[i].fromPath;
// Either their should be a servicePath
// Or their should be a default value for UI model
if ((!mapTree[i].hasOwnProperty('fromPath') || !mapTree[i].fromPath.length) &&
!mapTree[i].hasOwnProperty('toValue')) {
throw ('No from path or default value for \'to\' model is provided at index - `${i}` !');
}
if (!mapTree[i].hasOwnProperty('toPath') || !mapTree[i].toPath.length) {
throw ('No to path provided at index - `${i}` !');
}
var toPath = mapTree[i].toPath;
if (mapTree[i].hasOwnProperty('toValue')) {
updateObj(output, toPath, mapTree[i].toValue);
}
else {
var fromCurrentVal = input;
if (fromCurrentVal !== null && typeof (fromCurrentVal) !== 'undefined') {
// Get the value to be mapped from service model
for (var j = 0; j < fromPath.length; j++) {
if (fromCurrentVal.hasOwnProperty(fromPath[j])) {
fromCurrentVal = fromCurrentVal[fromPath[j]];
}
else {
fromCurrentVal = null;
break;
}
}
}
else {
fromCurrentVal = null;
}
if (mapTree[i].hasOwnProperty('toFunction') &&
typeof (mapTree[i]['toFunction']) === 'function') {
updateObj(output, toPath, mapTree[i].toFunction(fromCurrentVal));
}
else {
updateObj(output, toPath, fromCurrentVal);
}
}
}
return output;
}
function updateObj(obj, keyPath, value) {
var lastKeyIndex = keyPath.length - 1;
for (var i = 0; i < lastKeyIndex; ++i) {
var key = keyPath[i];
if (!(key in obj)) {
obj[key] = {};
}
obj = obj[key];
}
obj[keyPath[lastKeyIndex]] = value;
}
/**
*
* @param obj the object to be cleaned
* @param keys the keys in the object to be clened
*
* Any property with value null will be deleted
* Any property mentioned in the key array will be deleted
*/
export function cleanObject(obj, keysToRemove) {
if (keysToRemove === void 0) { keysToRemove = []; }
var allKeys = Object.keys(obj);
var _loop_1 = function (i) {
if (obj[allKeys[i]] === null ||
keysToRemove.findIndex(function (k) { return k === allKeys[i]; }) > -1) {
delete obj[allKeys[i]];
}
else if (typeof (obj[allKeys[i]]) === 'object' &&
!Array.isArray(obj[allKeys[i]])) {
obj[allKeys[i]] = cleanObject(obj[allKeys[i]], keysToRemove);
if (!Object.keys(obj[allKeys[i]]).length ||
(Object.keys(obj[allKeys[i]])[0] === 'type' &&
Object.keys(obj[allKeys[i]]).length === 1)) {
delete obj[allKeys[i]];
}
}
};
for (var i = 0; i < allKeys.length; i++) {
_loop_1(i);
}
return obj;
}
/**
* This function normalize the array to
* a key value paired dictionary
* @param arr
*/
export function normalizeArray(arr, id) {
if (id === void 0) { id = ''; }
if (!Array.isArray(arr)) {
throw (new Error('The input needs to be an array'));
}
var output = {};
arr.forEach(function (item, index) {
if (item.hasOwnProperty(id)) {
output[item[id].toString()] = item;
}
else if (item.hasOwnProperty('id')) {
output[item['id'].toString()] = item;
}
else {
output[index.toString()] = item;
}
});
return output;
}
//# sourceMappingURL=common.model.js.map