Skip to content

Commit

Permalink
fix(path): add parseExt() and removeExt()
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Sep 23, 2023
1 parent a2b48f7 commit 2670e49
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 45 deletions.
74 changes: 37 additions & 37 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/common",
"version": "4.13.2",
"version": "4.14.0",
"description": "The Athenna common helpers to use in any Node.js ESM project.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down Expand Up @@ -56,7 +56,7 @@
"chalk": "^5.3.0",
"change-case": "^4.1.2",
"collect.js": "^4.36.1",
"fastify": "^4.22.1",
"fastify": "^4.23.2",
"got": "^12.6.1",
"http-status-codes": "^2.2.0",
"is-wsl": "^2.2.0",
Expand All @@ -74,7 +74,7 @@
"youch-terminal": "^2.2.2"
},
"devDependencies": {
"@athenna/test": "^4.4.0",
"@athenna/test": "^4.7.0",
"@types/bytes": "^3.1.1",
"@types/callsite": "^1.0.31",
"@types/debug": "^4.1.7",
Expand Down
44 changes: 39 additions & 5 deletions src/helpers/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import callSite from 'callsite'
import { fileURLToPath } from 'node:url'
import { homedir, tmpdir } from 'node:os'
import type { PathDirs } from '#src/types'
import { sep, normalize, dirname } from 'node:path'
import { sep, normalize, dirname, parse } from 'node:path'

export class Path {
public static dirs: PathDirs = {
Expand Down Expand Up @@ -66,10 +66,7 @@ export class Path {
* Return js or ts extension depending on IS_TS.
*/
public static ext(): string {
const isTs = !!(
process.env.IS_TS &&
(process.env.IS_TS === 'true' || process.env.IS_TS === '(true)')
)
const isTs = !!(process.env.IS_TS && process.env.IS_TS === 'true')

if (isTs) {
return 'ts'
Expand All @@ -78,6 +75,43 @@ export class Path {
return 'js'
}

/**
* Remove the extension from a path.
*/
public static removeExt(path: string): string {
const parsedPath = parse(path)

if (!parsedPath.dir) {
return parsedPath.name
}

return parsedPath.dir.concat(sep, parsedPath.name)
}

/**
* Parse the extension of a path using the `Path.ext()` method.
* If the path ends with .js and `Path.ext()` returns .ts, the
* path will be parsed to end with .ts. The same happens when
* the path ends with .ts and `Path.ext()` returns .js.
*/
public static parseExt(path: string): string {
if (path.endsWith('.d.ts')) {
return path
}

const { ext } = parse(path)

if (!ext) {
return path
}

if (ext === `.${Path.ext()}`) {
return path
}

return `${Path.removeExt(path)}.${Path.ext()}`
}

/**
* Return the pwd path of your project.
*/
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/PathTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,28 @@ export default class PathTest {
assert.isTrue(Path.src().endsWith('src'))
assert.isTrue(Path.app().endsWith('app'))
}

@Test()
public shouldBeAbleToRemoveExtensionOfFilePath({ assert }: Context) {
assert.equal(Path.removeExt('file.ts'), 'file')
assert.equal(Path.removeExt('file.js'), 'file')
assert.equal(Path.removeExt('file'), 'file')
assert.equal(Path.removeExt(Path.app('hello.js')), Path.app('hello'))
assert.equal(Path.removeExt(Path.app('hello.java')), Path.app('hello'))
assert.equal(Path.removeExt(Path.app('hello')), Path.app('hello'))
assert.equal(Path.removeExt(Path.app('hello.service.js')), Path.app('hello.service'))
assert.equal(Path.removeExt(Path.app('hello.service.ts')), Path.app('hello.service'))
}

@Test()
public shouldBeAbleToParseExtensionOfFilePathUsingPathExt({ assert }: Context) {
assert.equal(Path.parseExt('file.ts'), 'file.ts')
assert.equal(Path.parseExt('file.js'), 'file.ts')
assert.equal(Path.parseExt('file'), 'file')
assert.equal(Path.parseExt(Path.app('hello.js')), Path.app('hello.ts'))
assert.equal(Path.parseExt(Path.app('hello.java')), Path.app('hello.ts'))
assert.equal(Path.parseExt(Path.app('hello')), Path.app('hello'))
assert.equal(Path.parseExt(Path.app('hello.service.js')), Path.app('hello.service.ts'))
assert.equal(Path.parseExt(Path.app('hello.service.ts')), Path.app('hello.service.ts'))
}
}

0 comments on commit 2670e49

Please sign in to comment.