Skip to content

Commit

Permalink
Clears whitespace on URLs (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmelberg-okta authored Jun 28, 2018
1 parent c96c4da commit 44a77f5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ util.getLink = function(obj, linkName, altName) {
if (!obj || !obj._links) {
return;
}

var link = util.clone(obj._links[linkName]);

// If a link has a name and we have an altName, return if they match
Expand Down Expand Up @@ -235,10 +235,12 @@ util.removeTrailingSlash = function(path) {
if (!path) {
return;
}
if (path.slice(-1) === '/') {
return path.slice(0, -1);
// Remove any whitespace before or after string
var trimmed = path.replace(/^\s+|\s+$/gm,'');
if (trimmed.slice(-1) === '/') {
return trimmed.slice(0, -1);
}
return path;
return trimmed;
};

util.isIE11OrLess = function() {
Expand Down
32 changes: 32 additions & 0 deletions test/spec/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,37 @@ define(function(require) {
});
});

describe('removeTrailingSlash', function() {
it('returns a url with a trailing slash without the trailing slash', function() {
var url = 'https://domain.com/';
expect(util.removeTrailingSlash(url)).toEqual('https://domain.com');
});

it('returns a url without a trailing slash as is', function() {
var url = 'https://domain.com';
expect(util.removeTrailingSlash(url)).toEqual('https://domain.com');
});

it('returns a url with a trailing slash and appended whitespace correctly', function() {
var url = 'https://domain.com/ ';
expect(util.removeTrailingSlash(url)).toEqual('https://domain.com');
});

it('returns a url with a trailing slash and prepended whitespace correctly', function() {
var url = ' https://domain.com/';
expect(util.removeTrailingSlash(url)).toEqual('https://domain.com');
});

it('returns a url without a trailing slash and appended whitespace correctly', function() {
var url = 'https://domain.com ';
expect(util.removeTrailingSlash(url)).toEqual('https://domain.com');
});

it('returns a url without a trailing slash and prepended whitespace correctly', function() {
var url = ' https://domain.com';
expect(util.removeTrailingSlash(url)).toEqual('https://domain.com');
});
});

});
});

0 comments on commit 44a77f5

Please sign in to comment.