amos-tool
Version:
amos ui tool
55 lines (50 loc) • 2.31 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"), _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")), memorize = require("./memo");
function pathToTree(e) {
for (var r = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {}, t = r.pathKey, o = void 0 === t ? "path" : t, i = r.childrenKey, n = void 0 === i ? "children" : i, a = r.processor, l = [], u = 0; u < e.length; u++) for (var d = e[u].split("/"), f = l, h = 0; h < d.length; h++) {
var p = d[h];
if ("" !== p) {
for (var s = f, v = 0; v < f.length; v++) if (f[v][o] === p) {
f = f[v][n];
break;
}
if (s === f) {
var y = void 0;
y = h === d.length - 1 ? e[u] : d.slice(0, h + 1).join("/") + "/";
var c = (0, _defineProperty2.default)((0, _defineProperty2.default)({
key: y
}, o, p), n, []);
a && a(c, p), h === d.length - 1 && delete c[n], f.push(c), f = c[n];
}
}
}
return l;
}
function pathToRoutes(e) {
for (var r = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {}, t = r.pathKey, o = void 0 === t ? "path" : t, i = r.childrenKey, n = void 0 === i ? "children" : i, a = r.processor, l = [], u = 0; u < e.length; u++) for (var d = e[u].split("/"), f = l, h = 0; h < d.length; h++) {
var p = d[h];
if ("" === p) break;
var s = f, v = void 0;
for (v = 0; v < f.length; v++) if (f[v][o] === p) {
f = f[v][n];
break;
}
if (s === f) {
var y = void 0;
y = h === d.length - 1 ? e[u] : d.slice(0, h + 1).join("/") + "/";
var c = (0, _defineProperty2.default)((0, _defineProperty2.default)({
key: y
}, o, p), n, []);
a && a(c, p), f[v] = c, f = c[n];
} else delete f[n];
}
return l;
}
function beautifyRoutes(e) {
var r = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {}, t = r.childrenKey, o = void 0 === t ? "children" : t;
e.forEach(function(e) {
o in e && e[o] && (e[o].length > 0 ? beautifyRoutes(e[o], r) : delete e[o]);
});
}
module.exports = pathToTree, module.exports.pathToRoutes = pathToRoutes, module.exports.beautifyRoutes = beautifyRoutes,
module.exports.memoPathToTree = memorize(pathToTree);