Natural Pacifist Manatees
Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

file-extension

4.0.5 • Public • Published

file-extension

Get the extension of a given filename or path

Differences to path.extname:

  • 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

<script src="file-extension.js"></script>
<!-- Available as global `fileExtension` -->

Example

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, distributed under BSD licence

install

npm i file-extension

Downloadsweekly downloads

5,315

version

4.0.5

license

BSD-2-Clause

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability