1 |
|
2 |
|
3 | export const absolutePath = /^(?:\/|(?:[A-Za-z]:)?[\\|\/])/;
|
4 | export const relativePath = /^\.?\.\//;
|
5 |
|
6 | export function isAbsolute ( path ) {
|
7 | return absolutePath.test( path );
|
8 | }
|
9 |
|
10 | export function isRelative ( path ) {
|
11 | return relativePath.test( path );
|
12 | }
|
13 |
|
14 | export function basename ( path ) {
|
15 | return path.split( /(\/|\\)/ ).pop();
|
16 | }
|
17 |
|
18 | export function dirname ( path ) {
|
19 | const match = /(\/|\\)[^\/\\]*$/.exec( path );
|
20 | if ( !match ) return '.';
|
21 |
|
22 | const dir = path.slice( 0, -match[0].length );
|
23 |
|
24 |
|
25 | return dir ? dir : '/';
|
26 | }
|
27 |
|
28 | export function extname ( path ) {
|
29 | const match = /\.[^\.]+$/.exec( basename( path ) );
|
30 | if ( !match ) return '';
|
31 | return match[0];
|
32 | }
|
33 |
|
34 | export function relative ( from, to ) {
|
35 | const fromParts = from.split( /[\/\\]/ ).filter( Boolean );
|
36 | const toParts = to.split( /[\/\\]/ ).filter( Boolean );
|
37 |
|
38 | while ( fromParts[0] && toParts[0] && fromParts[0] === toParts[0] ) {
|
39 | fromParts.shift();
|
40 | toParts.shift();
|
41 | }
|
42 |
|
43 | while ( toParts[0] === '.' || toParts[0] === '..' ) {
|
44 | const toPart = toParts.shift();
|
45 | if ( toPart === '..' ) {
|
46 | fromParts.pop();
|
47 | }
|
48 | }
|
49 |
|
50 | while ( fromParts.pop() ) {
|
51 | toParts.unshift( '..' );
|
52 | }
|
53 |
|
54 | return toParts.join( '/' );
|
55 | }
|
56 |
|
57 | export function resolve ( ...paths ) {
|
58 | let resolvedParts = paths.shift().split( /[\/\\]/ );
|
59 |
|
60 | paths.forEach( path => {
|
61 | if ( isAbsolute( path ) ) {
|
62 | resolvedParts = path.split( /[\/\\]/ );
|
63 | } else {
|
64 | const parts = path.split( /[\/\\]/ );
|
65 |
|
66 | while ( parts[0] === '.' || parts[0] === '..' ) {
|
67 | const part = parts.shift();
|
68 | if ( part === '..' ) {
|
69 | resolvedParts.pop();
|
70 | }
|
71 | }
|
72 |
|
73 | resolvedParts.push.apply( resolvedParts, parts );
|
74 | }
|
75 | });
|
76 |
|
77 | return resolvedParts.join( '/' );
|
78 | }
|