MCPcopy Create free account
hub / github.com/Acode-Foundation/Acode / formate

Function formate

src/utils/Url.js:224–256  ·  view source on GitHub ↗

* Formate Url object to string * @param {object} urlObj * @param {"ftp:"|"sftp:"|"http:"|"https:"} urlObj.protocol * @param {string|number} urlObj.hostname * @param {string} [urlObj.path] * @param {string} [urlObj.username] * @param {string} [urlObj.password] * @param {string|number} [

(urlObj)

Source from the content-addressed store, hash-verified

222 * @returns {string}
223 */
224 formate(urlObj) {
225 let { protocol, hostname, username, password, path, port, query } = urlObj;
226
227 const enc = (str) => encodeURIComponent(str);
228
229 if (!protocol || !hostname)
230 throw new Error("Cannot formate url. Missing 'protocol' and 'hostname'.");
231
232 let string = `${protocol}//`;
233
234 if (username && password) string += `${enc(username)}:${enc(password)}@`;
235 else if (username) string += `${username}@`;
236
237 string += hostname;
238
239 if (port) string += `:${port}`;
240
241 if (path) {
242 if (!path.startsWith("/")) path = "/" + path;
243
244 string += path;
245 }
246
247 if (query && typeof query === "object") {
248 string += "?";
249
250 for (let key in query) string += `${enc(key)}=${enc(query[key])}&`;
251
252 string = string.slice(0, -1);
253 }
254
255 return string;
256 },
257 /**
258 * Returns protocol of a url e.g. 'ftp:' from 'ftp://localhost/foo/bar'
259 * @param {string} url

Callers

nothing calls this directly

Calls 1

encFunction · 0.85

Tested by

no test coverage detected