@date-io/hijri
Version:
Abstraction over common javascript date management libraries
179 lines (173 loc) • 6.11 kB
JavaScript
'use strict';
var iMoment = require('moment-hijri');
var DefaultMomentUtils = require('@date-io/moment');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var iMoment__default = /*#__PURE__*/_interopDefaultLegacy(iMoment);
var DefaultMomentUtils__default = /*#__PURE__*/_interopDefaultLegacy(DefaultMomentUtils);
var symbolMap = {
1: "١",
2: "٢",
3: "٣",
4: "٤",
5: "٥",
6: "٦",
7: "٧",
8: "٨",
9: "٩",
0: "٠",
};
const defaultFormats = {
dayOfMonth: "iD",
fullDate: "iYYYY, iMMMM Do",
fullDateWithWeekday: "iYYYY, iMMMM Do, dddd",
fullDateTime: "iYYYY, iMMMM Do, hh:mm A",
fullDateTime12h: "iD iMMMM hh:mm A",
fullDateTime24h: "iD iMMMM HH:mm",
fullTime: "LT",
fullTime12h: "hh:mm A",
fullTime24h: "HH:mm",
hours12h: "hh",
hours24h: "HH",
keyboardDate: "iYYYY/iMM/iDD",
keyboardDateTime: "iYYYY/iMM/iDD LT",
keyboardDateTime12h: "iYYYY/iMM/iDD hh:mm A",
keyboardDateTime24h: "iYYYY/iMM/iDD HH:mm",
minutes: "mm",
month: "iMMMM",
monthAndDate: "iD iMMMM",
monthAndYear: "iMMMM iYYYY",
monthShort: "iMMM",
weekday: "dddd",
weekdayShort: "ddd",
normalDate: "dddd, iD iMMM",
normalDateWithWeekday: "DD iMMMM",
seconds: "ss",
shortDate: "iD iMMM",
year: "iYYYY",
};
class MomentUtils extends DefaultMomentUtils__default["default"] {
constructor({ instance, formats } = {}) {
super({ locale: "ar-SA", instance });
this.lib = "moment=hijiri";
this.toIMoment = (date) => {
return this.moment(date ? date.clone() : undefined).locale("ar-SA");
};
this.parse = (value, format) => {
if (value === "") {
return null;
}
return this.moment(value, format, true).locale("ar-SA");
};
this.isBeforeYear = (date, value) => {
return date.iYear() < value.iYear();
};
this.isAfterYear = (date, value) => {
return date.iYear() > value.iYear();
};
this.getWeek = (date) => {
return date.get("week");
};
this.getMonth = (date) => {
return date.iMonth();
};
this.getDaysInMonth = (date) => {
return date.daysInMonth();
};
this.startOfYear = (date) => {
return date.clone().startOf("iYear");
};
this.endOfYear = (date) => {
return date.clone().endOf("iYear");
};
this.startOfMonth = (date) => {
return date.clone().startOf("iMonth");
};
this.endOfMonth = (date) => {
return date.clone().endOf("iMonth");
};
this.getNextMonth = (date) => {
return date.clone().add(1, "iMonth");
};
this.getPreviousMonth = (date) => {
return date.clone().subtract(1, "iMonth");
};
this.getYear = (date) => {
return date.iYear();
};
this.setYear = (date, year) => {
return date.clone().iYear(year);
};
this.getDate = (date) => {
return date.iDate();
};
this.setDate = (date, year) => {
return date.clone().iDate(year);
};
this.getMeridiemText = (ampm) => {
return ampm === "am"
? this.toIMoment().hours(2).format("A")
: this.toIMoment().hours(14).format("A");
};
this.getWeekdays = () => {
return [0, 1, 2, 3, 4, 5, 6].map((dayOfWeek) => {
return this.toIMoment().weekday(dayOfWeek).format("dd");
});
};
this.isEqual = (value, comparing) => {
if (value === null && comparing === null) {
return true;
}
return this.moment(value).isSame(comparing);
};
this.formatNumber = (num) => {
return (num
// safe cast because we are matching only digits
.replace(/\d/g, (match) => symbolMap[match])
.replace(/,/g, "،"));
};
this.getWeekArray = (date) => {
const start = date.clone().startOf("iMonth").startOf("week");
const end = date.clone().endOf("iMonth").endOf("week");
let count = 0;
let current = start;
const nestedWeeks = [];
while (current.isBefore(end)) {
const weekNumber = Math.floor(count / 7);
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || [];
nestedWeeks[weekNumber].push(current);
current = current.clone().add(1, "day");
count += 1;
}
return nestedWeeks;
};
this.getYearRange = (start, end) => {
// moment-hijri only supports dates between 1356-01-01 H and 1499-12-29 H
// We need to throw if outside min/max bounds, otherwise the while loop below will be infinite.
if (start.isBefore("1937-03-14")) {
throw new Error("min date must be on or after 1356-01-01 H (1937-03-14)");
}
if (end.isAfter("2076-11-26")) {
throw new Error("max date must be on or before 1499-12-29 H (2076-11-26)");
}
const startDate = this.moment(start).startOf("iYear");
const endDate = this.moment(end).endOf("iYear");
const years = [];
let current = startDate;
while (current.isBefore(endDate)) {
years.push(current);
current = current.clone().add(1, "iYear");
}
return years;
};
this.moment = instance || iMoment__default["default"];
this.locale = "ar-SA";
this.formats = Object.assign({}, defaultFormats, formats);
}
date(value) {
if (value === null) {
return null;
}
return this.moment(value).locale("ar-SA");
}
}
module.exports = MomentUtils;