From ed72b2d0ecc761fbbded9af5a73541e2e1489f24 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Fri, 26 Jan 2018 11:58:16 +0000 Subject: [PATCH] Travis build: 1437 --- README.md | 58 ++++++++++++++++++++++++++++++++++++++++++++++ docs/index.html | 15 +++++++++++- snippets/pullBy.md | 4 ++-- 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index af9142ac8..0fe98de7d 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ average(1, 2, 3); * [`pull`](#pull) * [`pullAtIndex`](#pullatindex) * [`pullAtValue`](#pullatvalue) +* [`pullBy`](#pullby-) * [`reducedFilter`](#reducedfilter) * [`reduceSuccessive`](#reducesuccessive) * [`reduceWhich`](#reducewhich) @@ -355,6 +356,7 @@ average(1, 2, 3); * [`reverseString`](#reversestring) * [`sortCharactersInString`](#sortcharactersinstring) * [`splitLines`](#splitlines) +* [`stripHTMLtags`](#striphtmltags) * [`toCamelCase`](#tocamelcase) * [`toKebabCase`](#tokebabcase) * [`toSnakeCase`](#tosnakecase) @@ -1709,6 +1711,40 @@ let pulled = pullAtValue(myArray, ['b', 'd']); // myArray = [ 'a', 'c' ] , pulle
[⬆ Back to top](#table-of-contents) +### pullBy ![advanced](/advanced.svg) + +Mutates the original array to filter out the values specified, based on a given iterator function. + +Check if the last argument provided in a function. +Use `Array.map()` to apply the iterator function `fn` to all array 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 an array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values. + +```js +const pullBy = (arr, ...args) => { + const length = args.length; + let fn = length > 1 ? args[length - 1] : undefined; + fn = typeof fn == 'function' ? (args.pop(), fn) : undefined; + let argState = (Array.isArray(args[0]) ? args[0] : args).map(val => fn(val)); + let pulled = arr.filter((v, i) => !argState.includes(fn(v))); + arr.length = 0; + pulled.forEach(v => arr.push(v)); +}; +``` + +
+Examples + +```js +var myArray = [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 1 }]; +pullBy(myArray, [{ x: 1 }, { x: 3 }], o => o.x); // myArray = [{ x: 2 }] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### reducedFilter Filter an array of objects based on a condition while also filtering out unspecified keys. @@ -6269,6 +6305,28 @@ splitLines('This\nis a\nmultiline\nstring.\n'); // ['This', 'is a', 'multiline',
[⬆ Back to top](#table-of-contents) +### stripHTMLTags + +Removes HTML/XML tags from string. + +Use a regular expression to remove HTML/XML tags from a string. + +```js +const stripHTMLTags = str => str.replace(/<[^>]*>/g, ''); +``` + +
+Examples + +```js +stripHTMLTags('

lorem ipsum

'); // 'lorem ipsum' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### toCamelCase Converts a string to camelcase. diff --git a/docs/index.html b/docs/index.html index e2c8da5f7..2057fa49f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

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

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

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

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
@@ -296,6 +296,17 @@ Object.assig
 };
 
let myArray = ['a', 'b', 'c', 'd'];
 let pulled = pullAtValue(myArray, ['b', 'd']); // myArray = [ 'a', 'c' ] , pulled = [ 'b', 'd' ]
+

pullByadvanced

Mutates the original array to filter out the values specified, based on a given iterator function.

Check if the last argument provided in a function. Use Array.map() to apply the iterator function fn to all array 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 an array by resetting it's length to zero and Array.push() to re-populate it with only the pulled values.

const pullBy = (arr, ...args) => {
+  const length = args.length;
+  let fn = length > 1 ? args[length - 1] : undefined;
+  fn = typeof fn == 'function' ? (args.pop(), fn) : undefined;
+  let argState = (Array.isArray(args[0]) ? args[0] : args).map(val => fn(val));
+  let pulled = arr.filter((v, i) => !argState.includes(fn(v)));
+  arr.length = 0;
+  pulled.forEach(v => arr.push(v));
+};
+
var myArray = [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 1 }];
+pullBy(myArray, [{ x: 1 }, { x: 3 }], o => o.x); // myArray = [{ x: 2 }]
 

reducedFilter

Filter an array of objects based on a condition while also filtering out unspecified keys.

Use Array.filter() to filter the array based on the predicate fn so that it returns the objects for which the condition returned a truthy value. On the filtered array, use Array.map() to return the new object using Array.reduce() to filter out the keys which were not supplied as the keys argument.

const reducedFilter = (data, keys, fn) =>
   data.filter(fn).map(el =>
     keys.reduce((acc, key) => {
@@ -1422,6 +1433,8 @@ Foo.prototypeShow examples
sortCharactersInString('cabbage'); // 'aabbceg'
 

splitLines

Splits a multiline string into an array of lines.

Use String.split() and a regular expression to match line breaks and create an array.

const splitLines = str => str.split(/\r?\n/);
 
splitLines('This\nis a\nmultiline\nstring.\n'); // ['This', 'is a', 'multiline', 'string.' , '']
+

stripHTMLTags

Removes HTML/XML tags from string.

Use a regular expression to remove HTML/XML tags from a string.

const stripHTMLTags = str => str.replace(/<[^>]*>/g, '');
+
stripHTMLTags('<p><em>lorem</em> <strong>ipsum</strong></p>'); // 'lorem ipsum'
 

toCamelCase

Converts a string to camelcase.

Break the string into words and combine them capitalizing the first letter of each word, using a regexp.

const toCamelCase = str => {
   let s =
     str &&
diff --git a/snippets/pullBy.md b/snippets/pullBy.md
index b8d523a39..035ab06c8 100644
--- a/snippets/pullBy.md
+++ b/snippets/pullBy.md
@@ -20,6 +20,6 @@ const pullBy = (arr, ...args) => {
 ```
 
 ```js
-var myArray = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }];
-pullBy(myArray, [{ 'x': 1 }, { 'x': 3 }], o => o.x); // myArray = [{ x: 2 }]
+var myArray = [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 1 }];
+pullBy(myArray, [{ x: 1 }, { x: 3 }], o => o.x); // myArray = [{ x: 2 }]
 ```