| 
									
										
										
										
											2018-03-07 11:15:11 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * 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, ' ')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 15:44:50 -07:00
										 |  |  | const deEntityIfyMap: Record<string, string> = { | 
					
						
							|  |  |  |   lt: '<', | 
					
						
							|  |  |  |   gt: '>', | 
					
						
							|  |  |  |   quot: '"' | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-03-07 11:15:11 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @param str | 
					
						
							|  |  |  |  * @returns {string} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-08-26 15:44:50 -07:00
										 |  |  | export const deEntityIfy = (str: string): string => | 
					
						
							|  |  |  |   String(str).replace(/&([^&;]+);/g, (entity, sequence): string => { | 
					
						
							|  |  |  |     const character: string | undefined = deEntityIfyMap[sequence]; | 
					
						
							| 
									
										
										
										
											2018-03-07 11:15:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return typeof character === 'string' ? character : entity; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-26 15:44:50 -07:00
										 |  |  | const entityIfyMap: Record<string, string> = { | 
					
						
							|  |  |  |   '"': '"', | 
					
						
							|  |  |  |   '<': '<', | 
					
						
							|  |  |  |   '>': '>', | 
					
						
							|  |  |  |   '&': '&' | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const entityIfy = (str: string): string => String(str).replace(/["&<>]/g, (c): string => entityIfyMap[c]); |