Added tests for new date snippets
This commit is contained in:
@ -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
|
||||
|
||||
3
test/dayOfYear/dayOfYear.js
Normal file
3
test/dayOfYear/dayOfYear.js
Normal file
@ -0,0 +1,3 @@
|
||||
const dayOfYear = date =>
|
||||
Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
|
||||
module.exports = dayOfYear;
|
||||
6
test/dayOfYear/dayOfYear.test.js
Normal file
6
test/dayOfYear/dayOfYear.test.js
Normal file
@ -0,0 +1,6 @@
|
||||
const expect = require('expect');
|
||||
const dayOfYear = require('./dayOfYear.js');
|
||||
|
||||
test('dayOfYear is a Function', () => {
|
||||
expect(dayOfYear).toBeInstanceOf(Function);
|
||||
});
|
||||
@ -1,2 +1,2 @@
|
||||
const degreesToRads = deg => (deg * Math.PI) / 180.0;
|
||||
const degreesToRads = deg => deg * Math.PI / 180.0;
|
||||
module.exports = degreesToRads;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
2
test/isAfterDate/isAfterDate.js
Normal file
2
test/isAfterDate/isAfterDate.js
Normal file
@ -0,0 +1,2 @@
|
||||
const isAfterDate = (dateA, dateB) => dateA > dateB;
|
||||
module.exports = isAfterDate;
|
||||
12
test/isAfterDate/isAfterDate.test.js
Normal file
12
test/isAfterDate/isAfterDate.test.js
Normal 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();
|
||||
});
|
||||
2
test/isBeforeDate/isBeforeDate.js
Normal file
2
test/isBeforeDate/isBeforeDate.js
Normal file
@ -0,0 +1,2 @@
|
||||
const isBeforeDate = (dateA, dateB) => dateA < dateB;
|
||||
module.exports = isBeforeDate;
|
||||
13
test/isBeforeDate/isBeforeDate.test.js
Normal file
13
test/isBeforeDate/isBeforeDate.test.js
Normal 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();
|
||||
});
|
||||
|
||||
2
test/isSameDate/isSameDate.js
Normal file
2
test/isSameDate/isSameDate.js
Normal file
@ -0,0 +1,2 @@
|
||||
const isSameDate = (dateA, dateB) => dateA.toISOString() === dateB.toISOString();
|
||||
module.exports = isSameDate;
|
||||
12
test/isSameDate/isSameDate.test.js
Normal file
12
test/isSameDate/isSameDate.test.js
Normal 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();
|
||||
});
|
||||
@ -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;
|
||||
|
||||
@ -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
2
test/maxDate/maxDate.js
Normal file
@ -0,0 +1,2 @@
|
||||
const maxDate = (...dates) => new Date(Math.max.apply(null, ...dates));
|
||||
module.exports = maxDate;
|
||||
15
test/maxDate/maxDate.test.js
Normal file
15
test/maxDate/maxDate.test.js
Normal 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
2
test/minDate/minDate.js
Normal file
@ -0,0 +1,2 @@
|
||||
const minDate = (...dates) => new Date(Math.min.apply(null, ...dates));
|
||||
module.exports = minDate;
|
||||
15
test/minDate/minDate.test.js
Normal file
15
test/minDate/minDate.test.js
Normal 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));
|
||||
});
|
||||
@ -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;
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
const radsToDegrees = rad => (rad * 180.0) / Math.PI;
|
||||
const radsToDegrees = rad => rad * 180.0 / Math.PI;
|
||||
module.exports = radsToDegrees;
|
||||
|
||||
Reference in New Issue
Block a user