diff --git a/package.json b/package.json index a41cc38..bba6036 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.28.15", + "version": "0.28.16", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/string.ts b/src/string.ts index 147054a..2ee9969 100644 --- a/src/string.ts +++ b/src/string.ts @@ -27,6 +27,31 @@ export function camelCase(str?: string): string { return pascal.charAt(0).toLowerCase() + pascal.slice(1); } +/** + * Decode HTML escaped characters + * @param html HTML to clean up + * @returns {any} + */ +export function decodeHtml(html: string) { + return html + .replace(/ /g, '\u00A0') + .replace(/"/g, '"') + .replace(/'/g, "'") + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/¢/g, '¢') + .replace(/£/g, '£') + .replace(/¥/g, '¥') + .replace(/€/g, '€') + .replace(/©/g, '©') + .replace(/®/g, '®') + .replace(/™/g, '™') + .replace(/×/g, '×') + .replace(/÷/g, '÷') + .replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec)) + .replace(/&#x([0-9a-fA-F]+);/g, (match, hex) => String.fromCharCode(parseInt(hex, 16))) + .replace(/&/g, '&'); // Always last! +} /** * Convert number of bytes into a human-readable size