【发布时间】:2017-03-17 17:00:49
【问题描述】:
可以使用什么自定义函数(在节点中),如果传递三个参数,即filePath、basePath 和destPath,它将返回一个新路径。例如。
函数签名示例
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'
更多信息
- 从示例函数调用中可以看出,
basePathString将通知函数应该在哪里修剪filePath值,其目的类似于 gulp @987654321 @ 与gulp.src()函数一起使用。 - 参数
String值(即路径)将始终是相对的,但是,它们有时会包含当前目录前缀./,有时则不包含。尾部正斜杠/可能出现在路径String上,也可能不出现。 - 解决方案必须跨平台工作,没有任何额外的依赖关系。它还必须可用于早期节点版本 ( >= 0.10.28 )
- 我使用path 模块的实用程序进行了各种失败的尝试,例如
path.normalize然后拆分Array等等。但是,我很确定path 模块将用于解决方案。
提前致谢。
【问题讨论】:
标签: javascript node.js path gulp filepath