From b4b0909a1d05e991fb3ef1bf9f29e90cbf8e663c Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Wed, 14 Feb 2018 11:32:14 +0000 Subject: [PATCH] Travis build: 1674 --- README.md | 138 +++++++++++++++++++++++++++++++++++++++++++++ docs/index.html | 16 +++++- snippets/all.md | 2 +- snippets/allBy.md | 2 +- snippets/any.md | 2 +- snippets/anyBy.md | 2 +- snippets/none.md | 2 +- snippets/noneBy.md | 2 +- 8 files changed, 158 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5194f20c3..a258307c9 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,10 @@ average(1, 2, 3);
View contents +* [`all`](#all) +* [`allBy`](#allby) +* [`any`](#any) +* [`anyBy`](#anyby) * [`bifurcate`](#bifurcate) * [`bifurcateBy`](#bifurcateby) * [`chunk`](#chunk) @@ -136,6 +140,8 @@ average(1, 2, 3); * [`mapObject`](#mapobject-) * [`maxN`](#maxn) * [`minN`](#minn) +* [`none`](#none) +* [`noneBy`](#noneby) * [`nthElement`](#nthelement) * [`partition`](#partition) * [`pull`](#pull) @@ -767,6 +773,94 @@ const unary = fn => val => fn(val); --- ## 📚 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. + +```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`. + +```js +const allBy = (arr, fn) => arr.every(fn); +``` + +
+Examples + +```js +allBy([4, 2, 3], x => x > 1); // true +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + +### 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`. + +```js +const anyBy = (arr, fn) => arr.some(fn); +``` + +
+Examples + +```js +anyBy([0, 1, 2, 0], x => x >= 2); // true +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### 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. @@ -1718,6 +1812,50 @@ minN([1, 2, 3], 2); // [1,2]
[⬆ Back to top](#table-of-contents) +### 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`. + +```js +const noneBy = (arr, fn) => !arr.some(fn); +``` + +
+Examples + +```js +noneBy([0, 1, 3, 0], x => x == 2); // true +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### nthElement Returns the nth element of an array. diff --git a/docs/index.html b/docs/index.html index 75890c2f5..62c8251c6 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,7 +123,15 @@ 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

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) =>
+

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
+

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'] ]
 

bifurcateBy

Splits values into two groups according to a predicate function, which specifies which group an element in the input collection belongs to. If the predicate function returns a truthy value, the collection element 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 the value returned by fn for each element.

const bifurcateBy = (arr, fn) =>
@@ -289,6 +297,10 @@ 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
 

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'
diff --git a/snippets/all.md b/snippets/all.md
index cb976da09..e9faecd9d 100644
--- a/snippets/all.md
+++ b/snippets/all.md
@@ -9,5 +9,5 @@ const all = arr => arr.every(Boolean);
 ```
 
 ```js
-all([1,2,3]); // true
+all([1, 2, 3]); // true
 ```
diff --git a/snippets/allBy.md b/snippets/allBy.md
index d2dec44f8..36335714f 100644
--- a/snippets/allBy.md
+++ b/snippets/allBy.md
@@ -9,5 +9,5 @@ const allBy = (arr, fn) => arr.every(fn);
 ```
 
 ```js
-allBy([4,2,3], x => x > 1); // true
+allBy([4, 2, 3], x => x > 1); // true
 ```
diff --git a/snippets/any.md b/snippets/any.md
index d030d4451..09a5afad8 100644
--- a/snippets/any.md
+++ b/snippets/any.md
@@ -9,5 +9,5 @@ const any = arr => arr.some(Boolean);
 ```
 
 ```js
-any([0,0,1,0]); // true
+any([0, 0, 1, 0]); // true
 ```
diff --git a/snippets/anyBy.md b/snippets/anyBy.md
index 244c242d2..2345f82bc 100644
--- a/snippets/anyBy.md
+++ b/snippets/anyBy.md
@@ -9,5 +9,5 @@ const anyBy = (arr, fn) => arr.some(fn);
 ```
 
 ```js
-anyBy([0,1,2,0], x => x >= 2); // true
+anyBy([0, 1, 2, 0], x => x >= 2); // true
 ```
diff --git a/snippets/none.md b/snippets/none.md
index 8bf8fb830..a243b0409 100644
--- a/snippets/none.md
+++ b/snippets/none.md
@@ -9,5 +9,5 @@ const none = arr => !arr.some(Boolean);
 ```
 
 ```js
-none([0,0,0]); // true
+none([0, 0, 0]); // true
 ```
diff --git a/snippets/noneBy.md b/snippets/noneBy.md
index 20fcfcfb7..fa719d2df 100644
--- a/snippets/noneBy.md
+++ b/snippets/noneBy.md
@@ -9,5 +9,5 @@ const noneBy = (arr, fn) => !arr.some(fn);
 ```
 
 ```js
-noneBy([0,1,3,0], x => x == 2); // true
+noneBy([0, 1, 3, 0], x => x == 2); // true
 ```