Repository URL to install this package:
|
Version:
0.9.5 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const exotic_1 = require("exotic");
// should stare
const currentDate = new Date();
const currentMonth = currentDate.getMonth() + 1;
const currentYear = String(currentDate.getFullYear());
exports.currentYear = currentYear;
const currentYearTwoDigit = String(currentDate.getFullYear()).slice(2, 4);
exports.currentYearTwoDigit = currentYearTwoDigit;
const isValidMonth = month => month >= currentMonth && month <= 12;
const isValidYear = (year, month) => {
let validYear = false;
if (year >= currentYear) {
validYear = !!((year > currentYear && month > 0 && month <= 12) ||
(year === currentYear && isValidMonth(month)));
}
else {
const expiryYearLength = year.length;
if (expiryYearLength >= 4) {
validYear = false && isValidMonth(month);
}
else if (expiryYearLength === 2) {
validYear = !!((year > currentYearTwoDigit && month > 0 && month <= 12) ||
(year === currentYearTwoDigit && isValidMonth(month)));
}
}
return validYear;
};
exports.isValidYear = isValidYear;
const toFormatted = (_date) => {
// 01/10 => 01/2010
const date = _date.length === 5 ? _date.replace('/', '20') : _date;
const formattedDate = date.replace(/[^0-9]/g, '');
const formattedMonth = formattedDate
.replace(/^([2-9])|00/, '0$1')
.slice(0, 2)
.replace(/^([1][3-9])/, '1');
const formattedYear = formattedDate
.slice(2, 4)
.replace(/^([0|4-9]\d+|[0|4-9])/, '');
const obj = {
date: formattedDate,
formattedMonth,
formattedYear,
month: exotic_1.toNumber(formattedMonth),
year: exotic_1.toNumber(formattedYear),
};
return obj;
};
function isValidExpiryDate(date) {
if (exotic_1.isString(date) === false) {
return false;
}
const { formattedYear, formattedMonth, year, month } = toFormatted(date);
// 01/123 (invalid)
// 01123 (valid - but edger)
// 01/12 (valid - correct)
const isInvalidAmount = date.includes('/') && date.length === 6;
const isWithinMonthRange = month > 0 && month <= 12;
if (!isWithinMonthRange || isInvalidAmount) {
console.warn('isWithinMonthRange');
return false;
}
if (formattedYear.length > 0) {
const result = isValidYear(year, month);
return result;
}
else {
return false;
}
}
exports.isValidExpiryDate = isValidExpiryDate;
exports.default = isValidExpiryDate;
//# sourceMappingURL=isValidExpiryDate.js.map