forked from gulpjs/rechoir
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify: Remove regex from
lib/extension.js
- Loading branch information
Showing
1 changed file
with
35 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,44 @@ | ||
'use strict'; | ||
|
||
var path = require('path'); | ||
|
||
var EXTRE = /\.[^.]*/g; | ||
var LONGEXTRE = /^[.]?[^.]+([.].*[^.])$/; | ||
function getLongExtension(basename) { | ||
if (basename[basename.length - 1] === '.') { | ||
return null; | ||
} | ||
|
||
var startIndex = (basename[0] === '.') ? 1 : 0; | ||
|
||
var dotIndex = basename.indexOf('.', startIndex); | ||
if (dotIndex <= startIndex) { | ||
return null; | ||
} | ||
|
||
return basename.slice(dotIndex); | ||
} | ||
|
||
function getPossibleExtensions(longExtension) { | ||
var arr = [longExtension]; | ||
var len = longExtension.length; | ||
var startIndex = 1; | ||
|
||
while (startIndex < len) { | ||
var dotIndex = longExtension.indexOf('.', startIndex); | ||
if (dotIndex < 0) { | ||
break; | ||
} | ||
arr.push(longExtension.slice(dotIndex)); | ||
startIndex = dotIndex + 1; | ||
} | ||
|
||
return arr; | ||
} | ||
|
||
module.exports = function(input) { | ||
var basename = path.basename(input); | ||
var longExtension = LONGEXTRE.exec(basename); | ||
var longExtension = getLongExtension(basename); | ||
if (!longExtension) { | ||
return; | ||
} | ||
var possibleExtensions = longExtension[1].match(EXTRE); | ||
/* istanbul ignore if */ | ||
if (!possibleExtensions) { | ||
return; | ||
} | ||
return possibleExtensions.map(function(_, idx, exts) { | ||
return exts.slice(idx).join(''); | ||
}); | ||
return getPossibleExtensions(longExtension); | ||
}; |