veendor
Version:
a tool for stroing your npm dependencies in arbitraty storage
47 lines (46 loc) • 1.37 kB
JavaScript
;
/**
* returns sorted array of object contents
* see deepSortedJson.test.js for examples
* @param {Object} jsonObject
* @returns {string[]}
*/
Object.defineProperty(exports, "__esModule", { value: true });
function isJSONObject(obj) {
return obj !== null && typeof obj === 'object';
}
function deepSortedJson(jsonObject) {
const tmpObj = Object.assign({}, jsonObject);
const result = [];
while (true) {
const tmpObjkeys = Object.keys(tmpObj);
if (tmpObjkeys.length === 0) {
break;
}
for (const i of tmpObjkeys) {
const val = tmpObj[i];
if (val instanceof Array) {
for (const [index, value] of val.entries()) {
tmpObj[`${i}[${index}]`] = value;
}
}
else if (isJSONObject(val)) {
const keys = Object.keys(val);
if (keys.length === 0) {
result.push(`${i}`);
}
else {
for (const key of keys) {
tmpObj[`${i}.${key}`] = val[key];
}
}
}
else {
result.push(`${i}=${val}`);
}
delete tmpObj[i];
}
}
return result.sort();
}
exports.transform = deepSortedJson;