4
4
*/
5
5
'use strict' ;
6
6
7
+ const path = require ( 'path' ) ;
8
+
7
9
/**
8
10
* @type {string } filename without path
9
- * @param {string } path filename concat with path
11
+ * @param {string } p filename concat with path
10
12
*/
11
- const getFilename = ( path ) => path . substring ( path . lastIndexOf ( '/' ) + 1 ) ;
13
+ const getFilename = ( p ) => path . basename ( p ) ;
12
14
13
15
/**
14
16
* @type {string } path of folder
15
- * @param {string } path filename concat with path
17
+ * @param {string } p filename concat with path
16
18
*/
17
- const getFolderPath = ( path ) => path . substring ( 0 , path . lastIndexOf ( '/' ) + 1 ) ;
19
+ const getFolderPath = ( p ) => path . join ( path . dirname ( p ) , path . sep ) ;
18
20
19
21
/**
20
22
* @type {string } base name
@@ -29,24 +31,26 @@ const getBasename = (filename, ignoreMiddleExtensions = false) =>
29
31
30
32
/**
31
33
* @type {string[] } all folders
32
- * @param {string } path path of folder
34
+ * @param {string } p path of folder
33
35
*/
34
- const getAllFolders = ( path ) =>
35
- path . split ( '/' ) . filter ( ( folder ) => folder !== '' ) ;
36
+ const getAllFolders = ( p ) =>
37
+ p . split ( path . sep ) . filter ( ( folder ) => folder !== '' ) ;
36
38
37
39
/**
38
40
* @type {string[] } all sub paths
39
- * @param {string } path path of folder
41
+ * @param {string } p path of folder
40
42
*/
41
- const getSubPaths = ( path ) => {
42
- const folders = getAllFolders ( path ) ;
43
+ const getSubPaths = ( p ) => {
44
+ const folders = getAllFolders ( p ) ;
43
45
let subPaths = [ ] ;
44
46
45
47
const handler = ( array ) =>
46
48
array . reduce ( ( acc , folder , index ) => {
47
49
if ( folder ) {
48
50
acc . push (
49
- index === 0 ? `${ folder } /` : `${ acc [ acc . length - 1 ] } ${ folder } /`
51
+ index === 0
52
+ ? path . join ( folder , path . sep )
53
+ : path . join ( acc [ acc . length - 1 ] , folder , path . sep )
50
54
) ;
51
55
}
52
56
return acc ;
@@ -65,7 +69,7 @@ const getSubPaths = (path) => {
65
69
* @param {string } repositoryRoot path of repository root
66
70
*/
67
71
const getPathFromRepositoryRoot = ( fullPath , repositoryRoot ) =>
68
- fullPath . replace ( ` ${ repositoryRoot } /` , '' ) ;
72
+ fullPath . replace ( path . join ( repositoryRoot , path . sep ) , '' ) ;
69
73
70
74
module . exports = {
71
75
getFolderPath,
0 commit comments