Created
July 1, 2016 18:12
-
-
Save unscriptable/620224cc94c55dc95f7fd8a3aa95a697 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
/*@flow*/ | |
// Creates a function that clones dates and shifts by the | |
// seconds specified. | |
// Example: | |
// >>> import { seconds } from './shift' | |
// >>> const fiveSecondsLater = seconds(5) | |
// >>> const d = new Date() | |
// >>> fiveSecondsLater(d) - d | |
// 5000 | |
export const seconds | |
: (sec:number) => (date:Date) => Date | |
= sec => date => { | |
const shifted = new Date(date) // clone | |
shifted.setSeconds(shifted.getSeconds() + sec) | |
return shifted | |
}; | |
// Creates a function that clones dates and shifts them by the | |
// minutes specified. | |
// Example: | |
// >>> import { minutes } from './shift' | |
// >>> const minutesWindow = (lo, hi) => date => | |
// >>> [minutes(lo), minutes(hi)].map(f => f(date)) | |
// >>> const within2MinWindow = minutesWindow(-1, 1) | |
// >>> const d = new Date('2016-07-01T18:00:00Z') | |
// >>> within2MinWindow(d) | |
// [ 2016-07-01T17:59:00.000Z, 2016-07-01T18:01:00.000Z ] | |
export const minutes | |
: (min:number) => (date:Date) => Date | |
= min => | |
seconds(min * 60) | |
// Creates a function that clones dates and shifts them by the | |
// hours specified. | |
// Example: | |
// >>> import { hours } from './shift' | |
// >>> const halfDaySooner = hours(-12) | |
// >>> const d = new Date() | |
// >>> halfDaySooner(d) - d | |
// -43200000 | |
export const hours | |
: (hr:number) => (date:Date) => Date | |
= hr => | |
seconds(hr * 3600) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment