JS: Removing trailing dots from a string

Met with this bug https://bugzilla.wikimedia.org/71704 in the UploadWizard extension last day, which was to make sure that the file name string do not contain dots or whitespace as that can cause trouble later. Thanks to Mark Traucer ( Wikimedia ), we got a wonderful solution for the same.

Problem:
Remove trailing dots from a string foo.
Solution:

cleanedFoo = $.trim( foo.replace( /\.+$/g, '' ) );

That would remove ‘n’ number of dots trailing in string foo
My personal solution to this was a bit more easy, but would work with only one trailing dot:

if ( foo.slice( -1 ) === '.' ) {
        cleanedFoo = foo.slice( 0, -1 );
}

The change is in gerrit : https://gerrit.wikimedia.org/r/#/c/169326/. 🙂
Happy Hacking !!

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s