Created
February 22, 2015 20:48
-
-
Save neftaly/fc3cbe0022d0b1460cb4 to your computer and use it in GitHub Desktop.
extractExtension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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