From 19b72b2a8f3b9dc8adeb85cca3531728a61bd5c7 Mon Sep 17 00:00:00 2001 From: atomiks Date: Fri, 15 Dec 2017 09:04:08 +1100 Subject: [PATCH 01/17] Create compose-functions.md --- snippets/compose-functions.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 snippets/compose-functions.md diff --git a/snippets/compose-functions.md b/snippets/compose-functions.md new file mode 100644 index 000000000..cd60bb7e0 --- /dev/null +++ b/snippets/compose-functions.md @@ -0,0 +1,15 @@ +### Compose functions + +Use the `...rest` operator to gather all function arguments into an array. Return a function which takes +a single argument and uses `Array.reduceRight()` to return the result of applying each function. + +```js +const compose = (...fns) => n => fns.reduceRight((acc, fn) => fn(acc), n); +/* +const addOne = n => n + 1; +const square = n => n * n; +const double = n => n * 2; +compose(addOne, square, double)(2) -> 17 +equivalent to: addOne(square(double(2))) +*/ +``` From f6d61ade76e53b189b628c5710564f2d514aaf7f Mon Sep 17 00:00:00 2001 From: David Wu Date: Fri, 15 Dec 2017 14:34:55 +0100 Subject: [PATCH 02/17] Create 3DigHexcode-to-6DigHexcode.md --- snippets/3DigHexcode-to-6DigHexcode.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 snippets/3DigHexcode-to-6DigHexcode.md diff --git a/snippets/3DigHexcode-to-6DigHexcode.md b/snippets/3DigHexcode-to-6DigHexcode.md new file mode 100644 index 000000000..d424f41cd --- /dev/null +++ b/snippets/3DigHexcode-to-6DigHexcode.md @@ -0,0 +1,12 @@ +### 3DigHexcode to 6DigHexcode + +Use `Array.map()` to map the array created by `String.split()` and `Array.join()` to join +the mapped array for converting a three-digit RGB notated hexadecimal colorcode to the six-digit form. + +```js +const convertHex = shortHex => + shortHex[0] == '#' ? ('#' + shortHex.slice(1).split('').map(x => x+x).join('')) : + ('#' + shortHex.split('').map(x => x+x).join('')) +// convertHex('#03f') -> '#0033ff' +// convertHex('05a') -> '#0055aa' +``` From b10aa40c1b03171af7edb9e3fd0773aae9bed846 Mon Sep 17 00:00:00 2001 From: Yash Ghelani Date: Fri, 15 Dec 2017 21:03:53 +0530 Subject: [PATCH 03/17] Fix broken link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 776d746ef..5393cf1c8 100644 --- a/README.md +++ b/README.md @@ -967,7 +967,7 @@ const speak = message => { [⬆ back to top](#table-of-contents) ## Node -### Write a JSON to a file +### Write JSON to file Use `fs.writeFile()`, template literals and `JSON.stringify()` to write a `json` object to a `.json` file. From 7b8c7f48edce095b2df43720419b76ad47655863 Mon Sep 17 00:00:00 2001 From: Eduardo Cancino Date: Fri, 15 Dec 2017 11:44:53 -0500 Subject: [PATCH 04/17] Update initialize-array-with-range.md --- snippets/initialize-array-with-range.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/initialize-array-with-range.md b/snippets/initialize-array-with-range.md index def23fb9c..a601ddbb1 100644 --- a/snippets/initialize-array-with-range.md +++ b/snippets/initialize-array-with-range.md @@ -5,6 +5,6 @@ You can omit `start` to use a default value of `0`. ```js const initializeArrayRange = (end, start = 0) => - Array.apply(null, Array(end - start)).map((v, i) => i + start); + Array.from({ length: end - start }).map((v, i) => i + start) // initializeArrayRange(5) -> [0,1,2,3,4] ``` From 1fa3f7d3acdb1465ca6abb42403405335fe22d87 Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Fri, 15 Dec 2017 20:41:36 +0330 Subject: [PATCH 05/17] adding read file to array function --- snippets/read-file-to-array.md | 19 +++++++++++++++++++ tag_database | 1 + 2 files changed, 20 insertions(+) create mode 100644 snippets/read-file-to-array.md diff --git a/snippets/read-file-to-array.md b/snippets/read-file-to-array.md new file mode 100644 index 000000000..451e2dd06 --- /dev/null +++ b/snippets/read-file-to-array.md @@ -0,0 +1,19 @@ +### Read a file to an Array + +Use `readFileSync` function in `fs` node package to create a `Buffer` from a file. +convert buffer to string using `toString(encoding)` function. +creating an array from contents of file by `split`ing file content line by line(each `\n`). + + ```js +const fs = require('fs'); +const readFileToArray = filename => fs.readFileSync(filename).toString('UTF8').split('\n'); +/* + contents of test.txt : + line1 + line2 + line3 + ___________________________ + let arr = readFileToArray('test.txt') + console.log(arr) // -> ['line1', 'line2', 'line3'] + */ +``` \ No newline at end of file diff --git a/tag_database b/tag_database index 3acb709f1..8f05fb386 100644 --- a/tag_database +++ b/tag_database @@ -67,6 +67,7 @@ powerset:math promisify:function random-integer-in-range:utility random-number-in-range:utility +read-file-to-array:node redirect-to-URL:browser reverse-a-string:string RGB-to-hexadecimal:utility From a99d61cda5f8a3f7c1c9e0ebab52f87d92ea0ced Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Fri, 15 Dec 2017 21:24:55 +0330 Subject: [PATCH 06/17] adding function that make a new array containing every nth element of a given array. --- snippets/take-every-nth-element.md | 10 ++++++++++ tag_database | 1 + 2 files changed, 11 insertions(+) create mode 100644 snippets/take-every-nth-element.md diff --git a/snippets/take-every-nth-element.md b/snippets/take-every-nth-element.md new file mode 100644 index 000000000..26bffd8de --- /dev/null +++ b/snippets/take-every-nth-element.md @@ -0,0 +1,10 @@ +### Take every nth element of an array + +Use `Array.filter()` to create a new array that contains every nth element of a given array. + +```js +const everynth = (arr, nth) => arr.filter((e, i) => i % nth === 0) +/* + console.log(everynth([1,2,3,4,5,6], 2)) // -> [ 1, 3, 5 ] +*/ +``` diff --git a/tag_database b/tag_database index 3acb709f1..6199d7b59 100644 --- a/tag_database +++ b/tag_database @@ -85,6 +85,7 @@ swap-values-of-two-variables:utility tail-of-list:array take-right:array take:array +take-every-nth-element:array truncate-a-string:string unique-values-of-array:array URL-parameters:utility From 6aada8d95358b79b1dd3a931178a57fd89071e4e Mon Sep 17 00:00:00 2001 From: Gabriel Bonner <17228243+gabrielbonner@users.noreply.github.com> Date: Fri, 15 Dec 2017 10:46:46 -0800 Subject: [PATCH 07/17] Fix array.concat snippet Correct return value --- snippets/array-concatenation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/array-concatenation.md b/snippets/array-concatenation.md index 40bf43c29..a77b50327 100644 --- a/snippets/array-concatenation.md +++ b/snippets/array-concatenation.md @@ -4,5 +4,5 @@ Use `Array.concat()` to concatenate an array with any additional arrays and/or v ```js const ArrayConcat = (arr, ...args) => [].concat(arr, ...args); -// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 2, 3, [4]] +// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 1, 2, 3, [4]] ``` From 54ae81514986f2b7aa61f9bfc6ea3b8e2afce9b2 Mon Sep 17 00:00:00 2001 From: Anx Date: Sat, 16 Dec 2017 15:02:07 +0800 Subject: [PATCH 08/17] Update array-concatenation.md --- snippets/array-concatenation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/array-concatenation.md b/snippets/array-concatenation.md index 40bf43c29..a77b50327 100644 --- a/snippets/array-concatenation.md +++ b/snippets/array-concatenation.md @@ -4,5 +4,5 @@ Use `Array.concat()` to concatenate an array with any additional arrays and/or v ```js const ArrayConcat = (arr, ...args) => [].concat(arr, ...args); -// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 2, 3, [4]] +// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 1, 2, 3, [4]] ``` From 49ae8032eafb269e45aedc4270b4a71e4c00f83e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien?= Date: Sat, 16 Dec 2017 08:21:49 +0100 Subject: [PATCH 09/17] Fix `Write json to file` anchor --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 776d746ef..57f54e56d 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ * [Speech synthesis (experimental)](#speech-synthesis-experimental) ### Node -* [Write json to file](#write-json-to-file) +* [Write json to file](#write-a-json-to-a-file) ### Object * [Object from key value pairs](#object-from-key-value-pairs) From 64425dd017176a733e1c0f616143c652e5a5878d Mon Sep 17 00:00:00 2001 From: Yahya SayadArbabi Date: Sat, 16 Dec 2017 10:53:43 +0330 Subject: [PATCH 10/17] Fix Write json to file link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 776d746ef..57f54e56d 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ * [Speech synthesis (experimental)](#speech-synthesis-experimental) ### Node -* [Write json to file](#write-json-to-file) +* [Write json to file](#write-a-json-to-a-file) ### Object * [Object from key value pairs](#object-from-key-value-pairs) From ee73677158997e8737825a4a6a3ae7ecb9c9e22a Mon Sep 17 00:00:00 2001 From: Soorena Date: Sat, 16 Dec 2017 13:30:55 +0330 Subject: [PATCH 11/17] updating array-without.md for more terse implementation and resolving a bug in the comment a bug in comments(forgotten parentheses) resolved. and `Array.indexOf() === -1 ` changed to `Array.includes()` --- snippets/array-without.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/array-without.md b/snippets/array-without.md index dd657fe2a..b5b41aee8 100644 --- a/snippets/array-without.md +++ b/snippets/array-without.md @@ -1,9 +1,9 @@ ### Array without -Use `Array.filter()` to create an array excluding all given values. +Use `Array.filter()` to create an array excluding(using `!Array.includes()`) all given values. ```js -const without = (arr, ...args) => arr.filter(v => args.indexOf(v) === -1); -// without[2, 1, 2, 3], 1, 2) -> [3] +const without = (arr, ...args) => arr.filter(v => !args.includes(v)); +// without([2, 1, 2, 3], 1, 2) -> [3] // without([2, 1, 2, 3, 4, 5, 5, 5, 3, 2, 7, 7], 3, 1, 5, 2) -> [ 4, 7, 7 ] ``` From 5a610a03aeefbce663e92ee3d3ac5ca6896fa889 Mon Sep 17 00:00:00 2001 From: Soorena Date: Sat, 16 Dec 2017 13:41:55 +0330 Subject: [PATCH 12/17] updating array conactenation example to use ES6 --- snippets/array-concatenation.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/array-concatenation.md b/snippets/array-concatenation.md index 40bf43c29..692f29ce1 100644 --- a/snippets/array-concatenation.md +++ b/snippets/array-concatenation.md @@ -1,8 +1,8 @@ ### Array concatenation -Use `Array.concat()` to concatenate an array with any additional arrays and/or values, specified in `args`. +Use Array spread operators to concatenate an array with any additional arrays and/or values, specified in `args`. ```js -const ArrayConcat = (arr, ...args) => [].concat(arr, ...args); +const ArrayConcat = (arr, ...args) => [...arr,...args]; // ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 2, 3, [4]] ``` From 4f0b86c8bca9534bd6688e69fc1848c6bb8734fb Mon Sep 17 00:00:00 2001 From: Naman Sachdeva Date: Sat, 16 Dec 2017 16:04:26 +0530 Subject: [PATCH 13/17] Rename write-json-to-file.md to write-a-json-to-a-file.md --- snippets/{write-json-to-file.md => write-a-json-to-a-file.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename snippets/{write-json-to-file.md => write-a-json-to-a-file.md} (100%) diff --git a/snippets/write-json-to-file.md b/snippets/write-a-json-to-a-file.md similarity index 100% rename from snippets/write-json-to-file.md rename to snippets/write-a-json-to-a-file.md From ad155b8831b97c6b8a24206a1d23c97b91820c4d Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 13:03:46 +0200 Subject: [PATCH 14/17] Build --- README.md | 2 +- snippets/{write-a-json-to-a-file.md => write-json-to-file.md} | 2 +- tag_database | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename snippets/{write-a-json-to-a-file.md => write-json-to-file.md} (92%) diff --git a/README.md b/README.md index 1091edc7c..76e9af825 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ * [Speech synthesis (experimental)](#speech-synthesis-experimental) ### Node -* [Write json to file](#write-a-json-to-a-file) +* [Write JSON to file](#write-json-to-file) ### Object * [Object from key value pairs](#object-from-key-value-pairs) diff --git a/snippets/write-a-json-to-a-file.md b/snippets/write-json-to-file.md similarity index 92% rename from snippets/write-a-json-to-a-file.md rename to snippets/write-json-to-file.md index 17de5a44e..587e5f0f1 100644 --- a/snippets/write-a-json-to-a-file.md +++ b/snippets/write-json-to-file.md @@ -1,4 +1,4 @@ -### Write a JSON to a file +### Write JSON to file Use `fs.writeFile()`, template literals and `JSON.stringify()` to write a `json` object to a `.json` file. diff --git a/tag_database b/tag_database index 3acb709f1..bf8f8003b 100644 --- a/tag_database +++ b/tag_database @@ -92,4 +92,4 @@ UUID-generator:utility validate-email:utility validate-number:utility value-or-default:utility -write-json-to-file:node +write-JSON-to-file:node From 7a10db890b5f38353bcc21be2d2e782125b1ad78 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 13:33:43 +0200 Subject: [PATCH 15/17] Update array-concatenation.md --- snippets/array-concatenation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/array-concatenation.md b/snippets/array-concatenation.md index 692f29ce1..31e915bfc 100644 --- a/snippets/array-concatenation.md +++ b/snippets/array-concatenation.md @@ -1,6 +1,6 @@ ### Array concatenation -Use Array spread operators to concatenate an array with any additional arrays and/or values, specified in `args`. +Use Array spread operators (`...`) to concatenate an array with any additional arrays and/or values, specified in `args`. ```js const ArrayConcat = (arr, ...args) => [...arr,...args]; From a0d50c6648d275582c5c6aaf8bbdd49ac79350b0 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 13:35:24 +0200 Subject: [PATCH 16/17] Update take-every-nth-element.md --- snippets/take-every-nth-element.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/snippets/take-every-nth-element.md b/snippets/take-every-nth-element.md index 26bffd8de..639fb0e6c 100644 --- a/snippets/take-every-nth-element.md +++ b/snippets/take-every-nth-element.md @@ -3,8 +3,6 @@ Use `Array.filter()` to create a new array that contains every nth element of a given array. ```js -const everynth = (arr, nth) => arr.filter((e, i) => i % nth === 0) -/* - console.log(everynth([1,2,3,4,5,6], 2)) // -> [ 1, 3, 5 ] -*/ +const everynth = (arr, nth) => arr.filter((e, i) => i % nth === 0); +// everynth([1,2,3,4,5,6], 2) -> [ 1, 3, 5 ] ``` From 750dec5e45211ebf4004f810095751027200be1c Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 13:40:52 +0200 Subject: [PATCH 17/17] Update 3DigHexcode-to-6DigHexcode.md --- snippets/3DigHexcode-to-6DigHexcode.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/snippets/3DigHexcode-to-6DigHexcode.md b/snippets/3DigHexcode-to-6DigHexcode.md index d424f41cd..2e9b9b8fc 100644 --- a/snippets/3DigHexcode-to-6DigHexcode.md +++ b/snippets/3DigHexcode-to-6DigHexcode.md @@ -1,12 +1,11 @@ ### 3DigHexcode to 6DigHexcode -Use `Array.map()` to map the array created by `String.split()` and `Array.join()` to join -the mapped array for converting a three-digit RGB notated hexadecimal colorcode to the six-digit form. +Use `Array.map()`, `split()` and `Array.join()` to join the mapped array for converting a three-digit RGB notated hexadecimal colorcode to the six-digit form. ```js const convertHex = shortHex => shortHex[0] == '#' ? ('#' + shortHex.slice(1).split('').map(x => x+x).join('')) : - ('#' + shortHex.split('').map(x => x+x).join('')) + ('#' + shortHex.split('').map(x => x+x).join('')); // convertHex('#03f') -> '#0033ff' // convertHex('05a') -> '#0055aa' ```