-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add some convenience methods to the framework lib/string an…
…d lib/url files
- Loading branch information
1 parent
2d317d3
commit 98ac405
Showing
2 changed files
with
38 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const { stripFirstNCharsFromString, stripLastNCharsFromString } = require("./string"); | ||
|
||
module.exports = { | ||
getValidUrl, | ||
normalizeUrlPath, | ||
}; | ||
|
||
|
||
function getValidUrl(baseUrl, path, apiBasePath) { | ||
path = normalizeUrlPath(path) || ""; | ||
apiBasePath = normalizeUrlPath(apiBasePath) || ""; | ||
|
||
return baseUrl + (apiBasePath ? `/${apiBasePath}` : "") + `/${path}`; | ||
} | ||
|
||
function normalizeUrlPath(path) { | ||
path = String(path); | ||
|
||
if(path.startsWith("/")) { | ||
path = stripFirstNCharsFromString(path, 1); | ||
} | ||
|
||
if(path.endsWith("/")) { | ||
path = stripLastNCharsFromString(path, 1); | ||
} | ||
|
||
return path; | ||
} |