Skip to content

Instantly share code, notes, and snippets.

@neftaly
Created February 22, 2015 20:48
Show Gist options
  • Select an option

  • Save neftaly/fc3cbe0022d0b1460cb4 to your computer and use it in GitHub Desktop.

Select an option

Save neftaly/fc3cbe0022d0b1460cb4 to your computer and use it in GitHub Desktop.
extractExtension
/**
* Extract whatever comes after the last '.' in a string.
*
* @param {string} string - input path/URL
* @return {string} chars after last '.', else `undefined`.
*/
var extractExtension = function (string) {
var ext = string.match(/\.[0-9a-z]+$/i);
if (ext && ext[0]) {
return ext[0].replace('.', '').toLowerCase();
}
return undefined;
}
describe('extractExtension()', function () {
it('Standard', function () {
var result = helpers.extractExtension('https://example.com/script.js');
assert.equal(result, 'js');
});
it('Multi-part', function () {
var result = helpers.extractExtension('https://example.com/script.min.js');
assert.equal(result, 'js');
});
it('None', function () {
var result1 = helpers.extractExtension('');
assert.equal(result1, undefined);
var result2 = helpers.extractExtension('https://example.com/.');
assert.equal(result2, undefined);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment