-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.js
70 lines (61 loc) · 1.31 KB
/
utilities.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Get file name
* @param {string} path - file path or file name
* @returns {null | string}
*/
const getFileName = (path = '') => {
if (!path) {
return null;
}
const [fileName] = path.split('/').slice(-1);
if (!(fileName && fileName.includes('.'))) {
return null;
}
const partials = fileName.split('.');
if (partials[0] === '' && partials.length === 2) {
return null;
}
return fileName;
};
/**
* Get file extension
* @param {string} path - file path or file name
* @returns {null | string}
*/
const getFileExtension = (path = '') => {
const fileName = getFileName(path);
if (!fileName) {
return null;
}
return fileName.split('.').slice(-1)[0];
};
/**
* Normalize provided path
* @param {string} path - path string
* @returns {string}
*/
const normalizePath = (path = '') => {
if (!path) {
throw new Error('Path not provided!');
}
return path.slice(-1)[0] === '/'
? path.slice(0, path.length - 1)
: path;
};
/**
* Check if string starts with the dot
* @param {string} string - a string to check
* @returns {boolean}
*/
const startsWithDot = (string = '') => {
if (!string) {
throw new Error('Invalid string argument!');
}
return string[0] === '.';
};
module.exports = {
getFileExtension,
getFileName,
normalizePath,
startsWithDot,
};