SAIFIINDUSTRIES's picture
Add batch 7 (XUANTIE-RV_openc906, osresearch_spispy, efabless_caravel, ZipCPU_sdspi, Nitcloud_Digital-IDE)
7ca5cf2 verified
Raw History Blame Contribute Delete
1.76 kB
"use strict";
const fspath = require("path");
var pathOperation = {
log : console.log,
/**
* @descriptionCn 相对路径转绝对路径
* @descriptionEn turn relative path to absolute path
* @param {String} curPath current file path
* @param {String} path the path which is included in the current file
* @returns absolute path
*/
rel2abs : function (curPath, path) {
// check whether it is absolute
if (fspath.isAbsolute(path)) {
return path;
}
let parent = fspath.dirname(curPath);
let absPath = fspath.resolve(parent, path);
return absPath.replace(/\\/g,"\/");
},
/**
* @descriptionCn 获取文件的后缀
* @descriptionEn get the extname of path
* @param {String} path the path which is included in the current file
* @returns the extname of path e.g. : ".txt"
*/
extname : function (path) {
return fspath.extname(path).toLowerCase();
},
basename : function (path) {
return fspath.basename(path);
},
/**
* @descriptionEn Multiple times return the directory name of a path.
* @descriptionCn 往上进行指定次数的父级路径查找
* @param {String} path 初始文件路径
* @param {Number} times 往上查找的次数,省缺时指定为一次
* @returns {String} 返回对应的父级路径
*/
dirname : function (path, times) {
let dirPath = path;
if (!times) {
dirPath = fspath.dirname(dirPath);
return dirPath;
}
for (let i = 0; i < times; i++) {
dirPath = fspath.dirname(dirPath);
}
return dirPath;
}
}
module.exports = pathOperation;