index.js 927 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. const htmlEscape = string => string
  3. .replace(/&/g, '&')
  4. .replace(/"/g, '"')
  5. .replace(/'/g, ''')
  6. .replace(/</g, '&lt;')
  7. .replace(/>/g, '&gt;');
  8. const htmlUnescape = htmlString => htmlString
  9. .replace(/&gt;/g, '>')
  10. .replace(/&lt;/g, '<')
  11. .replace(/&#0?39;/g, '\'')
  12. .replace(/&quot;/g, '"')
  13. .replace(/&amp;/g, '&');
  14. exports.htmlEscape = (strings, ...values) => {
  15. if (typeof strings === 'string') {
  16. return htmlEscape(strings);
  17. }
  18. let output = strings[0];
  19. for (const [index, value] of values.entries()) {
  20. output = output + htmlEscape(String(value)) + strings[index + 1];
  21. }
  22. return output;
  23. };
  24. exports.htmlUnescape = (strings, ...values) => {
  25. if (typeof strings === 'string') {
  26. return htmlUnescape(strings);
  27. }
  28. let output = strings[0];
  29. for (const [index, value] of values.entries()) {
  30. output = output + htmlUnescape(String(value)) + strings[index + 1];
  31. }
  32. return output;
  33. };