diff --git a/HISTORY.md b/HISTORY.md index f61051e..77984c1 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,3 +1,8 @@ +1.0.4 / 2022-09-16 +================== + + * build: support ESM module system + 1.0.3 / 2015-09-01 ================== diff --git a/README.md b/README.md index 2557666..9a676b8 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,8 @@ The `escapeHtml` function is designed to accept a string input of text and return an escaped value to interpolate into HTML. ```js -var escapeHtml = require('escape-html') +var escapeHtml = require('escape-html') // CommonJS require +// import escapeHtml from 'escape-html' // ESM import // example values var desc = 'I think this is good.' diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..5a7533f --- /dev/null +++ b/index.mjs @@ -0,0 +1,78 @@ +/*! + * escape-html + * Copyright(c) 2012-2013 TJ Holowaychuk + * Copyright(c) 2015 Andreas Lubbe + * Copyright(c) 2015 Tiancheng "Timothy" Gu + * MIT Licensed + */ + +/** + * Module variables. + * @private + */ + +var matchHtmlRegExp = /["'&<>]/; + +/** + * Module exports. + * @public + */ + +var oss = escapeHtml; + +/** + * Escape special characters in the given string of text. + * + * @param {string} string The string to escape for inserting into HTML + * @return {string} + * @public + */ + +function escapeHtml (string) { + var str = '' + string; + var match = matchHtmlRegExp.exec(str); + + if (!match) { + return str + } + + 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 = '"'; + break + case 38: // & + escape = '&'; + break + case 39: // ' + escape = '''; + break + case 60: // < + escape = '<'; + break + case 62: // > + escape = '>'; + break + default: + continue + } + + if (lastIndex !== index) { + html += str.substring(lastIndex, index); + } + + lastIndex = index + 1; + html += escape; + } + + return lastIndex !== index + ? html + str.substring(lastIndex, index) + : html +} + +export { oss as default }; diff --git a/package.json b/package.json index b46dd7e..dd57f56 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,16 @@ "utility" ], "repository": "component/escape-html", + "exports": { + ".": { + "require": "./index.js", + "import": "./index.mjs" + } + }, "devDependencies": { - "benchmark": "2.1.4", + "@rollup/plugin-commonjs": "^22.0.2", "beautify-benchmark": "0.2.4", + "benchmark": "2.1.4", "eslint": "5.12.0", "eslint-config-standard": "12.0.0", "eslint-plugin-import": "2.14.0", @@ -20,7 +27,8 @@ "eslint-plugin-promise": "4.0.1", "eslint-plugin-standard": "4.0.0", "mocha": "5.2.0", - "nyc": "13.1.0" + "nyc": "13.1.0", + "rollup": "^2.79.0" }, "files": [ "HISTORY.md", @@ -32,6 +40,7 @@ "bench": "node benchmark/index.js", "lint": "eslint --plugin markdown --ext js,md .", "test": "mocha test/", - "test-cov": "nyc --reporter=html --reporter=text npm test" + "test-cov": "nyc --reporter=html --reporter=text npm test", + "roll": "rollup --config rollup.config.mjs" } } diff --git a/rollup.config.mjs b/rollup.config.mjs new file mode 100644 index 0000000..1676993 --- /dev/null +++ b/rollup.config.mjs @@ -0,0 +1,11 @@ +import { defineConfig } from "rollup"; +import commonjs from "@rollup/plugin-commonjs"; + +export default defineConfig({ + input: "./index.js", + output: { + file: "index.mjs", + format: "es", + }, + plugins: [commonjs()], +});