From 3ecb3ccc8b383857e6684e78cddcc07b50904223 Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Mon, 18 Dec 2017 00:41:26 +0100 Subject: [PATCH 01/40] Replace spread operator with split() for increased performance and compatibility --- snippets/reverseString.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/reverseString.md b/snippets/reverseString.md index a76c506db..5564a5897 100644 --- a/snippets/reverseString.md +++ b/snippets/reverseString.md @@ -2,10 +2,10 @@ Reverses a string. -Use array destructuring and `Array.reverse()` to reverse the order of the characters in the string. +Use `split('')` and `Array.reverse()` to reverse the order of the characters in the string. Combine characters to get a string using `join('')`. ```js -const reverseString = str => [...str].reverse().join(''); +const reverseString = str => str.split('').reverse().join(''); // reverseString('foobar') -> 'raboof' ``` From 0d30cf1376845f8bcc3a71636432ceabbb3817cd Mon Sep 17 00:00:00 2001 From: Simon Legg Date: Mon, 18 Dec 2017 14:11:16 +0000 Subject: [PATCH 02/40] add reference to #without --- snippets/pull.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/snippets/pull.md b/snippets/pull.md index 4913c5082..42efe6c1b 100644 --- a/snippets/pull.md +++ b/snippets/pull.md @@ -5,6 +5,8 @@ Mutates the original array to filter out the values specified. Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed. Use `Array.length = 0` to mutate the passed in array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values. +_(For a snippet that does not mutate the original array see [`without`](#without))_ + ```js const pull = (arr, ...args) => { let pulled = arr.filter((v, i) => !args.includes(v)); From 42a1bb0fe4a1e2ad32cb2a3c05412325bc6676e1 Mon Sep 17 00:00:00 2001 From: Simon Legg Date: Mon, 18 Dec 2017 14:14:46 +0000 Subject: [PATCH 03/40] Add reference to #pull --- snippets/without.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/snippets/without.md b/snippets/without.md index 70f2a20aa..532ec26d9 100644 --- a/snippets/without.md +++ b/snippets/without.md @@ -4,6 +4,8 @@ Filters out the elements of an array, that have one of the specified values. Use `Array.filter()` to create an array excluding(using `!Array.includes()`) all given values. +_(For a snippet that mutates the original array see [`pull`](#pull))_ + ```js const without = (arr, ...args) => arr.filter(v => !args.includes(v)); // without([2, 1, 2, 3], 1, 2) -> [3] From 141a1dc89c2f4d0d05d4f356ed3bcc9823c04f25 Mon Sep 17 00:00:00 2001 From: David Wu Date: Mon, 18 Dec 2017 18:11:24 +0100 Subject: [PATCH 04/40] Back to top link in footer --- static-parts/index-end.html | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/static-parts/index-end.html b/static-parts/index-end.html index 1dd086328..6ea0075e4 100644 --- a/static-parts/index-end.html +++ b/static-parts/index-end.html @@ -1,6 +1,9 @@ - + - + From 38ccb0717c392e2d701904020164c9182bf25707 Mon Sep 17 00:00:00 2001 From: David Wu Date: Mon, 18 Dec 2017 18:11:32 +0100 Subject: [PATCH 05/40] top anchor --- scripts/web-script.js | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/web-script.js b/scripts/web-script.js index edc221de3..27e84fcc4 100644 --- a/scripts/web-script.js +++ b/scripts/web-script.js @@ -60,6 +60,7 @@ try { output += '\n'; } output += `
`; + output += ` `; // Loop over tags and snippets to create the list of snippets for(let tag of [...new Set(Object.entries(tagDbData).map(t => t[1]))].filter(v => v).sort((a,b) => a.localeCompare(b))){ output +=md.render(`## ${capitalize(tag, true)}\n`).replace(/

/g,'

'); From 0370e9272d5d3e152e80ec657fd18da4398e6b96 Mon Sep 17 00:00:00 2001 From: David Wu Date: Mon, 18 Dec 2017 18:12:11 +0100 Subject: [PATCH 06/40] Build website --- docs/index.html | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/index.html b/docs/index.html index 7c2e08821..55dd98227 100644 --- a/docs/index.html +++ b/docs/index.html @@ -159,7 +159,7 @@ validateEmail validateNumber -

Array

+
 

Array

arrayMax

Returns the maximum value in an array.

Use Math.max() combined with the spread operator (...) to get the maximum value in the array.

@@ -1072,9 +1072,12 @@ Use Number() to check if the coercion holds.

// validateNumber('10') -> true

- +
- + From 001b950b7b9a1af21c4865ac025a9be2da0ce626 Mon Sep 17 00:00:00 2001 From: taltmann42 Date: Mon, 18 Dec 2017 19:00:24 +0100 Subject: [PATCH 07/40] Create difference-with.md --- snippets/difference-with.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 snippets/difference-with.md diff --git a/snippets/difference-with.md b/snippets/difference-with.md new file mode 100644 index 000000000..70f43a5c5 --- /dev/null +++ b/snippets/difference-with.md @@ -0,0 +1,9 @@ +### Array differenceWith + +`filter`s out all values from `arr` for which the comparator `comp(a,b)` function never returns `true` + +```js +const differenceWith = (arr, val, comp) => arr.filter(a => !val.find(b => comp(a, b))) + +//differenceWith([1, 1.2, 1.5, 3], [1.9, 3], (a,b) => Math.round(a) == Math.round(b)) -> [1, 1.2] +``` From 70ec53e2f09d4dcb9effcfde2557fb6088c816c2 Mon Sep 17 00:00:00 2001 From: taltmann42 Date: Mon, 18 Dec 2017 19:12:51 +0100 Subject: [PATCH 08/40] Create drop-right.md --- snippets/drop-right.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 snippets/drop-right.md diff --git a/snippets/drop-right.md b/snippets/drop-right.md new file mode 100644 index 000000000..9c162327a --- /dev/null +++ b/snippets/drop-right.md @@ -0,0 +1,13 @@ +### Array dropRight + +Returns a new array with `n` elements removed from the right + +Checks if `n` is shorter than the given array and slices it accordingly or returns an empty array + +```js +const dropRight = (arr, n = 1) => (arr.length - n ) > 0 ? arr.slice(0, arr.length - n) : [] + +//dropRight([1,2,3]) -> [1,2] +//dropRight([1,2,3], 2) -> [1] +//dropRight([1,2,3], 42) -> [] +``` From ae7732cd3ed8d20a99a3fb48fb2108ffc8fe8442 Mon Sep 17 00:00:00 2001 From: taltmann42 Date: Mon, 18 Dec 2017 19:15:51 +0100 Subject: [PATCH 09/40] Update drop-right.md shorter comparison of `n` and `arr.length` --- snippets/drop-right.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/drop-right.md b/snippets/drop-right.md index 9c162327a..b1f7e02ed 100644 --- a/snippets/drop-right.md +++ b/snippets/drop-right.md @@ -5,7 +5,7 @@ Returns a new array with `n` elements removed from the right Checks if `n` is shorter than the given array and slices it accordingly or returns an empty array ```js -const dropRight = (arr, n = 1) => (arr.length - n ) > 0 ? arr.slice(0, arr.length - n) : [] +const (arr, n = 1) => n < arr.length ? arr.slice(0, arr.length - n) : [] //dropRight([1,2,3]) -> [1,2] //dropRight([1,2,3], 2) -> [1] From c11862fdcd67797cf6e470fd621f1bd980fe6f6e Mon Sep 17 00:00:00 2001 From: King Date: Mon, 18 Dec 2017 14:32:05 -0500 Subject: [PATCH 10/40] add pullAll --- snippets/pullAll.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 snippets/pullAll.md diff --git a/snippets/pullAll.md b/snippets/pullAll.md new file mode 100644 index 000000000..d630aab99 --- /dev/null +++ b/snippets/pullAll.md @@ -0,0 +1,18 @@ +### pullAll + +This method is like pull except that it accepts an array of values to filter out before Mutating and pulling all the values from the original array. + +Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed. +Use `Array.length = 0` to mutate the passed in array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values. + +```js +const pullAll = (arr, pullArr) => { + let pulled = arr.filter((v, i) => !pullArr.includes(v)); + arr.length = 0; + pulled.forEach(v => arr.push(v)); +} + +// let myArray = ['a', 'b', 'c', 'a', 'b', 'c']; +// pullAll(myArray, ['a', 'c']); +// console.log(myArray) -> [ 'b', 'b' ] +``` \ No newline at end of file From 3fa8763e7eabbd372beb6a10ca719ae05fb4dd7d Mon Sep 17 00:00:00 2001 From: King Date: Mon, 18 Dec 2017 14:40:26 -0500 Subject: [PATCH 11/40] ran npm tagger & updated pullAll:array -> tag_database --- tag_database | 1 + 1 file changed, 1 insertion(+) diff --git a/tag_database b/tag_database index f48f069b3..d1f687a4b 100644 --- a/tag_database +++ b/tag_database @@ -71,6 +71,7 @@ pipe:function powerset:math promisify:function pull:array +pullAll:array randomIntegerInRange:math randomNumberInRange:math readFileLines:node From 874c66107626c012c99e93db79ed75da433fb343 Mon Sep 17 00:00:00 2001 From: King Date: Mon, 18 Dec 2017 17:42:47 -0500 Subject: [PATCH 12/40] add pullAtIndex.md --- snippets/pullAtIndex.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 snippets/pullAtIndex.md diff --git a/snippets/pullAtIndex.md b/snippets/pullAtIndex.md new file mode 100644 index 000000000..e9efc172d --- /dev/null +++ b/snippets/pullAtIndex.md @@ -0,0 +1,24 @@ +### pullAtIndex + +This method is like pull except that it accepts an array of indexes to filter out before Mutating and pulling all the values from the original array. Which then returns an array of removed elements. + +Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed. +Use `Array.length = 0` to mutate the passed in array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values. +Use `Array.push()` to keep track of pulled values + +```js +const pullAtIndex = (arr, pullArr) => { + let removed = []; + let pulled = arr.map((v, i) => pullArr.includes(i) ? removed.push(v) : v) + .filter((v, i) => !pullArr.includes(i)) + arr.length = 0; + pulled.forEach(v => arr.push(v)); + return removed; +} + +// let myArray = ['a', 'b', 'c', 'd']; +// let pulled = pullAtIndex(myArray, [1, 3]); + +// console.log(myArray); -> [ 'a', 'c' ] +// console.log(pulled); -> [ 'b', 'd' ] +``` \ No newline at end of file From 75e1cf5a002fd24145f33a2a7d1c6ad7fcc783db Mon Sep 17 00:00:00 2001 From: King Date: Mon, 18 Dec 2017 17:44:18 -0500 Subject: [PATCH 13/40] update tag DB with pullAtIndex:array & ran npm run tagger --- tag_database | 1 + 1 file changed, 1 insertion(+) diff --git a/tag_database b/tag_database index f48f069b3..c1a503097 100644 --- a/tag_database +++ b/tag_database @@ -71,6 +71,7 @@ pipe:function powerset:math promisify:function pull:array +pullAtIndex:array randomIntegerInRange:math randomNumberInRange:math readFileLines:node From 7d425e893375e90a753e5c231e011aaa58211d10 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 19 Dec 2017 10:58:11 +1100 Subject: [PATCH 14/40] Make median.md immutable `.sort()` mutates the original array --- snippets/median.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/median.md b/snippets/median.md index fd8ca3d7c..c7eea609c 100644 --- a/snippets/median.md +++ b/snippets/median.md @@ -7,7 +7,7 @@ Return the number at the midpoint if `length` is odd, otherwise the average of t ```js const median = arr => { - const mid = Math.floor(arr.length / 2), nums = arr.sort((a, b) => a - b); + const mid = Math.floor(arr.length / 2), nums = [...arr].sort((a, b) => a - b); return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2; }; // median([5,6,50,1,-5]) -> 5 From ea867bcd2977542e6a1f1ce2238d09d0f7315172 Mon Sep 17 00:00:00 2001 From: King Date: Tue, 19 Dec 2017 01:06:29 -0500 Subject: [PATCH 15/40] add pullAtValue.md --- snippets/pullAtValue.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 snippets/pullAtValue.md diff --git a/snippets/pullAtValue.md b/snippets/pullAtValue.md new file mode 100644 index 000000000..e7b3fd44e --- /dev/null +++ b/snippets/pullAtValue.md @@ -0,0 +1,27 @@ +### pullAtValue + +This method is like pull except that it accepts an array of values to filter out before Mutating and pulling all the values from the original array. Which then returns an array of removed elements. + +Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed. +Use `Array.length = 0` to mutate the passed in array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values. +Use `Array.push()` to keep track of pulled values + +```js +const pullAtValue = (arr, pullArr) => { + let removed = []; + let pushToRemove = arr.forEach((v, i) => pullArr.includes(v) ? removed.push(v) : v) + let mutateTo = arr.filter((v, i) => !pullArr.includes(v)) + arr.length = 0; + mutateTo.forEach(v => arr.push(v)); + return removed; +} + +let myArray = ['a', 'b', 'c', 'd']; +let pulled = pullAtValue(myArray, ['b', 'd']); + +// let myArray = ['a', 'b', 'c', 'd']; +// let pulled = pullAtValue(myArray, ['b', 'd']); + +// console.log(myArray); -> [ 'a', 'c' ] +// console.log(pulled); -> [ 'b', 'd' ] +``` \ No newline at end of file From c2e339760caa961e34a23216abf383a39a75fc47 Mon Sep 17 00:00:00 2001 From: King Date: Tue, 19 Dec 2017 01:08:11 -0500 Subject: [PATCH 16/40] ran npm run tagger & update tag_database with pullAtValue:array --- tag_database | 1 + 1 file changed, 1 insertion(+) diff --git a/tag_database b/tag_database index f48f069b3..1214acfde 100644 --- a/tag_database +++ b/tag_database @@ -71,6 +71,7 @@ pipe:function powerset:math promisify:function pull:array +pullAtValue:array randomIntegerInRange:math randomNumberInRange:math readFileLines:node From 14d683cc94b32d63e5c8c1f7f297cf928dbfdd4d Mon Sep 17 00:00:00 2001 From: Larry Botha Date: Tue, 19 Dec 2017 08:52:26 +0200 Subject: [PATCH 17/40] use Array.slice instead of Array.shift --- snippets/dropElements.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/dropElements.md b/snippets/dropElements.md index 34138eb93..adc81d79d 100644 --- a/snippets/dropElements.md +++ b/snippets/dropElements.md @@ -2,12 +2,12 @@ Removes elements in an array until the passed function returns `true`. Returns the remaining elements in the array. -Loop through the array, using `Array.shift()` to drop the first element of the array until the returned value from the function is `true`. +Loop through the array, using `Array.slice()` to drop the first element of the array until the returned value from the function is `true`. Returns the remaining elements. ```js const dropElements = (arr, func) => { - while (arr.length > 0 && !func(arr[0])) arr.shift(); + while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1); return arr; }; // dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4] From d73fa60f972be4760c9fa1dac3a2c53d576b1693 Mon Sep 17 00:00:00 2001 From: Larry Botha Date: Tue, 19 Dec 2017 08:53:18 +0200 Subject: [PATCH 18/40] build updated readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c4a6765d3..57328c55a 100644 --- a/README.md +++ b/README.md @@ -251,12 +251,12 @@ const distinctValuesOfArray = arr => [...new Set(arr)]; Removes elements in an array until the passed function returns `true`. Returns the remaining elements in the array. -Loop through the array, using `Array.shift()` to drop the first element of the array until the returned value from the function is `true`. +Loop through the array, using `Array.slice()` to drop the first element of the array until the returned value from the function is `true`. Returns the remaining elements. ```js const dropElements = (arr, func) => { - while (arr.length > 0 && !func(arr[0])) arr.shift(); + while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1); return arr; }; // dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4] From d2ea3c0bb8e581ec378adffa8165b52d61f0a401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=84=9A=E4=BA=BA=E7=A0=81=E5=A4=B4?= Date: Tue, 19 Dec 2017 14:58:08 +0800 Subject: [PATCH 19/40] code error ```js const bottomVisible = () => document.documentElement.clientHeight + window.scrollY >= (document.documentElement.scrollHeight || document.documentElement.clientHeight); // bottomVisible() -> true ``` --- snippets/bottomVisible.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/bottomVisible.md b/snippets/bottomVisible.md index 8e655db45..adac44881 100644 --- a/snippets/bottomVisible.md +++ b/snippets/bottomVisible.md @@ -6,6 +6,6 @@ Use `scrollY`, `scrollHeight` and `clientHeight` to determine if the bottom of t ```js const bottomVisible = () => - document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight || document.documentElement.clientHeight; + document.documentElement.clientHeight + window.scrollY >= (document.documentElement.scrollHeight || document.documentElement.clientHeight); // bottomVisible() -> true ``` From e7df322d69b3992574d57a846085c86030d17c12 Mon Sep 17 00:00:00 2001 From: David Wu Date: Tue, 19 Dec 2017 10:23:25 +0100 Subject: [PATCH 20/40] Add subtitle to header --- static-parts/index-start.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/static-parts/index-start.html b/static-parts/index-start.html index 0aeee1e31..cc270b31f 100644 --- a/static-parts/index-start.html +++ b/static-parts/index-start.html @@ -22,8 +22,10 @@ -
-

 30 seconds of code

+
+

 30 seconds of code + Curated collection of useful Javascript snippets that you can understand in 30 seconds or less. +

From d7e8686ffac4a55e8718196ce9d3ed8297fb053b Mon Sep 17 00:00:00 2001 From: David Wu Date: Tue, 19 Dec 2017 10:23:38 +0100 Subject: [PATCH 21/40] Build site --- docs/index.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/index.html b/docs/index.html index 7c2e08821..2cead4570 100644 --- a/docs/index.html +++ b/docs/index.html @@ -22,8 +22,10 @@ -
-

 30 seconds of code

+
+

 30 seconds of code + Curated collection of useful Javascript snippets that you can understand in 30 seconds or less. +

From 16b843ce5b2de95877844c49263a4b9f1621e9bf Mon Sep 17 00:00:00 2001 From: King Date: Tue, 19 Dec 2017 04:52:44 -0500 Subject: [PATCH 22/40] fix/update intializeArrayWithRange.md --- snippets/initializeArrayWithRange.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index 75dbc8adf..8a58c1bbb 100644 --- a/snippets/initializeArrayWithRange.md +++ b/snippets/initializeArrayWithRange.md @@ -1,12 +1,13 @@ ### initializeArrayWithRange -Initializes an array containing the numbers in the specified range. +Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive. Use `Array(end-start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range. You can omit `start` to use a default value of `0`. ```js -const initializeArrayWithRange = (end, start = 0) => - Array.from({ length: end - start }).map((v, i) => i + start); -// initializeArrayWithRange(5) -> [0,1,2,3,4] +const initializeArrayWithRange = (end, start = 0) => + Array.from({ length: (end + 1) - start }).map((v, i) => i + start); +// initializeArrayWithRange(5) -> [0,1,2,3,4,5] +// initializeArrayWithRange(7, 3) -> [3,4,5,6,7] ``` From e7e1feb949b8d3b819792ae5104038ad7aace803 Mon Sep 17 00:00:00 2001 From: King Date: Tue, 19 Dec 2017 05:00:49 -0500 Subject: [PATCH 23/40] fix up description --- snippets/initializeArrayWithRange.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/initializeArrayWithRange.md b/snippets/initializeArrayWithRange.md index 8a58c1bbb..c3ef5a0a0 100644 --- a/snippets/initializeArrayWithRange.md +++ b/snippets/initializeArrayWithRange.md @@ -2,7 +2,7 @@ Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive. -Use `Array(end-start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range. +Use `Array((end + 1) - start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range. You can omit `start` to use a default value of `0`. ```js From 1c3034fa9aaeab72dacd306d29f16048d213bd23 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 19 Dec 2017 12:01:29 +0200 Subject: [PATCH 24/40] Rebuild website --- docs/index.html | 2 +- static-parts/index-start.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/index.html b/docs/index.html index d95229aa1..3e4e53b93 100644 --- a/docs/index.html +++ b/docs/index.html @@ -28,7 +28,7 @@

-
+