Updated all, any, none

Merged them with their by counterparts.
This commit is contained in:
Angelos Chalaris
2018-02-16 13:43:43 +02:00
parent ce77e2b8b7
commit 9a3bd8ffae
22 changed files with 58 additions and 135 deletions

View File

@ -1,13 +1,15 @@
### all ### all
Returns `true` if all elements in a collection are truthy, `false` otherwise. Returns `true` if the provided predicate function returns `true` for all elements in a collection, `false` otherwise.
Use `Array.every(Boolean)` to test if all elements in the collection are truthy. 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 ```js
const all = arr => arr.every(Boolean); const all = (arr, fn = Boolean) => arr.every(fn);
``` ```
```js ```js
all([4, 2, 3], x => x > 1); // true
all([1, 2, 3]); // true all([1, 2, 3]); // true
``` ```

View File

@ -1,13 +0,0 @@
### 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);
```
```js
allBy([4, 2, 3], x => x > 1); // true
```

View File

@ -1,13 +1,15 @@
### any ### any
Returns `true` if at least one element in a collection is truthy, `false` otherwise. Returns `true` if the provided predicate function returns `true` for at least one element in a collection, `false` otherwise.
Use `Array.some(Boolean)` to test if any elements in the collection are truthy. 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 ```js
const any = arr => arr.some(Boolean); const any = (arr, fn = Boolean) => arr.some(fn);
``` ```
```js ```js
any([0, 1, 2, 0], x => x >= 2); // true
any([0, 0, 1, 0]); // true any([0, 0, 1, 0]); // true
``` ```

View File

@ -1,13 +0,0 @@
### 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);
```
```js
anyBy([0, 1, 2, 0], x => x >= 2); // true
```

View File

@ -1,13 +1,15 @@
### none ### none
Returns `true` if no elements in a collection are truthy, `false` otherwise. Returns `true` if the provided predicate function returns `false` for all elements in a collection, `false` otherwise.
Use `!Array.some(Boolean)` to test if any elements in the collection are truthy. 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 ```js
const none = arr => !arr.some(Boolean); const none = (arr, fn = Boolean) => !arr.some(fn);
``` ```
```js ```js
none([0, 1, 3, 0], x => x == 2); // true
none([0, 0, 0]); // true none([0, 0, 0]); // true
``` ```

View File

@ -1,13 +0,0 @@
### 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);
```
```js
noneBy([0, 1, 3, 0], x => x == 2); // true
```

View File

@ -1,8 +1,6 @@
all:array all:array,function
allBy:array,function
anagrams:string,recursion anagrams:string,recursion
any:array any:array,function
anyBy:array,function
approximatelyEqual:math approximatelyEqual:math
arrayToHtmlList:browser,array arrayToHtmlList:browser,array
ary:adapter,function ary:adapter,function
@ -164,8 +162,7 @@ minBy:math,array,function
minN:array,math minN:array,math
mostPerformant:utility,function mostPerformant:utility,function
negate:function negate:function
none:array none:array,function
noneBy:array,function
nthArg:utility,function nthArg:utility,function
nthElement:array nthElement:array
objectFromPairs:object,array objectFromPairs:object,array

View File

@ -1,2 +1,2 @@
const all = arr => arr.every(Boolean); const all = (arr, fn = Boolean) => arr.every(fn);
module.exports = all; module.exports = all;

View File

@ -11,6 +11,8 @@ test('Testing all', (t) => {
t.false(all([undefined,1]), 'Returns false for arrays with undefined'); t.false(all([undefined,1]), 'Returns false for arrays with undefined');
t.false(all([null,1]), 'Returns false for arrays with null'); t.false(all([null,1]), 'Returns false for arrays with null');
t.false(all(['',1]), 'Returns false for arrays with empty strings'); t.false(all(['',1]), 'Returns false for arrays with empty strings');
t.true(all([4,1,2,3], x => x >= 1), 'Returns true with predicate function');
t.false(all([0,1], x => x >= 1), 'Returns false with a predicate function');
//t.deepEqual(all(args..), 'Expected'); //t.deepEqual(all(args..), 'Expected');
//t.equal(all(args..), 'Expected'); //t.equal(all(args..), 'Expected');
//t.false(all(args..), 'Expected'); //t.false(all(args..), 'Expected');

View File

@ -1,2 +0,0 @@
const allBy = (arr, fn) => arr.every(fn);
module.exports = allBy;

View File

@ -1,15 +0,0 @@
const test = require('tape');
const allBy = require('./allBy.js');
test('Testing allBy', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof allBy === 'function', 'allBy is a Function');
t.true(allBy([4,1,2,3], x => x >= 1), 'Returns true with predicate function');
t.false(allBy([0,1], x => x >= 1), 'Returns false with a predicate function');
//t.deepEqual(allBy(args..), 'Expected');
//t.equal(allBy(args..), 'Expected');
//t.false(allBy(args..), 'Expected');
//t.throws(allBy(args..), 'Expected');
t.end();
});

View File

@ -1,2 +1,2 @@
const any = arr => arr.some(Boolean); const any = (arr, fn = Boolean) => arr.some(fn);
module.exports = any; module.exports = any;

View File

@ -8,6 +8,8 @@ test('Testing any', (t) => {
t.true(any([0,1,2,3]), 'Returns true for arrays with at least one truthy value'); t.true(any([0,1,2,3]), 'Returns true for arrays with at least one truthy value');
t.false(any([0,0]), 'Returns false for arrays with no truthy values'); t.false(any([0,0]), 'Returns false for arrays with no truthy values');
t.false(any([NaN,0,undefined,null,'']), 'Returns false for arrays with no truthy values'); t.false(any([NaN,0,undefined,null,'']), 'Returns false for arrays with no truthy values');
t.true(any([4,1,0,3], x => x >= 1), 'Returns true with predicate function');
t.false(any([0,1], x => x < 0), 'Returns false with a predicate function');
//t.deepEqual(any(args..), 'Expected'); //t.deepEqual(any(args..), 'Expected');
//t.equal(any(args..), 'Expected'); //t.equal(any(args..), 'Expected');
//t.false(any(args..), 'Expected'); //t.false(any(args..), 'Expected');

View File

@ -1,2 +0,0 @@
const anyBy = (arr, fn) => arr.some(fn);
module.exports = anyBy;

View File

@ -1,15 +0,0 @@
const test = require('tape');
const anyBy = require('./anyBy.js');
test('Testing anyBy', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof anyBy === 'function', 'anyBy is a Function');
t.true(anyBy([4,1,0,3], x => x >= 1), 'Returns true with predicate function');
t.false(anyBy([0,1], x => x < 0), 'Returns false with a predicate function');
//t.deepEqual(anyBy(args..), 'Expected');
//t.equal(anyBy(args..), 'Expected');
//t.false(anyBy(args..), 'Expected');
//t.throws(anyBy(args..), 'Expected');
t.end();
});

View File

@ -0,0 +1,9 @@
const mostPerformant = (fns, iterations = 10000) => {
const times = fns.map(fn => {
const before = performance.now();
for (let i = 0; i < iterations; i++) fn();
return performance.now() - before;
});
return times.indexOf(Math.min(...times));
};
module.exports = mostPerformant;

View File

@ -0,0 +1,14 @@
const test = require('tape');
const mostPerformant = require('./mostPerformant.js');
test('Testing mostPerformant', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof mostPerformant === 'function', 'mostPerformant is a Function');
t.pass('Tested by @chalarangelo on 16/02/2018');
//t.deepEqual(mostPerformant(args..), 'Expected');
//t.equal(mostPerformant(args..), 'Expected');
//t.false(mostPerformant(args..), 'Expected');
//t.throws(mostPerformant(args..), 'Expected');
t.end();
});

View File

@ -1,2 +1,2 @@
const none = arr => !arr.some(Boolean); const none = (arr, fn = Boolean) => !arr.some(fn);
module.exports = none; module.exports = none;

View File

@ -7,6 +7,8 @@ test('Testing none', (t) => {
t.true(typeof none === 'function', 'none is a Function'); t.true(typeof none === 'function', 'none is a Function');
t.true(none([0,undefined,NaN,null,'']), 'Returns true for arrays with no truthy values'); t.true(none([0,undefined,NaN,null,'']), 'Returns true for arrays with no truthy values');
t.false(none([0,1]), 'Returns false for arrays with at least one truthy value'); t.false(none([0,1]), 'Returns false for arrays with at least one truthy value');
t.true(none([4,1,0,3], x => x < 0), 'Returns true with a predicate function');
t.false(none([0,1,2], x => x === 1), 'Returns false with predicate function');
//t.deepEqual(none(args..), 'Expected'); //t.deepEqual(none(args..), 'Expected');
//t.equal(none(args..), 'Expected'); //t.equal(none(args..), 'Expected');
//t.false(none(args..), 'Expected'); //t.false(none(args..), 'Expected');

View File

@ -1,2 +0,0 @@
const noneBy = (arr, fn) => !arr.some(fn);
module.exports = noneBy;

View File

@ -1,15 +0,0 @@
const test = require('tape');
const noneBy = require('./noneBy.js');
test('Testing noneBy', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof noneBy === 'function', 'noneBy is a Function');
t.true(noneBy([4,1,0,3], x => x < 0), 'Returns true with a predicate function');
t.false(noneBy([0,1,2], x => x === 1), 'Returns false with predicate function');
//t.deepEqual(noneBy(args..), 'Expected');
//t.equal(noneBy(args..), 'Expected');
//t.false(noneBy(args..), 'Expected');
//t.throws(noneBy(args..), 'Expected');
t.end();
});

View File

@ -1,11 +1,9 @@
Test log for: Wed Feb 14 2018 11:45:44 GMT+0200 (GTB Standard Time) Test log for: Fri Feb 16 2018 13:42:15 GMT+0200 (GTB Standard Time)
Test log for: Wed Feb 14 2018 12:46:59 GMT+0200 (GTB Standard Time)
> 30-seconds-of-code@0.0.1 test G:\My Files\git Repositories\30-seconds-of-code > 30-seconds-of-code@0.0.1 test G:\My Files\git Repositories\30-seconds-of-code
> tape test/**/*.test.js | tap-spec > tape test/**/*.test.js | tap-spec
Testing all Testing all
√ all is a Function √ all is a Function
@ -15,10 +13,6 @@ Test log for: Wed Feb 14 2018 12:46:59 GMT+0200 (GTB Standard Time)
√ Returns false for arrays with undefined √ Returns false for arrays with undefined
√ Returns false for arrays with null √ Returns false for arrays with null
√ Returns false for arrays with empty strings √ Returns false for arrays with empty strings
Testing allBy
√ allBy is a Function
√ Returns true with predicate function √ Returns true with predicate function
√ Returns false with a predicate function √ Returns false with a predicate function
@ -35,20 +29,9 @@ Test log for: Wed Feb 14 2018 12:46:59 GMT+0200 (GTB Standard Time)
√ Returns true for arrays with at least one truthy value √ Returns true for arrays with at least one truthy value
√ Returns false for arrays with no truthy values √ Returns false for arrays with no truthy values
√ Returns false for arrays with no truthy values √ Returns false for arrays with no truthy values
Testing anyBy
√ anyBy is a Function
√ Returns true with predicate function √ Returns true with predicate function
√ Returns false with a predicate function √ Returns false with a predicate function
Testing anagrams
√ anagrams is a Function
√ Generates all anagrams of a string
√ Works for single-letter strings
√ Works for empty strings
Testing approximatelyEqual Testing approximatelyEqual
√ approximatelyEqual is a Function √ approximatelyEqual is a Function
@ -1089,6 +1072,10 @@ Test log for: Wed Feb 14 2018 12:46:59 GMT+0200 (GTB Standard Time)
√ Returns the n minimum elements from the provided array √ Returns the n minimum elements from the provided array
√ Returns the n minimum elements from the provided array √ Returns the n minimum elements from the provided array
Testing mostPerformant
√ mostPerformant is a Function
Testing negate Testing negate
√ negate is a Function √ negate is a Function
@ -1099,10 +1086,6 @@ Test log for: Wed Feb 14 2018 12:46:59 GMT+0200 (GTB Standard Time)
√ none is a Function √ none is a Function
√ Returns true for arrays with no truthy values √ Returns true for arrays with no truthy values
√ Returns false for arrays with at least one truthy value √ Returns false for arrays with at least one truthy value
Testing noneBy
√ noneBy is a Function
√ Returns true with a predicate function √ Returns true with a predicate function
√ Returns false with predicate function √ Returns false with predicate function
@ -1892,15 +1875,13 @@ Test log for: Wed Feb 14 2018 12:46:59 GMT+0200 (GTB Standard Time)
√ zipWith is a Function √ zipWith is a Function
√ Runs the function provided √ Runs the function provided
√ Sends a GET request √ Sends a GET request
√ Sends a GET request
√ Runs the function provided
√ Runs promises in series √ Runs promises in series
√ Sends a POST request √ Sends a POST request
√ Works with multiple promises √ Works with multiple promises
total: 924 total: 945
passing: 924 passing: 945
duration: 2.5s duration: 4.9s