From 210976364c03912aa668eeb51b2eebda686c5079 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 fce4b2b0f3c040f8b0d443001d01738b21d8ad67 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 bbd04774f03754c9ad81b70b5e791403ca8a9266 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 d03271b73386399ef0f17c0d8246e99eab2a198d 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 bd3ed7103b693007827efde9ffe4539f8c52c6a0 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 1f2edeac153d807ac60fbd5a70c410acabb25767 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 2e37a71e8bcae4966fc857ae213650640288b49b 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 df840befc537bf56489d86f103d1bcd7a4e48f00 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 2c8667f347bb2e5c76c82caefe5617252d391ae0 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 73016da6c7ef33674c3b9db0ebb322d498fc578d 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 3e8d595511133797861fa6cd8240a89dd648c241 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 1b2ae33831ac436aa1cb65e3354a5affb5d818f3 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 2e13210c8207915afa08e1730dd7567b33de07f5 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 c3fb3a65a143ebf229e93c3078ad5b24d8a8659f 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 055646fdb61d30a1f72f14e698eeab0b9f2b0357 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 a7f9e284a38864cc4df8641ca8f5f4d2dff2455c 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 69e8f840753b54184f735ca3f379765bf9d1ad63 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' ```