Merge pull request #757 from 30-seconds/date-snippets
[FEATURE] Date snippets
This commit is contained in:
@ -12,9 +12,6 @@ const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
|
||||
```js
|
||||
const add5 = x => x + 5;
|
||||
const multiply = (x, y) => x * y;
|
||||
const multiplyAndAdd5 = compose(
|
||||
add5,
|
||||
multiply
|
||||
);
|
||||
const multiplyAndAdd5 = compose(add5, multiply);
|
||||
multiplyAndAdd5(5, 2); // 15
|
||||
```
|
||||
|
||||
15
snippets/dayOfYear.md
Normal file
15
snippets/dayOfYear.md
Normal file
@ -0,0 +1,15 @@
|
||||
### dayOfYear
|
||||
|
||||
Gets the day of the year from a `Date` object.
|
||||
|
||||
Use `new Date()` and `Date.prototype.getFullYear()` to get the first day of the year as a `Date` object, subtract it from the provided `date` and divide with the milliseconds in each day to get the result.
|
||||
Use `Math.floor()` to appropriately round the resulting day count to an integer.
|
||||
|
||||
```js
|
||||
const dayOfYear = date =>
|
||||
Math.floor((date - new Date(date.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24);
|
||||
```
|
||||
|
||||
```js
|
||||
dayOfYear(new Date()); // 272
|
||||
```
|
||||
@ -5,7 +5,7 @@ Converts an angle from degrees to radians.
|
||||
Use `Math.PI` and the degree to radian formula to convert the angle from degrees to radians.
|
||||
|
||||
```js
|
||||
const degreesToRads = deg => (deg * Math.PI) / 180.0;
|
||||
const degreesToRads = deg => deg * Math.PI / 180.0;
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
@ -11,8 +11,8 @@ const getMeridiemSuffixOfInteger = num =>
|
||||
: num === 12
|
||||
? 12 + 'pm'
|
||||
: num < 12
|
||||
? (num % 12) + 'am'
|
||||
: (num % 12) + 'pm';
|
||||
? num % 12 + 'am'
|
||||
: num % 12 + 'pm';
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
@ -11,7 +11,7 @@ Omit the second argument, `iterations`, to use the default of 100 iterations.
|
||||
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);
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
13
snippets/isAfterDate.md
Normal file
13
snippets/isAfterDate.md
Normal file
@ -0,0 +1,13 @@
|
||||
### isAfterDate
|
||||
|
||||
Check if a date is after another date.
|
||||
|
||||
Use the greater than operator (`>`) to check if the first date comes after the second one.
|
||||
|
||||
```js
|
||||
const isAfterDate = (dateA, dateB) => dateA > dateB;
|
||||
```
|
||||
|
||||
```js
|
||||
isAfterDate(new Date(2010, 10, 21), new Date(2010, 10, 20)); // true
|
||||
```
|
||||
13
snippets/isBeforeDate.md
Normal file
13
snippets/isBeforeDate.md
Normal file
@ -0,0 +1,13 @@
|
||||
### isBeforeDate
|
||||
|
||||
Check if a date is before another date.
|
||||
|
||||
Use the less than operator (`<`) to check if the first date comes before the second one.
|
||||
|
||||
```js
|
||||
const isBeforeDate = (dateA, dateB) => dateA < dateB;
|
||||
```
|
||||
|
||||
```js
|
||||
isBeforeDate(new Date(2010, 10, 20), new Date(2010, 10, 21)); // true
|
||||
```
|
||||
13
snippets/isSameDate.md
Normal file
13
snippets/isSameDate.md
Normal file
@ -0,0 +1,13 @@
|
||||
### isSameDate
|
||||
|
||||
Check if a date is the same as another date.
|
||||
|
||||
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.toISOString() === dateB.toISOString();
|
||||
```
|
||||
|
||||
```js
|
||||
isSameDate(new Date(2010, 10, 20), new Date(2010, 10, 20)); // true
|
||||
```
|
||||
@ -8,7 +8,7 @@ The GCD formula uses recursion.
|
||||
```js
|
||||
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));
|
||||
};
|
||||
```
|
||||
|
||||
@ -15,7 +15,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;
|
||||
};
|
||||
|
||||
19
snippets/maxDate.md
Normal file
19
snippets/maxDate.md
Normal file
@ -0,0 +1,19 @@
|
||||
### maxDate
|
||||
|
||||
Returns the maximum of the given dates.
|
||||
|
||||
Use `Math.max.apply()` to find the maximum date value, `new Date()` to convert it to a `Date` object.
|
||||
|
||||
```js
|
||||
const maxDate = (...dates) => new Date(Math.max.apply(null, ...dates));
|
||||
```
|
||||
|
||||
```js
|
||||
const array = [
|
||||
new Date(2017, 4, 13),
|
||||
new Date(2018, 2, 12),
|
||||
new Date(2016, 0, 10),
|
||||
new Date(2016, 0, 9)
|
||||
];
|
||||
maxDate(array); // 2018-03-11T22:00:00.000Z
|
||||
```
|
||||
19
snippets/minDate.md
Normal file
19
snippets/minDate.md
Normal file
@ -0,0 +1,19 @@
|
||||
### minDate
|
||||
|
||||
Returns the minimum of the given dates.
|
||||
|
||||
Use `Math.min.apply()` to find the minimum date value, `new Date()` to convert it to a `Date` object.
|
||||
|
||||
```js
|
||||
const minDate = (...dates) => new Date(Math.min.apply(null, ...dates));
|
||||
```
|
||||
|
||||
```js
|
||||
const array = [
|
||||
new Date(2017, 4, 13),
|
||||
new Date(2018, 2, 12),
|
||||
new Date(2016, 0, 10),
|
||||
new Date(2016, 0, 9)
|
||||
];
|
||||
minDate(array); // 2016-01-08T22:00:00.000Z
|
||||
```
|
||||
@ -6,7 +6,7 @@ Use `Array.prototype.reduce()` to calculate how many numbers are below the value
|
||||
|
||||
```js
|
||||
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;
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
@ -5,7 +5,7 @@ Converts an angle from radians to degrees.
|
||||
Use `Math.PI` and the radian to degree formula to convert the angle from radians to degrees.
|
||||
|
||||
```js
|
||||
const radsToDegrees = rad => (rad * 180.0) / Math.PI;
|
||||
const radsToDegrees = rad => rad * 180.0 / Math.PI;
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
@ -44,6 +44,7 @@ CSVToArray:string,array,utility,intermediate
|
||||
CSVToJSON:string,array,object,advanced
|
||||
currentURL:browser,url,beginner
|
||||
curry:function,recursion,intermediate
|
||||
dayOfYear:date,beginner
|
||||
debounce:function,intermediate
|
||||
decapitalize:string,array,intermediate
|
||||
deepClone:object,recursion,intermediate
|
||||
@ -130,8 +131,10 @@ intersectionWith:array,function,intermediate
|
||||
invertKeyValues:object,function,intermediate
|
||||
is:type,array,regexp,beginner
|
||||
isAbsoluteURL:string,utility,browser,url,intermediate
|
||||
isAfterDate:date,utility,beginner
|
||||
isAnagram:string,regexp,intermediate
|
||||
isArrayLike:type,array,intermediate
|
||||
isBeforeDate:date,utility,beginner
|
||||
isBoolean:type,beginner
|
||||
isBrowser:utility,browser,intermediate
|
||||
isBrowserTabFocused:browser,beginner
|
||||
@ -149,6 +152,7 @@ isPlainObject:type,object,intermediate
|
||||
isPrime:math,beginner,intermediate
|
||||
isPrimitive:type,function,array,string,intermediate
|
||||
isPromiseLike:type,function,promise,intermediate
|
||||
isSameDate:date,utility,beginner
|
||||
isSorted:array,intermediate
|
||||
isString:type,string,beginner
|
||||
isSymbol:type,beginner
|
||||
@ -172,11 +176,13 @@ mask:string,utility,regexp,intermediate
|
||||
matches:object,type,intermediate
|
||||
matchesWith:object,type,function,intermediate
|
||||
maxBy:math,array,function,beginner
|
||||
maxDate:date,math,beginner
|
||||
maxN:array,math,beginner
|
||||
median:math,array,intermediate
|
||||
memoize:function,advanced
|
||||
merge:object,array,intermediate
|
||||
minBy:math,array,function,beginner
|
||||
minDate:date,math,beginner
|
||||
minN:array,math,beginner
|
||||
mostPerformant:utility,function
|
||||
negate:function,beginner
|
||||
|
||||
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