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

Clean up _getFileExtension() method and fix all its callers #4846

Closed
wants to merge 5 commits into from

Conversation

megatolya
Copy link
Contributor

Fixes #4575

I've taken into account comments on this pull request and fixed it.

@ghost ghost assigned JeffryBooher Aug 21, 2013
@@ -402,7 +390,7 @@ define(function (require, exports, module) {
*/
function _getFilenameWithoutExtension(filename) {
var extension = getFilenameExtension(filename);
return extension ? filename.replace(new RegExp(extension + "$"), "") : filename;
return extension ? filename.replace(new RegExp("." + extension + "$"), "") : filename;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would more performant to just rewrite this as:

return filename.slice(0, filename.getLastIndexOf('.'));

@JeffryBooher
Copy link
Contributor

Done with initial review.

@TomMalbran
Copy link
Contributor

This PR has a jslint warning, since is now using getFilenameExtension before it was declared. So it should be moved to fix that.

@@ -402,7 +390,7 @@ define(function (require, exports, module) {
*/
function _getFilenameWithoutExtension(filename) {
var extension = getFilenameExtension(filename);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line isn't necessary.

@JeffryBooher
Copy link
Contributor

You have a lint error on line 325. Move getBaseName() up and that should fix it.

* @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;
return filename.slice(0, filename.lastIndexOf('.'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this breaks if the filename has no extension -- slice(0, -1) removes the last char.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this and it was fine. slice (0, -1) will basically return a new string that starts with 0 and has the length of the string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing this again...

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slice(0, -1) will remove the last char according to the JavaScript docs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, @peterflynn is right. You could pass undefined to slice if lastIndexOf() returned -1 e.g.

var index = lastIndexOf(".");
return filename.slice(0, index === -1 ? undefined : index);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or return index === -1 ? filename : filename.slice(0, index);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I've missed slice(0, -1). What do you think about movin regexp+replace back? I think this func would be more obvious then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure I understand your meaning of this func being more obvious. I think the regex adds a lot of overhead and is very slow compared to this method. This is far easier to read than regex, imo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed -- a simple indexOf+slice seems cleaner to me too.

@JeffryBooher
Copy link
Contributor

Closing in lieu of #5224

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FileUtils contains two different 'get file extension' functions
5 participants