all files / src/ date.js

100% Statements 101/101
98.97% Branches 96/97
100% Functions 21/21
100% Lines 88/88
1 statement, 2 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160           32× 32× 28×                               33× 33× 33×   33× 47× 47× 11×           36×   32× 30× 30×   29×   28×   24×   23×   22×               40×     34×   30× 30×         10×     23×   21×   19×         36× 92× 92× 92× 92×     99× 99× 61×   38× 38× 264×     92×         87× 87× 249× 249×   87× 87× 64× 64× 183×   64× 64× 64×   64× 64×   23×         64×   64×  
import React from 'react'
import { FormattedMessage } from 'react-intl'
import { formatMessage, prepare, trunc, memoize, DEFAULT_ALLOW_BLANK } from './helpers'
 
 
const DATE_RANGE_ERROR = (
  <FormattedMessage id="form.errors.dateRange" defaultMessage="shoud be {op} {date}" />
)
 
const DATE_METHODS = {
  y: function(d) { return d.getFullYear() },
  m: function(d) { return d.getMonth() + 1 },
  d: function(d) { return d.getDate() }
}
 
const PARSE_REG = /(y+|m+|d+)/g
 
const TO_STRING = ({}).toString;
 
let date = memoize(function ({
      format,
      ymd,
      '=': eq,
      '!=': diff,
      '>': gt,
      '>=': gte,
      '<': lt,
      '<=': lte,
      message, msg,
      'if': ifCond, unless,
      allowBlank=DEFAULT_ALLOW_BLANK
    }) {
  msg = formatMessage(msg || message)
  let normFormat = normalizeFormat(format, ymd)
 
  return prepare(ifCond, unless, allowBlank, function(value) {
    let date = normParseDate(value, normFormat, false)
    if ('wrongFormat' === date) {
      return msg || (
        <FormattedMessage id="form.errors.dateFormat"
            defaultMessage="expected format: {format}"
            values={{ format: format }} />
      )
    }
    if ('invalid' === date) {
      return msg || (<FormattedMessage id="form.errors.dateInvalid" defaultMessage="is not a valid date"/>)
    }
    if (date) {
      let date2
      if (eq && +date !== +(date2 = getDate(eq))) {
        return msg || dateRangeError({ op: '=', date: normFormatDate(date2, normFormat) })
      }
      if (diff && +date === +(date2 = getDate(diff))) {
        return msg || dateRangeError({ op: '!=', date: normFormatDate(date2, normFormat) })
      }
      if (gt && date <= (date2 = getDate(gt))) {
        return msg || dateRangeError({ op: '>', date: normFormatDate(date2, normFormat) })
      }
      if (gte && date < (date2 = getDate(gte))) {
        return msg || dateRangeError({ op: '>=', date: normFormatDate(date2, normFormat) })
      }
      if (lt && date >= (date2 = getDate(lt))) {
        return msg || dateRangeError({ op: '<', date: normFormatDate(date2, normFormat) })
      }
      if (lte && date > (date2 = getDate(lte))) {
        return msg || dateRangeError({ op: '<=', date: normFormatDate(date2, normFormat) })
      }
    }
  })
})
 
export default date
 
export function parseDate (strDate, format, ymd) {
  return normParseDate(strDate, normalizeFormat(format, ymd), true)
}
 
export function formatDate (date, format, ymd) {
  if (!(date instanceof Date) && '[object Date]' !== TO_STRING.call(date)) {
    return null;
  }
  let t = new Date(date).getTime();
  return t !== t ? null : normFormatDate(date, normalizeFormat(format, ymd))
}
 
 
 
function dateRangeError (values) {
  return formatMessage({ id: 'form.errors.dateRange', values })
}
 
function getDate (d) {
  if ('function' === typeof d) {
    return new Date(+d())
  }
  if (isNaN(d) && 'today' === ('' + d).toLowerCase()) {
    let today = new Date()
    today.setHours(0, 0, 0, 0)
    return today
  }
  return new Date(+d)
}
 
 
// FORMAT
function normFormatDate (date, format) {
  return format.replace(PARSE_REG, function(m) {
    let sym = m.charAt(0)
    let len = m.length
    let padded = padding(DATE_METHODS[sym](date), len)
    return 'y' === sym ? padded.slice(padded.length - len, padded.length) : padded;
  })
}
function normalizeFormat (format, ymd) {
  if (!ymd) {
    return format
  }
  let reverseMapping = { [ymd.charAt(0)]: 'y', [ymd.charAt(1)]: 'm', [ymd.charAt(2)]: 'd' }
  return format.replace(new RegExp(`[${ymd}]`, 'g'), function(sym) {
    return reverseMapping[sym]
  })
}
function padding (num, pad) {
  return '0'.repeat(Math.max(0, pad - ('' + num).length)) + num
}
 
 
// PARSE
function normParseDate (value, format, parse) {
  let order = []
  let reg = new RegExp('^' + format.replace(PARSE_REG, function(m) {
    order.push(m.charAt(0))
    return `([0-9]{${m.length}})`
  }) + '$')
  let match = value.match(reg)
  if (match) {
    let flags = {}
    order.forEach(function(token, index) {
      flags[token] = +match[index + 1]
    })
    let comparable = null != flags.y ? (null != flags.m ? true : null == flags.d) : false
    flags = Object.assign({ y: 1970, m: 1, d: 1 }, flags)
    if (flags.y < 100) {
      flags.y = currentCentury(flags.y >= 69 ? -1 : 0) * 100 + flags.y
    }
    let date = new Date(flags.y, flags.m - 1, flags.d)
    return checkFlags(date, flags) ? (comparable || parse ? date : null) : (parse ? new Date(NaN) : 'invalid')
  }
  return parse ? new Date(NaN) : 'wrongFormat'
}
 
function currentCentury (add) {
  let century = trunc(new Date().getFullYear() / 100)
  return century < 0 ? century - add : century + add
}
 
function checkFlags (date, flags) {
  let [year, month, day] = [date.getFullYear(), date.getMonth() + 1, date.getDate()]
  return year === flags.y && month === flags.m && day === flags.d
}