1
- import { join , relative } from 'node:path'
1
+ import { dirname , relative , sep } from 'node:path'
2
2
import { fileURLToPath } from 'node:url'
3
3
4
4
5
5
export const appDir = process . cwd ( )
6
6
7
- export function fileShortPath ( importUrl : string ) : string {
8
- const path = relative ( appDir , genCurrentFilename ( importUrl ) ) . replace ( / \\ / ug, '/' )
7
+ export function fileShortPath ( importUrl : string , separator = '/' ) : string {
8
+ let path = relative ( appDir , genCurrentFilename ( importUrl ) )
9
+ if ( separator && separator !== sep ) {
10
+ path = path . replaceAll ( sep , separator )
11
+ }
9
12
return path
10
13
}
11
14
12
15
/**
13
- * generate __filename for ESM
16
+ * Generate __filename for ESM
14
17
* @param inputUrl import.meta.url
15
18
*/
16
- export function genCurrentFilename ( inputUrl : string ) : string {
17
- return fileURLToPath ( inputUrl ) . replace ( / \\ / ug, '/' )
19
+ export function genCurrentFilename ( inputUrl : string , separator = '/' ) : string {
20
+ let path = fileURLToPath ( inputUrl )
21
+ if ( separator && separator !== sep ) {
22
+ path = path . replaceAll ( sep , separator )
23
+ }
24
+ return path
18
25
}
19
26
/**
20
- * generate __dirname for ESM
27
+ * Generate __dirname for ESM
21
28
* @param inputUrl import.meta.url
22
29
*/
23
- export function genCurrentDirname ( inputUrl : string ) : string {
24
- const __filename = genCurrentFilename ( inputUrl )
25
- const dir = join ( __filename , '..' ) . replace ( / \\ / ug, '/' )
30
+ export function genCurrentDirname ( inputUrl : string , separator = '/' ) : string {
31
+ const __filename = genCurrentFilename ( inputUrl , sep )
32
+ let dir = dirname ( __filename )
33
+ if ( separator && separator !== sep ) {
34
+ dir = dir . replaceAll ( sep , separator )
35
+ }
26
36
return dir
27
37
}
28
38
@@ -52,7 +62,7 @@ const lookup: Formater[] = [
52
62
*
53
63
* @link https://stackoverflow.com/a/9462382
54
64
*/
55
- export function nFormatter ( positiveNum : number , digits = 2 , sep = '' ) : string {
65
+ export function nFormatter ( positiveNum : number , digits = 2 , separator = '' ) : string {
56
66
if ( positiveNum <= 0 ) {
57
67
return positiveNum . toString ( )
58
68
}
@@ -64,6 +74,6 @@ export function nFormatter(positiveNum: number, digits = 2, sep = ''): string {
64
74
}
65
75
66
76
const rx = / \. 0 + $ | ( \. [ 0 - 9 ] * [ 1 - 9 ] ) 0 + $ / u
67
- const ret = ( positiveNum / item . value ) . toFixed ( digits ) . replace ( rx , '$1' ) + sep + item . symbol
77
+ const ret = ( positiveNum / item . value ) . toFixed ( digits ) . replace ( rx , '$1' ) + separator + item . symbol
68
78
return ret
69
79
}
0 commit comments