t-comm
Version: 
专业、稳定、纯粹的工具库
34 lines (30 loc) • 1.05 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
function parseComponentPath(filePath, relativePath) {
  var REG = /(\/|^)[^/]+$/; // `abc/xxx` 中的 `/xxx`
  var SAME_REG = /^\.\//; // 以 `./` 开头
  var DIFF_REG = /^\.\.\//; // 以 `../` 开头
  var generatePath = function generatePath(list) {
    return list.filter(function (item) {
      return item;
    }).join('/');
  };
  if (filePath.startsWith('/')) {
    return filePath.replace(/^\//, '');
  }
  if (relativePath.startsWith('/')) {
    return relativePath.replace(/^\//, '');
  }
  var tempResult = filePath;
  tempResult = tempResult.replace(REG, '');
  if (SAME_REG.test(relativePath)) {
    return generatePath([tempResult, relativePath.replace(SAME_REG, '')]);
  }
  var tempRelative = relativePath;
  while (DIFF_REG.test(tempRelative)) {
    tempResult = tempResult.replace(REG, '');
    tempRelative = tempRelative.replace(DIFF_REG, '');
  }
  return generatePath([tempResult, tempRelative]);
}
exports.parseComponentPath = parseComponentPath;