From 7d5515c47563348a3dfeef69fa18116973b24ff8 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 d3d2307b2797f5c903067c790567e34bd7058d70 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 5bd000c1a748f53e42a8b475f50a320e4c073305 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 8433f0e445afd1bf6ccb726ebe68dbd47c8c7e38 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 05f5e3c9c921cc3a606720fa17d5a5a1a1bfe8db 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 3f568671dd3aa2d00973e1928e3f48eed01aa3a6 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 3c5d387a21f40877f88250138a720f461967dd9c 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 dcc9b508b327a3608480a24b0e077ea42fa0dcba 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 => ({ '&': '&', '<': '<', '>': '>',