1.0.2 • Published 2 years ago

@jo-sword/file-ext v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

File Ext

npm.io npm.io

Receives the extension or mimeType of a 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)
  • Can get mimeType optionally

Installation

$ npm install --save @jo-sword/file-ext

Example

const fileExt = require('@jo-sword/file-ext')

fileExt('assets/dog.png')    //=> 'png'
fileExt('file.zip')          //=> 'zip'
fileExt('.Eslintrc')         //=> 'eslintrc'
fileExt('.tar.gz')           //=> 'gz'

// With mimeType
fileExt('js', { withType: true }) //=> { extName: 'js', type: "application/javascript; charset=utf-8" }
fileExt('gitignore', { withType: true }) //=> { extName: 'gitignore', type: false }

// Or keep case.
fileExt('.Jo-Sword', { keepCase: true }) //=> 'Jo-Sword'
fileExt('INDEX.HTML', { keepCase: true })   //=> 'HTML'

API

FileExt(filename, options)

  • filename {string} The file name or path. Required.
  • options {object} Options object.

Options

  • withType {Boolean} Whether type should be include. Default: false.
  • keepCase {Boolean} Whether case should be preserved. Default: false.

© orlandobricenob, distributed under TSC licence