This commit is contained in:
Rohit Tanwar
2018-02-16 14:10:25 +05:30
54 changed files with 1390 additions and 173 deletions

2
test/all/all.js Normal file
View File

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

19
test/all/all.test.js Normal file
View File

@ -0,0 +1,19 @@
const test = require('tape');
const all = require('./all.js');
test('Testing all', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof all === 'function', 'all is a Function');
t.true(all([4,1,2,3]), 'Returns true for arrays with no falsey values');
t.false(all([0,1]), 'Returns false for arrays with 0');
t.false(all([NaN,1]), 'Returns false for arrays with NaN');
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(['',1]), 'Returns false for arrays with empty strings');
//t.deepEqual(all(args..), 'Expected');
//t.equal(all(args..), 'Expected');
//t.false(all(args..), 'Expected');
//t.throws(all(args..), 'Expected');
t.end();
});

2
test/allBy/allBy.js Normal file
View File

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

15
test/allBy/allBy.test.js Normal file
View File

@ -0,0 +1,15 @@
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();
});

2
test/any/any.js Normal file
View File

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

16
test/any/any.test.js Normal file
View File

@ -0,0 +1,16 @@
const test = require('tape');
const any = require('./any.js');
test('Testing any', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof any === 'function', 'any is a Function');
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([NaN,0,undefined,null,'']), 'Returns false for arrays with no truthy values');
//t.deepEqual(any(args..), 'Expected');
//t.equal(any(args..), 'Expected');
//t.false(any(args..), 'Expected');
//t.throws(any(args..), 'Expected');
t.end();
});

2
test/anyBy/anyBy.js Normal file
View File

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

15
test/anyBy/anyBy.test.js Normal file
View File

@ -0,0 +1,15 @@
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,2 @@
const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon;
module.exports = approximatelyEqual;

View File

@ -0,0 +1,17 @@
const test = require('tape');
const approximatelyEqual = require('./approximatelyEqual.js');
test('Testing approximatelyEqual', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof approximatelyEqual === 'function', 'approximatelyEqual is a Function');
t.true(approximatelyEqual(Math.PI / 2.0 , 1.5708), 'Works for PI / 2');
t.true(approximatelyEqual(0.1 + 0.2, 0.3), 'Works for 0.1 + 0.2 === 0.3');
t.true(approximatelyEqual(0.5, 0.5), 'Works for exactly equal values');
t.true(approximatelyEqual(0.501, 0.5, 0.1), 'Works for a custom epsilon');
//t.deepEqual(approximatelyEqual(args..), 'Expected');
//t.equal(approximatelyEqual(args..), 'Expected');
//t.false(approximatelyEqual(args..), 'Expected');
//t.throws(approximatelyEqual(args..), 'Expected');
t.end();
});

View File

@ -0,0 +1,3 @@
const bifurcate = (arr, filter) =>
arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]);
module.exports = bifurcate;

View File

@ -0,0 +1,14 @@
const test = require('tape');
const bifurcate = require('./bifurcate.js');
test('Testing bifurcate', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof bifurcate === 'function', 'bifurcate is a Function');
t.deepEqual(bifurcate([ 'beep', 'boop', 'foo', 'bar' ], [ true, true, false, true ]), [ ['beep', 'boop', 'bar'], ['foo'] ], 'Splits the collection into two groups');
//t.deepEqual(bifurcate(args..), 'Expected');
//t.equal(bifurcate(args..), 'Expected');
//t.false(bifurcate(args..), 'Expected');
//t.throws(bifurcate(args..), 'Expected');
t.end();
});

View File

@ -0,0 +1,3 @@
const bifurcateBy = (arr, fn) =>
arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [[], []]);
module.exports = bifurcateBy;

View File

@ -0,0 +1,14 @@
const test = require('tape');
const bifurcateBy = require('./bifurcateBy.js');
test('Testing bifurcateBy', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof bifurcateBy === 'function', 'bifurcateBy is a Function');
t.deepEqual(bifurcateBy([ 'beep', 'boop', 'foo', 'bar' ], x => x[0] === 'b'), [ ['beep', 'boop', 'bar'], ['foo'] ], 'Splits the collection into two groups');
//t.deepEqual(bifurcateBy(args..), 'Expected');
//t.equal(bifurcateBy(args..), 'Expected');
//t.false(bifurcateBy(args..), 'Expected');
//t.throws(bifurcateBy(args..), 'Expected');
t.end();
});

View File

@ -0,0 +1,11 @@
const binomialCoefficient = (n, k) => {
if (Number.isNaN(n) || Number.isNaN(k)) return NaN;
if (k < 0 || k > n) return 0;
if (k === 0 || k === n) return 1;
if (k === 1 || k === n - 1) return n;
if (n - k < k) k = n - k;
let res = n;
for (let j = 2; j <= k; j++) res *= (n - j + 1) / j;
return Math.round(res);
};
module.exports = binomialCoefficient;

View File

@ -0,0 +1,18 @@
const test = require('tape');
const binomialCoefficient = require('./binomialCoefficient.js');
test('Testing binomialCoefficient', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof binomialCoefficient === 'function', 'binomialCoefficient is a Function');
t.equal(binomialCoefficient(8, 2), 28, 'Returns the appropriate value');
t.equal(binomialCoefficient(0, 0), 1, 'Returns the appropriate value');
t.equal(binomialCoefficient(5, 3), 10, 'Returns the appropriate value');
t.true(Number.isNaN(binomialCoefficient(NaN, 3)), 'Returns NaN');
t.true(Number.isNaN(binomialCoefficient(5, NaN)), 'Returns NaN');
//t.deepEqual(binomialCoefficient(args..), 'Expected');
//t.equal(binomialCoefficient(args..), 'Expected');
//t.false(binomialCoefficient(args..), 'Expected');
//t.throws(binomialCoefficient(args..), 'Expected');
t.end();
});

View File

@ -1,10 +1,8 @@
const debounce = (fn, wait = 0) => {
let inDebounce;
return function() {
const context = this,
args = arguments;
clearTimeout(inDebounce);
inDebounce = setTimeout(() => fn.apply(context, args), wait);
const debounce = (fn, ms = 0) => {
let timeoutId;
return function(...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => fn.apply(this, args), ms);
};
};
module.exports = debounce;

View File

@ -0,0 +1,2 @@
const degreesToRads = deg => deg * Math.PI / 180.0;
module.exports = degreesToRads;

View File

@ -0,0 +1,15 @@
const test = require('tape');
const degreesToRads = require('./degreesToRads.js');
test('Testing degreesToRads', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
const approxeq = (v1,v2, diff = 0.001) => Math.abs(v1 - v2) < diff; // Use to account for rounding errors
t.true(typeof degreesToRads === 'function', 'degreesToRads is a Function');
t.true(approxeq(degreesToRads(90.0), Math.PI / 2), 'Returns the appropriate value');
//t.deepEqual(degreesToRads(args..), 'Expected');
//t.equal(degreesToRads(args..), 'Expected');
//t.false(degreesToRads(args..), 'Expected');
//t.throws(degreesToRads(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,13 @@
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.deepEqual(mostPerformant(args..), 'Expected');
//t.equal(mostPerformant(args..), 'Expected');
//t.false(mostPerformant(args..), 'Expected');
//t.throws(mostPerformant(args..), 'Expected');
t.end();
});

2
test/none/none.js Normal file
View File

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

15
test/none/none.test.js Normal file
View File

@ -0,0 +1,15 @@
const test = require('tape');
const none = require('./none.js');
test('Testing none', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
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.false(none([0,1]), 'Returns false for arrays with at least one truthy value');
//t.deepEqual(none(args..), 'Expected');
//t.equal(none(args..), 'Expected');
//t.false(none(args..), 'Expected');
//t.throws(none(args..), 'Expected');
t.end();
});

2
test/noneBy/noneBy.js Normal file
View File

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

View File

@ -0,0 +1,15 @@
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

@ -0,0 +1,2 @@
const radsToDegrees = rad => rad * 180.0 / Math.PI;
module.exports = radsToDegrees;

View File

@ -0,0 +1,14 @@
const test = require('tape');
const radsToDegrees = require('./radsToDegrees.js');
test('Testing radsToDegrees', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof radsToDegrees === 'function', 'radsToDegrees is a Function');
t.equal(radsToDegrees(Math.PI / 2), 90, 'Returns the appropriate value');
//t.deepEqual(radsToDegrees(args..), 'Expected');
//t.equal(radsToDegrees(args..), 'Expected');
//t.false(radsToDegrees(args..), 'Expected');
//t.throws(radsToDegrees(args..), 'Expected');
t.end();
});

View File

@ -1,4 +1,4 @@
Test log for: Sun Feb 11 2018 20:20:34 GMT+0000 (UTC)
Test log for: Thu Feb 15 2018 20:32:12 GMT+0000 (UTC)
> 30-seconds-of-code@0.0.1 test /home/travis/build/Chalarangelo/30-seconds-of-code
> tape test/**/*.test.js | tap-spec
@ -35,6 +35,22 @@ Test log for: Sun Feb 11 2018 20:20:34 GMT+0000 (UTC)
✔ Contains dashes in the proper places
✔ Only contains hexadecimal digits
Testing all
✔ all is a Function
✔ Returns true for arrays with no falsey values
✔ Returns false for arrays with 0
✔ Returns false for arrays with NaN
✔ Returns false for arrays with undefined
✔ Returns false for arrays with null
✔ Returns false for arrays with empty strings
Testing allBy
✔ allBy is a Function
✔ Returns true with predicate function
✔ Returns false with a predicate function
Testing anagrams
✔ anagrams is a Function
@ -42,6 +58,27 @@ Test log for: Sun Feb 11 2018 20:20:34 GMT+0000 (UTC)
✔ Works for single-letter strings
✔ Works for empty strings
Testing any
✔ any is a Function
✔ 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
Testing anyBy
✔ anyBy is a Function
✔ Returns true with predicate function
✔ Returns false with a predicate function
Testing approximatelyEqual
✔ approximatelyEqual is a Function
✔ Works for PI / 2
✔ Works for 0.1 + 0.2 === 0.3
✔ Works for exactly equal values
✔ Works for a custom epsilon
Testing arrayToHtmlList
✔ arrayToHtmlList is a Function
@ -84,6 +121,16 @@ Test log for: Sun Feb 11 2018 20:20:34 GMT+0000 (UTC)
✔ Produces the right result with a function
✔ Produces the right result with a property name
Testing bifurcate
✔ bifurcate is a Function
✔ Splits the collection into two groups
Testing bifurcateBy
✔ bifurcateBy is a Function
✔ Splits the collection into two groups
Testing binarySearch
✔ binarySearch is a Function
@ -107,6 +154,15 @@ Test log for: Sun Feb 11 2018 20:20:34 GMT+0000 (UTC)
✔ bindKey is a Function
✔ Binds function to an object context
Testing binomialCoefficient
✔ binomialCoefficient is a Function
✔ Returns the appropriate value
✔ Returns the appropriate value
✔ Returns the appropriate value
✔ Returns NaN
✔ Returns NaN
Testing bottomVisible
✔ bottomVisible is a Function
@ -299,6 +355,11 @@ Test log for: Sun Feb 11 2018 20:20:34 GMT+0000 (UTC)
✔ defer is a Function
Testing degreesToRads
✔ degreesToRads is a Function
✔ Returns the appropriate value
Testing delay
✔ delay is a Function
@ -1041,11 +1102,27 @@ Test log for: Sun Feb 11 2018 20:20:34 GMT+0000 (UTC)
✔ 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
✔ negate is a Function
✔ Negates a predicate function
Testing none
✔ none is a Function
✔ Returns true for arrays with no truthy values
✔ Returns false for arrays with at least one truthy value
Testing noneBy
✔ noneBy is a Function
✔ Returns true with a predicate function
✔ Returns false with predicate function
Testing nthArg
✔ nthArg is a Function
@ -1233,6 +1310,11 @@ Test log for: Sun Feb 11 2018 20:20:34 GMT+0000 (UTC)
✔ quickSort(undefined) throws an error
✔ quickSort([11, 1, 324, 23232, -1, 53, 2, 524, 32, 13, 156, 133, 62, 12, 4]) takes less than 2s to run
Testing radsToDegrees
✔ radsToDegrees is a Function
✔ Returns the appropriate value
Testing randomHexColorCode
✔ randomHexColorCode is a Function
@ -1631,6 +1713,13 @@ Test log for: Sun Feb 11 2018 20:20:34 GMT+0000 (UTC)
✔ unary is a Function
✔ Discards arguments after the first one
Testing uncurry
✔ uncurry is a Function
✔ Works without a provided value for n
✔ Works without n = 2
✔ Works withoutn = 3
Testing unescapeHTML
✔ unescapeHTML is a Function
@ -1796,15 +1885,15 @@ Test log for: Sun Feb 11 2018 20:20:34 GMT+0000 (UTC)
Testing zipWith
✔ zipWith is a Function
✔ Sends a GET request
✔ Runs the function provided
✔ Sends a GET request
✔ Sends a POST request
✔ Runs promises in series
✔ Works with multiple promises
total: 901
passing: 901
duration: 2.4s
total: 948
passing: 948
duration: 2.7s

6
test/uncurry/uncurry.js Normal file
View File

@ -0,0 +1,6 @@
const uncurry = (fn, n = 1) => (...args) => {
const next = acc => args => args.reduce((x, y) => x(y), acc);
if (n > args.length) throw new RangeError('Arguments too few!');
return next(fn)(args.slice(0, n));
};
module.exports = uncurry;

View File

@ -0,0 +1,20 @@
const test = require('tape');
const uncurry = require('./uncurry.js');
test('Testing uncurry', (t) => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
t.true(typeof uncurry === 'function', 'uncurry is a Function');
const add = x => y => z => x + y + z;
const add1 = uncurry(add);
const add2 = uncurry(add, 2);
const add3 = uncurry(add, 3);
t.equal(add1(1)(2)(3), 6, 'Works without a provided value for n');
t.equal(add2(1,2)(3), 6, 'Works without n = 2');
t.equal(add3(1,2,3), 6, 'Works withoutn = 3');
//t.deepEqual(uncurry(args..), 'Expected');
//t.equal(uncurry(args..), 'Expected');
//t.false(uncurry(args..), 'Expected');
//t.throws(uncurry(args..), 'Expected');
t.end();
});