1- export const absolutePath = / ^ (?: \/ | (?: [ A - Z a - z ] : ) ? [ \\ | / ] ) / ;
2- export const relativePath = / ^ \. ? \. \/ / ;
1+ const ABSOLUTE_PATH_REGEX = / ^ (?: \/ | (?: [ A - Z a - z ] : ) ? [ \\ | / ] ) / ;
2+ const RELATIVE_PATH_REGEX = / ^ \. ? \. \/ / ;
3+ const ALL_BACKSLASHES_REGEX = / \\ / g;
4+ const ANY_SLASH_REGEX = / [ / \\ ] / ;
5+ const EXTNAME_REGEX = / \. [ ^ . ] + $ / ;
36
47export function isAbsolute ( path : string ) : boolean {
5- return absolutePath . test ( path ) ;
8+ return ABSOLUTE_PATH_REGEX . test ( path ) ;
69}
710
811export function isRelative ( path : string ) : boolean {
9- return relativePath . test ( path ) ;
12+ return RELATIVE_PATH_REGEX . test ( path ) ;
1013}
1114
1215export function normalize ( path : string ) : string {
13- return path . replace ( / \\ / g , '/' ) ;
16+ return path . replace ( ALL_BACKSLASHES_REGEX , '/' ) ;
1417}
1518
1619export function basename ( path : string ) : string {
17- return path . split ( / [ / \\ ] / ) . pop ( ) || '' ;
20+ return path . split ( ANY_SLASH_REGEX ) . pop ( ) || '' ;
1821}
1922
2023export function dirname ( path : string ) : string {
@@ -28,14 +31,13 @@ export function dirname(path: string): string {
2831}
2932
3033export function extname ( path : string ) : string {
31- const match = / \. [ ^ . ] + $ / . exec ( basename ( path ) ! ) ;
32- if ( ! match ) return '' ;
33- return match [ 0 ] ;
34+ const match = EXTNAME_REGEX . exec ( basename ( path ) ! ) ;
35+ return match ? match [ 0 ] : '' ;
3436}
3537
3638export function relative ( from : string , to : string ) : string {
37- const fromParts = from . split ( / [ / \\ ] / ) . filter ( Boolean ) ;
38- const toParts = to . split ( / [ / \\ ] / ) . filter ( Boolean ) ;
39+ const fromParts = from . split ( ANY_SLASH_REGEX ) . filter ( Boolean ) ;
40+ const toParts = to . split ( ANY_SLASH_REGEX ) . filter ( Boolean ) ;
3941
4042 if ( fromParts [ 0 ] === '.' ) fromParts . shift ( ) ;
4143 if ( toParts [ 0 ] === '.' ) toParts . shift ( ) ;
@@ -62,13 +64,13 @@ export function resolve(...paths: string[]): string {
6264 if ( ! firstPathSegment ) {
6365 return '/' ;
6466 }
65- let resolvedParts = firstPathSegment . split ( / [ / \\ ] / ) ;
67+ let resolvedParts = firstPathSegment . split ( ANY_SLASH_REGEX ) ;
6668
6769 for ( const path of paths ) {
6870 if ( isAbsolute ( path ) ) {
69- resolvedParts = path . split ( / [ / \\ ] / ) ;
71+ resolvedParts = path . split ( ANY_SLASH_REGEX ) ;
7072 } else {
71- const parts = path . split ( / [ / \\ ] / ) ;
73+ const parts = path . split ( ANY_SLASH_REGEX ) ;
7274
7375 while ( parts [ 0 ] === '.' || parts [ 0 ] === '..' ) {
7476 const part = parts . shift ( ) ;
0 commit comments