Migrated tests to jest

Used jest-codemods to migrate, will have to pass everything by hand before we can merge.
This commit is contained in:
Angelos Chalaris
2018-06-18 14:18:25 +03:00
parent ae8cd7ee50
commit 5afe81452a
890 changed files with 6950 additions and 6921 deletions

View File

@ -0,0 +1,2 @@
const RGBToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
module.exports = RGBToHex;

View File

@ -0,0 +1,9 @@
const expect = require('expect');
const RGBToHex = require('./RGBToHex.js');
test('Testing RGBToHex', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof RGBToHex === 'function').toBeTruthy();
expect(RGBToHex(255, 165, 1)).toBe('ffa501');
});

5
test5/omitBy/omitBy.js Normal file
View File

@ -0,0 +1,5 @@
const omitBy = (obj, fn) =>
Object.keys(obj)
.filter(k => !fn(obj[k], k))
.reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
module.exports = omitBy;

View File

@ -0,0 +1,9 @@
const expect = require('expect');
const omitBy = require('./omitBy.js');
test('Testing omitBy', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof omitBy === 'function').toBeTruthy();
expect(omitBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'number')).toEqual({ b: '2' });
});

6
test5/on/on.js Normal file
View File

@ -0,0 +1,6 @@
const on = (el, evt, fn, opts = {}) => {
const delegatorFn = e => e.target.matches(opts.target) && fn.call(e.target, e);
el.addEventListener(evt, opts.target ? delegatorFn : fn, opts.options || false);
if (opts.target) return delegatorFn;
};
module.exports = on;

8
test5/on/on.test.js Normal file
View File

@ -0,0 +1,8 @@
const expect = require('expect');
const on = require('./on.js');
test('Testing on', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof on === 'function').toBeTruthy();
});

View File

@ -0,0 +1,15 @@
const onUserInputChange = callback => {
let type = 'mouse',
lastTime = 0;
const mousemoveHandler = () => {
const now = performance.now();
if (now - lastTime < 20)
(type = 'mouse'), callback(type), document.removeEventListener('mousemove', mousemoveHandler);
lastTime = now;
};
document.addEventListener('touchstart', () => {
if (type === 'touch') return;
(type = 'touch'), callback(type), document.addEventListener('mousemove', mousemoveHandler);
});
};
module.exports = onUserInputChange;

View File

@ -0,0 +1,8 @@
const expect = require('expect');
const onUserInputChange = require('./onUserInputChange.js');
test('Testing onUserInputChange', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof onUserInputChange === 'function').toBeTruthy();
});

9
test5/once/once.js Normal file
View File

@ -0,0 +1,9 @@
const once = fn => {
let called = false;
return function(...args) {
if (called) return;
called = true;
return fn.apply(this, args);
};
};
module.exports = once;

8
test5/once/once.test.js Normal file
View File

@ -0,0 +1,8 @@
const expect = require('expect');
const once = require('./once.js');
test('Testing once', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof once === 'function').toBeTruthy();
});

11
test5/orderBy/orderBy.js Normal file
View File

@ -0,0 +1,11 @@
const orderBy = (arr, props, orders) =>
[...arr].sort((a, b) =>
props.reduce((acc, prop, i) => {
if (acc === 0) {
const [p1, p2] = orders && orders[i] === 'desc' ? [b[prop], a[prop]] : [a[prop], b[prop]];
acc = p1 > p2 ? 1 : p1 < p2 ? -1 : 0;
}
return acc;
}, 0)
);
module.exports = orderBy;

View File

@ -0,0 +1,15 @@
const expect = require('expect');
const orderBy = require('./orderBy.js');
test('Testing orderBy', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof orderBy === 'function').toBeTruthy();
const users = [{ name: 'fred', age: 48 }, { name: 'barney', age: 36 }, { name: 'fred', age: 40 }];
expect(orderBy(users, ['name', 'age'], ['asc', 'desc'])).toEqual(
[{name: 'barney', age: 36}, {name: 'fred', age: 48}, {name: 'fred', age: 40}]
);
expect(orderBy(users, ['name', 'age'])).toEqual(
[{name: 'barney', age: 36}, {name: 'fred', age: 40}, {name: 'fred', age: 48}]
);
});

2
test5/over/over.js Normal file
View File

@ -0,0 +1,2 @@
const over = (...fns) => (...args) => fns.map(fn => fn.apply(null, args));
module.exports = over;

10
test5/over/over.test.js Normal file
View File

@ -0,0 +1,10 @@
const expect = require('expect');
const over = require('./over.js');
test('Testing over', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof over === 'function').toBeTruthy();
const minMax = over(Math.min, Math.max);
expect(minMax(1, 2, 3, 4, 5)).toEqual([1,5]);
});

View File

@ -0,0 +1,2 @@
const overArgs = (fn, transforms) => (...args) => fn(...args.map((val, i) => transforms[i](val)));
module.exports = overArgs;

View File

@ -0,0 +1,12 @@
const expect = require('expect');
const overArgs = require('./overArgs.js');
test('Testing overArgs', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof overArgs === 'function').toBeTruthy();
const square = n => n * n;
const double = n => n * 2;
const fn = overArgs((x, y) => [x, y], [square, double]);
expect(fn(9, 3)).toEqual([81, 6]);
});

3
test5/pad/pad.js Normal file
View File

@ -0,0 +1,3 @@
const pad = (str, length, char = ' ') =>
str.padStart((str.length + length) / 2, char).padEnd(length, char);
module.exports = pad;

12
test5/pad/pad.test.js Normal file
View File

@ -0,0 +1,12 @@
const expect = require('expect');
const pad = require('./pad.js');
test('Testing pad', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof pad === 'function').toBeTruthy();
expect(pad('cat',8)).toBe(' cat ');
expect(pad('cat',8).length).toBe(8);
expect(pad(String(42), 6, '0')).toBe('004200');
expect(pad('foobar', 3)).toBe('foobar');
});

View File

@ -0,0 +1,5 @@
const palindrome = str => {
const s = str.toLowerCase().replace(/[\W_]/g, '');
return s === [...s].reverse().join('');
};
module.exports = palindrome;

View File

@ -0,0 +1,10 @@
const expect = require('expect');
const palindrome = require('./palindrome.js');
test('Testing palindrome', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof palindrome === 'function').toBeTruthy();
expect(palindrome('taco cat')).toBe(true);
expect(palindrome('foobar')).toBe(false);
});

View File

@ -0,0 +1,9 @@
const parseCookie = str =>
str
.split(';')
.map(v => v.split('='))
.reduce((acc, v) => {
acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
return acc;
}, {});
module.exports = parseCookie;

View File

@ -0,0 +1,9 @@
const expect = require('expect');
const parseCookie = require('./parseCookie.js');
test('Testing parseCookie', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof parseCookie === 'function').toBeTruthy();
expect(parseCookie('foo=bar; equation=E%3Dmc%5E2')).toEqual({ foo: 'bar', equation: 'E=mc^2' });
});

2
test5/partial/partial.js Normal file
View File

@ -0,0 +1,2 @@
const partial = (fn, ...partials) => (...args) => fn(...partials, ...args);
module.exports = partial;

View File

@ -0,0 +1,13 @@
const expect = require('expect');
const partial = require('./partial.js');
test('Testing partial', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof partial === 'function').toBeTruthy();
function greet(greeting, name) {
return greeting + ' ' + name + '!';
}
const greetHello = partial(greet, 'Hello');
expect(greetHello('John')).toBe('Hello John!');
});

View File

@ -0,0 +1,2 @@
const partialRight = (fn, ...partials) => (...args) => fn(...args, ...partials);
module.exports = partialRight;

View File

@ -0,0 +1,13 @@
const expect = require('expect');
const partialRight = require('./partialRight.js');
test('Testing partialRight', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof partialRight === 'function').toBeTruthy();
function greet(greeting, name) {
return greeting + ' ' + name + '!';
}
const greetJohn = partialRight(greet, 'John');
expect(greetJohn('Hello')).toBe('Hello John!');
});

View File

@ -0,0 +1,9 @@
const partition = (arr, fn) =>
arr.reduce(
(acc, val, i, arr) => {
acc[fn(val, i, arr) ? 0 : 1].push(val);
return acc;
},
[[], []]
);
module.exports = partition;

View File

@ -0,0 +1,12 @@
const expect = require('expect');
const partition = require('./partition.js');
test('Testing partition', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof partition === 'function').toBeTruthy();
const users = [{ user: 'barney', age: 36, active: false }, { user: 'fred', age: 40, active: true }];
expect(partition(users, o => o.active)).toEqual(
[[{ 'user': 'fred', 'age': 40, 'active': true }],[{ 'user': 'barney', 'age': 36, 'active': false }]]
);
});

View File

@ -0,0 +1,3 @@
const percentile = (arr, val) =>
100 * arr.reduce((acc, v) => acc + (v < val ? 1 : 0) + (v === val ? 0.5 : 0), 0) / arr.length;
module.exports = percentile;

View File

@ -0,0 +1,9 @@
const expect = require('expect');
const percentile = require('./percentile.js');
test('Testing percentile', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof percentile === 'function').toBeTruthy();
expect(percentile([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 6)).toBe(55);
});

View File

@ -0,0 +1,11 @@
const permutations = arr => {
if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr;
return arr.reduce(
(acc, item, i) =>
acc.concat(
permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [item, ...val])
),
[]
);
};
module.exports = permutations;

View File

@ -0,0 +1,11 @@
const expect = require('expect');
const permutations = require('./permutations.js');
test('Testing permutations', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof permutations === 'function').toBeTruthy();
expect(permutations([1, 33, 5])).toEqual(
[ [ 1, 33, 5 ], [ 1, 5, 33 ], [ 33, 1, 5 ], [ 33, 5, 1 ], [ 5, 1, 33 ], [ 5, 33, 1 ] ]
);
});

3
test5/pick/pick.js Normal file
View File

@ -0,0 +1,3 @@
const pick = (obj, arr) =>
arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
module.exports = pick;

9
test5/pick/pick.test.js Normal file
View File

@ -0,0 +1,9 @@
const expect = require('expect');
const pick = require('./pick.js');
test('Testing pick', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof pick === 'function').toBeTruthy();
expect(pick({ a: 1, b: '2', c: 3 }, ['a', 'c'])).toEqual({ 'a': 1, 'c': 3 });
});

5
test5/pickBy/pickBy.js Normal file
View File

@ -0,0 +1,5 @@
const pickBy = (obj, fn) =>
Object.keys(obj)
.filter(k => fn(obj[k], k))
.reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
module.exports = pickBy;

View File

@ -0,0 +1,9 @@
const expect = require('expect');
const pickBy = require('./pickBy.js');
test('Testing pickBy', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof pickBy === 'function').toBeTruthy();
expect(pickBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'number')).toEqual({ 'a': 1, 'c': 3 });
});

View File

@ -0,0 +1,2 @@
const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Promise.resolve(arg));
module.exports = pipeAsyncFunctions;

View File

@ -0,0 +1,19 @@
const expect = require('expect');
const pipeAsyncFunctions = require('./pipeAsyncFunctions.js');
test('Testing pipeAsyncFunctions', async () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof pipeAsyncFunctions === 'function').toBeTruthy();
//t.deepEqual(pipeAsyncFunctions(args..), 'Expected');
//t.equal(pipeAsyncFunctions(args..), 'Expected');
//t.false(pipeAsyncFunctions(args..), 'Expected');
//t.throws(pipeAsyncFunctions(args..), 'Expected');
expect(await pipeAsyncFunctions(
(x) => x + 1,
(x) => new Promise((resolve) => setTimeout(() => resolve(x + 2), 0)),
(x) => x + 3,
async (x) => await x + 4,
)
(5)).toBe(15);
});

View File

@ -0,0 +1,2 @@
const pipeFunctions = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));
module.exports = pipeFunctions;

View File

@ -0,0 +1,12 @@
const expect = require('expect');
const pipeFunctions = require('./pipeFunctions.js');
test('Testing pipeFunctions', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof pipeFunctions === 'function').toBeTruthy();
const add5 = x => x + 5;
const multiply = (x, y) => x * y;
const multiplyAndAdd5 = pipeFunctions(multiply, add5);
expect(multiplyAndAdd5(5, 2)).toBe(15);
});

View File

@ -0,0 +1,7 @@
const pluralize = (val, word, plural = word + 's') => {
const _pluralize = (num, word, plural = word + 's') =>
[1, -1].includes(Number(num)) ? word : plural;
if (typeof val === 'object') return (num, word) => _pluralize(num, word, val[word]);
return _pluralize(val, word, plural);
};
module.exports = pluralize;

View File

@ -0,0 +1,18 @@
const expect = require('expect');
const pluralize = require('./pluralize.js');
test('Testing pluralize', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof pluralize === 'function').toBeTruthy();
expect(pluralize(0, 'apple')).toBe('apples');
expect(pluralize(1, 'apple')).toBe('apple');
expect(pluralize(2, 'apple')).toBe('apples');
expect(pluralize(2, 'person', 'people')).toBe('people');
const PLURALS = {
person: 'people',
radius: 'radii'
};
const autoPluralize = pluralize(PLURALS);
expect(autoPluralize(2, 'person')).toBe('people');
});

View File

@ -0,0 +1,2 @@
const powerset = arr => arr.reduce((a, v) => a.concat(a.map(r => [v].concat(r))), [[]]);
module.exports = powerset;

View File

@ -0,0 +1,9 @@
const expect = require('expect');
const powerset = require('./powerset.js');
test('Testing powerset', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof powerset === 'function').toBeTruthy();
expect(powerset([1, 2])).toEqual([[], [1], [2], [2,1]]);
});

9
test5/prefix/prefix.js Normal file
View File

@ -0,0 +1,9 @@
const prefix = prop => {
const capitalizedProp = prop.charAt(0).toUpperCase() + prop.slice(1);
const prefixes = ['', 'webkit', 'moz', 'ms', 'o'];
const i = prefixes.findIndex(
prefix => typeof document.body.style[prefix ? prefix + capitalizedProp : prop] !== 'undefined'
);
return i !== -1 ? (i === 0 ? prop : prefixes[i] + capitalizedProp) : null;
};
module.exports = prefix;

View File

@ -0,0 +1,8 @@
const expect = require('expect');
const prefix = require('./prefix.js');
test('Testing prefix', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof prefix === 'function').toBeTruthy();
});

View File

@ -0,0 +1,8 @@
const prettyBytes = (num, precision = 3, addSpace = true) => {
const UNITS = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
if (Math.abs(num) < 1) return num + (addSpace ? ' ' : '') + UNITS[0];
const exponent = Math.min(Math.floor(Math.log10(num < 0 ? -num : num) / 3), UNITS.length - 1);
const n = Number(((num < 0 ? -num : num) / 1000 ** exponent).toPrecision(precision));
return (num < 0 ? '-' : '') + n + (addSpace ? ' ' : '') + UNITS[exponent];
};
module.exports = prettyBytes;

View File

@ -0,0 +1,11 @@
const expect = require('expect');
const prettyBytes = require('./prettyBytes.js');
test('Testing prettyBytes', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof prettyBytes === 'function').toBeTruthy();
expect(prettyBytes(1000)).toBe('1 KB');
expect(prettyBytes(-27145424323.5821, 5)).toBe('-27.145 GB');
expect(prettyBytes(123456789, 3, false)).toBe('123MB');
});

8
test5/primes/primes.js Normal file
View File

@ -0,0 +1,8 @@
const primes = num => {
let arr = Array.from({ length: num - 1 }).map((x, i) => i + 2),
sqroot = Math.floor(Math.sqrt(num)),
numsTillSqroot = Array.from({ length: sqroot - 1 }).map((x, i) => i + 2);
numsTillSqroot.forEach(x => (arr = arr.filter(y => y % x !== 0 || y === x)));
return arr;
};
module.exports = primes;

View File

@ -0,0 +1,9 @@
const expect = require('expect');
const primes = require('./primes.js');
test('Testing primes', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof primes === 'function').toBeTruthy();
expect(primes(10)).toEqual([2, 3, 5, 7]);
});

View File

@ -0,0 +1,5 @@
const promisify = func => (...args) =>
new Promise((resolve, reject) =>
func(...args, (err, result) => (err ? reject(err) : resolve(result)))
);
module.exports = promisify;

View File

@ -0,0 +1,12 @@
const expect = require('expect');
const promisify = require('./promisify.js');
test('Testing promisify', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof promisify === 'function').toBeTruthy();
const x = promisify(Math.max);
expect(x() instanceof Promise).toBeTruthy();
const delay = promisify((d, cb) => setTimeout(cb, d));
delay(200).then(() => );
});

7
test5/pull/pull.js Normal file
View File

@ -0,0 +1,7 @@
const pull = (arr, ...args) => {
let argState = Array.isArray(args[0]) ? args[0] : args;
let pulled = arr.filter((v, i) => !argState.includes(v));
arr.length = 0;
pulled.forEach(v => arr.push(v));
};
module.exports = pull;

11
test5/pull/pull.test.js Normal file
View File

@ -0,0 +1,11 @@
const expect = require('expect');
const pull = require('./pull.js');
test('Testing pull', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof pull === 'function').toBeTruthy();
let myArray = ['a', 'b', 'c', 'a', 'b', 'c'];
pull(myArray, 'a', 'c');
expect(myArray).toEqual(['b','b']);
});

View File

@ -0,0 +1,10 @@
const pullAtIndex = (arr, pullArr) => {
let removed = [];
let pulled = arr
.map((v, i) => (pullArr.includes(i) ? removed.push(v) : v))
.filter((v, i) => !pullArr.includes(i));
arr.length = 0;
pulled.forEach(v => arr.push(v));
return removed;
};
module.exports = pullAtIndex;

View File

@ -0,0 +1,12 @@
const expect = require('expect');
const pullAtIndex = require('./pullAtIndex.js');
test('Testing pullAtIndex', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof pullAtIndex === 'function').toBeTruthy();
let myArray = ['a', 'b', 'c', 'd'];
let pulled = pullAtIndex(myArray, [1, 3]);
expect(myArray).toEqual([ 'a', 'c' ]);
expect(pulled).toEqual([ 'b', 'd' ]);
});

View File

@ -0,0 +1,9 @@
const pullAtValue = (arr, pullArr) => {
let removed = [],
pushToRemove = arr.forEach((v, i) => (pullArr.includes(v) ? removed.push(v) : v)),
mutateTo = arr.filter((v, i) => !pullArr.includes(v));
arr.length = 0;
mutateTo.forEach(v => arr.push(v));
return removed;
};
module.exports = pullAtValue;

View File

@ -0,0 +1,12 @@
const expect = require('expect');
const pullAtValue = require('./pullAtValue.js');
test('Testing pullAtValue', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof pullAtValue === 'function').toBeTruthy();
let myArray = ['a', 'b', 'c', 'd'];
let pulled = pullAtValue(myArray, ['b', 'd']);
expect(myArray).toEqual([ 'a', 'c' ]);
expect(pulled).toEqual([ 'b', 'd' ]);
});

10
test5/pullBy/pullBy.js Normal file
View File

@ -0,0 +1,10 @@
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));
};
module.exports = pullBy;

View File

@ -0,0 +1,11 @@
const expect = require('expect');
const pullBy = require('./pullBy.js');
test('Testing pullBy', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof pullBy === 'function').toBeTruthy();
var myArray = [{ x: 1 }, { x: 2 }, { x: 3 }, { x: 1 }];
pullBy(myArray, [{ x: 1 }, { x: 3 }], o => o.x);
expect(myArray).toEqual([{ x: 2 }]);
});

View File

@ -0,0 +1,9 @@
const quickSort = ([n, ...nums], desc) =>
isNaN(n)
? []
: [
...quickSort(nums.filter(v => (desc ? v > n : v <= n)), desc),
n,
...quickSort(nums.filter(v => (!desc ? v > n : v <= n)), desc)
];
module.exports = quickSort;

View File

@ -0,0 +1,20 @@
const expect = require('expect');
const quickSort = require('./quickSort.js');
test('Testing quickSort', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof quickSort === 'function').toBeTruthy();
expect(quickSort([5, 6, 4, 3, 1, 2])).toEqual([1, 2, 3, 4, 5, 6]);
expect(quickSort([-1, 0, -2])).toEqual([-2, -1, 0]);
expect(() => quickSort()).toThrow();
expect(() => quickSort(123)).toThrow();
expect(() => quickSort({ 234: string})).toThrow();
expect(() => quickSort(null)).toThrow();
expect(() => quickSort(undefined)).toThrow();
let start = new Date().getTime();
quickSort([11, 1, 324, 23232, -1, 53, 2, 524, 32, 13, 156, 133, 62, 12, 4]);
let end = new Date().getTime();
expect((end - start) < 2000).toBeTruthy();
});

View File

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

View File

@ -0,0 +1,9 @@
const expect = require('expect');
const radsToDegrees = require('./radsToDegrees.js');
test('Testing radsToDegrees', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof radsToDegrees === 'function').toBeTruthy();
expect(radsToDegrees(Math.PI / 2)).toBe(90);
});

View File

@ -0,0 +1,5 @@
const randomHexColorCode = () => {
let n = (Math.random() * 0xfffff * 1000000).toString(16);
return '#' + n.slice(0, 6);
};
module.exports = randomHexColorCode;

View File

@ -0,0 +1,12 @@
const expect = require('expect');
const randomHexColorCode = require('./randomHexColorCode.js');
test('Testing randomHexColorCode', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof randomHexColorCode === 'function').toBeTruthy();
//t.deepEqual(randomHexColorCode(args..), 'Expected');
expect(randomHexColorCode().length).toBe(7);
expect(randomHexColorCode().startsWith('#')).toBeTruthy();
expect(randomHexColorCode().slice(1).match(/[^0123456789abcdef]/i) === null).toBeTruthy();
});

View File

@ -0,0 +1,3 @@
const randomIntArrayInRange = (min, max, n = 1) =>
Array.from({ length: n }, () => Math.floor(Math.random() * (max - min + 1)) + min);
module.exports = randomIntArrayInRange;

View File

@ -0,0 +1,14 @@
const expect = require('expect');
const randomIntArrayInRange = require('./randomIntArrayInRange.js');
test('Testing randomIntArrayInRange', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof randomIntArrayInRange === 'function').toBeTruthy();
const lowerLimit = Math.floor(Math.random() * 20);
const upperLimit = Math.floor(lowerLimit + Math.random() * 10);
const arr = randomIntArrayInRange(lowerLimit,upperLimit, 10);
expect(arr.every(x => typeof x === 'number')).toBeTruthy();
expect(arr.length).toBe(10);
expect(arr.every(x => (x >= lowerLimit) && (x <= upperLimit))).toBeTruthy();
});

View File

@ -0,0 +1,2 @@
const randomIntegerInRange = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
module.exports = randomIntegerInRange;

View File

@ -0,0 +1,13 @@
const expect = require('expect');
const randomIntegerInRange = require('./randomIntegerInRange.js');
test('Testing randomIntegerInRange', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof randomIntegerInRange === 'function').toBeTruthy();
const lowerLimit = Math.floor(Math.random() * 20);
const upperLimit = Math.floor(lowerLimit + Math.random() * 10);
expect(Number.isInteger(randomIntegerInRange(lowerLimit,upperLimit))).toBeTruthy();
const numberForTest = randomIntegerInRange(lowerLimit,upperLimit);
expect((numberForTest >= lowerLimit) && (numberForTest <= upperLimit)).toBeTruthy();
});

View File

@ -0,0 +1,2 @@
const randomNumberInRange = (min, max) => Math.random() * (max - min) + min;
module.exports = randomNumberInRange;

View File

@ -0,0 +1,13 @@
const expect = require('expect');
const randomNumberInRange = require('./randomNumberInRange.js');
test('Testing randomNumberInRange', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof randomNumberInRange === 'function').toBeTruthy();
const lowerLimit = Math.floor(Math.random() * 20);
const upperLimit = Math.floor(lowerLimit + Math.random() * 10);
expect(typeof randomNumberInRange(lowerLimit,upperLimit) === 'number').toBeTruthy();
const numberForTest = randomNumberInRange(lowerLimit,upperLimit);
expect((numberForTest >= lowerLimit) && (numberForTest <= upperLimit)).toBeTruthy();
});

View File

@ -0,0 +1,7 @@
const fs = require('fs');
const readFileLines = filename =>
fs
.readFileSync(filename)
.toString('UTF8')
.split('\n');
module.exports = readFileLines;

View File

@ -0,0 +1,8 @@
const expect = require('expect');
const readFileLines = require('./readFileLines.js');
test('Testing readFileLines', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof readFileLines === 'function').toBeTruthy();
});

8
test5/rearg/rearg.js Normal file
View File

@ -0,0 +1,8 @@
const rearg = (fn, indexes) => (...args) =>
fn(
...args.reduce(
(acc, val, i) => ((acc[indexes.indexOf(i)] = val), acc),
Array.from({ length: indexes.length })
)
);
module.exports = rearg;

15
test5/rearg/rearg.test.js Normal file
View File

@ -0,0 +1,15 @@
const expect = require('expect');
const rearg = require('./rearg.js');
test('Testing rearg', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof rearg === 'function').toBeTruthy();
var rearged = rearg(
function(a, b, c) {
return [a, b, c];
},
[2, 0, 1]
);
expect(rearged('b', 'c', 'a')).toEqual(['a', 'b', 'c']);
});

View File

@ -0,0 +1,21 @@
const recordAnimationFrames = (callback, autoStart = true) => {
let running = true,
raf;
const stop = () => {
running = false;
cancelAnimationFrame(raf);
};
const start = () => {
running = true;
run();
};
const run = () => {
raf = requestAnimationFrame(() => {
callback();
if (running) run();
});
};
if (autoStart) start();
return { start, stop };
};
module.exports = recordAnimationFrames;

View File

@ -0,0 +1,8 @@
const expect = require('expect');
const recordAnimationFrames = require('./recordAnimationFrames.js');
test('Testing recordAnimationFrames', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof recordAnimationFrames === 'function').toBeTruthy();
});

View File

@ -0,0 +1,3 @@
const redirect = (url, asLink = true) =>
asLink ? (window.location.href = url) : window.location.replace(url);
module.exports = redirect;

View File

@ -0,0 +1,8 @@
const expect = require('expect');
const redirect = require('./redirect.js');
test('Testing redirect', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof redirect === 'function').toBeTruthy();
});

View File

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

View File

@ -0,0 +1,9 @@
const expect = require('expect');
const reduceSuccessive = require('./reduceSuccessive.js');
test('Testing reduceSuccessive', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof reduceSuccessive === 'function').toBeTruthy();
expect(reduceSuccessive([1, 2, 3, 4, 5, 6], (acc, val) => acc + val, 0)).toEqual([0, 1, 3, 6, 10, 15, 21]);
});

View File

@ -0,0 +1,3 @@
const reduceWhich = (arr, comparator = (a, b) => a - b) =>
arr.reduce((a, b) => (comparator(a, b) >= 0 ? b : a));
module.exports = reduceWhich;

View File

@ -0,0 +1,14 @@
const expect = require('expect');
const reduceWhich = require('./reduceWhich.js');
test('Testing reduceWhich', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof reduceWhich === 'function').toBeTruthy();
expect(reduceWhich([1, 3, 2])).toBe(1);
expect(reduceWhich([1, 3, 2], (a, b) => b - a)).toBe(3);
expect(reduceWhich(
[{ name: 'Tom', age: 12 }, { name: 'Jack', age: 18 }, { name: 'Lucy', age: 9 }],
(a, b) => a.age - b.age
)).toEqual({name: "Lucy", age: 9});
});

View File

@ -0,0 +1,8 @@
const reducedFilter = (data, keys, fn) =>
data.filter(fn).map(el =>
keys.reduce((acc, key) => {
acc[key] = el[key];
return acc;
}, {})
);
module.exports = reducedFilter;

View File

@ -0,0 +1,21 @@
const expect = require('expect');
const reducedFilter = require('./reducedFilter.js');
test('Testing reducedFilter', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof reducedFilter === 'function').toBeTruthy();
const data = [
{
id: 1,
name: 'john',
age: 24
},
{
id: 2,
name: 'mike',
age: 50
}
];
expect(reducedFilter(data, ['id', 'name'], item => item.age > 24)).toEqual([{ id: 2, name: 'mike'}]);
});

2
test5/reject/reject.js Normal file
View File

@ -0,0 +1,2 @@
const reject = (pred, array) => array.filter((...args) => !pred(...args));
module.exports = reject;

View File

@ -0,0 +1,22 @@
const expect = require('expect');
const reject = require('./reject.js');
test('Testing reject', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof reject === 'function').toBeTruthy();
const noEvens = reject(
(x) => x % 2 === 0,
[1, 2, 3, 4, 5]
);
expect(noEvens).toEqual([1, 3, 5]);
const fourLettersOrLess = reject(
(word) => word.length > 4,
['Apple', 'Pear', 'Kiwi', 'Banana']
);
expect(fourLettersOrLess).toEqual(['Pear', 'Kiwi']);
});

8
test5/remove/remove.js Normal file
View File

@ -0,0 +1,8 @@
const remove = (arr, func) =>
Array.isArray(arr)
? arr.filter(func).reduce((acc, val) => {
arr.splice(arr.indexOf(val), 1);
return acc.concat(val);
}, [])
: [];
module.exports = remove;

View File

@ -0,0 +1,9 @@
const expect = require('expect');
const remove = require('./remove.js');
test('Testing remove', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof remove === 'function').toBeTruthy();
expect(remove([1, 2, 3, 4], n => n % 2 === 0)).toEqual([2, 4]);
});

View File

@ -0,0 +1,2 @@
const removeNonASCII = str => str.replace(/[^\x20-\x7E]/g, '');
module.exports = removeNonASCII;

View File

@ -0,0 +1,9 @@
const expect = require('expect');
const removeNonASCII = require('./removeNonASCII.js');
test('Testing removeNonASCII', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof removeNonASCII === 'function').toBeTruthy();
expect(removeNonASCII('äÄçÇéÉêlorem-ipsumöÖÐþúÚ')).toBe('lorem-ipsum');
});

View File

@ -0,0 +1,2 @@
const removeVowels = (str, repl = '') => str.replace(/[aeiou]/gi,repl);
module.exports = removeVowels;

View File

@ -0,0 +1,8 @@
const expect = require('expect');
const removeVowels = require('./removeVowels.js');
test('Testing removeVowels', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof removeVowels === 'function').toBeTruthy();
});

View File

@ -0,0 +1,9 @@
const renameKeys = (keysMap, obj) =>
Object.keys(obj).reduce(
(acc, key) => ({
...acc,
...{ [keysMap[key] || key]: obj[key] }
}),
{}
);
module.exports = renameKeys;

View File

@ -0,0 +1,13 @@
const expect = require('expect');
const renameKeys = require('./renameKeys.js');
test('Testing renameKeys', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof renameKeys === 'function').toBeTruthy();
const obj = { name: 'Bobo', job: 'Front-End Master', shoeSize: 100 };
const renamedObj = renameKeys({ name: 'firstName', job: 'passion' }, obj);
expect(renamedObj).toEqual({ firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 });
});

View File

@ -0,0 +1,2 @@
const reverseString = str => [...str].reverse().join('');
module.exports = reverseString;

View File

@ -0,0 +1,9 @@
const expect = require('expect');
const reverseString = require('./reverseString.js');
test('Testing reverseString', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof reverseString === 'function').toBeTruthy();
expect(reverseString('foobar')).toBe('raboof');
});

2
test5/round/round.js Normal file
View File

@ -0,0 +1,2 @@
const round = (n, decimals = 0) => Number(`${Math.round(`${n}e${decimals}`)}e-${decimals}`);
module.exports = round;

22
test5/round/round.test.js Normal file
View File

@ -0,0 +1,22 @@
const expect = require('expect');
const round = require('./round.js');
test('Testing round', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof round === 'function').toBeTruthy();
expect(round(1.005, 2)).toBe(1.01);
expect(round(123.3423345345345345344, 11)).toBe(123.34233453453);
expect(round(3.342, 11)).toBe(3.342);
expect(round(1.005)).toBe(1);
expect(isNaN(round([1.005, 2]))).toBeTruthy();
expect(isNaN(round('string'))).toBeTruthy();
expect(isNaN(round())).toBeTruthy();
expect(isNaN(round(132, 413, 4134))).toBeTruthy();
expect(isNaN(round({a: 132}, 413))).toBeTruthy();
let start = new Date().getTime();
round(123.3423345345345345344, 11);
let end = new Date().getTime();
expect((end - start) < 2000).toBeTruthy();
});