Skip to content

Commit

Permalink
refactor: add some convenience methods to the framework lib/string an…
Browse files Browse the repository at this point in the history
…d lib/url files
  • Loading branch information
simplymichael committed Jun 21, 2024
1 parent 2d317d3 commit 98ac405
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/framework/lib/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module.exports = {
decode: decodeFromBase,
deflate,
inflate,
stripFirstNCharsFromString,
stripLastNCharsFromString,
};

function convertBackSlashToForwardSlash(str) {
Expand Down Expand Up @@ -76,3 +78,11 @@ function encode(str, opts) {

return encoded;
}

function stripFirstNCharsFromString(str, n = 1) {
return str.substring(n);
}

function stripLastNCharsFromString(str, n = 1) {
return str.substring(0, str.length - n);
}
28 changes: 28 additions & 0 deletions src/framework/lib/url.js
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;
}

0 comments on commit 98ac405

Please sign in to comment.