Repository URL to install this package:
|
Version:
3.0.10 ▾
|
import { observable } from 'xmobx/mobx'
import { toNumber } from 'exotic'
import {
isValidDate,
currentYear,
// currentMonth,
currentYearTwoDigit,
} from './isValidExpiryDate'
type MonthYearType = string | number | any
// time match hours, minutes, and seconds, 24-hour clock
const matchTimeString = /^(2[0-3]|[01]?[0-9]):([0-5]?[0-9]):([0-5]?[0-9])$/
// dateString match m/d/yy and mm/dd/yyyy, allowing any combination of one or two digits for the day and month, and two or four digits for the year
const matchFullDate = /^(1[0-2]|0?[1-9])([\/-])(3[01]|[12][0-9]|0?[1-9])(?:\2)(?:[0-9]{2})?[0-9]{2}$/
const matchMonthSpecicalChar = /^\d{0,2}(?:\.\d)?$/
const matchMonth = /^[0-9]*$/gm
/**
* @param {String | Date} value
* @return {Boolean}
*/
// function isValidMonth(value) {
// // .value == new Date().getFullYear()
// const isBeforeOneMonthAhead = value < new Date().getMonth() + 1
// // The credit card has expired. Choose a different expiration date.
// if (isBeforeOneMonthAhead) {
// return false
// }
// return true
// }
const dateContainer = observable({
month: '',
year: '',
})
/**
* @see https://date-fns.org/
*
* @todo - use date_fns
* @todo - use in the dateParse util
* @todo - use in time atom
*/
const monthValidateToZero = item => {
const zero = 0
const value = item.length >= 2 ? item : zero + item
return value
}
const matchLimit = month => {
// const removeSpecialCharacters = month.replace(matchMonthSpecicalChar, '')
// const validateToZero = monthValidateToZero(month)
return month > 0 && month <= 12
}
export const monthValidationForYear = (month, year): boolean => {
const currentDate = new Date()
const currentMonth = currentDate.getMonth() + 1
if (year == toNumber(currentYear) || year == toNumber(currentYearTwoDigit)) {
return currentMonth <= month
} else {
return matchLimit(month)
}
}
// function isValidDate() {
// //
// }
// @observer
// extends React.Component
class ExpiryDateValidation {
isValidMonth = (year: MonthYearType, month: MonthYearType): boolean => {
if (
month &&
monthValidationForYear(month, year) &&
matchMonthSpecicalChar.test(month)
) {
return true
} else {
return false
}
}
isValidYear = (year: MonthYearType, month: MonthYearType): boolean => {
const expiryYearLength = year.toString().length
const validYear = false
if (
(expiryYearLength === 4 && year >= toNumber(currentYear)) ||
(expiryYearLength === 2 && year >= toNumber(currentYearTwoDigit))
) {
const result = isValidMonth(year, month)
// result ? this.isValidMonth(year, month) : false
return result
}
return validYear
}
isValidTimeOrDay() {
//
}
}
const expiryDate = new ExpiryDateValidation()
const isValidMonth = expiryDate.isValidMonth
const isValidYear = expiryDate.isValidYear
export { isValidMonth, isValidYear, expiryDate }