datahub/datahub-web/@datahub/utils/addon/api/encode-decode-uri-component-with-space.ts
Charlie Tran 843a6c5bbb
feat(frontend): update datahub-web client UI code (#1806)
* Releases updated version of datahub-web client UI code

* Fix typo in yarn lock

* Change yarn lock to match yarn registry directories

* Previous commit missed some paths

* Even more changes to yarnlock missing in previous commit

* Include codegen file for typings

* Add files to get parity for datahub-web and current OS datahub-midtier

* Add in typo fix from previous commit - change to proper license

* Implement proper OS fix for person entity picture url

* Workarounds for open source DH issues

* Fixes institutional memory api and removes unopensourced tabs for datasets

* Fixes search dataset deprecation and user search issue as a result of changes

* Remove internal only options in the avatar menu
2020-08-26 15:44:50 -07:00

43 lines
1.4 KiB
TypeScript

/**
* Spaces should replaced with `+` to conform with application/x-www-form-urlencoded encoding
* https://url.spec.whatwg.org/#concept-urlencoded
* https://www.w3.org/TR/REC-html40/interact/forms.html#form-content-type
* @param {string} sequence to be encoded
* @returns {string} the encoded URI component sequence with spaces represented by plus
*/
export const encode = (sequence: string): string => encodeURIComponent(sequence).replace(/%20/g, '+');
/**
* Decodes a URI component sequence by replacing `+` with spaces prior to decoding
* https://www.w3.org/TR/REC-html40/interact/forms.html#form-content-type
* @param {string} sequence to be decoded
* @returns {string} the decoded URI component sequence with spaces
*/
export const decode = (sequence: string): string => decodeURIComponent(String(sequence).replace(/\+/g, ' '));
const deEntityIfyMap: Record<string, string> = {
lt: '<',
gt: '>',
quot: '"'
};
/**
*
* @param str
* @returns {string}
*/
export const deEntityIfy = (str: string): string =>
String(str).replace(/&([^&;]+);/g, (entity, sequence): string => {
const character: string | undefined = deEntityIfyMap[sequence];
return typeof character === 'string' ? character : entity;
});
const entityIfyMap: Record<string, string> = {
'"': '&quot;',
'<': '&lt;',
'>': '&gt;',
'&': '&amp;'
};
export const entityIfy = (str: string): string => String(str).replace(/["&<>]/g, (c): string => entityIfyMap[c]);