mirror of
https://github.com/datahub-project/datahub.git
synced 2025-07-28 11:59:54 +00:00
61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
/**
|
|
* Cached RegExp object for a global search of /
|
|
* @type {RegExp}
|
|
*/
|
|
const encodedSlashRegExp = new RegExp(encodeURIComponent('/'), 'g');
|
|
/**
|
|
* Replaces any occurrence of / with the encoded equivalent
|
|
* @param {string} urn
|
|
* @return {string}
|
|
*/
|
|
export const encodeForwardSlash = (urn: string): string => urn.replace(/\//g, () => encodeURIComponent('/'));
|
|
|
|
/**
|
|
* Replaces encoded slashes with /
|
|
* @param {string} urn
|
|
* @return {string}
|
|
*/
|
|
export const decodeForwardSlash = (urn: string): string =>
|
|
urn.replace(encodedSlashRegExp, () => decodeURIComponent('/'));
|
|
|
|
/**
|
|
* Replaces occurrences of / with the encoded counterpart in a urn string
|
|
* @param {string} urn
|
|
* @return {string}
|
|
*/
|
|
export const encodeUrn = (urn: string): string => encodeForwardSlash(urn);
|
|
|
|
/**
|
|
* Replaces encoded occurrences of / with the string /
|
|
* @param {string} urn
|
|
* @return {string}
|
|
*/
|
|
export const decodeUrn = (urn: string): string => decodeForwardSlash(urn);
|
|
|
|
/**
|
|
* Stores the encoded URL for the asterisk/wildcard symbol since encodeURIComponent doesn't catch these
|
|
* as a reserved symbol
|
|
* @type {string}
|
|
*/
|
|
const encodedWildcard = '%2A';
|
|
|
|
/**
|
|
* Cached RegExp object for a global search of /
|
|
* @type {RegExp}
|
|
*/
|
|
const encodedWildcardRegExp = new RegExp(encodedWildcard, 'g');
|
|
|
|
/**
|
|
* Replaces any occurence of * with the encoded equivalent
|
|
* @param {string} urn
|
|
* @return {string}
|
|
*/
|
|
export const encodeWildcard = (urn: string): string => urn.replace(/\*/g, encodedWildcard);
|
|
|
|
/**
|
|
* Replaces encoded slashes with /
|
|
* @param {string} urn
|
|
* @return {string}
|
|
*/
|
|
export const decodeWildcard = (urn: string): string => urn.replace(encodedWildcardRegExp, decodeURIComponent('*'));
|