├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 22 14 | - 20 15 | - 18 16 | steps: 17 | - uses: actions/checkout@v4 18 | - uses: actions/setup-node@v4 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | Change the file extension of a path. 3 | 4 | @param filePath - The file path whose extension needs to be changed. 5 | @param extension - The new extension to replace the old one. To remove the extension, pass an empty string. 6 | 7 | @example 8 | ``` 9 | import changeFileExtension from 'change-file-extension'; 10 | 11 | changeFileExtension('foo/bar.txt', 'md'); 12 | //=> 'foo/bar.md' 13 | 14 | changeFileExtension('foo/bar.txt', '.md'); 15 | //=> 'foo/bar.md' 16 | 17 | changeFileExtension('bar.txt', ''); 18 | //=> 'bar' 19 | ``` 20 | */ 21 | export default function changeFileExtension(filePath: string, extension: string): string; 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import path from 'node:path'; 2 | 3 | export default function changeFileExtension(filePath, extension) { 4 | if (typeof filePath !== 'string') { 5 | throw new TypeError(`Expected \`filePath\` to be a string, got \`${typeof filePath}\`.`); 6 | } 7 | 8 | if (typeof extension !== 'string') { 9 | throw new TypeError(`Expected \`extension\` to be a string, got \`${typeof extension}\`.`); 10 | } 11 | 12 | if (filePath === '') { 13 | return ''; 14 | } 15 | 16 | extension = extension ? (extension.startsWith('.') ? extension : `.${extension}`) : ''; 17 | 18 | const basename = path.basename(filePath, path.extname(filePath)); 19 | return path.join(path.dirname(filePath), basename + extension); 20 | } 21 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "change-file-extension", 3 | "version": "1.0.0", 4 | "description": "Change the file extension of a path", 5 | "license": "MIT", 6 | "repository": "sindresorhus/change-file-extension", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": { 15 | "types": "./index.d.ts", 16 | "default": "./index.js" 17 | }, 18 | "sideEffects": false, 19 | "engines": { 20 | "node": ">=18" 21 | }, 22 | "scripts": { 23 | "test": "xo && ava && tsc index.d.ts" 24 | }, 25 | "files": [ 26 | "index.js", 27 | "index.d.ts" 28 | ], 29 | "keywords": [ 30 | "change", 31 | "file", 32 | "filepath", 33 | "extension", 34 | "ext", 35 | "path", 36 | "replace", 37 | "modify", 38 | "rename", 39 | "extname", 40 | "basename", 41 | "file-extension", 42 | "file-path" 43 | ], 44 | "devDependencies": { 45 | "ava": "^6.1.3", 46 | "typescript": "^5.5.4", 47 | "xo": "^0.59.3" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # change-file-extension 2 | 3 | > Change the file extension of a path 4 | 5 | ## Install 6 | 7 | ```sh 8 | npm install change-file-extension 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import changeFileExtension from 'change-file-extension'; 15 | 16 | changeFileExtension('foo/bar.txt', 'md'); 17 | //=> 'foo/bar.md' 18 | 19 | changeFileExtension('foo/bar.txt', '.md'); 20 | //=> 'foo/bar.md' 21 | 22 | changeFileExtension('bar.txt', ''); 23 | //=> 'bar' 24 | ``` 25 | 26 | ## API 27 | 28 | ### changeFileExtension(filePath, extension) 29 | 30 | #### filePath 31 | 32 | Type: `string` 33 | 34 | The file path whose extension needs to be changed. 35 | 36 | #### extension 37 | 38 | Type: `string` 39 | 40 | The new extension to replace the old one. 41 | 42 | To remove the extension, pass an empty string. 43 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import changeFileExtension from './index.js'; 3 | 4 | test('replaces extension on path', t => { 5 | const newPath = changeFileExtension('/long/path/to/user/file.txt', 'md'); 6 | t.is(newPath, '/long/path/to/user/file.md'); 7 | }); 8 | 9 | test('replaces extension on filename', t => { 10 | const newPath = changeFileExtension('file.txt', 'md'); 11 | t.is(newPath, 'file.md'); 12 | }); 13 | 14 | test('returns same path for empty string', t => { 15 | const newPath = changeFileExtension('', 'md'); 16 | t.is(newPath, ''); 17 | }); 18 | 19 | test('removes extension on path', t => { 20 | const newPath = changeFileExtension('/long/path/to/user/file.txt', ''); 21 | t.is(newPath, '/long/path/to/user/file'); 22 | }); 23 | 24 | test('removes extension on filename', t => { 25 | const newPath = changeFileExtension('file.txt', ''); 26 | t.is(newPath, 'file'); 27 | }); 28 | 29 | test('handles relative paths', t => { 30 | const newPath = changeFileExtension('./src/file.txt', 'md'); 31 | t.is(newPath, 'src/file.md'); 32 | }); 33 | 34 | test('handles absolute paths', t => { 35 | const newPath = changeFileExtension('/usr/local/src/file.txt', 'md'); 36 | t.is(newPath, '/usr/local/src/file.md'); 37 | }); 38 | 39 | test('handles file names starting with dot', t => { 40 | const newPath = changeFileExtension('.gitignore', 'txt'); 41 | t.is(newPath, '.gitignore.txt'); 42 | }); 43 | 44 | test('handles file names with multiple dots', t => { 45 | const newPath = changeFileExtension('index.spec.js', 'ts'); 46 | t.is(newPath, 'index.spec.ts'); 47 | }); 48 | 49 | test('handles extension with multiple dots', t => { 50 | const newPath = changeFileExtension('file.txt', '.tar.gz'); 51 | t.is(newPath, 'file.tar.gz'); 52 | }); 53 | --------------------------------------------------------------------------------