From 0549931ec6edbae571e2eb87b2eb72d84462a1ac Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Fri, 16 Feb 2018 11:46:02 +0000 Subject: [PATCH] Travis build: 1683 --- README.md | 87 +++++++------------------------------------------ docs/index.html | 23 ++++++------- 2 files changed, 22 insertions(+), 88 deletions(-) diff --git a/README.md b/README.md index 223fd8bbd..6bff27e25 100644 --- a/README.md +++ b/README.md @@ -99,9 +99,7 @@ average(1, 2, 3); View contents * [`all`](#all) -* [`allBy`](#allby) * [`any`](#any) -* [`anyBy`](#anyby) * [`bifurcate`](#bifurcate) * [`bifurcateBy`](#bifurcateby) * [`chunk`](#chunk) @@ -141,7 +139,6 @@ average(1, 2, 3); * [`maxN`](#maxn) * [`minN`](#minn) * [`none`](#none) -* [`noneBy`](#noneby) * [`nthElement`](#nthelement) * [`partition`](#partition) * [`pull`](#pull) @@ -776,41 +773,21 @@ const unary = fn => val => fn(val); ### all -Returns `true` if all elements in a collection are truthy, `false` otherwise. - -Use `Array.every(Boolean)` to test if all elements in the collection are truthy. - -```js -const all = arr => arr.every(Boolean); -``` - -
-Examples - -```js -all([1, 2, 3]); // true -``` - -
- -
[⬆ Back to top](#table-of-contents) - - -### allBy - Returns `true` if the provided predicate function returns `true` for all elements in a collection, `false` otherwise. Use `Array.every()` to test if all elements in the collection return `true` based on `fn`. +Omit the second argument, `fn`, to use `Boolean` as a default. ```js -const allBy = (arr, fn) => arr.every(fn); +const all = (arr, fn = Boolean) => arr.every(fn); ```
Examples ```js -allBy([4, 2, 3], x => x > 1); // true +all([4, 2, 3], x => x > 1); // true +all([1, 2, 3]); // true ```
@@ -820,41 +797,21 @@ allBy([4, 2, 3], x => x > 1); // true ### any -Returns `true` if at least one element in a collection is truthy, `false` otherwise. - -Use `Array.some(Boolean)` to test if any elements in the collection are truthy. - -```js -const any = arr => arr.some(Boolean); -``` - -
-Examples - -```js -any([0, 0, 1, 0]); // true -``` - -
- -
[⬆ Back to top](#table-of-contents) - - -### anyBy - Returns `true` if the provided predicate function returns `true` for at least one element in a collection, `false` otherwise. Use `Array.some()` to test if any elements in the collection return `true` based on `fn`. +Omit the second argument, `fn`, to use `Boolean` as a default. ```js -const anyBy = (arr, fn) => arr.some(fn); +const any = (arr, fn = Boolean) => arr.some(fn); ```
Examples ```js -anyBy([0, 1, 2, 0], x => x >= 2); // true +any([0, 1, 2, 0], x => x >= 2); // true +any([0, 0, 1, 0]); // true ```
@@ -1815,41 +1772,21 @@ minN([1, 2, 3], 2); // [1,2] ### none -Returns `true` if no elements in a collection are truthy, `false` otherwise. - -Use `!Array.some(Boolean)` to test if any elements in the collection are truthy. - -```js -const none = arr => !arr.some(Boolean); -``` - -
-Examples - -```js -none([0, 0, 0]); // true -``` - -
- -
[⬆ Back to top](#table-of-contents) - - -### noneBy - Returns `true` if the provided predicate function returns `false` for all elements in a collection, `false` otherwise. Use `Array.some()` to test if any elements in the collection return `true` based on `fn`. +Omit the second argument, `fn`, to use `Boolean` as a default. ```js -const noneBy = (arr, fn) => !arr.some(fn); +const none = (arr, fn = Boolean) => !arr.some(fn); ```
Examples ```js -noneBy([0, 1, 3, 0], x => x == 2); // true +none([0, 1, 3, 0], x => x == 2); // true +none([0, 0, 0]); // true ```
diff --git a/docs/index.html b/docs/index.html index e56217194..141de6d37 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);
@@ -123,14 +123,12 @@ Object.assig
 arrayMax([1, 2, 3]); // 3
 

unary

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

Call the provided function, fn, with just the first argument given.

const unary = fn => val => fn(val);
 
['6', '8', '10'].map(unary(parseInt)); // [6, 8, 10]
-

Array

all

Returns true if all elements in a collection are truthy, false otherwise.

Use Array.every(Boolean) to test if all elements in the collection are truthy.

const all = arr => arr.every(Boolean);
-
all([1, 2, 3]); // true
-

allBy

Returns true if the provided predicate function returns true for all elements in a collection, false otherwise.

Use Array.every() to test if all elements in the collection return true based on fn.

const allBy = (arr, fn) => arr.every(fn);
-
allBy([4, 2, 3], x => x > 1); // true
-

any

Returns true if at least one element in a collection is truthy, false otherwise.

Use Array.some(Boolean) to test if any elements in the collection are truthy.

const any = arr => arr.some(Boolean);
-
any([0, 0, 1, 0]); // true
-

anyBy

Returns true if the provided predicate function returns true for at least one element in a collection, false otherwise.

Use Array.some() to test if any elements in the collection return true based on fn.

const anyBy = (arr, fn) => arr.some(fn);
-
anyBy([0, 1, 2, 0], x => x >= 2); // true
+

Array

all

Returns true if the provided predicate function returns true for all elements in a collection, false otherwise.

Use Array.every() to test if all elements in the collection return true based on fn. Omit the second argument, fn, to use Boolean as a default.

const all = (arr, fn = Boolean) => arr.every(fn);
+
all([4, 2, 3], x => x > 1); // true
+all([1, 2, 3]); // true
+

any

Returns true if the provided predicate function returns true for at least one element in a collection, false otherwise.

Use Array.some() to test if any elements in the collection return true based on fn. Omit the second argument, fn, to use Boolean as a default.

const any = (arr, fn = Boolean) => arr.some(fn);
+
any([0, 1, 2, 0], x => x >= 2); // true
+any([0, 0, 1, 0]); // true
 

bifurcate

Splits values into two groups. If an element in filter is truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.

Use Array.reduce() and Array.push() to add elements to groups, based on filter.

const bifurcate = (arr, filter) =>
   arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]);
 
bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]); // [ ['beep', 'boop', 'bar'], ['foo'] ]
@@ -297,10 +295,9 @@ Object.assig
 

minN

Returns the n minimum elements from the provided array. If n is greater than or equal to the provided array's length, then return the original array(sorted in ascending order).

Use Array.sort() combined with the spread operator (...) to create a shallow clone of the array and sort it in ascending order. Use Array.slice() to get the specified number of elements. Omit the second argument, n, to get a one-element array.

const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
 
minN([1, 2, 3]); // [1]
 minN([1, 2, 3], 2); // [1,2]
-

none

Returns true if no elements in a collection are truthy, false otherwise.

Use !Array.some(Boolean) to test if any elements in the collection are truthy.

const none = arr => !arr.some(Boolean);
-
none([0, 0, 0]); // true
-

noneBy

Returns true if the provided predicate function returns false for all elements in a collection, false otherwise.

Use Array.some() to test if any elements in the collection return true based on fn.

const noneBy = (arr, fn) => !arr.some(fn);
-
noneBy([0, 1, 3, 0], x => x == 2); // true
+

none

Returns true if the provided predicate function returns false for all elements in a collection, false otherwise.

Use Array.some() to test if any elements in the collection return true based on fn. Omit the second argument, fn, to use Boolean as a default.

const none = (arr, fn = Boolean) => !arr.some(fn);
+
none([0, 1, 3, 0], x => x == 2); // true
+none([0, 0, 0]); // true
 

nthElement

Returns the nth element of an array.

Use Array.slice() to get an array containing the nth element at the first place. If the index is out of bounds, return []. Omit the second argument, n, to get the first element of the array.

const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0];
 
nthElement(['a', 'b', 'c'], 1); // 'b'
 nthElement(['a', 'b', 'b'], -3); // 'a'