escape.ts 420 B

123456789101112131415161718
  1. function escape(input: string): string {
  2. if (!input || typeof input !== 'string')
  3. return ''
  4. const res = input
  5. .replaceAll('\\', '\\\\')
  6. .replaceAll('\0', '\\0')
  7. .replaceAll('\b', '\\b')
  8. .replaceAll('\f', '\\f')
  9. .replaceAll('\n', '\\n')
  10. .replaceAll('\r', '\\r')
  11. .replaceAll('\t', '\\t')
  12. .replaceAll('\v', '\\v')
  13. .replaceAll('\'', '\\\'')
  14. return res
  15. }
  16. export default escape