File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change 1+ import { trimStartDir } from "./path" ;
2+
3+ test ( "trimStartDir" , ( ) => {
4+ const startDirVariants = [
5+ "some-dir/" ,
6+ "./some-dir/" ,
7+ "some-dir" ,
8+ "./some-dir" ,
9+ ] ;
10+ for ( const p of [ "some-dir/" , "./some-dir/" , "some-dir" , "./some-dir" ] ) {
11+ for ( const startDir of startDirVariants ) {
12+ expect ( trimStartDir ( p , startDir ) ) . toBe ( "." ) ;
13+ }
14+ }
15+
16+ for ( const p of [ "some-dir/some-file" , "./some-dir/some-file" ] ) {
17+ for ( const startDir of startDirVariants ) {
18+ expect ( trimStartDir ( p , startDir ) ) . toBe ( "some-file" ) ;
19+ }
20+ }
21+
22+ for ( const p of [ "some-dir/some-dir/" , "./some-dir/some-dir/" ] ) {
23+ for ( const startDir of startDirVariants ) {
24+ expect ( trimStartDir ( p , startDir ) ) . toBe ( "some-dir" ) ;
25+ }
26+ }
27+
28+ for ( const p of [
29+ "../some-dir/some-file" ,
30+ "unknown-dir/some-file" ,
31+ "/some-abs-dir/some-file" ,
32+ ] ) {
33+ for ( const startDir of startDirVariants ) {
34+ expect ( trimStartDir ( p , startDir ) ) . toBe ( p ) ;
35+ }
36+ }
37+ } ) ;
Original file line number Diff line number Diff line change @@ -3,3 +3,10 @@ import { posix as pp } from "path";
33export function joinPath ( ...paths : string [ ] ) : string {
44 return pp . join ( ...paths ) ;
55}
6+
7+ export function trimStartDir ( p : string , startDir : string ) : string {
8+ const res = pp . relative ( startDir , p ) ;
9+
10+ if ( res . startsWith ( "../" ) ) return p ;
11+ else return res || "." ;
12+ }
You can’t perform that action at this time.
0 commit comments