From 5f74a3462b4312fb0be4c2e8d03906158d794548 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Fri, 27 Mar 2020 14:32:07 +0100 Subject: [PATCH] Applying back real-dual-module --- README.md | 14 ++++++++++---- cjs/index.js | 36 +++++++++++++++--------------------- esm/index.js | 34 ++++++++++++++-------------------- index.js | 32 ++++++++++++++------------------ min.js | 2 +- package.json | 9 +++++++-- 6 files changed, 61 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 3dca341..a77ad3e 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,22 @@ A simple module to escape/unescape common problematic entities. +## V3 ESM Only Release + +The version 3 of this module ditches entirely legacy browsers and _nodejs_ with broken loaders, such as `v13.0.0` and `v13.1.0`. + +As the code is basically identical, simply stick with version 2 if you have any issue with this one 👋 + + ### How This package is available in npm so `npm install html-escaper` is all you need to do, using eventually the global flag too. Once the module is present ```js -var html = require('html-escaper'); +import {escape, unescape} from 'html-escaper'; -// two basic methods -html.escape('string'); -html.unescape('escaped string'); +escape('string'); +unescape('escaped string'); ``` diff --git a/cjs/index.js b/cjs/index.js index 164a34c..a934818 100644 --- a/cjs/index.js +++ b/cjs/index.js @@ -21,19 +21,27 @@ * THE SOFTWARE. */ -var replace = ''.replace; +const {replace} = ''; -var ca = /[&<>'"]/g; -var es = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g; +// escape +const es = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g; +const ca = /[&<>'"]/g; -var esca = { +const esca = { '&': '&', '<': '<', '>': '>', "'": ''', '"': '"' }; -var unes = { +const pe = m => esca[m]; + +const escape = es => replace.call(es, ca, pe); +exports.escape = escape; + + +// unescape +const unes = { '&': '&', '&': '&', '<': '<', @@ -45,21 +53,7 @@ var unes = { '"': '"', '"': '"' }; +const cape = m => unes[m]; -function escape(es) { - return replace.call(es, ca, pe); -} -exports.escape = escape; - -function unescape(un) { - return replace.call(un, es, cape); -} +const unescape = un => replace.call(un, es, cape); exports.unescape = unescape; - -function pe(m) { - return esca[m]; -} - -function cape(m) { - return unes[m]; -} diff --git a/esm/index.js b/esm/index.js index e05b93f..92564bf 100644 --- a/esm/index.js +++ b/esm/index.js @@ -20,19 +20,26 @@ * THE SOFTWARE. */ -var replace = ''.replace; +const {replace} = ''; -var ca = /[&<>'"]/g; -var es = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g; +// escape +const es = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g; +const ca = /[&<>'"]/g; -var esca = { +const esca = { '&': '&', '<': '<', '>': '>', "'": ''', '"': '"' }; -var unes = { +const pe = m => esca[m]; + +export const escape = es => replace.call(es, ca, pe); + + +// unescape +const unes = { '&': '&', '&': '&', '<': '<', @@ -44,19 +51,6 @@ var unes = { '"': '"', '"': '"' }; +const cape = m => unes[m]; -export function escape(es) { - return replace.call(es, ca, pe); -}; - -export function unescape(un) { - return replace.call(un, es, cape); -}; - -function pe(m) { - return esca[m]; -} - -function cape(m) { - return unes[m]; -} +export const unescape = un => replace.call(un, es, cape); diff --git a/index.js b/index.js index 6c3f2ab..33ccc6e 100644 --- a/index.js +++ b/index.js @@ -23,19 +23,26 @@ var html = (function (exports) { * THE SOFTWARE. */ - var replace = ''.replace; + const {replace} = ''; - var ca = /[&<>'"]/g; - var es = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g; + // escape + const es = /&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g; + const ca = /[&<>'"]/g; - var esca = { + const esca = { '&': '&', '<': '<', '>': '>', "'": ''', '"': '"' }; - var unes = { + const pe = m => esca[m]; + + const escape = es => replace.call(es, ca, pe); + + + // unescape + const unes = { '&': '&', '&': '&', '<': '<', @@ -47,20 +54,9 @@ var html = (function (exports) { '"': '"', '"': '"' }; + const cape = m => unes[m]; - function escape(es) { - return replace.call(es, ca, pe); - } - function unescape(un) { - return replace.call(un, es, cape); - } - function pe(m) { - return esca[m]; - } - - function cape(m) { - return unes[m]; - } + const unescape = un => replace.call(un, es, cape); exports.escape = escape; exports.unescape = unescape; diff --git a/min.js b/min.js index 54d0c23..50c48fc 100644 --- a/min.js +++ b/min.js @@ -1 +1 @@ -var html=function(t){"use strict";var n="".replace,u=/[&<>'"]/g,r=/&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g,a={"&":"&","<":"<",">":">","'":"'",'"':"""},e={"&":"&","&":"&","<":"<","<":"<",">":">",">":">","'":"'","'":"'",""":'"',""":'"'};function c(t){return a[t]}function o(t){return e[t]}return t.escape=function(t){return n.call(t,u,c)},t.unescape=function(t){return n.call(t,r,o)},t}({}); \ No newline at end of file +var html=function(t){"use strict";const{replace:a}="",l=/&(?:amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g,c=/[&<>'"]/g,e={"&":"&","<":"<",">":">","'":"'",'"':"""},p=t=>e[t],o={"&":"&","&":"&","<":"<","<":"<",">":">",">":">","'":"'","'":"'",""":'"',""":'"'},s=t=>o[t];return t.escape=(t=>a.call(t,c,p)),t.unescape=(t=>a.call(t,l,s)),t}({}); \ No newline at end of file diff --git a/package.json b/package.json index 81d06a9..2ff85f3 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,11 @@ "test": "istanbul cover ./test/index.js" }, "module": "./esm/index.js", + "type": "module", + "exports": { + "import": "./esm/index.js", + "default": "./cjs/index.js" + }, "repository": { "type": "git", "url": "https://github.com/WebReflection/html-escaper.git" @@ -36,7 +41,7 @@ "ascjs": "^3.1.2", "coveralls": "^3.0.11", "istanbul": "^0.4.5", - "rollup": "^2.1.0", - "uglify-js": "^3.8.0" + "rollup": "^2.2.0", + "uglify-es": "^3.3.9" } }