From 556ef539fd8601c818a334fbce9948a89a18ac6d Mon Sep 17 00:00:00 2001 From: Farhad Date: Wed, 13 Dec 2017 09:15:41 +0330 Subject: [PATCH 01/10] Add bottomVisible --- README.md | 18 ++++++++++++++++++ snippets/bottom-visible.md | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 snippets/bottom-visible.md diff --git a/README.md b/README.md index 87b66d7ae..0341f5fa4 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ * [Anagrams of string (with duplicates)](#anagrams-of-string-with-duplicates) * [Average of array of numbers](#average-of-array-of-numbers) +* [Bottom visible](#bottom-visible) * [Capitalize first letter of every word](#capitalize-first-letter-of-every-word) * [Capitalize first letter](#capitalize-first-letter) * [Check for palindrome](#check-for-palindrome) @@ -85,6 +86,23 @@ const average = arr => // average([1,2,3]) -> 2 ``` +### Bottom visible + +Returns `true` if bottom of the page is visible. It adds `scrollY` to +the height of the visible portion of the page (`clientHeight`) and +compares it to `pageHeight` to see if bottom of the page is visible. + +```js +const bottomVisible = () => { + const scrollY = window.scrollY; + const visibleHeight = document.documentElement.clientHeight; + const pageHeight = document.documentElement.scrollHeight; + const bottomOfPage = visibleHeight + scrollY >= pageHeight; + + return bottomOfPage || pageHeight < visibleHeight; +} +``` + ### Capitalize first letter of every word Use `replace()` to match the first character of each word and `toUpperCase()` to capitalize it. diff --git a/snippets/bottom-visible.md b/snippets/bottom-visible.md new file mode 100644 index 000000000..403a157f8 --- /dev/null +++ b/snippets/bottom-visible.md @@ -0,0 +1,16 @@ +### Bottom visible + +Returns `true` if bottom of the page is visible. It adds `scrollY` to +the height of the visible portion of the page (`clientHeight`) and +compares it to `pageHeight` to see if bottom of the page is visible. + +```js +const bottomVisible = () => { + const scrollY = window.scrollY; + const visibleHeight = document.documentElement.clientHeight; + const pageHeight = document.documentElement.scrollHeight; + const bottomOfPage = visibleHeight + scrollY >= pageHeight; + + return bottomOfPage || pageHeight < visibleHeight; +} +``` From fd4597560d52fe7654747d705b3364da08c7c4de Mon Sep 17 00:00:00 2001 From: Daniel Ramos Date: Wed, 13 Dec 2017 10:27:43 +0000 Subject: [PATCH 02/10] Create promisify.md --- README.md | 19 +++++++++++++++++++ snippets/promisify.md | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 snippets/promisify.md diff --git a/README.md b/README.md index 7308593a0..557d44232 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ * [Object from key value pairs](#object-from-key-value-pairs) * [Pipe](#pipe) * [Powerset](#powerset) +* [Promisify](#promisify) * [Random integer in range](#random-integer-in-range) * [Random number in range](#random-number-in-range) * [Randomize order of array](#randomize-order-of-array) @@ -416,6 +417,24 @@ const powerset = arr => // powerset([1,2]) -> [[], [1], [2], [2,1]] ``` +### Promisify + +Creates a promise version of the given callback-style function. In Node 8+, you +can use [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original) + +```js +const promisify = func => + (...args) => + new Promise((resolve, reject) => + func(...args, (err, result) => + err + ? reject(err) + : resolve(result)) + ) +// const stat = promisify(fs.stat) +// When called, stat returns a promise +``` + ### Random integer in range Use `Math.random()` to generate a random number and map it to the desired range, using `Math.floor()` to make it an integer. diff --git a/snippets/promisify.md b/snippets/promisify.md new file mode 100644 index 000000000..b2007a5ab --- /dev/null +++ b/snippets/promisify.md @@ -0,0 +1,17 @@ +### Promisify + +Creates a promise version of the given callback-style function. In Node 8+, you +can use [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original) + +```js +const promisify = func => + (...args) => + new Promise((resolve, reject) => + func(...args, (err, result) => + err + ? reject(err) + : resolve(result)) + ) +// const stat = promisify(fs.stat) +// When called, stat returns a promise +``` From b74ba8bbfc700b2c4ffb1866a63b7bc0df78d8ee Mon Sep 17 00:00:00 2001 From: Daniel Ramos Date: Wed, 13 Dec 2017 10:29:34 +0000 Subject: [PATCH 03/10] Fixed typo in promisify.md --- README.md | 2 +- snippets/promisify.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 557d44232..ea30128d0 100644 --- a/README.md +++ b/README.md @@ -419,7 +419,7 @@ const powerset = arr => ### Promisify -Creates a promise version of the given callback-style function. In Node 8+, you +Creates a promisified version of the given callback-style function. In Node 8+, you can use [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original) ```js diff --git a/snippets/promisify.md b/snippets/promisify.md index b2007a5ab..efecd794a 100644 --- a/snippets/promisify.md +++ b/snippets/promisify.md @@ -1,6 +1,6 @@ ### Promisify -Creates a promise version of the given callback-style function. In Node 8+, you +Creates a promisified version of the given callback-style function. In Node 8+, you can use [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original) ```js From f8ee54604bcd0bf0f7b54a388a803cc2cfb8c695 Mon Sep 17 00:00:00 2001 From: Daniel Ramos Date: Wed, 13 Dec 2017 10:33:39 +0000 Subject: [PATCH 04/10] Improving example in promisify.md --- README.md | 2 +- snippets/promisify.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ea30128d0..01a7f9c30 100644 --- a/README.md +++ b/README.md @@ -432,7 +432,7 @@ const promisify = func => : resolve(result)) ) // const stat = promisify(fs.stat) -// When called, stat returns a promise +// stat('foo.txt') -> Promise resolves if `foo.txt` exists, otherwise rejects ``` ### Random integer in range diff --git a/snippets/promisify.md b/snippets/promisify.md index efecd794a..b1c34a20c 100644 --- a/snippets/promisify.md +++ b/snippets/promisify.md @@ -13,5 +13,5 @@ const promisify = func => : resolve(result)) ) // const stat = promisify(fs.stat) -// When called, stat returns a promise +// stat('foo.txt') -> Promise resolves if `foo.txt` exists, otherwise rejects ``` From 1a0d41d2b87d93537d9d1ed7b0d7342e68de89c0 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 13 Dec 2017 12:39:24 +0200 Subject: [PATCH 05/10] Update promisify.md --- snippets/promisify.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/snippets/promisify.md b/snippets/promisify.md index b1c34a20c..d6b437589 100644 --- a/snippets/promisify.md +++ b/snippets/promisify.md @@ -8,9 +8,7 @@ const promisify = func => (...args) => new Promise((resolve, reject) => func(...args, (err, result) => - err - ? reject(err) - : resolve(result)) + err ? reject(err) : resolve(result)); ) // const stat = promisify(fs.stat) // stat('foo.txt') -> Promise resolves if `foo.txt` exists, otherwise rejects From 5522a4c207ca07cc858405b4ca5c657e08c4fead Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 13 Dec 2017 14:09:25 +0200 Subject: [PATCH 06/10] Updated URL parameters --- README.md | 7 ++++--- snippets/URL-parameters.md | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 7f583154b..c6bcb3855 100644 --- a/README.md +++ b/README.md @@ -615,13 +615,14 @@ const unique = arr => [...new Set(arr)]; ### URL parameters -Use `match()` with an appropriate regular expression to get all key-value pairs, `map()` them appropriately. -Combine all key-value pairs into a single object using `Object.assign()` and the spread operator (`...`). +Use `match()` with an appropriate regular expression to get all key-value pairs, `Array.reduce()` to map and combine them into a single object. Pass `location.search` as the argument to apply to the current `url`. ```js const getUrlParameters = url => - Object.assign(...url.match(/([^?=&]+)(=([^&]*))?/g).map(m => {[f,v] = m.split('='); return {[f]:v}})); + url.match(/([^?=&]+)(=([^&]*))?/g).reduce( + (a,v) => (a[v.slice(0,v.indexOf('='))] = v.slice(v.indexOf('=')), a), {} + ); // getUrlParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'} ``` diff --git a/snippets/URL-parameters.md b/snippets/URL-parameters.md index 4620513dc..42a43cc22 100644 --- a/snippets/URL-parameters.md +++ b/snippets/URL-parameters.md @@ -1,11 +1,12 @@ ### URL parameters -Use `match()` with an appropriate regular expression to get all key-value pairs, `map()` them appropriately. -Combine all key-value pairs into a single object using `Object.assign()` and the spread operator (`...`). +Use `match()` with an appropriate regular expression to get all key-value pairs, `Array.reduce()` to map and combine them into a single object. Pass `location.search` as the argument to apply to the current `url`. ```js const getUrlParameters = url => - Object.assign(...url.match(/([^?=&]+)(=([^&]*))?/g).map(m => {[f,v] = m.split('='); return {[f]:v}})); + url.match(/([^?=&]+)(=([^&]*))?/g).reduce( + (a,v) => (a[v.slice(0,v.indexOf('='))] = v.slice(v.indexOf('=')), a), {} + ); // getUrlParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'} ``` From bab7996b3b72178ccdea68b0bc95d14614e50858 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 13 Dec 2017 14:14:04 +0200 Subject: [PATCH 07/10] Update bottom-visible.md --- snippets/bottom-visible.md | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/snippets/bottom-visible.md b/snippets/bottom-visible.md index 403a157f8..9045c6bff 100644 --- a/snippets/bottom-visible.md +++ b/snippets/bottom-visible.md @@ -1,16 +1,9 @@ ### Bottom visible -Returns `true` if bottom of the page is visible. It adds `scrollY` to -the height of the visible portion of the page (`clientHeight`) and -compares it to `pageHeight` to see if bottom of the page is visible. +Use `scrollY`, `scrollHeight` and `clientHeight` to determine if the bottom of the page is visible. ```js -const bottomVisible = () => { - const scrollY = window.scrollY; - const visibleHeight = document.documentElement.clientHeight; - const pageHeight = document.documentElement.scrollHeight; - const bottomOfPage = visibleHeight + scrollY >= pageHeight; - - return bottomOfPage || pageHeight < visibleHeight; -} +const bottomVisible = _ => + document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight || document.documentElement.clientHeight; +// bottomVisible() -> true ``` From fceabcc75f55d1d7bdfaa3ce6c50a4e29d621d13 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 13 Dec 2017 14:15:07 +0200 Subject: [PATCH 08/10] Build README --- README.md | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5c7bbdd49..d828616ad 100644 --- a/README.md +++ b/README.md @@ -95,19 +95,12 @@ const average = arr => ### Bottom visible -Returns `true` if bottom of the page is visible. It adds `scrollY` to -the height of the visible portion of the page (`clientHeight`) and -compares it to `pageHeight` to see if bottom of the page is visible. +Use `scrollY`, `scrollHeight` and `clientHeight` to determine if the bottom of the page is visible. ```js -const bottomVisible = () => { - const scrollY = window.scrollY; - const visibleHeight = document.documentElement.clientHeight; - const pageHeight = document.documentElement.scrollHeight; - const bottomOfPage = visibleHeight + scrollY >= pageHeight; - - return bottomOfPage || pageHeight < visibleHeight; -} +const bottomVisible = _ => + document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight || document.documentElement.clientHeight; +// bottomVisible() -> true ``` ### Capitalize first letter of every word From babb858ce2ce41cb876c802d0afdfec0b0a4b4d1 Mon Sep 17 00:00:00 2001 From: Daniel Ramos Date: Wed, 13 Dec 2017 12:15:39 +0000 Subject: [PATCH 09/10] Moving semicolon, changing example in promisify.md --- snippets/promisify.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/snippets/promisify.md b/snippets/promisify.md index d6b437589..f9516aeed 100644 --- a/snippets/promisify.md +++ b/snippets/promisify.md @@ -1,15 +1,14 @@ ### Promisify -Creates a promisified version of the given callback-style function. In Node 8+, you -can use [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original) +Use currying to return a function returning a Promise that calls the original function. Use the rest operator to pass in all the parameters. In Node 8+, you can use [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original) ```js const promisify = func => (...args) => new Promise((resolve, reject) => func(...args, (err, result) => - err ? reject(err) : resolve(result)); - ) -// const stat = promisify(fs.stat) -// stat('foo.txt') -> Promise resolves if `foo.txt` exists, otherwise rejects + err ? reject(err) : resolve(result)) + ); +// const delay = promisify((d, cb) => setTimeout(cb, d)) +// delay(2000).then(() => console.log('Hi!')) -> Promise resolves after 2s ``` From 36bff4628dd72f6f4416e3762a80da2f72b3dcde Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 13 Dec 2017 14:17:24 +0200 Subject: [PATCH 10/10] Update promisify.md --- snippets/promisify.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/snippets/promisify.md b/snippets/promisify.md index f9516aeed..bdedec054 100644 --- a/snippets/promisify.md +++ b/snippets/promisify.md @@ -1,6 +1,9 @@ ### Promisify -Use currying to return a function returning a Promise that calls the original function. Use the rest operator to pass in all the parameters. In Node 8+, you can use [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original) +Use currying to return a function returning a `Promise` that calls the original function. +Use the `...rest` operator to pass in all the parameters. + +*In Node 8+, you can use [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original)* ```js const promisify = func =>