From 9bcd90e6c0790c26689bae163f375d4c34180244 Mon Sep 17 00:00:00 2001 From: King Date: Sat, 16 Dec 2017 23:02:33 -0500 Subject: [PATCH 1/4] add pull snippet --- snippets/pull.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 snippets/pull.md diff --git a/snippets/pull.md b/snippets/pull.md new file mode 100644 index 000000000..2507270be --- /dev/null +++ b/snippets/pull.md @@ -0,0 +1,21 @@ +### Pull + +Use `Array.filter()` 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. +Use `Array.push()` to re-populate the original array to equal the pulled values + +```js +const pull = (arr, ...args) => { + let pulled = arr.filter((v, i) => args.indexOf(v) === -1); + arr.length = 0; + pulled.forEach(v => arr.push(v)); +} + +// let myArray = ['a', 'b', 'c', 'a', 'b', 'c']; +// pull(myArray, 'a', 'c'); +// console.log(myArray) -> [ 'b', 'b' ] + +// let test = ['4', '1', '1', '5', '4', '2']; +// pull(test, '4', '1'); +// console.log(myArray) -> [ '5', '2' ] +``` From 80a4a65840046d56b8aed6f080446034dfcfdfc3 Mon Sep 17 00:00:00 2001 From: King Date: Sat, 16 Dec 2017 23:05:18 -0500 Subject: [PATCH 2/4] ran npm run tagger & add array tag to DB --- tag_database | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tag_database b/tag_database index 6cce617ea..07e07435a 100644 --- a/tag_database +++ b/tag_database @@ -56,7 +56,7 @@ is-function:utility is-number:utility is-string:utility is-symbol:utility -JSON-to-date:date +json-to-date: last-of-list:array log-function-name:function measure-time-taken-by-function:utility @@ -71,6 +71,7 @@ pick:array pipe-functions:function powerset:math promisify:function +pull:array random-integer-in-range:math random-number-in-range:math read-file-as-array-of-lines:node @@ -98,4 +99,4 @@ URL-parameters:utility UUID-generator:utility validate-email:utility validate-number:utility -write-JSON-to-file:node +write-json-to-file: From dea5272fa2b40da38213bacf788c75585dfb03ae Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 17 Dec 2017 11:46:41 +0200 Subject: [PATCH 3/4] Update and rename pull.md to array-pull-(mutates-array).md --- .../{pull.md => array-pull-(mutates-array).md} | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) rename snippets/{pull.md => array-pull-(mutates-array).md} (62%) diff --git a/snippets/pull.md b/snippets/array-pull-(mutates-array).md similarity index 62% rename from snippets/pull.md rename to snippets/array-pull-(mutates-array).md index 2507270be..9b91c2c17 100644 --- a/snippets/pull.md +++ b/snippets/array-pull-(mutates-array).md @@ -1,4 +1,4 @@ -### Pull +### Array pull (mutates array) Use `Array.filter()` 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. @@ -6,16 +6,10 @@ Use `Array.push()` to re-populate the original array to equal the pulled values ```js const pull = (arr, ...args) => { - let pulled = arr.filter((v, i) => args.indexOf(v) === -1); - arr.length = 0; - pulled.forEach(v => arr.push(v)); -} - + let pulled = arr.filter((v, i) => args.includes(v)); + arr.length = 0; pulled.forEach(v => arr.push(v)); +}; // let myArray = ['a', 'b', 'c', 'a', 'b', 'c']; // pull(myArray, 'a', 'c'); // console.log(myArray) -> [ 'b', 'b' ] - -// let test = ['4', '1', '1', '5', '4', '2']; -// pull(test, '4', '1'); -// console.log(myArray) -> [ '5', '2' ] ``` From 3530880ca88872d0fb4771dd67b3527cb9b8dc37 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 17 Dec 2017 11:47:17 +0200 Subject: [PATCH 4/4] Update array-pull-(mutates-array).md --- snippets/array-pull-(mutates-array).md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/snippets/array-pull-(mutates-array).md b/snippets/array-pull-(mutates-array).md index 9b91c2c17..3a58225ca 100644 --- a/snippets/array-pull-(mutates-array).md +++ b/snippets/array-pull-(mutates-array).md @@ -1,8 +1,7 @@ ### Array pull (mutates array) -Use `Array.filter()` 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. -Use `Array.push()` to re-populate the original array to equal the pulled values +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 pull = (arr, ...args) => {