Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

skava / exotic   js

Repository URL to install this package:

Version: 2.0.8 

/ src / types / @omni / presets / toPrettyDate.ts

import { format, distanceInWordsToNow, isThisWeek } from 'date-fns'
import { coerce, curry, pipeTwo, identity, toDate } from 'chaind'

// llll
const DATE_FORMAT = 'YYYY-MM-DD h:mm A'
const DATE_ISO_FORMAT = 'YYYY-MM-DD hh:mm:ss a ZZ'

// or nthArg
const coerceDate = coerce('_', [identity, toDate])
const toDateFormat = curry(2, (formatOption, date) =>
  format(date, formatOption)
)
const coerceToDateFormat = pipeTwo(coerceDate, toDateFormat)

const toLongDate = coerceToDateFormat(DATE_FORMAT)
const toISODate = coerceToDateFormat(DATE_ISO_FORMAT)
const toTimeAgoInWords = coerceDate(distanceInWordsToNow)

/**
 * @desc if date is > 7 days, longer date
 *       otherwise, short date
 *
 * @param {Date | *} date coerced to date
 * @return {String} formatted date
 *
 * @name toPrettyDate
 * @see https://stackoverflow.com/questions/10003683/javascript-get-number-from-string
 * @type {Function}
 */
const toPrettyDate = coerceDate(date => {
  if (isThisWeek(date)) return toTimeAgoInWords(date)
  else return toLongDate(date)
})

export { toPrettyDate, coerceDate, toISODate }