Skip to content

Commit

Permalink
refactor(utils): expose formatExt as utility
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Dec 9, 2022
1 parent dabb689 commit e58f9e2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 44 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"types": "./dist/index.d.mts",
"typesVersions": {
"*": {
"./utils/*": [
"./dist/utils/*.d.mts"
"./utils": [
"./dist/utils/index.d.mts"
]
}
},
Expand Down
22 changes: 0 additions & 22 deletions src/internal/__tests__/format-ext.spec.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/internal/format-ext.ts

This file was deleted.

27 changes: 27 additions & 0 deletions src/utils/__tests__/format-ext.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @file Unit Tests - formatExt
* @module pathe/utils/tests/unit/formatExt
*/

import type { Ext } from '#src/types'
import testSubject from '../format-ext'

describe('unit:utils/formatExt', () => {
let ext: Ext

beforeEach(() => {
ext = '.mjs'
})

it('should return empty string if ext is empty string', () => {
expect(testSubject('')).to.equal('')
})

it('should return ext without modifications if ext starts with dot', () => {
expect(testSubject(ext)).to.equal(ext)
})

it('should return formatted file extension', () => {
expect(testSubject(ext.slice(1))).to.equal(ext)
})
})
23 changes: 23 additions & 0 deletions src/utils/format-ext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @file Utilities - formatExt
* @module pathe/utils/formatExt
*/

import type { Ext } from '#src/types'
import type { EmptyString } from '@flex-development/tutils'

/**
* Formats a file extension.
*
* This includes:
*
* - Prepending a `.` (dot) character if not already present
*
* @param {string} [ext=''] - File extension to format
* @return {EmptyString | Ext} Formatted file extension or empty string
*/
const formatExt = (ext: string = ''): EmptyString | Ext => {
return ext.trim().replace(/^([^.])/, '.$1') as EmptyString | Ext
}

export default formatExt
6 changes: 6 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @file Entry Point - Utilities
* @module pathe/utils
*/

export { default as formatExt } from './format-ext'

0 comments on commit e58f9e2

Please sign in to comment.