2017-06-23 23:32:24 +02:00
|
|
|
function dirname(path) {
|
|
|
|
if (!path) throw new Error('Path is empty');
|
2017-11-11 00:18:00 +02:00
|
|
|
let s = path.split(/\/|\\/);
|
2017-06-23 23:32:24 +02:00
|
|
|
s.pop();
|
|
|
|
return s.join('/');
|
|
|
|
}
|
|
|
|
|
2017-06-23 20:51:02 +02:00
|
|
|
function basename(path) {
|
|
|
|
if (!path) throw new Error('Path is empty');
|
2017-11-11 00:18:00 +02:00
|
|
|
let s = path.split(/\/|\\/);
|
2017-06-23 20:51:02 +02:00
|
|
|
return s[s.length - 1];
|
|
|
|
}
|
|
|
|
|
2017-06-25 01:19:11 +02:00
|
|
|
function filename(path) {
|
|
|
|
if (!path) throw new Error('Path is empty');
|
2017-06-25 13:39:42 +02:00
|
|
|
let output = basename(path);
|
2017-06-25 01:19:11 +02:00
|
|
|
if (output.indexOf('.') < 0) return output;
|
|
|
|
|
|
|
|
output = output.split('.');
|
|
|
|
output.pop();
|
|
|
|
return output.join('.');
|
|
|
|
}
|
|
|
|
|
2017-07-10 20:17:03 +02:00
|
|
|
function fileExtension(path) {
|
|
|
|
if (!path) throw new Error('Path is empty');
|
|
|
|
|
|
|
|
let output = path.split('.');
|
|
|
|
if (output.length <= 1) return '';
|
|
|
|
return output[output.length - 1];
|
|
|
|
}
|
|
|
|
|
2017-06-23 20:51:02 +02:00
|
|
|
function isHidden(path) {
|
|
|
|
let b = basename(path);
|
|
|
|
if (!b.length) throw new Error('Path empty or not a valid path: ' + path);
|
|
|
|
return b[0] === '.';
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:15:49 +02:00
|
|
|
function safeFileExtension(e) {
|
|
|
|
if (!e || !e.replace) return '';
|
|
|
|
return e.replace(/[^a-zA-Z0-9]/g, '')
|
|
|
|
}
|
|
|
|
|
2017-12-08 23:51:59 +02:00
|
|
|
function toSystemSlashes(path, os) {
|
|
|
|
if (os === 'win32') return path.replace(/\//g, "\\");
|
|
|
|
return path.replace(/\\/g, "/");
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { basename, dirname, filename, isHidden, fileExtension, safeFileExtension, toSystemSlashes };
|