Skip to content

Commit 7097d7a

Browse files
committed
feat(core): add 2nd param separator of functions
default value '/' - fileShortPath() - genCurrentFilename() - genCurrentDirname()
1 parent 0e46bbb commit 7097d7a

File tree

2 files changed

+74
-12
lines changed

2 files changed

+74
-12
lines changed

packages/core/src/lib/helper.ts

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
1-
import { join, relative } from 'node:path'
1+
import { dirname, relative, sep } from 'node:path'
22
import { fileURLToPath } from 'node:url'
33

44

55
export const appDir = process.cwd()
66

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+
}
912
return path
1013
}
1114

1215
/**
13-
* generate __filename for ESM
16+
* Generate __filename for ESM
1417
* @param inputUrl import.meta.url
1518
*/
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
1825
}
1926
/**
20-
* generate __dirname for ESM
27+
* Generate __dirname for ESM
2128
* @param inputUrl import.meta.url
2229
*/
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+
}
2636
return dir
2737
}
2838

@@ -52,7 +62,7 @@ const lookup: Formater[] = [
5262
*
5363
* @link https://stackoverflow.com/a/9462382
5464
*/
55-
export function nFormatter(positiveNum: number, digits = 2, sep = ''): string {
65+
export function nFormatter(positiveNum: number, digits = 2, separator = ''): string {
5666
if (positiveNum <= 0) {
5767
return positiveNum.toString()
5868
}
@@ -64,6 +74,6 @@ export function nFormatter(positiveNum: number, digits = 2, sep = ''): string {
6474
}
6575

6676
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
6878
return ret
6979
}

packages/core/test/21.helper.test.ts

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import assert from 'node:assert/strict'
2+
import { sep } from 'node:path'
3+
4+
import { fileShortPath, genCurrentDirname, genCurrentFilename } from '../src/lib/helper.js'
5+
6+
7+
describe.only(fileShortPath(import.meta.url), () => {
8+
9+
describe('should genCurrentFilename() work', () => {
10+
it('common', async () => {
11+
const __filename = genCurrentFilename(import.meta.url)
12+
assert(__filename.endsWith('packages/core/test/21.helper.test.ts'), __filename)
13+
})
14+
15+
it('separator -', async () => {
16+
const __filename = genCurrentFilename(import.meta.url, '-')
17+
assert(__filename.endsWith('-core-test-21.helper.test.ts'), __filename)
18+
})
19+
20+
it(`separator ${sep}`, async () => {
21+
const __filename = genCurrentFilename(import.meta.url, sep)
22+
if (sep === '/') {
23+
assert(__filename.endsWith('packages/core/test/21.helper.test.ts'), __filename)
24+
}
25+
else if (sep === '\\') {
26+
assert(__filename.endsWith('packages\\core\\test\\21.helper.test.ts'), __filename)
27+
}
28+
})
29+
})
30+
31+
describe('should genCurrentDirname() work', () => {
32+
it('common', async () => {
33+
const __dirname = genCurrentDirname(import.meta.url)
34+
assert(__dirname.endsWith('packages/core/test'), __dirname)
35+
})
36+
37+
it('separator -', async () => {
38+
const __dirname = genCurrentDirname(import.meta.url, '-')
39+
assert(__dirname.endsWith('-core-test'), __dirname)
40+
})
41+
42+
it(`separator ${sep}`, async () => {
43+
const __dirname = genCurrentDirname(import.meta.url, sep)
44+
if (sep === '/') {
45+
assert(__dirname.endsWith('packages/core/test'), __dirname)
46+
}
47+
else if (sep === '\\') {
48+
assert(__dirname.endsWith('packages\\core\\test'), __dirname)
49+
}
50+
})
51+
})
52+
})

0 commit comments

Comments
 (0)