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 defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj);
module.exports = defaults;

View File

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

2
test2/defer/defer.js Normal file
View File

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

View File

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

View File

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

View File

@ -0,0 +1,10 @@
const expect = require('expect');
const degreesToRads = require('./degreesToRads.js');
test('Testing degreesToRads', () => {
//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
expect(typeof degreesToRads === 'function').toBeTruthy();
expect(approxeq(degreesToRads(90.0), Math.PI / 2)).toBeTruthy();
});

2
test2/delay/delay.js Normal file
View File

@ -0,0 +1,2 @@
const delay = (fn, wait, ...args) => setTimeout(fn, wait, ...args);
module.exports = delay;

15
test2/delay/delay.test.js Normal file
View File

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

View File

@ -0,0 +1,5 @@
const detectDeviceType = () =>
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
? 'Mobile'
: 'Desktop';
module.exports = detectDeviceType;

View File

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

View File

@ -0,0 +1,5 @@
const difference = (a, b) => {
const s = new Set(b);
return a.filter(x => !s.has(x));
};
module.exports = difference;

View File

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

View File

@ -0,0 +1,5 @@
const differenceBy = (a, b, fn) => {
const s = new Set(b.map(v => fn(v)));
return a.filter(x => !s.has(fn(x)));
};
module.exports = differenceBy;

View File

@ -0,0 +1,10 @@
const expect = require('expect');
const differenceBy = require('./differenceBy.js');
test('Testing differenceBy', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof differenceBy === 'function').toBeTruthy();
expect(differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor)).toEqual([1.2]);
expect(differenceBy([{ x: 2 }, { x: 1 }], [{ x: 1 }], v => v.x)).toEqual([ { x: 2 } ]);
});

View File

@ -0,0 +1,2 @@
const differenceWith = (arr, val, comp) => arr.filter(a => val.findIndex(b => comp(a, b)) === -1);
module.exports = differenceWith;

View File

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

View File

@ -0,0 +1,2 @@
const digitize = n => [...`${n}`].map(i => parseInt(i));
module.exports = digitize;

View File

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

View File

@ -0,0 +1,2 @@
const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
module.exports = distance;

View File

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

2
test2/drop/drop.js Normal file
View File

@ -0,0 +1,2 @@
const drop = (arr, n = 1) => arr.slice(n);
module.exports = drop;

11
test2/drop/drop.test.js Normal file
View File

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

View File

@ -0,0 +1,2 @@
const dropRight = (arr, n = 1) => arr.slice(0, -n);
module.exports = dropRight;

View File

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

View File

@ -0,0 +1,5 @@
const dropRightWhile = (arr, func) => {
while (arr.length > 0 && !func(arr[arr.length - 1])) arr = arr.slice(0, -1);
return arr;
};
module.exports = dropRightWhile;

View File

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

View File

@ -0,0 +1,5 @@
const dropWhile = (arr, func) => {
while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1);
return arr;
};
module.exports = dropWhile;

View File

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

View File

@ -0,0 +1,9 @@
const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
const { top, left, bottom, right } = el.getBoundingClientRect();
const { innerHeight, innerWidth } = window;
return partiallyVisible
? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
};
module.exports = elementIsVisibleInViewport;

View File

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

19
test2/elo/elo.js Normal file
View File

@ -0,0 +1,19 @@
const elo = ([...ratings], kFactor = 32, selfRating) => {
const [a, b] = ratings;
const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400));
const newRating = (rating, i) =>
(selfRating || rating) + kFactor * (i - expectedScore(i ? a : b, i ? b : a));
if (ratings.length === 2) {
return [newRating(a, 1), newRating(b, 0)];
} else {
for (let i = 0; i < ratings.length; i++) {
let j = i;
while (j < ratings.length - 1) {
[ratings[i], ratings[j + 1]] = elo([ratings[i], ratings[j + 1]], kFactor);
j++;
}
}
}
return ratings;
};
module.exports = elo;

11
test2/elo/elo.test.js Normal file
View File

@ -0,0 +1,11 @@
const expect = require('expect');
const elo = require('./elo.js');
test('Testing elo', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof elo === 'function').toBeTruthy();
expect(elo([1200, 1200])).toEqual([1216, 1184]);
expect(elo([1200, 1200], 64)).toEqual([1232, 1168]), "Standard 1v1s";
expect(elo([1200, 1200, 1200, 1200]).map(Math.round)).toEqual([1246, 1215, 1185, 1154]);
});

11
test2/equals/equals.js Normal file
View File

@ -0,0 +1,11 @@
const equals = (a, b) => {
if (a === b) return true;
if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
if (!a || !b || (typeof a != 'object' && typeof b !== 'object')) return a === b;
if (a === null || a === undefined || b === null || b === undefined) return false;
if (a.prototype !== b.prototype) return false;
let keys = Object.keys(a);
if (keys.length !== Object.keys(b).length) return false;
return keys.every(k => equals(a[k], b[k]));
};
module.exports = equals;

View File

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

View File

@ -0,0 +1,13 @@
const escapeHTML = str =>
str.replace(
/[&<>'"]/g,
tag =>
({
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
"'": '&#39;',
'"': '&quot;'
}[tag] || tag)
);
module.exports = escapeHTML;

View File

@ -0,0 +1,9 @@
const expect = require('expect');
const escapeHTML = require('./escapeHTML.js');
test('Testing escapeHTML', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof escapeHTML === 'function').toBeTruthy();
expect(escapeHTML('<a href="#">Me & you</a>')).toBe('&lt;a href=&quot;#&quot;&gt;Me &amp; you&lt;/a&gt;');
});

View File

@ -0,0 +1,2 @@
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
module.exports = escapeRegExp;

View File

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

View File

@ -0,0 +1,2 @@
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
module.exports = everyNth;

View File

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

View File

@ -0,0 +1,8 @@
const extendHex = shortHex =>
'#' +
shortHex
.slice(shortHex.startsWith('#') ? 1 : 0)
.split('')
.map(x => x + x)
.join('');
module.exports = extendHex;

View File

@ -0,0 +1,10 @@
const expect = require('expect');
const extendHex = require('./extendHex.js');
test('Testing extendHex', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof extendHex === 'function').toBeTruthy();
expect(extendHex('#03f')).toBe('#0033ff');
expect(extendHex('05a')).toBe('#0055aa');
});

View File

@ -0,0 +1,9 @@
const factorial = n =>
n < 0
? (() => {
throw new TypeError('Negative numbers are not allowed!');
})()
: n <= 1
? 1
: n * factorial(n - 1);
module.exports = factorial;

View File

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

20
test2/factors/factors.js Normal file
View File

@ -0,0 +1,20 @@
const factors = (num, primes = false) => {
const isPrime = num => {
const boundary = Math.floor(Math.sqrt(num));
for (var i = 2; i <= boundary; i++) if (num % i === 0) return false;
return num >= 2;
};
const isNeg = num < 0;
num = isNeg ? -num : num;
let array = Array.from({ length: num - 1 })
.map((val, i) => (num % (i + 2) === 0 ? i + 2 : false))
.filter(val => val);
if (isNeg)
array = array.reduce((acc, val) => {
acc.push(val);
acc.push(-val);
return acc;
}, []);
return primes ? array.filter(isPrime) : array;
};
module.exports = factors;

View File

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

View File

@ -0,0 +1,6 @@
const fibonacci = n =>
Array.from({ length: n }).reduce(
(acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
[]
);
module.exports = fibonacci;

View File

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

View File

@ -0,0 +1,3 @@
const fibonacciCountUntilNum = num =>
Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
module.exports = fibonacciCountUntilNum;

View File

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

View File

@ -0,0 +1,8 @@
const fibonacciUntilNum = num => {
let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
return Array.from({ length: n }).reduce(
(acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),
[]
);
};
module.exports = fibonacciUntilNum;

View File

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

View File

@ -0,0 +1,2 @@
const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
module.exports = filterNonUnique;

View File

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

2
test2/findKey/findKey.js Normal file
View File

@ -0,0 +1,2 @@
const findKey = (obj, fn) => Object.keys(obj).find(key => fn(obj[key], key, obj));
module.exports = findKey;

View File

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

View File

@ -0,0 +1,2 @@
const findLast = (arr, fn) => arr.filter(fn).pop();
module.exports = findLast;

View File

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

View File

@ -0,0 +1,6 @@
const findLastIndex = (arr, fn) =>
arr
.map((val, i) => [i, val])
.filter(([i, val]) => fn(val, i, arr))
.pop()[0];
module.exports = findLastIndex;

View File

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

View File

@ -0,0 +1,5 @@
const findLastKey = (obj, fn) =>
Object.keys(obj)
.reverse()
.find(key => fn(obj[key], key, obj));
module.exports = findLastKey;

View File

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

3
test2/flatten/flatten.js Normal file
View File

@ -0,0 +1,3 @@
const flatten = (arr, depth = 1) =>
arr.reduce((a, v) => a.concat(depth > 1 && Array.isArray(v) ? flatten(v, depth - 1) : v), []);
module.exports = flatten;

View File

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

View File

@ -0,0 +1,8 @@
const flattenObject = (obj, prefix = '') =>
Object.keys(obj).reduce((acc, k) => {
const pre = prefix.length ? prefix + '.' : '';
if (typeof obj[k] === 'object') Object.assign(acc, flattenObject(obj[k], pre + k));
else acc[pre + k] = obj[k];
return acc;
}, {});
module.exports = flattenObject;

View File

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

2
test2/flip/flip.js Normal file
View File

@ -0,0 +1,2 @@
const flip = fn => (first, ...rest) => fn(...rest, first);
module.exports = flip;

13
test2/flip/flip.test.js Normal file
View File

@ -0,0 +1,13 @@
const expect = require('expect');
const flip = require('./flip.js');
test('Testing flip', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof flip === 'function').toBeTruthy();
let a = { name: 'John Smith' };
let b = {};
const mergeFrom = flip(Object.assign);
let mergePerson = mergeFrom.bind(null, a);
expect(mergePerson(b)).toEqual(a);
});

View File

@ -0,0 +1,6 @@
const forEachRight = (arr, callback) =>
arr
.slice(0)
.reverse()
.forEach(callback);
module.exports = forEachRight;

View File

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

2
test2/forOwn/forOwn.js Normal file
View File

@ -0,0 +1,2 @@
const forOwn = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key, obj));
module.exports = forOwn;

View File

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

View File

@ -0,0 +1,5 @@
const forOwnRight = (obj, fn) =>
Object.keys(obj)
.reverse()
.forEach(key => fn(obj[key], key, obj));
module.exports = forOwnRight;

View File

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

View File

@ -0,0 +1,15 @@
const formatDuration = ms => {
if (ms < 0) ms = -ms;
const time = {
day: Math.floor(ms / 86400000),
hour: Math.floor(ms / 3600000) % 24,
minute: Math.floor(ms / 60000) % 60,
second: Math.floor(ms / 1000) % 60,
millisecond: Math.floor(ms) % 1000
};
return Object.entries(time)
.filter(val => val[1] !== 0)
.map(val => val[1] + ' ' + (val[1] !== 1 ? val[0] + 's' : val[0]))
.join(', ');
};
module.exports = formatDuration;

View File

@ -0,0 +1,10 @@
const expect = require('expect');
const formatDuration = require('./formatDuration.js');
test('Testing formatDuration', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof formatDuration === 'function').toBeTruthy();
expect(formatDuration(1001)).toBe('1 second, 1 millisecond');
expect(formatDuration(34325055574)).toBe('397 days, 6 hours, 44 minutes, 15 seconds, 574 milliseconds');
});

View File

@ -0,0 +1,6 @@
const fromCamelCase = (str, separator = '_') =>
str
.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2')
.replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2')
.toLowerCase();
module.exports = fromCamelCase;

View File

@ -0,0 +1,11 @@
const expect = require('expect');
const fromCamelCase = require('./fromCamelCase.js');
test('Testing fromCamelCase', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof fromCamelCase === 'function').toBeTruthy();
expect(fromCamelCase('someDatabaseFieldName', ' ')).toBe('some database field name');
expect(fromCamelCase('someLabelThatNeedsToBeCamelized', '-')).toBe('some-label-that-needs-to-be-camelized');
expect(fromCamelCase('someJavascriptProperty', '_')).toBe('some_javascript_property');
});

View File

@ -0,0 +1,2 @@
const functionName = fn => (console.debug(fn.name), fn);
module.exports = functionName;

View File

@ -0,0 +1,19 @@
import expect from 'expect';
let output = '';
const console = {};
console.debug = (x) => output = x;
// Override for testing
const functionName = fn => (console.debug(fn.name), fn);
test('Testing functionName', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof functionName === 'function').toBeTruthy();
functionName(Math.max);
expect(output).toBe('max');
function fun(x) {return x;}
functionName(fun);
expect(output).toBe('fun');
const fn = x => x;
functionName(fn);
expect(output).toBe('fn');
});

View File

@ -0,0 +1,6 @@
const functions = (obj, inherited = false) =>
(inherited
? [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))]
: Object.keys(obj)
).filter(key => typeof obj[key] === 'function');
module.exports = functions;

View File

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

5
test2/gcd/gcd.js Normal file
View File

@ -0,0 +1,5 @@
const gcd = (...arr) => {
const _gcd = (x, y) => (!y ? x : gcd(y, x % y));
return [...arr].reduce((a, b) => _gcd(a, b));
};
module.exports = gcd;

10
test2/gcd/gcd.test.js Normal file
View File

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

View File

@ -0,0 +1,5 @@
const geometricProgression = (end, start = 1, step = 2) =>
Array.from({ length: Math.floor(Math.log(end / start) / Math.log(step)) + 1 }).map(
(v, i) => start * step ** i
);
module.exports = geometricProgression;

View File

@ -0,0 +1,11 @@
const expect = require('expect');
const geometricProgression = require('./geometricProgression.js');
test('Testing geometricProgression', () => {
//For more information on all the methods supported by tape
//Please go to https://github.com/substack/tape
expect(typeof geometricProgression === 'function').toBeTruthy();
expect(geometricProgression(256)).toEqual([1, 2, 4, 8, 16, 32, 64, 128, 256]);
expect(geometricProgression(256, 3)).toEqual([3, 6, 12, 24, 48, 96, 192]);
expect(geometricProgression(256, 1, 4)).toEqual([1, 4, 16, 64, 256]);
});

9
test2/get/get.js Normal file
View File

@ -0,0 +1,9 @@
const get = (from, ...selectors) =>
[...selectors].map(s =>
s
.replace(/\[([^\[\]]*)\]/g, '.$1.')
.split('.')
.filter(t => t !== '')
.reduce((prev, cur) => prev && prev[cur], from)
);
module.exports = get;

10
test2/get/get.test.js Normal file
View File

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

View File

@ -0,0 +1,2 @@
const getColonTimeFromDate = date => date.toTimeString().slice(0, 8);
module.exports = getColonTimeFromDate;

View File

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

View File

@ -0,0 +1,3 @@
const getDaysDiffBetweenDates = (dateInitial, dateFinal) =>
(dateFinal - dateInitial) / (1000 * 3600 * 24);
module.exports = getDaysDiffBetweenDates;

View File

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

View File

@ -0,0 +1,9 @@
const getMeridiemSuffixOfInteger = num =>
num === 0 || num === 24
? 12 + 'am'
: num === 12
? 12 + 'pm'
: num < 12
? num % 12 + 'am'
: num % 12 + 'pm';
module.exports = getMeridiemSuffixOfInteger;

View File

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

View File

@ -0,0 +1,5 @@
const getScrollPosition = (el = window) => ({
x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft,
y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop
});
module.exports = getScrollPosition;

View File

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

View File

@ -0,0 +1,2 @@
const getStyle = (el, ruleName) => getComputedStyle(el)[ruleName];
module.exports = getStyle;

View File

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

3
test2/getType/getType.js Normal file
View File

@ -0,0 +1,3 @@
const getType = v =>
v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
module.exports = getType;

View File

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

Some files were not shown because too many files have changed in this diff Show More