From f26c94edabcce1eb3b4de959cd03c13bc3a86e29 Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Thu, 14 Dec 2017 18:41:38 +0330 Subject: [PATCH 01/29] adding function to clean json objects, deeply. --- snippets/clean-json-objects.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 snippets/clean-json-objects.md diff --git a/snippets/clean-json-objects.md b/snippets/clean-json-objects.md new file mode 100644 index 000000000..46ef745dc --- /dev/null +++ b/snippets/clean-json-objects.md @@ -0,0 +1,20 @@ +### Clean Json objects + +Clean your json object from unwanted keys, deeply. +provide set of `keys` to keep and an indicator for `children` if there is any. + +```js +const cleanObj = (obj, keys = [], childIndicator) => { + Object.keys(obj).forEach(key => { + if (key === childIndicator) { + cleanObj(obj[key], keys, childIndicator) + } else if (!keys.includes(key)) { + delete obj[key] + } + }) +} +/* + dirtyObj = { a: 1, b: 2, children: {a: 1, b :2}} + let cleaned = cleanObj(dirtyObj, [a]) // { a: 1, children : { a: 1}} + */ +``` From 210976364c03912aa668eeb51b2eebda686c5079 Mon Sep 17 00:00:00 2001 From: atomiks Date: Fri, 15 Dec 2017 09:04:08 +1100 Subject: [PATCH 02/29] 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 c8a3a91642c823c9d2ed10ac78749d1ced40e275 Mon Sep 17 00:00:00 2001 From: Huseyin Sekmenoglu Date: Fri, 15 Dec 2017 11:35:57 +0300 Subject: [PATCH 03/29] Create convert-json-date.md --- snippets/convert-json-date.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 snippets/convert-json-date.md diff --git a/snippets/convert-json-date.md b/snippets/convert-json-date.md new file mode 100644 index 000000000..3c0d276dc --- /dev/null +++ b/snippets/convert-json-date.md @@ -0,0 +1,14 @@ +### Convert Json Date + +Convert dates coming from ajax as json to readable format +Example: converts "/Date(1489525200000)/" to "15/2/2017" + + ```js +cconst JsonToDate = arr => + { + const dt = new Date(parseInt(arr.toString().substr(6))); + return dt.getDate() + '/' + dt.getMonth() + '/' + dt.getFullYear(); + }; + // JsonToDate(/Date(1489525200000)/) -> 15/2/2017 + ``` + From cec3db48a67a94ea2e41305325984a409cbf5f8c Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Fri, 15 Dec 2017 13:08:12 +0330 Subject: [PATCH 04/29] resolving some issues about comments --- snippets/clean-json-objects.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/snippets/clean-json-objects.md b/snippets/clean-json-objects.md index 46ef745dc..2835b37fd 100644 --- a/snippets/clean-json-objects.md +++ b/snippets/clean-json-objects.md @@ -14,7 +14,8 @@ const cleanObj = (obj, keys = [], childIndicator) => { }) } /* - dirtyObj = { a: 1, b: 2, children: {a: 1, b :2}} - let cleaned = cleanObj(dirtyObj, [a]) // { a: 1, children : { a: 1}} - */ + dirtyObj = {a: 1, b: 2, children: {a: 1, b: 2}} + cleanObj(dirtyObj, ["a"],"children") + console.log(dirtyObj)// { a: 1, children : { a: 1}} +*/ ``` From fce4b2b0f3c040f8b0d443001d01738b21d8ad67 Mon Sep 17 00:00:00 2001 From: David Wu Date: Fri, 15 Dec 2017 14:34:55 +0100 Subject: [PATCH 05/29] 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 06/29] 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 3c48c3b5d8d19b29c4e5b0e534d1c10beab1e20f Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Fri, 15 Dec 2017 07:39:39 -0800 Subject: [PATCH 07/29] Added compose such that the rightmost function may have any arity. Added spread operator to explanation of pipe. --- snippets/compose.md | 14 ++++++++++++++ snippets/pipe.md | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 snippets/compose.md diff --git a/snippets/compose.md b/snippets/compose.md new file mode 100644 index 000000000..90088ebe2 --- /dev/null +++ b/snippets/compose.md @@ -0,0 +1,14 @@ +### Compose + +Use `Array.reduce()` with the spread operator (`...`) to perform right-to-;eft function composition. +The last (rightmost) function can accept one or more arguments; the remaining functions must be unary. + +```js +const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args))); +/* +const add5 = x => x + 5 +const multiply = (x, y) => x * y +const multiplyAndAdd5 = compose(add5, multiply) +multiplyAndAdd5(5, 2) -> 15 +*/ +``` diff --git a/snippets/pipe.md b/snippets/pipe.md index e5136777b..434b4c0c3 100644 --- a/snippets/pipe.md +++ b/snippets/pipe.md @@ -1,6 +1,6 @@ ### Pipe -Use `Array.reduce()` to perform left-to-right function composition. +Use `Array.reduce()` with the spread operator (`...`) to perform left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary. ```js From fd3ef95ab3ba1f69ae3a8cae42c564ab558292f3 Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Fri, 15 Dec 2017 08:44:13 -0800 Subject: [PATCH 08/29] Modified zip to use spread operator instead of Array.apply --- snippets/array-zip.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/array-zip.md b/snippets/array-zip.md index 6c9a2491b..5c3193605 100644 --- a/snippets/array-zip.md +++ b/snippets/array-zip.md @@ -6,7 +6,7 @@ If lengths of the argument-arrays vary, `undefined` is used where no value could ```js const zip = (...arrays) => { - const maxLength = Math.max.apply(null, arrays.map(a => a.length)); + const maxLength = Math.max(...arrays.map(x => x.length)); return Array.from({length: maxLength}).map((_, i) => { return Array.from({length: arrays.length}, (_, k) => arrays[k][i]); }) From d03271b73386399ef0f17c0d8246e99eab2a198d Mon Sep 17 00:00:00 2001 From: Eduardo Cancino Date: Fri, 15 Dec 2017 11:44:53 -0500 Subject: [PATCH 09/29] 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 fb24d54bf1778af402b75c272c8a4880c50a7452 Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Fri, 15 Dec 2017 20:18:18 +0330 Subject: [PATCH 10/29] change description for clean json object function to explain more about it's inner workings. --- snippets/clean-json-objects.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/snippets/clean-json-objects.md b/snippets/clean-json-objects.md index 2835b37fd..5268e3911 100644 --- a/snippets/clean-json-objects.md +++ b/snippets/clean-json-objects.md @@ -1,7 +1,7 @@ ### Clean Json objects -Clean your json object from unwanted keys, deeply. -provide set of `keys` to keep and an indicator for `children` if there is any. +Use `Object.keys()` method to iter through given json object and deleting keys that are not `include`d in given array. +also if you give it a special key(`childIndicator`) it will search deeply inside it to apply function to inner objects too. ```js const cleanObj = (obj, keys = [], childIndicator) => { @@ -14,8 +14,8 @@ const cleanObj = (obj, keys = [], childIndicator) => { }) } /* - dirtyObj = {a: 1, b: 2, children: {a: 1, b: 2}} - cleanObj(dirtyObj, ["a"],"children") - console.log(dirtyObj)// { a: 1, children : { a: 1}} + testObj = {a: 1, b: 2, children: {a: 1, b: 2}} + cleanObj(testObj, ["a"],"children") + console.log(testObj)// { a: 1, children : { a: 1}} */ ``` From 68a2c3a7e87c4164c03471a1864b94dd579fb799 Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Fri, 15 Dec 2017 20:23:23 +0330 Subject: [PATCH 11/29] refactoring grammatical problems and assigning better name to keys argument. --- snippets/clean-json-objects.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/snippets/clean-json-objects.md b/snippets/clean-json-objects.md index 5268e3911..5af7b6221 100644 --- a/snippets/clean-json-objects.md +++ b/snippets/clean-json-objects.md @@ -1,14 +1,14 @@ ### Clean Json objects -Use `Object.keys()` method to iter through given json object and deleting keys that are not `include`d in given array. +Use `Object.keys()` method to loop over given json object and deleting keys that are not `include`d in given array. also if you give it a special key(`childIndicator`) it will search deeply inside it to apply function to inner objects too. ```js -const cleanObj = (obj, keys = [], childIndicator) => { +const cleanObj = (obj, keysToKeep = [], childIndicator) => { Object.keys(obj).forEach(key => { if (key === childIndicator) { - cleanObj(obj[key], keys, childIndicator) - } else if (!keys.includes(key)) { + cleanObj(obj[key], keysToKeep, childIndicator) + } else if (!keysToKeep.includes(key)) { delete obj[key] } }) From bd3ed7103b693007827efde9ffe4539f8c52c6a0 Mon Sep 17 00:00:00 2001 From: iamsoorena Date: Fri, 15 Dec 2017 20:41:36 +0330 Subject: [PATCH 12/29] 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 13/29] 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 14/29] 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 15/29] 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 16/29] 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 17/29] 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 18/29] 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 19/29] 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 20/29] 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 21/29] 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 22/29] 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 23/29] 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 24/29] 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' ``` From 0b844e4b3abbda87b55398f6b57f63dfd1e567d7 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 13:43:15 +0200 Subject: [PATCH 25/29] Update and rename compose.md to compose-functions.md --- snippets/{compose.md => compose-functions.md} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename snippets/{compose.md => compose-functions.md} (95%) diff --git a/snippets/compose.md b/snippets/compose-functions.md similarity index 95% rename from snippets/compose.md rename to snippets/compose-functions.md index 90088ebe2..718755719 100644 --- a/snippets/compose.md +++ b/snippets/compose-functions.md @@ -1,4 +1,4 @@ -### Compose +### Compose functions Use `Array.reduce()` with the spread operator (`...`) to perform right-to-;eft function composition. The last (rightmost) function can accept one or more arguments; the remaining functions must be unary. From e0ae406574db1cfd4bb67360d3778832a6b48d1e Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 13:56:55 +0200 Subject: [PATCH 26/29] Housekeeping, tag and build --- README.md | 89 ++++++++++++++++--- ... => 3-digit-hexcode-to-6-digit-hexcode.md} | 8 +- snippets/initialize-array-with-range.md | 2 +- snippets/{pipe.md => pipe-functions.md} | 2 +- ...rray.md => read-file-as-array-of-lines.md} | 4 +- snippets/take-every-nth-element.md | 2 +- tag_database | 8 +- 7 files changed, 92 insertions(+), 23 deletions(-) rename snippets/{3DigHexcode-to-6DigHexcode.md => 3-digit-hexcode-to-6-digit-hexcode.md} (73%) rename snippets/{pipe.md => pipe-functions.md} (95%) rename snippets/{read-file-to-array.md => read-file-as-array-of-lines.md} (93%) diff --git a/README.md b/README.md index 76e9af825..b40db071e 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ * [Similarity between arrays](#similarity-between-arrays) * [Sum of array of numbers](#sum-of-array-of-numbers) * [Tail of list](#tail-of-list) +* [Take every nth element](#take-every-nth-element) * [Take right](#take-right) * [Take](#take) * [Unique values of array](#unique-values-of-array) @@ -61,8 +62,9 @@ ### Function * [Chain asynchronous functions](#chain-asynchronous-functions) +* [Compose functions](#compose-functions) * [Curry](#curry) -* [Pipe](#pipe) +* [Pipe functions](#pipe-functions) * [Promisify](#promisify) * [Run promises in series](#run-promises-in-series) * [Sleep](#sleep) @@ -85,6 +87,7 @@ * [Speech synthesis (experimental)](#speech-synthesis-experimental) ### Node +* [Read file as array of lines](#read-file-as-array-of-lines) * [Write JSON to file](#write-json-to-file) ### Object @@ -102,6 +105,7 @@ * [Truncate a string](#truncate-a-string) ### Utility +* [3 digit hexcode to 6 digit hexcode](#3-digit-hexcode-to-6-digit-hexcode) * [Escape regular expression](#escape-regular-expression) * [Get native type of value](#get-native-type-of-value) * [Hexcode to RGB](#hexcode-to-rgb) @@ -128,11 +132,11 @@ ### 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); -// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 2, 3, [4]] +const ArrayConcat = (arr, ...args) => [...arr,...args]; +// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 1, 2, 3, [4]] ``` [⬆ back to top](#table-of-contents) @@ -213,11 +217,11 @@ const union = (a, b) => Array.from(new Set([...a, ...b])); ### 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 ] ``` @@ -231,7 +235,7 @@ If lengths of the argument-arrays vary, `undefined` is used where no value could ```js const zip = (...arrays) => { - const maxLength = Math.max.apply(null, arrays.map(a => a.length)); + const maxLength = Math.max(...arrays.map(x => x.length)); return Array.from({length: maxLength}).map((_, i) => { return Array.from({length: arrays.length}, (_, k) => arrays[k][i]); }) @@ -434,7 +438,7 @@ 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] ``` @@ -551,6 +555,17 @@ const tail = arr => arr.length > 1 ? arr.slice(1) : arr; [⬆ back to top](#table-of-contents) +### Take every nth element + +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); +// everynth([1,2,3,4,5,6], 2) -> [ 1, 3, 5 ] +``` + +[⬆ back to top](#table-of-contents) + ### Take right Use `Array.slice()` to create a slice of the array with `n` elements taken from the end. @@ -707,6 +722,22 @@ chainAsync([ [⬆ back to top](#table-of-contents) +### Compose functions + +Use `Array.reduce()` to perform right-to-left function composition. +The last (rightmost) function can accept one or more arguments; the remaining functions must be unary. + +```js +const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args))); +/* +const add5 = x => x + 5 +const multiply = (x, y) => x * y +const multiplyAndAdd5 = compose(add5, multiply) +multiplyAndAdd5(5, 2) -> 15 +*/ +``` +[⬆ back to top](#table-of-contents) + ### Curry Use recursion. @@ -725,9 +756,9 @@ const curry = (fn, arity = fn.length, ...args) => [⬆ back to top](#table-of-contents) -### Pipe +### Pipe functions -Use `Array.reduce()` to perform left-to-right function composition. +Use `Array.reduce()` with the spread operator (`...`) to perform left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary. ```js @@ -967,6 +998,28 @@ const speak = message => { [⬆ back to top](#table-of-contents) ## Node +### Read file as array of lines + +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'] + */ +``` + +[⬆ back to top](#table-of-contents) + ### Write JSON to file Use `fs.writeFile()`, template literals and `JSON.stringify()` to write a `json` object to a `.json` file. @@ -1114,6 +1167,20 @@ const truncate = (str, num) => [⬆ back to top](#table-of-contents) ## Utility +### 3-digit hexcode to 6-digit hexcode + +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('')); +// convertHex('#03f') -> '#0033ff' +// convertHex('05a') -> '#0055aa' +``` + +[⬆ back to top](#table-of-contents) + ### Escape regular expression Use `replace()` to escape special characters. diff --git a/snippets/3DigHexcode-to-6DigHexcode.md b/snippets/3-digit-hexcode-to-6-digit-hexcode.md similarity index 73% rename from snippets/3DigHexcode-to-6DigHexcode.md rename to snippets/3-digit-hexcode-to-6-digit-hexcode.md index 2e9b9b8fc..a138b34bb 100644 --- a/snippets/3DigHexcode-to-6DigHexcode.md +++ b/snippets/3-digit-hexcode-to-6-digit-hexcode.md @@ -1,10 +1,10 @@ -### 3DigHexcode to 6DigHexcode +### 3-digit hexcode to 6-digit hexcode -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. +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('')) : +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' diff --git a/snippets/initialize-array-with-range.md b/snippets/initialize-array-with-range.md index a601ddbb1..082325699 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.from({ length: end - start }).map((v, i) => i + start) + Array.from({ length: end - start }).map((v, i) => i + start); // initializeArrayRange(5) -> [0,1,2,3,4] ``` diff --git a/snippets/pipe.md b/snippets/pipe-functions.md similarity index 95% rename from snippets/pipe.md rename to snippets/pipe-functions.md index 434b4c0c3..6e34e1063 100644 --- a/snippets/pipe.md +++ b/snippets/pipe-functions.md @@ -1,4 +1,4 @@ -### Pipe +### Pipe functions Use `Array.reduce()` with the spread operator (`...`) to perform left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary. diff --git a/snippets/read-file-to-array.md b/snippets/read-file-as-array-of-lines.md similarity index 93% rename from snippets/read-file-to-array.md rename to snippets/read-file-as-array-of-lines.md index 451e2dd06..c64b61c6f 100644 --- a/snippets/read-file-to-array.md +++ b/snippets/read-file-as-array-of-lines.md @@ -1,4 +1,4 @@ -### Read a file to an Array +### Read file as array of lines Use `readFileSync` function in `fs` node package to create a `Buffer` from a file. convert buffer to string using `toString(encoding)` function. @@ -16,4 +16,4 @@ const readFileToArray = filename => fs.readFileSync(filename).toString('UTF8').s let arr = readFileToArray('test.txt') console.log(arr) // -> ['line1', 'line2', 'line3'] */ -``` \ No newline at end of file +``` diff --git a/snippets/take-every-nth-element.md b/snippets/take-every-nth-element.md index 639fb0e6c..8db496353 100644 --- a/snippets/take-every-nth-element.md +++ b/snippets/take-every-nth-element.md @@ -1,4 +1,4 @@ -### Take every nth element of an array +### Take every nth element Use `Array.filter()` to create a new array that contains every nth element of a given array. diff --git a/tag_database b/tag_database index 34efb5b8c..0e529695e 100644 --- a/tag_database +++ b/tag_database @@ -1,3 +1,4 @@ +3-digit-hexcode-to-6-digit-hexcode:utility anagrams-of-string-(with-duplicates):string array-concatenation:array array-difference:array @@ -17,6 +18,7 @@ check-for-palindrome:string chunk-array:array collatz-algorithm:math compact:array +compose-functions:function count-occurrences-of-a-value-in-array:array current-URL:browser curry:function @@ -62,12 +64,12 @@ object-to-key-value-pairs:object ordinal-suffix-of-number:utility percentile:math pick:array -pipe:function +pipe-functions:function powerset:math promisify:function random-integer-in-range:utility random-number-in-range:utility -read-file-to-array:node +read-file-as-array-of-lines:node redirect-to-URL:browser reverse-a-string:string RGB-to-hexadecimal:utility @@ -84,9 +86,9 @@ standard-deviation:math sum-of-array-of-numbers:array swap-values-of-two-variables:utility tail-of-list:array +take-every-nth-element:array take-right:array take:array -take-every-nth-element:array truncate-a-string:string unique-values-of-array:array URL-parameters:utility From 46ca8b33b770c00285cb6d4cbff33a66ed63be2b Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 14:22:08 +0200 Subject: [PATCH 27/29] Removed swap --- README.md | 12 ------------ snippets/swap-values-of-two-variables.md | 8 -------- tag_database | 1 - 3 files changed, 21 deletions(-) delete mode 100644 snippets/swap-values-of-two-variables.md diff --git a/README.md b/README.md index b40db071e..51eefca5a 100644 --- a/README.md +++ b/README.md @@ -121,7 +121,6 @@ * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) * [RGB to hexadecimal](#rgb-to-hexadecimal) -* [Swap values of two variables](#swap-values-of-two-variables) * [URL parameters](#url-parameters) * [UUID generator](#uuid-generator) * [Validate email](#validate-email) @@ -1367,17 +1366,6 @@ const rgbToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6 [⬆ back to top](#table-of-contents) -### Swap values of two variables - -Use array destructuring to swap values between two variables. - -```js -[varA, varB] = [varB, varA]; -// [x, y] = [y, x] -``` - -[⬆ back to top](#table-of-contents) - ### URL parameters Use `match()` with an appropriate regular expression to get all key-value pairs, `Array.reduce()` to map and combine them into a single object. diff --git a/snippets/swap-values-of-two-variables.md b/snippets/swap-values-of-two-variables.md deleted file mode 100644 index bb2f67150..000000000 --- a/snippets/swap-values-of-two-variables.md +++ /dev/null @@ -1,8 +0,0 @@ -### Swap values of two variables - -Use array destructuring to swap values between two variables. - -```js -[varA, varB] = [varB, varA]; -// [x, y] = [y, x] -``` diff --git a/tag_database b/tag_database index 0e529695e..cbf76124d 100644 --- a/tag_database +++ b/tag_database @@ -84,7 +84,6 @@ sort-characters-in-string-(alphabetical):string speech-synthesis-(experimental):media standard-deviation:math sum-of-array-of-numbers:array -swap-values-of-two-variables:utility tail-of-list:array take-every-nth-element:array take-right:array From 4fc9b6904a6d66afbb4b0bb1fb13f0a5e1a11f9f Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 14:24:50 +0200 Subject: [PATCH 28/29] Tag and build --- README.md | 25 +++++++++++++++++++ ...n-json-objects.md => clean-JSON-object.md} | 6 ++--- tag_database | 1 + 3 files changed, 29 insertions(+), 3 deletions(-) rename snippets/{clean-json-objects.md => clean-JSON-object.md} (85%) diff --git a/README.md b/README.md index 51eefca5a..c15a1a0cb 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ * [Write JSON to file](#write-json-to-file) ### Object +* [Clean JSON object](#clean-json-object) * [Object from key value pairs](#object-from-key-value-pairs) * [Object to key value pairs](#object-to-key-value-pairs) * [Shallow clone object](#shallow-clone-object) @@ -1032,6 +1033,30 @@ const jsonToFile = (obj, filename) => fs.writeFile(`${filename}.json`, JSON.stri [⬆ back to top](#table-of-contents) ## Object +### Clean JSON object + +Use `Object.keys()` method to loop over given json object and deleting keys that are not `include`d in given array. +also if you give it a special key(`childIndicator`) it will search deeply inside it to apply function to inner objects too. + +```js +const cleanObj = (obj, keysToKeep = [], childIndicator) => { + Object.keys(obj).forEach(key => { + if (key === childIndicator) { + cleanObj(obj[key], keysToKeep, childIndicator); + } else if (!keysToKeep.includes(key)) { + delete obj[key]; + } + }) +} +/* + testObj = {a: 1, b: 2, children: {a: 1, b: 2}} + cleanObj(testObj, ["a"],"children") + console.log(testObj)// { a: 1, children : { a: 1}} +*/ +``` + +[⬆ back to top](#table-of-contents) + ### Object from key-value pairs Use `Array.reduce()` to create and combine key-value pairs. diff --git a/snippets/clean-json-objects.md b/snippets/clean-JSON-object.md similarity index 85% rename from snippets/clean-json-objects.md rename to snippets/clean-JSON-object.md index 5af7b6221..241f6ce09 100644 --- a/snippets/clean-json-objects.md +++ b/snippets/clean-JSON-object.md @@ -1,4 +1,4 @@ -### Clean Json objects +### Clean JSON object Use `Object.keys()` method to loop over given json object and deleting keys that are not `include`d in given array. also if you give it a special key(`childIndicator`) it will search deeply inside it to apply function to inner objects too. @@ -7,9 +7,9 @@ also if you give it a special key(`childIndicator`) it will search deeply inside const cleanObj = (obj, keysToKeep = [], childIndicator) => { Object.keys(obj).forEach(key => { if (key === childIndicator) { - cleanObj(obj[key], keysToKeep, childIndicator) + cleanObj(obj[key], keysToKeep, childIndicator); } else if (!keysToKeep.includes(key)) { - delete obj[key] + delete obj[key]; } }) } diff --git a/tag_database b/tag_database index cbf76124d..9f1b18f46 100644 --- a/tag_database +++ b/tag_database @@ -16,6 +16,7 @@ capitalize-first-letter:string chain-asynchronous-functions:function check-for-palindrome:string chunk-array:array +clean-JSON-object:object collatz-algorithm:math compact:array compose-functions:function From 0ebfd8ab39d97a63ca8aab46482944967a418626 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sat, 16 Dec 2017 14:28:58 +0200 Subject: [PATCH 29/29] Update and rename convert-json-date.md to json-to-date.md --- snippets/{convert-json-date.md => json-to-date.md} | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) rename snippets/{convert-json-date.md => json-to-date.md} (64%) diff --git a/snippets/convert-json-date.md b/snippets/json-to-date.md similarity index 64% rename from snippets/convert-json-date.md rename to snippets/json-to-date.md index 3c0d276dc..7e5efcdc7 100644 --- a/snippets/convert-json-date.md +++ b/snippets/json-to-date.md @@ -1,7 +1,5 @@ -### Convert Json Date +### JSON to date -Convert dates coming from ajax as json to readable format -Example: converts "/Date(1489525200000)/" to "15/2/2017" ```js cconst JsonToDate = arr =>