转圈圈

# simplifyPath


/**
 * @param {string} path
 * @return {string}
 */
function simplifyPath(path) {
  const pathArr = path.split('/').filter(item => item !== '');

  const result = ['/'];

  for (const i of pathArr) {
    if (i === '..') {
      if (result.length > 1) {
        result.pop();
      }
    } else if (i !== '.') {
      result.push(i);
    }
  }
  return result.join('/').replace(/^\/{2,}/g, '/').replace(/\/+$/g, '').replace(/\/+/g, '/') || '/';
}

posted @ 2019-05-31 12:02  rosendolu  阅读(96)  评论(0编辑  收藏  举报