Added tests for new date snippets

This commit is contained in:
Angelos Chalaris
2018-09-29 14:29:56 +03:00
parent e8d4ad78d9
commit 29bd1d0a3c
20 changed files with 96 additions and 10 deletions

View File

@ -2,10 +2,10 @@
Check if a date is the same as another date.
Use strict equality checking (`===`) to check if the first date is the same as the second one.
Use `Date.prototype.toISOString()` and strict equality checking (`===`) to check if the first date is the same as the second one.
```js
const isSameDate = (dateA, dateB) => dateA === dateB;
const isSameDate = (dateA, dateB) => dateA.toISOString() === dateB.toISOString();
```
```js

View File

@ -0,0 +1,3 @@
const dayOfYear = date =>
Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
module.exports = dayOfYear;

View File

@ -0,0 +1,6 @@
const expect = require('expect');
const dayOfYear = require('./dayOfYear.js');
test('dayOfYear is a Function', () => {
expect(dayOfYear).toBeInstanceOf(Function);
});

View File

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

View File

@ -4,6 +4,6 @@ const getMeridiemSuffixOfInteger = num =>
: num === 12
? 12 + 'pm'
: num < 12
? (num % 12) + 'am'
: (num % 12) + 'pm';
? num % 12 + 'am'
: num % 12 + 'pm';
module.exports = getMeridiemSuffixOfInteger;

View File

@ -1,6 +1,6 @@
const hz = (fn, iterations = 100) => {
const before = performance.now();
for (let i = 0; i < iterations; i++) fn();
return (1000 * iterations) / (performance.now() - before);
return 1000 * iterations / (performance.now() - before);
};
module.exports = hz;

View File

@ -0,0 +1,2 @@
const isAfterDate = (dateA, dateB) => dateA > dateB;
module.exports = isAfterDate;

View File

@ -0,0 +1,12 @@
const expect = require('expect');
const isAfterDate = require('./isAfterDate.js');
test('isAfterDate is a Function', () => {
expect(isAfterDate).toBeInstanceOf(Function);
});
test('isAfterDate produces the correct result', () => {
expect(isAfterDate(new Date(2010, 10, 21), new Date(2010, 10, 20))).toBeTruthy();
});
test('isBeforeDate produces the correct result', () => {
expect(isAfterDate(new Date(2010, 10, 20), new Date(2010, 10, 21))).toBeFalsy();
});

View File

@ -0,0 +1,2 @@
const isBeforeDate = (dateA, dateB) => dateA < dateB;
module.exports = isBeforeDate;

View File

@ -0,0 +1,13 @@
const expect = require('expect');
const isBeforeDate = require('./isBeforeDate.js');
test('isBeforeDate is a Function', () => {
expect(isBeforeDate).toBeInstanceOf(Function);
});
test('isBeforeDate produces the correct result', () => {
expect(isBeforeDate(new Date(2010, 10, 20), new Date(2010, 10, 21))).toBeTruthy();
});
test('isBeforeDate produces the correct result', () => {
expect(isBeforeDate(new Date(2010, 10, 21), new Date(2010, 10, 20))).toBeFalsy();
});

View File

@ -0,0 +1,2 @@
const isSameDate = (dateA, dateB) => dateA.toISOString() === dateB.toISOString();
module.exports = isSameDate;

View File

@ -0,0 +1,12 @@
const expect = require('expect');
const isSameDate = require('./isSameDate.js');
test('isSameDate is a Function', () => {
expect(isSameDate).toBeInstanceOf(Function);
});
test('isSameDate produces the correct result', () => {
expect(isSameDate(new Date(2010, 10, 20), new Date(2010, 10, 20))).toBeTruthy();
});
test('isSameDate produces the correct result', () => {
expect(isSameDate(new Date(2010, 10, 20), new Date(2010, 10, 21))).toBeFalsy();
});

View File

@ -1,6 +1,6 @@
const lcm = (...arr) => {
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
const _lcm = (x, y) => (x * y) / gcd(x, y);
const _lcm = (x, y) => x * y / gcd(x, y);
return [...arr].reduce((a, b) => _lcm(a, b));
};
module.exports = lcm;

View File

@ -4,7 +4,7 @@ const luhnCheck = num => {
.reverse()
.map(x => parseInt(x));
let lastDigit = arr.splice(0, 1)[0];
let sum = arr.reduce((acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val * 2) % 9) || 9), 0);
let sum = arr.reduce((acc, val, i) => (i % 2 !== 0 ? acc + val : acc + (val * 2) % 9 || 9), 0);
sum += lastDigit;
return sum % 10 === 0;
};

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

@ -0,0 +1,2 @@
const maxDate = (...dates) => new Date(Math.max.apply(null, ...dates));
module.exports = maxDate;

View File

@ -0,0 +1,15 @@
const expect = require('expect');
const maxDate = require('./maxDate.js');
test('maxDate is a Function', () => {
expect(maxDate).toBeInstanceOf(Function);
});
test('maxDate produces the maximum date', () => {
const array = [
new Date(2017, 4, 13),
new Date(2018, 2, 12),
new Date(2016, 0, 10),
new Date(2016, 0, 9)
];
expect(maxDate(array)).toEqual(new Date(2018, 2, 12));
});

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

@ -0,0 +1,2 @@
const minDate = (...dates) => new Date(Math.min.apply(null, ...dates));
module.exports = minDate;

View File

@ -0,0 +1,15 @@
const expect = require('expect');
const minDate = require('./minDate.js');
test('minDate is a Function', () => {
expect(minDate).toBeInstanceOf(Function);
});
test('minDate produces the maximum date', () => {
const array = [
new Date(2017, 4, 13),
new Date(2018, 2, 12),
new Date(2016, 0, 10),
new Date(2016, 0, 9)
];
expect(minDate(array)).toEqual(new Date(2016, 0, 9));
});

View File

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

View File

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