@selfcommunity/utils
Version:
Utilities to integrate a Community.
115 lines (114 loc) • 3.64 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateQueryStringParameter = exports.getQueryStringParameter = exports.urlB64ToUint8Array = exports.appendURLSearchParams = exports.isValidUrls = exports.isValidUrl = exports.getDomain = exports.urlReplacer = void 0;
/**
* Utility Url Replacer
* @param path
*/
const urlReplacer = (path) => {
const replacer = function (tpl, data) {
const re = /\$\(([^)]+)?\)/g;
let match = re.exec(tpl);
while (match) {
tpl = tpl.replace(match[0], data[match[1]]);
re.lastIndex = 0;
match = re.exec(tpl);
}
return tpl;
};
return (params) => replacer(path, params);
};
exports.urlReplacer = urlReplacer;
/**
* Get domain
* @param url
*/
const getDomain = (url) => {
// eslint-disable-next-line no-useless-escape,@typescript-eslint/prefer-regexp-exec
const matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
if (matches && matches[1]) {
return matches[1];
}
return '';
};
exports.getDomain = getDomain;
/**
* Check a str is a valid url pattern
* @param url
*/
const isValidUrl = (url) => {
const regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/;
return regexp.test(url);
};
exports.isValidUrl = isValidUrl;
/**
* Check a str is a valid list of urls separated by delimiter
* @param value
* @param delimiter
*/
const isValidUrls = (value, delimiter) => {
const urls = value.trim().split(delimiter);
return urls.every(exports.isValidUrl);
};
exports.isValidUrls = isValidUrls;
/**
* Append params
* @param baseUrl
* @param queryParams
*/
function appendURLSearchParams(baseUrl, queryParams) {
let _url = baseUrl;
if (queryParams.length && _url) {
const key = Object.keys(queryParams[0])[0];
_url += (_url.split('?')[1] ? '&' : '?') + `${key}=${queryParams[0][key]}`;
queryParams.slice(1).map((p) => {
const key = Object.keys(p)[0];
_url += `&${key}=${p[key]}`;
});
}
return _url;
}
exports.appendURLSearchParams = appendURLSearchParams;
/**
* Take the application server's public key, which is Base64 URL-safe encoded,
* and convert it to a UInt8Array, because this is the expected input of the subscribe()
*/
const urlB64ToUint8Array = (base64String) => {
const padding = '='.repeat((4 - (base64String.length % 4)) % 4);
const base64 = (base64String + padding).replace(/-/g, '+').replace(/_/g, '/');
const rawData = window.atob(base64);
const outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
};
exports.urlB64ToUint8Array = urlB64ToUint8Array;
/**
* Get a query string parameter
*/
const getQueryStringParameter = (uri, key) => {
if (uri && key) {
let params = new URL(uri).searchParams;
return params.get(key);
}
return null;
};
exports.getQueryStringParameter = getQueryStringParameter;
/**
* Add or update a query string parameter
*/
const updateQueryStringParameter = (uri, key, value) => {
if (uri && key && value) {
let re = new RegExp('([?&])' + key + '=.*?(&|$)', 'i');
let separator = uri.indexOf('?') !== -1 ? '&' : '?';
if (uri.match(re)) {
return uri.replace(re, '$1' + key + '=' + value + '$2');
}
else {
return uri + separator + key + '=' + value;
}
}
return uri;
};
exports.updateQueryStringParameter = updateQueryStringParameter;
;