diff --git a/src/internal/__tests__/format-ext.spec.ts b/src/internal/__tests__/format-ext.spec.ts new file mode 100644 index 00000000..5aaa8049 --- /dev/null +++ b/src/internal/__tests__/format-ext.spec.ts @@ -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) + }) +}) diff --git a/src/internal/format-ext.ts b/src/internal/format-ext.ts new file mode 100644 index 00000000..c3131c7d --- /dev/null +++ b/src/internal/format-ext.ts @@ -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