react-native-moyasar-sdk
Version:
Official React Native Moyasar SDK - Integrate Credit Cards, Apple Pay, Samsung Pay, and STC Pay with simple interfaces for a seamless payment experience in your React Native app
46 lines (43 loc) • 1.31 kB
JavaScript
;
export class ExpiryDateUtil {
constructor(month, year) {
this.month = month;
this.year = year;
}
dateInstance() {
if (!this.isValid()) {
return null;
}
return new Date(this.year, this.month - 1);
}
isValid() {
return this.month >= 1 && this.month <= 12 && this.year > 1900;
}
isExpired() {
const expiry = this.dateInstance();
if (!expiry) {
return false;
}
const now = new Date();
const currentDate = new Date(now.getFullYear(), now.getMonth());
return expiry < currentDate;
}
static fromPattern(pattern) {
const clean = pattern.replace(/[\s\/]/g, '');
// Checks if the cleaned string is digits only
if (!/^\d+$/.test(clean)) {
return null;
}
if (clean.length === 4) {
// TODO: Optimize to init the Date object once for this class
const millennium = Math.floor(new Date().getFullYear() / 100) * 100;
const year = parseInt(clean.substring(2, 4), 10);
return new ExpiryDateUtil(parseInt(clean.substring(0, 2), 10), year + millennium);
} else if (clean.length === 6) {
return new ExpiryDateUtil(parseInt(clean.substring(0, 2), 10), parseInt(clean.substring(2, 6), 10));
} else {
return null;
}
}
}
//# sourceMappingURL=expiry_date_util.js.map