【问题标题】:Node/JavaScript function to return a new path, given a filepath, basepath and new destpath给定文件路径、基本路径和新目标路径,返回新路径的节点/JavaScript 函数
【发布时间】:2017-03-17 17:00:49
【问题描述】:

可以使用什么自定义函数(在节点中),如果传递三个参数,即filePathbasePathdestPath,它将返回一个新路径。例如。

函数签名示例

var path = require('path'); // Nodes `path` module is likely to help?

/**
 * Returns a new filepath
 * @param {String} filePath - The path to the source file.
 * @param {String} basePath - The path to determine which part of the
 *     FilePath argument is to be appended to the destPath argument.
 * @param {String} destPath - The destination file path.
 * @return {String} The new filepath.
 */
function foo(filePath, basePath, destPath) {

    // ... ?

    return newFilePath;
}

使用示例:

以下是调用函数的一些示例,以及在传递参数的情况下我期望返回的值:

// Example A

var a = foo('./foo/bar/baz/quux/filename.json', 'foo/bar/baz/', './dist');

console.log(a) // --> 'dist/baz/quux/filename.json' 

// Example B

var b = foo('./a/b/c/d/e/filename.png', './a/b/c', './images/logos/');

console.log(b) // --> 'images/logos/c/d/e/filename.png' 

// Example C

var c = foo('images/a/b/filename.png', './images/a/b', 'pictures/');

console.log(c) // --> 'pictures/b/filename.png' 

更多信息

  1. 从示例函数调用中可以看出,basePath String 将通知函数应该在哪里修剪 filePath 值,其目的类似于 gulp @987654321 @ 与gulp.src() 函数一起使用。
  2. 参数String 值(即路径)将始终是相对的,但是,它们有时会包含当前目录前缀./,有时则不包含。尾部正斜杠 / 可能出现在路径 String 上,也可能不出现。
  3. 解决方案必须跨平台工作,没有任何额外的依赖关系。它还必须可用于早期节点版本 ( >= 0.10.28 )
  4. 我使用path 模块的实用程序进行了各种失败的尝试,例如path.normalize 然后拆分Array 等等。但是,我很确定path 模块将用于解决方案。

提前致谢。

【问题讨论】:

    标签: javascript node.js path gulp filepath


    【解决方案1】:

    您可以使用简单的字符串替换来实现这一点。只需在此之前规范化路径。

    1. filePath 和 basePath:如果它们以./ 开头,请将其删除。
    2. basePath : 如果它以/ 结尾,则删除它,然后删除最后一个路径部分(在您的第一个示例中为baz
    3. filePath.replace(basePath, destPath)

    【讨论】:

      【解决方案2】:

      function foo(filePath, basePath, destPath) {
          if(filePath.charAt(0)=='.'){
            filePath=filePath.substr(1,filePath.length);
          }
          if(filePath.charAt(0)=='/'){
            filePath=filePath.substr(1,filePath.length);
          }
          if(basePath.charAt(0)=='.'){
            basePath=basePath.substr(1,basePath.length);
          }
          if(basePath.charAt(0)=='/'){
            basePath=basePath.substr(1,basePath.length);
          }
          if(destPath.charAt(0)=='.'){
            destPath=destPath.substr(1,destPath.length);
          }
          if(destPath.charAt(0)=='/'){
            destPath=destPath.substr(1,destPath.length);
          }
          if(basePath.charAt(basePath.length-1)!='/'){
            basePath+="\/";
          }
          if(destPath.charAt(destPath.length-1)!='/'){
            destPath+="\/";
          }
          var temp = basePath.split('/')[basePath.split('/').length-2];
          var newFilePath = temp+'/'+filePath.split(basePath)[1];
          newFilePath=destPath+'/'+newFilePath;
          return newFilePath.replace(/\/\//g,"/");
      }
      // Example A
      
      var a = foo('./foo/bar/baz/quux/filename.json', 'foo/bar/baz/', './dist');
      
      console.log(a) // --> 'dist/baz/quux/filename.json' 
      
      // Example B
      
      var b = foo('./a/b/c/d/e/filename.png', './a/b/c', './images/logos/');
      
      console.log(b) // --> 'images/logos/c/d/e/filename.png' 
      
      // Example C
      
      var c = foo('images/a/b/filename.png', './images/a/b', 'pictures/');
      
      console.log(c) // --> 'pictures/b/filename.png'

      【讨论】:

      • 感谢您查看此内容,但是示例 B 和 C 导致双斜线。 a = ./images/logos//b//d/e/filename.png 和 c = pictures//a//filename.png
      • 这些参数是什么?你能告诉我他们的函数调用,以便我可以重现(并希望修复)错误吗?
      • 道歉我之前的评论说 a = 应该说 b = 只需使用示例 B 和 C 在节点中调用您的函数,(如定义在我的 OP 中),您应该会在结果日志中看到双斜杠。
      • 哦,我明白你在说什么——给出的文件路径应该是运行它们配对的示例的结果。好吧,我修复了双斜杠问题和参数 2 或 3 没有结尾斜杠时发生的问题。
      猜你喜欢
      • 2022-08-18
      • 2021-08-15
      • 1970-01-01
      • 2018-09-30
      • 1970-01-01
      • 2019-07-08
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      相关资源
      最近更新 更多