From 0537140cb63bafbb687c744904a3f25e9e39312e Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 00:23:56 +0530 Subject: [PATCH 01/10] Snippet to convert string to array of words --- snippets/stringToArrayOfWords.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 snippets/stringToArrayOfWords.md diff --git a/snippets/stringToArrayOfWords.md b/snippets/stringToArrayOfWords.md new file mode 100644 index 000000000..5cc742c60 --- /dev/null +++ b/snippets/stringToArrayOfWords.md @@ -0,0 +1,14 @@ +### stringToArrayOfWords + +Converts a given string into an array of words + +First create an array of dirt that you want to remove from the string. Then replace each dirt with an empty string. Use `String.split(' ')` to conver string to array with the space a delimiter. + +```js +const stringToArray = str =>{ + [".", ",", "?", "!", "& ", "(", ")", "[", "]"].map(d => str = str.replace(d, "")); + return str.split(" "); +} + +// stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"] +``` \ No newline at end of file From 5316aed7929301e84260f83cf110e399519fedc6 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 00:28:34 +0530 Subject: [PATCH 02/10] Fixed typo and added another example --- snippets/stringToArrayOfWords.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/snippets/stringToArrayOfWords.md b/snippets/stringToArrayOfWords.md index 5cc742c60..851f93397 100644 --- a/snippets/stringToArrayOfWords.md +++ b/snippets/stringToArrayOfWords.md @@ -2,7 +2,7 @@ Converts a given string into an array of words -First create an array of dirt that you want to remove from the string. Then replace each dirt with an empty string. Use `String.split(' ')` to conver string to array with the space a delimiter. +First create an array of dirt that you want to remove from the string. Then replace each dirt with an empty string. Use `String.split(' ')` to conver the string to an array with the space as a delimiter. ```js const stringToArray = str =>{ @@ -11,4 +11,5 @@ const stringToArray = str =>{ } // stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"] +// stringToArrayOfWords("python, javaScript & coffee") -> ["python", "javaScript", "coffee"] ``` \ No newline at end of file From a480757eef602070e8ea586874e4983fb6990f4a Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 00:30:10 +0530 Subject: [PATCH 03/10] Fixed function name --- snippets/stringToArrayOfWords.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/stringToArrayOfWords.md b/snippets/stringToArrayOfWords.md index 851f93397..c54e2cec4 100644 --- a/snippets/stringToArrayOfWords.md +++ b/snippets/stringToArrayOfWords.md @@ -5,7 +5,7 @@ Converts a given string into an array of words First create an array of dirt that you want to remove from the string. Then replace each dirt with an empty string. Use `String.split(' ')` to conver the string to an array with the space as a delimiter. ```js -const stringToArray = str =>{ +const stringToArrayOfWords = str =>{ [".", ",", "?", "!", "& ", "(", ")", "[", "]"].map(d => str = str.replace(d, "")); return str.split(" "); } From 05d52fe35a9d415949d862d3490cb04214c5e654 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 10:28:54 +0530 Subject: [PATCH 04/10] updated function --- snippets/stringToArrayOfWords.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/stringToArrayOfWords.md b/snippets/stringToArrayOfWords.md index c54e2cec4..d0cea5793 100644 --- a/snippets/stringToArrayOfWords.md +++ b/snippets/stringToArrayOfWords.md @@ -5,9 +5,9 @@ Converts a given string into an array of words First create an array of dirt that you want to remove from the string. Then replace each dirt with an empty string. Use `String.split(' ')` to conver the string to an array with the space as a delimiter. ```js -const stringToArrayOfWords = str =>{ - [".", ",", "?", "!", "& ", "(", ")", "[", "]"].map(d => str = str.replace(d, "")); - return str.split(" "); +const stringToArrayOfWords = str => { + [".", ",", "?", "!", "& ", "(", ")", "[", "]"].map(d => str = str.split(d).join("")); + return str.split(/[\W_]+/); } // stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"] From 41041bb54d45da82b2179caef1a25ade7a8c48e0 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 10:33:07 +0530 Subject: [PATCH 05/10] updated discription --- snippets/stringToArrayOfWords.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/stringToArrayOfWords.md b/snippets/stringToArrayOfWords.md index d0cea5793..2749a6869 100644 --- a/snippets/stringToArrayOfWords.md +++ b/snippets/stringToArrayOfWords.md @@ -2,7 +2,7 @@ Converts a given string into an array of words -First create an array of dirt that you want to remove from the string. Then replace each dirt with an empty string. Use `String.split(' ')` to conver the string to an array with the space as a delimiter. +First create an array of dirt that you want to remove from the string. Then replace each dirt with an empty string. Use `String.split(/[\W_]+/)` to convert the string to an array with the use of regex. ```js const stringToArrayOfWords = str => { From 4280957a102c0196041234786c2cd97189bd4761 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 12:26:10 +0530 Subject: [PATCH 06/10] Updated function with regex --- snippets/stringToArrayOfWords.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/snippets/stringToArrayOfWords.md b/snippets/stringToArrayOfWords.md index 2749a6869..f0c5f149b 100644 --- a/snippets/stringToArrayOfWords.md +++ b/snippets/stringToArrayOfWords.md @@ -2,13 +2,10 @@ Converts a given string into an array of words -First create an array of dirt that you want to remove from the string. Then replace each dirt with an empty string. Use `String.split(/[\W_]+/)` to convert the string to an array with the use of regex. +Use `String.split(/[\W_]+/)` to convert the string to an array with the use of regex. ```js -const stringToArrayOfWords = str => { - [".", ",", "?", "!", "& ", "(", ")", "[", "]"].map(d => str = str.split(d).join("")); - return str.split(/[\W_]+/); -} +const stringToArrayOfWords = (str, pattern = /[\W_]+/) => str.split(pattern).filter(Boolean) // stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"] // stringToArrayOfWords("python, javaScript & coffee") -> ["python", "javaScript", "coffee"] From 251edb535b538532be3793b485bf48f307c450b2 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 12:34:03 +0530 Subject: [PATCH 07/10] updated description --- snippets/stringToArrayOfWords.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/stringToArrayOfWords.md b/snippets/stringToArrayOfWords.md index f0c5f149b..4a7aa7c8a 100644 --- a/snippets/stringToArrayOfWords.md +++ b/snippets/stringToArrayOfWords.md @@ -2,11 +2,11 @@ Converts a given string into an array of words -Use `String.split(/[\W_]+/)` to convert the string to an array with the use of regex. +Use `String.split` to convert the string to an array. ```js const stringToArrayOfWords = (str, pattern = /[\W_]+/) => str.split(pattern).filter(Boolean) // stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"] // stringToArrayOfWords("python, javaScript & coffee") -> ["python", "javaScript", "coffee"] -``` \ No newline at end of file +``` From 5ef9c25163d4c1225f82cf3e58dceb9e2c331b40 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 12:54:08 +0530 Subject: [PATCH 08/10] Updated description --- snippets/stringToArrayOfWords.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/stringToArrayOfWords.md b/snippets/stringToArrayOfWords.md index 4a7aa7c8a..01bd6eb8b 100644 --- a/snippets/stringToArrayOfWords.md +++ b/snippets/stringToArrayOfWords.md @@ -2,7 +2,7 @@ Converts a given string into an array of words -Use `String.split` to convert the string to an array. +Use String.split with a supplied pattern(defaults to non alpha as a regex) to convert to an Array of Strings and then filter to remove any empty strings. ```js const stringToArrayOfWords = (str, pattern = /[\W_]+/) => str.split(pattern).filter(Boolean) From 1fc0e8754044d6db6b5071ddaa5ff325f90e12fe Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Wed, 20 Dec 2017 13:06:12 +0530 Subject: [PATCH 09/10] Updated function for not splitting hyphenated words --- snippets/stringToArrayOfWords.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/stringToArrayOfWords.md b/snippets/stringToArrayOfWords.md index 01bd6eb8b..885216a58 100644 --- a/snippets/stringToArrayOfWords.md +++ b/snippets/stringToArrayOfWords.md @@ -5,7 +5,7 @@ Converts a given string into an array of words Use String.split with a supplied pattern(defaults to non alpha as a regex) to convert to an Array of Strings and then filter to remove any empty strings. ```js -const stringToArrayOfWords = (str, pattern = /[\W_]+/) => str.split(pattern).filter(Boolean) +const stringToArrayOfWords = (str, pattern = /[^a-zA-Z-]+/) => str.split(pattern).filter(Boolean) // stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"] // stringToArrayOfWords("python, javaScript & coffee") -> ["python", "javaScript", "coffee"] From 00c3ff7a72922b0efdce4ff955017f216e8d7db5 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 20 Dec 2017 11:19:07 +0200 Subject: [PATCH 10/10] Update stringToArrayOfWords.md --- snippets/stringToArrayOfWords.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/stringToArrayOfWords.md b/snippets/stringToArrayOfWords.md index 885216a58..0630fe08a 100644 --- a/snippets/stringToArrayOfWords.md +++ b/snippets/stringToArrayOfWords.md @@ -1,12 +1,12 @@ ### stringToArrayOfWords -Converts a given string into an array of words +Converts a given string into an array of words. -Use String.split with a supplied pattern(defaults to non alpha as a regex) to convert to an Array of Strings and then filter to remove any empty strings. +Use `String.split()` with a supplied pattern (defaults to non alpha as a regex) to convert to an array of strings. Use `Array.filter()` to remove any empty strings. +Omit the second argument to use the default regex. ```js -const stringToArrayOfWords = (str, pattern = /[^a-zA-Z-]+/) => str.split(pattern).filter(Boolean) - +const stringToArrayOfWords = (str, pattern = /[^a-zA-Z-]+/) => str.split(pattern).filter(Boolean); // stringToArrayOfWords("I love javaScript!!") -> ["I", "love", "javaScript"] // stringToArrayOfWords("python, javaScript & coffee") -> ["python", "javaScript", "coffee"] ```