mirror of
https://github.com/mickael-kerjean/filestash.git
synced 2025-11-01 10:56:31 +08:00
38 lines
864 B
JavaScript
38 lines
864 B
JavaScript
import Path from 'path';
|
|
|
|
export function pathBuilder(path, filename, type = 'file'){
|
|
let tmp = Path.resolve(path, filename)
|
|
if(type === 'file'){
|
|
return tmp;
|
|
}else{
|
|
return tmp + '/';
|
|
}
|
|
}
|
|
|
|
export function basename(path){
|
|
return Path.basename(path);
|
|
}
|
|
|
|
export function dirname(path){
|
|
const dir = Path.dirname(path);
|
|
if(dir === '/') return dir;
|
|
return dir + "/";
|
|
}
|
|
|
|
export function filetype(path) {
|
|
return path.slice(-1) === "/" ? "directory" : "file";
|
|
}
|
|
|
|
export function absoluteToRelative(from, to){
|
|
// remove any trace of file that would be interpreted by the path lib as a folder
|
|
from = from.replace(/\/[^\/]+$/, "/");
|
|
let r = Path.relative(from, to);
|
|
if(r.substring(0,3) !== "../"){
|
|
r = "./"+r
|
|
}
|
|
if(/\/$/.test(to) === true && r !== "./"){
|
|
r += "/"
|
|
}
|
|
return r;
|
|
}
|