Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5224 from adobe/jeff/pr4846
Browse files Browse the repository at this point in the history
Clean up _getFileExtension() method and fix all its callers
  • Loading branch information
peterflynn committed Sep 27, 2013
2 parents d6ea2f4 + 6df5ca6 commit 2b17d39
Showing 1 changed file with 30 additions and 42 deletions.
72 changes: 30 additions & 42 deletions src/file/FileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,18 @@ define(function (require, exports, module) {
return path;
}
}


/**
* Get the base name of a file or a directory.
* @param {string} fullPath full path to a file or directory
* @return {string} Returns the base name of a file or the name of a
* directory
*/
function getBaseName(fullPath) {
fullPath = canonicalizeFolderPath(fullPath);
return fullPath.substr(fullPath.lastIndexOf("/") + 1);
}

/**
* Returns a native absolute path to the 'brackets' source directory.
* Note that this only works when run in brackets/src/index.html, so it does
Expand Down Expand Up @@ -315,17 +326,23 @@ define(function (require, exports, module) {
}

/**
* Returns the file extension for a file name
* @param {string} fileName file name with extension or just a file extension
* @return {string} File extension if found, otherwise return the original file name
* Get the filename extension.
*
* @param {string} fullPath full path to a file or directory
* @return {string} Returns the extension of a filename or empty string if
* the argument is a directory or a filename with no extension
*/
function _getFileExtension(fileName) {
var i = fileName.lastIndexOf("."),
ext = (i === -1 || i >= fileName.length - 1) ? fileName : fileName.substr(i + 1);
function getFilenameExtension(fullPath) {
var baseName = getBaseName(fullPath),
idx = baseName.lastIndexOf(".");

if (idx === -1) {
return "";
}

return ext;
return baseName.substr(idx);
}

/** @const - hard-coded for now, but may want to make these preferences */
var _staticHtmlFileExts = ["htm", "html"],
_serverHtmlFileExts = ["php", "php3", "php4", "php5", "phtm", "phtml", "cfm", "cfml", "asp", "aspx", "jsp", "jspx", "shtm", "shtml"];
Expand All @@ -340,7 +357,7 @@ define(function (require, exports, module) {
return false;
}

return (_staticHtmlFileExts.indexOf(_getFileExtension(fileExt).toLowerCase()) !== -1);
return (_staticHtmlFileExts.indexOf(getFilenameExtension(fileExt).toLowerCase()) !== -1);
}

/**
Expand All @@ -353,7 +370,7 @@ define(function (require, exports, module) {
return false;
}

return (_serverHtmlFileExts.indexOf(_getFileExtension(fileExt).toLowerCase()) !== -1);
return (_serverHtmlFileExts.indexOf(getFilenameExtension(fileExt).toLowerCase()) !== -1);
}

/**
Expand All @@ -365,44 +382,15 @@ define(function (require, exports, module) {
return fullPath.substr(0, fullPath.lastIndexOf("/") + 1);
}

/**
* Get the base name of a file or a directory.
* @param {string} fullPath full path to a file or directory
* @return {string} Returns the base name of a file or the name of a
* directory
*/
function getBaseName(fullPath) {
fullPath = canonicalizeFolderPath(fullPath);
return fullPath.substr(fullPath.lastIndexOf("/") + 1);
}

/**
* Get the filename extension.
*
* @param {string} fullPath full path to a file or directory
* @return {string} Returns the extension of a filename or empty string if
* the argument is a directory or a filename with no extension
*/
function getFilenameExtension(fullPath) {
var baseName = getBaseName(fullPath),
idx = baseName.lastIndexOf(".");

if (idx === -1) {
return "";
}

return baseName.substr(idx);
}

/**
* @private
* Get the file name without the extension.
* @param {string} filename File name of a file or directory
* @return {string} Returns the file name without the extension
*/
function _getFilenameWithoutExtension(filename) {
var extension = getFilenameExtension(filename);
return extension ? filename.replace(new RegExp(extension + "$"), "") : filename;
var index = filename.lastIndexOf(".");
return index === -1 ? filename : filename.slice(0, index);
}

/**
Expand Down

0 comments on commit 2b17d39

Please sign in to comment.