Skip to content

Commit

Permalink
Merge b34fc31 into 076f790
Browse files Browse the repository at this point in the history
  • Loading branch information
marlon360 committed May 18, 2018
2 parents 076f790 + b34fc31 commit 5db5d41
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/

var db = require('mime-db')
var extname = require('path').extname

/**
* Module variables.
Expand Down Expand Up @@ -135,9 +134,7 @@ function lookup (path) {
}

// get the extension ("ext" or ".ext" or full path)
var extension = extname('x.' + path)
.toLowerCase()
.substr(1)
var extension = getExtension(path);

if (!extension) {
return false
Expand All @@ -146,6 +143,48 @@ function lookup (path) {
return exports.types[extension] || false
}

/**
* Get the extension of a path
* @private
*/

function getExtension(path){

// Split by Slash to seperate Path from File
const pathArray = path.split("/");
// if split return just 1 item -> no path
if (pathArray.length === 1) {
// split path by dot to get extension
return path.split('.').pop().toLowerCase();
} else {
// get last part of path which is the filename
const file = pathArray.pop();
// if filename starts with a dot -> dotfile
if (file.startsWith('.')) {
// split dotfile by dot
const fileArray = file.split('.');
// if length is greater than 2 it has more than 1 dot -> dotfile with extension
if(fileArray.length > 2){
// get extension of dotfile
return fileArray.pop().toLowerCase();
} else {
// if dotfile has no extension
return null;
}
} else {
// split filename by dot to get extension
const fileArray = file.split('.');
// if just 1 item -> no extension
if(fileArray.length === 1){
return null;
} else {
// get extension of filename
return fileArray.pop().toLowerCase();
}
}
}
}

/**
* Populate the extensions and types maps.
* @private
Expand Down

0 comments on commit 5db5d41

Please sign in to comment.