nope-js-node
Version:
NoPE Runtime for Nodejs. For Browser-Support please use nope-browser
109 lines (108 loc) • 3.58 kB
JavaScript
;
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @desc [description]
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLeastCommonPathSegment = exports.varifyPath = exports.convertPath = void 0;
const objectMethods_1 = require("./objectMethods");
const pathMatchingMethods_1 = require("./pathMatchingMethods");
const stringMethods_1 = require("./stringMethods");
/**
* Converts the path to a correct path.
* @param {string} path
* @returns {string} The Path
*/
function convertPath(path) {
return (0, stringMethods_1.replaceAll)(path, [".", "[", ["]", ""]], objectMethods_1.SPLITCHAR);
}
exports.convertPath = convertPath;
/**
* Helper to convert the segments of a path to a valid var name.
*
* @param {string} path The Path to adapt
* @returns {string} The Adapted path.
*/
function varifyPath(path) {
// We devided the Path in its segments and make shure, that these segments are
// called correctly.
return path
.split(objectMethods_1.SPLITCHAR)
.map((item) => (0, stringMethods_1.varifyString)(item))
.join(objectMethods_1.SPLITCHAR);
}
exports.varifyPath = varifyPath;
/**
* Returns the least common segmet of all pathes, included in the pathes array.
*
* The Following options are available.
*
* "considerSingleLevel":boolean -> allows "singlelevel"-wildcards in the segments
* "considerMultiLevel":boolean -> allows "multilevel"-wildcards in the segments
*
* @param pathes The Segments to compare
* @param opts Additional Options.
* @returns
*/
function getLeastCommonPathSegment(pathes, opts = {}) {
let currentPath = pathes.pop();
while (pathes.length > 0) {
let next = pathes.pop();
currentPath = _getLeastCommonPathSegment(currentPath, next, opts);
if (!currentPath) {
return currentPath;
}
}
return currentPath;
}
exports.getLeastCommonPathSegment = getLeastCommonPathSegment;
function _getLeastCommonPathSegment(path01, path02, opts = {}) {
const p1 = convertPath(path01).split(objectMethods_1.SPLITCHAR);
const p2 = convertPath(path02).split(objectMethods_1.SPLITCHAR);
const ret = [];
let idx = 0;
const max = Math.min(p1.length, p2.length);
while (idx < max) {
if (p1[idx] == p2[idx]) {
// Add the Item.
ret.push(p1[idx]);
}
else if (opts.considerSingleLevel) {
if (p1[idx] === pathMatchingMethods_1.SINGLE_LEVEL_WILDCARD) {
// Add the Item.
ret.push(p2[idx]);
}
else if (p2[idx] === pathMatchingMethods_1.SINGLE_LEVEL_WILDCARD) {
// Add the Item.
ret.push(p1[idx]);
}
else {
break;
}
}
else if (opts.considerMultiLevel) {
if (p1[idx] === pathMatchingMethods_1.MULTI_LEVEL_WILDCARD) {
// Add the Item.
ret.push(...p2.slice(idx));
break;
}
else if (p2[idx] === pathMatchingMethods_1.MULTI_LEVEL_WILDCARD) {
// Add the Item.
ret.push(...p1.slice(idx));
break;
}
else {
break;
}
}
else {
break;
}
idx += 1;
}
if (ret.length) {
return ret.join(objectMethods_1.SPLITCHAR);
}
return false;
}