Created
February 26, 2018 23:00
-
-
Save goldhand/4602f93092c173ab5ced64501f549b7c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Converts a Date or TimeStamp into a human readable format | |
* @module {Function} utils/displayDate | |
* @flow | |
*/ | |
import memoizeWith from 'ramda/src/memoizeWith'; | |
import identity from 'ramda/src/identity'; | |
import pipe from 'ramda/src/pipe'; | |
import ifElse from 'ramda/src/ifElse'; | |
import useWith from 'ramda/src/useWith'; | |
import equals from 'ramda/src/equals'; | |
import inc from 'ramda/src/inc'; | |
import length from 'ramda/src/length'; | |
import join from 'ramda/src/join'; | |
import converge from 'ramda/src/converge'; | |
import toString from 'ramda/src/toString'; | |
import unapply from 'ramda/src/unapply'; | |
import curry from 'ramda/src/curry'; | |
import translate from 'utils/translate'; | |
import * as translations from 'constants/translations'; | |
export type DateInput = Date | number | string; | |
type DisplayDateFn = (date?: DateInput | null) => string; | |
const doubleDigit = ifElse( | |
useWith(equals(2), [length]), | |
identity, | |
n => `0${n}`, | |
); | |
// gets the prototype function of an object so it can be called against a value | |
const protoOf = curry((Obj, method, value, ...args) => | |
Obj.prototype[method].call(value, ...args)); | |
const protoOfDate = protoOf(Date); | |
export const getDate: (date: Date) => string = pipe( | |
protoOfDate('getDate'), | |
toString, | |
doubleDigit, | |
); | |
export const getMonth: (date: Date) => string = pipe( | |
protoOfDate('getMonth'), | |
inc, // month count is zero based | |
toString, | |
doubleDigit, | |
); | |
export const getYear: (date: Date) => string = pipe( | |
protoOfDate('getFullYear'), | |
toString, | |
); | |
/** | |
* Converts a date to "yyyy-mm-dd" format | |
* @param {Date} date - Date to convert | |
* @returns {string} yyyy-mm-dd | |
*/ | |
export const hyphenDate: (date: Date) => string = converge( | |
pipe(unapply(identity), join('-')), | |
[getYear, getMonth, getDate], | |
); | |
// Always returns a valid date | |
const createDate = (dateInput?: DateInput): Date => (dateInput | |
? new Date(dateInput) | |
: new Date() | |
); | |
export const incDate = (dateInput?: DateInput, amount: number = 1): Date => { | |
const next = createDate(dateInput); | |
next.setDate(next.getDate() + amount); | |
return next; | |
}; | |
export const decDate = (dateInput?: DateInput, amount: number = 1): Date => { | |
const next = createDate(dateInput); | |
next.setDate(next.getDate() - amount); | |
return next; | |
}; | |
// validate the date input | |
const isValidDate = (date: Date): boolean => !isNaN(date); | |
export const createDateDisplay = ( | |
getDisplay: Date => string, | |
invalidDisplay: string = translate(translations.NOT_AVAILABLE), | |
): DisplayDateFn => memoizeWith(identity, (dateInput: DateInput) => { | |
if (dateInput) { | |
const date = createDate(dateInput); | |
if (isValidDate(date)) return getDisplay(date); | |
} | |
return invalidDisplay; | |
}); | |
export const displayDate: DisplayDateFn = createDateDisplay(date => date.toLocaleDateString(), translate(translations.ONGOING)); | |
export const displayTime: DisplayDateFn = createDateDisplay(date => date.toLocaleTimeString()); | |
export const displayDateTime: DisplayDateFn = createDateDisplay(date => date.toLocaleString()); | |
export const displayHyphenDate: DisplayDateFn = createDateDisplay(hyphenDate); | |
displayDate.date = displayDate; | |
displayDate.time = displayTime; | |
displayDate.dateTime = displayDateTime; | |
displayDate.create = createDateDisplay; | |
displayDate.hyphenDate = displayHyphenDate; | |
export default displayDate; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment