escape-html icon indicating copy to clipboard operation
escape-html copied to clipboard

ESM support

Open aderchox opened this issue 3 years ago • 1 comments

Hi, I've added ESM support to this package. Things done in this commit: - Converts CommonJS module.exports to ESM export using a Rollup plugin - Adds an ESM import to the example in README.md - Adds the change in HISTORY.md - Adds "exports" key in package.json to detect proper export automatically I don't know Travis CI though, that should probably be updated too so that index.mjs will not be ignored when installing the package using NPM. Thank you.

aderchox avatar Sep 16 '22 12:09 aderchox

I have confirmed that the new file only contains gluing and formatting changes (if you add semicolons, the differences go away). This means that the functionality is the same.

diff
@@ -6,19 +6,21 @@
  * MIT Licensed
  */
 
+'use strict'
+
 /**
  * Module variables.
  * @private
  */
 
-var matchHtmlRegExp = /["'&<>]/;
+var matchHtmlRegExp = /["'&<>]/
 
 /**
  * Module exports.
  * @public
  */
 
-var oss = escapeHtml;
+module.exports = escapeHtml
 
 /**
  * Escape special characters in the given string of text.
@@ -29,50 +31,48 @@ var oss = escapeHtml;
  */
 
 function escapeHtml (string) {
-  var str = '' + string;
-  var match = matchHtmlRegExp.exec(str);
+  var str = '' + string
+  var match = matchHtmlRegExp.exec(str)
 
   if (!match) {
     return str
   }
 
-  var escape;
-  var html = '';
-  var index = 0;
-  var lastIndex = 0;
+  var escape
+  var html = ''
+  var index = 0
+  var lastIndex = 0
 
   for (index = match.index; index < str.length; index++) {
     switch (str.charCodeAt(index)) {
       case 34: // "
-        escape = '&quot;';
+        escape = '&quot;'
         break
       case 38: // &
-        escape = '&amp;';
+        escape = '&amp;'
         break
       case 39: // '
-        escape = '&#39;';
+        escape = '&#39;'
         break
       case 60: // <
-        escape = '&lt;';
+        escape = '&lt;'
         break
       case 62: // >
-        escape = '&gt;';
+        escape = '&gt;'
         break
       default:
         continue
     }
 
     if (lastIndex !== index) {
-      html += str.substring(lastIndex, index);
+      html += str.substring(lastIndex, index)
     }
 
-    lastIndex = index + 1;
-    html += escape;
+    lastIndex = index + 1
+    html += escape
   }
 
   return lastIndex !== index
     ? html + str.substring(lastIndex, index)
     : html
-}
-
-export { oss as default };
\ No newline at end of file
+}
\ No newline at end of file

WofWca avatar Oct 10 '25 13:10 WofWca