Skip to content

Commit

Permalink
feat(internal): formatExt
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Dec 7, 2022
1 parent 6fb7173 commit 6287a90
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/internal/__tests__/format-ext.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @file Unit Tests - formatExt
* @module pathe/internal/tests/unit/formatExt
*/

import testSubject from '../format-ext'

describe('unit:internal/formatExt', () => {
let ext: string

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

it('should do nothing if value begins with dot character', () => {
expect(testSubject(ext)).to.equal(ext)
})

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

import type { Ext } from '#src/types'
import validateString from './validate-string'

/**
* File extension formatter.
*
* @param {string} ext - File extension to format
* @return {Ext} `ext` formatted
*/
const formatExt = (ext: string): Ext => {
validateString(ext, 'ext')
return ext.replace(/^([^.])/, '.$1') as Ext
}

export default formatExt

0 comments on commit 6287a90

Please sign in to comment.