process-reporting-ts
Version:
Process reporting with typescript
65 lines (64 loc) • 2.42 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const deflateImpl = (json, prefix, jsonPart) => {
const keys = Object.keys(jsonPart);
keys.forEach((key) => {
let _prefix;
if (jsonPart[key] && typeof jsonPart[key] === 'object') {
const _currPrefix = key.concat('.');
_prefix = prefix ? prefix.concat(_currPrefix) : _currPrefix;
deflateImpl(json, _prefix, jsonPart[key]);
}
else {
_prefix = prefix ? prefix.concat(key) : key;
json[(_prefix)] = jsonPart[key];
}
});
return json;
};
const inflateImpl = (json, key, jsonPart) => {
const keys = Object.keys(jsonPart);
keys.forEach((key) => {
const parts = key.split('.'); // 'options.0.height'
if (parts.length > 1) {
const preKey = parts[0]; // 'options'
const postKey = key.substring(preKey.length + 1); // '0.height'
const position = postKey.split('.')[0]; // '0'
const isArray = isNumeric(position);
const _jsonPart = {};
if (isArray) {
const nestedObject = json[preKey] || [];
json[(preKey)] = nestedObject; // assign new value
const propName = postKey.substring(position.length + 1);
const index = Number.parseInt(position);
const val = jsonPart[key];
let obj = val;
if (propName === '') {
nestedObject[index] = val;
}
else {
_jsonPart[(propName)] = val;
obj = nestedObject[index] || {};
nestedObject[index] = obj;
}
inflateImpl(obj, postKey, _jsonPart);
}
else {
const nestedObject = json[preKey] || {};
json[(preKey)] = nestedObject; // assign new value
_jsonPart[(postKey)] = jsonPart[key];
inflateImpl(nestedObject, postKey, _jsonPart);
}
}
else {
json[(key)] = jsonPart[key];
}
});
return json;
};
const isNumeric = (str) => {
return !Number.isNaN(Number.parseInt(str));
};
const deflate = (json) => deflateImpl({}, '', json);
const inflate = (json) => inflateImpl({}, '', json);
exports.default = { deflate, inflate };