From 1afa5b657d16a02286eede3293505b04a644e26b Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 29 Dec 2017 14:16:40 +0200 Subject: [PATCH 1/8] Add escape/unescape string --- snippets/escapeString.md | 14 ++++++++++++++ snippets/unescapeString.md | 14 ++++++++++++++ tag_database | 10 ++++++---- 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 snippets/escapeString.md create mode 100644 snippets/unescapeString.md diff --git a/snippets/escapeString.md b/snippets/escapeString.md new file mode 100644 index 000000000..a1bf2fc7f --- /dev/null +++ b/snippets/escapeString.md @@ -0,0 +1,14 @@ +### escapeString + +Escapes a string for use in HTML. + +Use a chain of `String.replace()` calls combined with regular expressions to replace special characters with the proper symbols. + +```js +const escapeString = str => + str.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(//g, '>'); +``` + +```js +escapeString('Me & you'); // '<a href="#">Me & you</a>' +``` diff --git a/snippets/unescapeString.md b/snippets/unescapeString.md new file mode 100644 index 000000000..5e6e14a01 --- /dev/null +++ b/snippets/unescapeString.md @@ -0,0 +1,14 @@ +### unescapeString + +Unescapes a string from HTML. + +Use a chain of `String.replace()` calls combined with regular expressions to replace special characters with the proper symbols. + +```js +const unescapeString = str => + str.replace(/>/g, '>').replace(/</g, '<').replace(/'/g, '\'').replace(/"/g, '"').replace(/&/g, '&'); +``` + +```js +unescapeString('<a href="#">Me & you</a>'); // 'Me & you' +``` diff --git a/tag_database b/tag_database index 5c3a9153b..e8874cf82 100644 --- a/tag_database +++ b/tag_database @@ -1,6 +1,6 @@ anagrams:string arrayToHtmlList:browser -average:uncategorized +average:math bottomVisible:browser call:adapter capitalize:string @@ -30,6 +30,7 @@ dropElements:array dropRight:array elementIsVisibleInViewport:browser escapeRegExp:string +escapeString:string everyNth:array extendHex:utility factorial:math @@ -76,9 +77,9 @@ JSONToFile:node last:array lcm:math mapObject:array -max:uncategorized +max:math median:math -min:uncategorized +min:math negate:logic nthElement:array objectFromPairs:object @@ -120,7 +121,7 @@ sortCharactersInString:string speechSynthesis:media spreadOver:adapter standardDeviation:math -sum:uncategorized +sum:math symmetricDifference:array tail:array take:array @@ -136,6 +137,7 @@ toOrdinalSuffix:utility toSnakeCase:string truncateString:string truthCheckCollection:object +unescapeString:string union:array UUIDGeneratorBrowser:browser UUIDGeneratorNode:node From 0914af981466edc524d0040089ce4657cbdedd7d Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 30 Dec 2017 00:03:47 +1100 Subject: [PATCH 2/8] Rename to escapeHTML, use a dictionary instead --- snippets/escapeString.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/snippets/escapeString.md b/snippets/escapeString.md index a1bf2fc7f..7ca23af6d 100644 --- a/snippets/escapeString.md +++ b/snippets/escapeString.md @@ -1,14 +1,19 @@ -### escapeString +### escapeHTML Escapes a string for use in HTML. -Use a chain of `String.replace()` calls combined with regular expressions to replace special characters with the proper symbols. +Use `String.replace()` with a regex that matches the characters that need to be escaped, using a callback function to replace each character instance with its associated escaped character using a dictionary (object). ```js -const escapeString = str => - str.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(//g, '>'); +const escapeHTML = str => str.replace(/[&<>'"]/g, tag => ({ + '&': '&', + '<': '<', + '>': '>', + '\'': ''', + '"': '"' + })[tag] || tag); ``` ```js -escapeString('Me & you'); // '<a href="#">Me & you</a>' +escapeHTML('Me & you'); // '<a href="#">Me & you</a>' ``` From edc437636629790dc8cdd46fa1233a0faa828b63 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 30 Dec 2017 00:08:16 +1100 Subject: [PATCH 3/8] Update unescapeString.md --- snippets/unescapeString.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/snippets/unescapeString.md b/snippets/unescapeString.md index 5e6e14a01..8408fc744 100644 --- a/snippets/unescapeString.md +++ b/snippets/unescapeString.md @@ -1,14 +1,17 @@ -### unescapeString +### unescapeHTML -Unescapes a string from HTML. +Unescapes escaped HTML characters. -Use a chain of `String.replace()` calls combined with regular expressions to replace special characters with the proper symbols. +Use `String.replace()` with a regex that matches the characters that need to be escaped, using a callback function to replace each escaped character instance with its associated unescaped character using a dictionary (object). ```js -const unescapeString = str => - str.replace(/>/g, '>').replace(/</g, '<').replace(/'/g, '\'').replace(/"/g, '"').replace(/&/g, '&'); -``` - +const escapeHTML = str => str.replace(/[&<>'"]/g, tag => ({ + '&': '&', + '<': '<', + '>': '>', + ''': '\'', + '"': '"' + })[tag] || tag);``` ```js -unescapeString('<a href="#">Me & you</a>'); // 'Me & you' +unescapeHTML('<a href="#">Me & you</a>'); // 'Me & you' ``` From d49f5366211b50b541b2181d1f98f4f3d52bb812 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 30 Dec 2017 00:08:55 +1100 Subject: [PATCH 4/8] Update unescapeString.md --- snippets/unescapeString.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/unescapeString.md b/snippets/unescapeString.md index 8408fc744..add33a861 100644 --- a/snippets/unescapeString.md +++ b/snippets/unescapeString.md @@ -5,7 +5,7 @@ Unescapes escaped HTML characters. Use `String.replace()` with a regex that matches the characters that need to be escaped, using a callback function to replace each escaped character instance with its associated unescaped character using a dictionary (object). ```js -const escapeHTML = str => str.replace(/[&<>'"]/g, tag => ({ +const unescapeHTML = str => str.replace(/[&<>'"]/g, tag => ({ '&': '&', '<': '<', '>': '>', From c55906cdf817f41ba91f3d3707ee21d0d2b27563 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 30 Dec 2017 00:09:10 +1100 Subject: [PATCH 5/8] Rename unescapeString.md to unescapeHTML.md --- snippets/{unescapeString.md => unescapeHTML.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename snippets/{unescapeString.md => unescapeHTML.md} (100%) diff --git a/snippets/unescapeString.md b/snippets/unescapeHTML.md similarity index 100% rename from snippets/unescapeString.md rename to snippets/unescapeHTML.md From 05a28ddfd769dbb39c546a9aa6cbef1f4c2196bf Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 30 Dec 2017 00:09:21 +1100 Subject: [PATCH 6/8] Rename escapeString.md to escapeHTML.md --- snippets/{escapeString.md => escapeHTML.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename snippets/{escapeString.md => escapeHTML.md} (100%) diff --git a/snippets/escapeString.md b/snippets/escapeHTML.md similarity index 100% rename from snippets/escapeString.md rename to snippets/escapeHTML.md From 96cdd3b7ff1d0621e135d2477874ab4a06925f53 Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 30 Dec 2017 00:09:49 +1100 Subject: [PATCH 7/8] Update tag_database --- tag_database | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tag_database b/tag_database index e8874cf82..95318406d 100644 --- a/tag_database +++ b/tag_database @@ -30,7 +30,7 @@ dropElements:array dropRight:array elementIsVisibleInViewport:browser escapeRegExp:string -escapeString:string +escapeHTML:string everyNth:array extendHex:utility factorial:math @@ -137,7 +137,7 @@ toOrdinalSuffix:utility toSnakeCase:string truncateString:string truthCheckCollection:object -unescapeString:string +unescapeHTML:string union:array UUIDGeneratorBrowser:browser UUIDGeneratorNode:node From ca880a61bf8f7281cc5204002c3b09ae3f13c38c Mon Sep 17 00:00:00 2001 From: atomiks Date: Sat, 30 Dec 2017 00:13:15 +1100 Subject: [PATCH 8/8] Update unescapeHTML.md --- snippets/unescapeHTML.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/unescapeHTML.md b/snippets/unescapeHTML.md index add33a861..3325e1400 100644 --- a/snippets/unescapeHTML.md +++ b/snippets/unescapeHTML.md @@ -5,7 +5,7 @@ Unescapes escaped HTML characters. Use `String.replace()` with a regex that matches the characters that need to be escaped, using a callback function to replace each escaped character instance with its associated unescaped character using a dictionary (object). ```js -const unescapeHTML = str => str.replace(/[&<>'"]/g, tag => ({ +const unescapeHTML = str => str.replace(/&|<|>|'|"/g, tag => ({ '&': '&', '<': '<', '>': '>',