@web3r/flowerkit
Version:
Tree-shakable JavaScript and TypeScript utility library for frontend/browser apps: DOM, events, arrays, objects, strings, date, JSON, and network helpers (ESM/CJS, SSR-friendly).
1 lines • 1.52 kB
Source Map (JSON)
{"version":3,"file":"index.cjs","sources":[""],"sourcesContent":["export type TGetStrEscapedArgs = Parameters<typeof getStrEscaped>;\r\n\r\nexport type TGetStrEscapedReturn = ReturnType<typeof getStrEscaped>;\r\n\r\n/**\r\n * Escapes special HTML characters to their corresponding entities.\r\n * Escaped: `& < > \" '` \r\n * @param {string} str Source string\r\n * @returns {string} Escaped string safe for HTML text context\r\n * @throws {TypeError} If str is not a string\r\n * @example\r\n * getStrEscaped('<b>Hello & \"world\"</b>');\r\n * // => \"<b>Hello & "world"</b>\"\r\n */\r\nexport const getStrEscaped = (str: string): string => {\r\n\r\n if (typeof str !== \"string\") {\r\n throw new TypeError(\"getStrEscaped: str must be a string\");\r\n }\r\n\r\n const symbols: Record<string, string> = {\r\n \"&\": \"&\",\r\n \"<\": \"<\",\r\n \">\": \">\",\r\n \"\\\"\": \""\",\r\n \"'\": \"'\",\r\n };\r\n\r\n const regExp = /[&<>\"']/g;\r\n\r\n const getEscaped = (char: \"&\" | \"<\" | \">\" | \"\\\"\" | \"'\"): string => symbols[char];\r\n\r\n return str.replace(regExp, getEscaped as (m: string) => string);\r\n};\r\n"],"names":["getStrEscaped","str","TypeError","symbols","regExp","getEscaped","char","replace"],"mappings":";;;;;;;;;;GAcO,MAAMA,cAAiBC,MAE5B,UAAWA,MAAQ,SACjB,MAAM,IAAIC,UAAU,uCAGtB,MAAMC,QAAkC,CACtC,IAAK,QACL,IAAK,OACL,IAAK,OACL,IAAM,SACN,IAAK,SAGP,MAAMC,OAAS,WAEf,MAAMC,WAAcC,MAA+CH,QAAQG,MAE3E,OAAOL,IAAIM,QAAQH,OAAQC"}