-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #400 from Esri/feat/trim
add utility method to trim whitespace and trailing slashes from input urls
- Loading branch information
Showing
8 changed files
with
100 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
/** | ||
* Helper method to ensure that user supplied urls don't include whitespace or a trailing slash. | ||
*/ | ||
export function cleanUrl(url: string) { | ||
// trim leading and trailing spaces, but not spaces inside the url | ||
url = url.trim(); | ||
|
||
// remove the trailing slash to the url if one was included | ||
if (url[url.length - 1] === "/") { | ||
url = url.slice(0, -1); | ||
} | ||
return url; | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { cleanUrl } from "../../src/utils/clean-url"; | ||
|
||
const validUrl = | ||
"https://server.com/arcgis/rest/services/Custom Space/MapServer"; | ||
|
||
describe("cleanUrl", () => { | ||
it("should not mangle a valid url", () => { | ||
expect( | ||
cleanUrl( | ||
"https://server.com/arcgis/rest/services/Custom Space/MapServer/" | ||
) | ||
).toEqual(validUrl); | ||
}); | ||
|
||
it("should add a trailing slash", () => { | ||
expect( | ||
cleanUrl("https://server.com/arcgis/rest/services/Custom Space/MapServer") | ||
).toEqual(validUrl); | ||
}); | ||
|
||
it("should remove leading whitespace", () => { | ||
expect( | ||
cleanUrl( | ||
" https://server.com/arcgis/rest/services/Custom Space/MapServer/" | ||
) | ||
).toEqual(validUrl); | ||
}); | ||
|
||
it("should remove trailing whitespace", () => { | ||
expect( | ||
cleanUrl( | ||
"https://server.com/arcgis/rest/services/Custom Space/MapServer/ " | ||
) | ||
).toEqual(validUrl); | ||
}); | ||
|
||
it("should do it all at once", () => { | ||
expect( | ||
cleanUrl( | ||
" https://server.com/arcgis/rest/services/Custom Space/MapServer " | ||
) | ||
).toEqual(validUrl); | ||
}); | ||
}); |