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

Commit

Permalink
Deprecate FileUtils.getFilenameExtension() (which includes leading "."),
Browse files Browse the repository at this point in the history
introduce new FileUtils.getFileExtension() (which excludes it).
Fixes bug #5365.
  • Loading branch information
peterflynn committed Sep 27, 2013
1 parent b4497a6 commit d10c13d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
36 changes: 27 additions & 9 deletions src/file/FileUtils.js
Expand Up @@ -239,7 +239,7 @@ define(function (require, exports, module) {
}

/**
* Get the base name of a file or a directory.
* Get the name of a file or a directory, removing any preceding path.
* @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
Expand Down Expand Up @@ -326,21 +326,37 @@ define(function (require, exports, module) {
}

/**
* Get the filename extension.
* Get the file extension (excluding ".") given a path OR a bare filename.
* Returns "" for names with no extension. If the name starts with ".", the
* full remaining text is considered the 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) {
function getFileExtension(fullPath) {
var baseName = getBaseName(fullPath),
idx = baseName.lastIndexOf(".");

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

return baseName.substr(idx);
return baseName.substr(idx + 1);
}

/**
* Similar to getFileExtension(), but includes the leading "." in the returned value.
* @deprecated Use getFileExtension() instead. This API will be removed soon.
*/
function getFilenameExtension(fullPath) {
console.error("Warning: FileUtils.getFilenameExtension() is deprecated. Use FileUtils.getFileExtension() (which omits the '.') instead.");

var ext = getFileExtension(fullPath);
if (ext !== "") {
ext = "." + ext;
}
return ext;
}

/** @const - hard-coded for now, but may want to make these preferences */
Expand All @@ -357,7 +373,7 @@ define(function (require, exports, module) {
return false;
}

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

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

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

/**
* Get the parent directory of a file. If a directory is passed in the directory is returned.
* @param {string} fullPath full path to a file or directory
* @return {string} Returns the path to the parent directory of a file or the path of a directory
* @return {string} Returns the path to the parent directory of a file or the path of a directory,
* including trailing "/"
*/
function getDirectoryPath(fullPath) {
return fullPath.substr(0, fullPath.lastIndexOf("/") + 1);
Expand All @@ -402,8 +419,8 @@ define(function (require, exports, module) {
* @return {number} The result of the local compare function
*/
function compareFilenames(filename1, filename2, extFirst) {
var ext1 = getFilenameExtension(filename1),
ext2 = getFilenameExtension(filename2),
var ext1 = getFileExtension(filename1),
ext2 = getFileExtension(filename2),
cmpExt = ext1.toLocaleLowerCase().localeCompare(ext2.toLocaleLowerCase(), undefined, {numeric: true}),
cmpNames;

Expand Down Expand Up @@ -438,6 +455,7 @@ define(function (require, exports, module) {
exports.isServerHtmlFileExt = isServerHtmlFileExt;
exports.getDirectoryPath = getDirectoryPath;
exports.getBaseName = getBaseName;
exports.getFileExtension = getFileExtension;
exports.getFilenameExtension = getFilenameExtension;
exports.compareFilenames = compareFilenames;
});
2 changes: 1 addition & 1 deletion src/language/JSUtils.js
Expand Up @@ -379,7 +379,7 @@ define(function (require, exports, module) {
if (!keepAllFiles) {
// Filter fileInfos for .js files
jsFiles = fileInfos.filter(function (fileInfo) {
return (/^\.js/i).test(FileUtils.getFilenameExtension(fileInfo.fullPath));
return FileUtils.getFileExtension(fileInfo.fullPath).toLowerCase() === "js";
});
} else {
jsFiles = fileInfos;
Expand Down
2 changes: 1 addition & 1 deletion src/project/FileIndexManager.js
Expand Up @@ -439,7 +439,7 @@ define(function (require, exports, module) {
_addIndex(
"css",
function (entry) {
return FileUtils.getFilenameExtension(entry.name) === ".css";
return FileUtils.getFileExtension(entry.name) === "css";
}
);

Expand Down

0 comments on commit d10c13d

Please sign in to comment.