https://github.com/silverwind/file-extension
Get the extension of a given filename or path
https://github.com/silverwind/file-extension
Last synced: 10 months ago
JSON representation
Get the extension of a given filename or path
- Host: GitHub
- URL: https://github.com/silverwind/file-extension
- Owner: silverwind
- License: bsd-2-clause
- Created: 2015-05-03T01:34:44.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2018-05-31T18:22:15.000Z (about 8 years ago)
- Last Synced: 2025-06-30T16:06:40.222Z (11 months ago)
- Language: Makefile
- Homepage:
- Size: 128 KB
- Stars: 30
- Watchers: 2
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# file-extension
[](https://www.npmjs.org/package/file-extension) [](https://www.npmjs.org/package/file-extension) [](https://travis-ci.org/silverwind/file-extension)
> Get the extension of a given filename or path
Differences to [`path.extname`](https://nodejs.org/api/path.html#path_path_extname_path):
* Treats dotfiles as extension (`.eslintrc` => `eslintrc`)
* Treats extensionless files as extension (`Makefile` => `makefile`)
* Doesn't include the dot in the extension (`package.json` => `json`)
* Returns lowercase by default (`LICENCE` => `licence`)
Works in Node.js and the browser. The module's size is currently 267 bytes gzipped.
## Installation
#### Node.js
```
$ npm install --save file-extension
```
#### Browser
```html
```
## Example
```js
var fileExtension = require('file-extension'); // Skip this in the browser
// Case insensitive
fileExtension('file.zip'); //=> 'zip'
fileExtension('.Dockerfile'); //=> 'dockerfile'
fileExtension('file'); //=> 'file'
fileExtension('.file.tar'); //=> 'tar'
// Or with case preserved
fileExtension('.Vagrantfile', {preserveCase: true}); //=> 'Vagrantfile'
fileExtension('INDEX.HTML', {preserveCase: true}); //=> 'HTML'
```
## API
### fileExtension(filename, [options])
- `filename` {String} The file name. Required.
- `options` {Object} Options object.
#### Options
- `preserveCase` {Boolean} Whether case should be preserved. Default: false.
© [silverwind](https://github.com/silverwind), distributed under BSD licence