zoukankan      html  css  js  c++  java
  • path.resolve源码实现(简单版)

    //解析文件路径。类似于path.resolve
      resolve(...paths){
            let resolvePath = '';
            let isAbsolutePath = false;
            for(let i = paths.length-1; i > -1; i--){
                let path = paths[i];
                if(isAbsolutePath){
                    break;
                }
                if(!path){
                    continue
                }
                resolvePath = path + '/' + resolvePath;
                isAbsolutePath = path.charCodeAt(0) === 47;
            }
            if(/^\/+$/.test(resolvePath)){
                resolvePath = resolvePath.replace(/(\/+)/,'/')
            }else{
                resolvePath = resolvePath.replace(/(?!^)\w+\/+\.{2}\//g, '')
                .replace(/(?!^)\.\//g,'')
                .replace(/\/+$/, '')
            }
            return resolvePath;
        }
    console.log(resolve('/aa','../bb','cc','dd'))
    console.log(resolve('/aa','../bb','./cc','dd'))
    console.log(resolve('/','/system','user','userIndex'))
  • 相关阅读:
    defer与async的区别
    Promise 的含义
    SCSS 与 Sass 异同
    CSS总结2
    CSS总结1
    jQuery-插件,优化
    jQuery-表格以及表单
    jQuery-事件以及动画
    jQuery-ajax
    jQuery-DOM操作
  • 原文地址:https://www.cnblogs.com/littleboyck/p/15676527.html
Copyright © 2011-2022 走看看