headless-datetimepicker
Version:
React headless datepicker
565 lines (564 loc) • 13.6 kB
JavaScript
import { startOfToday as q, startOfDay as B, eachDayOfInterval as K, addDays as J } from "date-fns";
var D = {
toJalaali: Q,
toGregorian: R,
isValidJalaaliDate: ee,
isLeapJalaaliYear: Z,
jalaaliMonthLength: $,
jalCal: x,
j2d: T,
d2j: Y,
g2d: O,
d2g: b,
jalaaliToDateObject: P,
jalaaliWeek: re
}, y = [
-61,
9,
38,
199,
426,
686,
756,
818,
1111,
1181,
1210,
1635,
2060,
2097,
2192,
2262,
2324,
2394,
2456,
3178
];
function Q(e, t, r) {
return Object.prototype.toString.call(e) === "[object Date]" && (r = e.getDate(), t = e.getMonth() + 1, e = e.getFullYear()), Y(O(e, t, r));
}
function R(e, t, r) {
return b(T(e, t, r));
}
function ee(e, t, r) {
return e >= -61 && e <= 3177 && t >= 1 && t <= 12 && r >= 1 && r <= $(e, t);
}
function Z(e) {
return te(e) === 0;
}
function $(e, t) {
return t <= 6 ? 31 : t <= 11 || Z(e) ? 30 : 29;
}
function te(e) {
var t = y.length, r = y[0], n, a, o, i, s;
if (e < r || e >= y[t - 1])
throw new Error("Invalid Jalaali year " + e);
for (s = 1; s < t && (n = y[s], a = n - r, !(e < n)); s += 1)
r = n;
return i = e - r, a - i < 6 && (i = i - a + u(a + 4, 33) * 33), o = d(d(i + 1, 33) - 1, 4), o === -1 && (o = 4), o;
}
function x(e, t) {
var r = y.length, n = e + 621, a = -14, o = y[0], i, s, m, p, h, c, g;
if (e < o || e >= y[r - 1])
throw new Error("Invalid Jalaali year " + e);
for (g = 1; g < r && (i = y[g], s = i - o, !(e < i)); g += 1)
a = a + u(s, 33) * 8 + u(d(s, 33), 4), o = i;
return c = e - o, a = a + u(c, 33) * 8 + u(d(c, 33) + 3, 4), d(s, 33) === 4 && s - c === 4 && (a += 1), p = u(n, 4) - u((u(n, 100) + 1) * 3, 4) - 150, h = 20 + a - p, t ? { gy: n, march: h } : (s - c < 6 && (c = c - s + u(s + 4, 33) * 33), m = d(d(c + 1, 33) - 1, 4), m === -1 && (m = 4), {
leap: m,
gy: n,
march: h
});
}
function T(e, t, r) {
var n = x(e, !0);
return O(n.gy, 3, n.march) + (t - 1) * 31 - u(t, 7) * (t - 7) + r - 1;
}
function Y(e) {
var t = b(e).gy, r = t - 621, n = x(r, !1), a = O(t, 3, n.march), o, i, s;
if (s = e - a, s >= 0) {
if (s <= 185)
return i = 1 + u(s, 31), o = d(s, 31) + 1, {
jy: r,
jm: i,
jd: o
};
s -= 186;
} else
r -= 1, s += 179, n.leap === 1 && (s += 1);
return i = 7 + u(s, 30), o = d(s, 30) + 1, {
jy: r,
jm: i,
jd: o
};
}
function O(e, t, r) {
var n = u((e + u(t - 8, 6) + 100100) * 1461, 4) + u(153 * d(t + 9, 12) + 2, 5) + r - 34840408;
return n = n - u(u(e + 100100 + u(t - 8, 6), 100) * 3, 4) + 752, n;
}
function b(e) {
var t, r, n, a, o;
return t = 4 * e + 139361631, t = t + u(u(4 * e + 183187720, 146097) * 3, 4) * 4 - 3908, r = u(d(t, 1461), 4) * 5 + 308, n = u(d(r, 153), 5) + 1, a = d(u(r, 153), 12) + 1, o = u(t, 1461) - 100100 + u(8 - a, 6), {
gy: o,
gm: a,
gd: n
};
}
function re(e, t, r) {
var n = P(e, t, r).getDay(), a = n == 6 ? 0 : -(n + 1), o = 6 + a;
return {
saturday: Y(T(e, t, r + a)),
friday: Y(T(e, t, r + o))
};
}
function P(e, t, r, n, a, o, i) {
var s = R(e, t, r);
return new Date(
s.gy,
s.gm - 1,
s.gd,
n || 0,
a || 0,
o || 0,
i || 0
);
}
function u(e, t) {
return ~~(e / t);
}
function d(e, t) {
return e - ~~(e / t) * t;
}
const I = [
"فر",
"ارد",
"خر",
"تیر",
"مرد",
"شهر",
"مهر",
"آبا",
"آذر",
"دی",
"بهم",
"اسفن"
], ne = ["یک", "دو", "سه", "چهار", "پنج", "جمعه", "شنبه"], ae = ["یک", "دو", "سه", "چه", "پن", "جم", "شن"], oe = /\[([^\]]+)]|YYYY|YY?|yyyy|yy?|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|Z{1,2}|S{1,3}|w{1,2}|x|X|a|A/g;
function l(e, t = 2) {
let r = `${Math.abs(e)}`;
const n = e < 0 ? "-" : "";
for (; r.length < t; )
r = `0${r}`;
return n + r;
}
function N(e) {
return Math.round(e.timezoneOffset / 15) * 15;
}
function v(e, t = "") {
const r = e > 0 ? "-" : "+", n = Math.abs(e), a = Math.floor(n / 60), o = n % 60;
return r + l(a, 2) + t + l(o, 2);
}
const C = (e, t, r) => {
const n = e < 12 ? "AM" : "PM";
return r ? n.toLocaleLowerCase() : n;
}, j = {
Y(e) {
const t = e.year;
return t <= 9999 ? `${t}` : `+${t}`;
},
// Year: 00, 01, ..., 99
yy(e) {
return l(e.year, 4).substr(2);
},
// Year: 1900, 1901, ..., 2099
yyyy(e) {
return l(e.year, 4);
},
// Month: 1, 2, ..., 12
M(e) {
return e.month + 1;
},
// Month: 01, 02, ..., 12
MM(e) {
return l(e.month + 1, 2);
},
MMM(e) {
return I[e.month];
},
MMMM(e) {
return L.monthNames[e.month];
},
// Day of month: 1, 2, ..., 31
d(e) {
return e.day;
},
// Day of month: 01, 02, ..., 31
dd(e) {
return l(e.day, 2);
},
// Hour: 0, 1, ... 23
H(e) {
return e.hour;
},
// Hour: 00, 01, ..., 23
HH(e) {
return l(e.hour, 2);
},
// Hour: 1, 2, ..., 12
h(e) {
const t = e.hour;
return t === 0 ? 12 : t > 12 ? t % 12 : t;
},
// Hour: 01, 02, ..., 12
hh(...e) {
const t = j.h(...e);
return l(t, 2);
},
// Minute: 0, 1, ..., 59
m(e) {
return e.minute;
},
// Minute: 00, 01, ..., 59
mm(e) {
return l(e.minute, 2);
},
// Second: 0, 1, ..., 59
s(e) {
return e.second;
},
// Second: 00, 01, ..., 59
ss(e) {
return l(e.second, 2);
},
// 1/10 of second: 0, 1, ..., 9
S(e) {
return Math.floor(e.millisecond / 100);
},
// 1/100 of second: 00, 01, ..., 99
SS(e) {
return l(Math.floor(e.millisecond / 10), 2);
},
// Millisecond: 000, 001, ..., 999
SSS(e) {
return l(e.millisecond, 3);
},
// Day of week: 0, 1, ..., 6
D(e) {
return e.day;
},
// Day of week: 'Su', 'Mo', ..., 'Sa'
DD(e) {
return ae[e.day];
},
// Day of week: 'Sun', 'Mon',..., 'Sat'
ddd(e) {
return ne[e.day];
},
// Day of week: 'Sunday', 'Monday', ...,'Saturday'
dddd(e) {
return L.dayNames[e.day];
},
// AM, PM
A(e) {
return C(e.hour, e.minute, !1);
},
// am, pm
a(e) {
return C(e.hour, e.minute, !0);
},
// Timezone: -01:00, +00:00, ... +12:00
Z(e) {
return v(N(e), ":");
},
// Timezone: -0100, +0000, ... +1200
ZZ(e) {
return v(N(e));
},
// Seconds timestamp: 512969520
X(e) {
return Math.floor(e.timestamp / 1e3);
},
// Milliseconds timestamp: 512969520900
x(e) {
return e.timestamp;
},
// w(date) {
// return getWeek(date, {
// firstDayOfWeek: firstDayOfWeek,
// });
// },
ww(e) {
return l(j.w(e), 2);
}
};
function se(e, t) {
return (t ? String(t) : "YYYY-MM-DDTHH:mm:ss.SSSZ").replace(oe, (n, a) => a || (typeof j[n] == "function" ? `${j[n](e)}` : n));
}
const F = [
"فروردین",
"اردیبهشت",
"خرداد",
"تیر",
"مرداد",
"شهریور",
"مهر",
"آبان",
"آذر",
"دی",
"بهمن",
"اسفند"
], ie = /(\[[^[]*\])|(MM?M?M?|Do|DD?|ddd?d?|w[o|w]?|YYYY|YY|yyyy|yy|a|A|hh?|HH?|mm?|ss?|S{1,3}|x|X|ZZ?|.)/g, ue = /\d/, M = /\d\d/, ce = /\d{3}/, fe = /\d{4}/, w = /\d\d?/, le = /[+-]\d\d:?\d\d/, de = /[+-]?\d+/, E = "year", H = "month", z = "day", W = "hour", G = "minute", X = "second", A = "millisecond", _ = {}, f = (e, t, r) => {
const n = Array.isArray(e) ? e : [e];
let a;
typeof r == "string" ? a = (o) => {
const i = parseInt(o, 10);
return { [r]: i };
} : a = r, n.forEach((o) => {
_[o] = [t, a];
});
}, me = (e) => e.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&"), U = (e) => () => new RegExp(e.map(me).join("|")), V = (e, t) => (r) => {
const n = e.indexOf(r);
if (n < 0)
throw new Error("Invalid Word");
return { [t]: n };
};
f("Y", de, E);
f("YY", M, (e) => {
const t = (/* @__PURE__ */ new Date()).getFullYear(), r = Math.floor(t / 100);
let n = parseInt(e, 10);
return n = (n > 68 ? r - 1 : r) * 100 + n, { [E]: n };
});
f("yyyy", fe, E);
f("M", w, (e) => ({ [H]: parseInt(e, 10) - 1 }));
f("MM", M, (e) => ({ [H]: parseInt(e, 10) - 1 }));
f(
"MMM",
U(I),
V(I, H)
);
f(
"MMMM",
U(F),
V(F, H)
);
f("d", w, z);
f("dd", M, z);
f(["H", "h"], w, W);
f(["HH", "hh"], M, W);
f("m", w, G);
f("mm", M, G);
f("s", w, X);
f("ss", M, X);
f("S", ue, (e) => ({
[A]: parseInt(e, 10) * 100
}));
f("SS", M, (e) => ({
[A]: parseInt(e, 10) * 10
}));
f("SSS", ce, A);
function he() {
return /[ap]\.?m?\.?/i;
}
function ye(e) {
return `${e}`.toLowerCase().charAt(0) === "p";
}
f(["A", "a"], he, (e) => ({ isPM: ye(e) }));
function ge(e) {
const [t, r, n] = e.match(/([+-]|\d\d)/g) || ["-", "0", "0"], a = parseInt(r, 10) * 60 + parseInt(n, 10);
return a === 0 ? 0 : t === "+" ? -a : +a;
}
f(["Z", "ZZ"], le, (e) => ({ offset: ge(e) }));
function Me(e, t) {
if (e !== void 0 && t !== void 0) {
if (t) {
if (e < 12)
return e + 12;
} else if (e === 12)
return 0;
}
return e;
}
function pe(e, t) {
const r = t.match(ie);
if (!r)
throw new Error();
const { length: n } = r;
let a = {};
for (let o = 0; o < n; o += 1) {
const i = r[o], s = _[i];
if (s) {
const m = typeof s[0] == "function" ? s[0]() : s[0], p = s[1], h = (m.exec(e) || [])[0], c = p(h || "");
a = { ...a, ...c }, e = e.replace(h || "", "");
} else {
const m = i.replace(/^\[|\]$/g, "");
if (e.indexOf(m) === 0)
e = e.substr(m.length);
else
throw new Error("not match");
}
}
return a;
}
function De(e, t, r = /* @__PURE__ */ new Date()) {
const n = {
year: r.getFullYear(),
month: r.getMonth(),
day: r.getDate(),
hour: r.getHours(),
minute: r.getMinutes(),
second: r.getSeconds(),
millisecond: r.getMilliseconds(),
timezoneOffset: r.getTimezoneOffset()
}, a = pe(e, t);
return ["year", "month", "day", "hour", "minute", "second", "millisecond"].forEach((o) => {
a[o] && (n[o] = a[o] || 0);
}), n.hour = Me(n.hour, a.isPM) || 0, a.offset !== void 0 && (n.timezoneOffset = a.offset), n;
}
const L = {
dayNames: [
"یکشنبه",
"دوشنبه",
"سهشنبه",
"چهارشنبه",
"پنجشنبه",
"جمعه",
"شنبه"
],
monthNames: [
"فروردین",
"اردیبهشت",
"خرداد",
"تیر",
"مرداد",
"شهریور",
"مهر",
"آبان",
"آذر",
"دی",
"بهمن",
"اسفند"
],
format: function(e, t) {
if (!e)
return "";
const r = D.toJalaali(e);
return se(
{
year: r.jy,
month: r.jm - 1,
day: r.jd,
hour: e.getHours(),
minute: e.getMinutes(),
second: e.getSeconds(),
millisecond: e.getMilliseconds(),
timestamp: e.getTime(),
timezoneOffset: e.getTimezoneOffset()
},
t
);
},
parse: function(e, t, r) {
const n = De(e, t, r || /* @__PURE__ */ new Date()), a = D.jalaaliToDateObject(
n.year,
n.month + 1,
n.day
);
return a.setHours(n.hour, n.minute, n.second, n.millisecond), a;
},
toDateParts: function(e) {
return new Intl.DateTimeFormat("fa-IR-u-nu-latn", {
year: "numeric",
month: "numeric",
day: "numeric"
}).formatToParts(e).reduce((t, r) => (r.type !== "literal" && (t[r.type] = +r.value), t), {});
},
years: function({ type: e, year: t }) {
const r = D.toJalaali(/* @__PURE__ */ new Date()).jy;
return [...Array(200).keys()].map((n) => ({
type: e,
key: e + n,
isToday: r === n + 1300,
isSelected: t === n + 1300,
isHeader: !1,
isDisabled: !1,
value: n + 1300,
text: n + 1300 + ""
}));
},
months: function({ type: e, month: t }) {
const r = D.toJalaali(/* @__PURE__ */ new Date()).jm;
return [...this.monthNames.keys()].map((n) => ({
type: e,
key: e + n,
isToday: r === n + 1,
isSelected: t === n + 1,
isHeader: !1,
isDisabled: !1,
value: n + 1,
text: this.monthNames[n]
}));
},
days: function({ type: e, month: t, startOfWeek: r, year: n, value: a }) {
const o = D.jalaaliToDateObject(n, t, 1), i = D.jalaaliToDateObject(n, t + 1, 1);
i.setDate(i.getDate() - 1);
const s = S(r - 1, 7), m = new Intl.DateTimeFormat("fa-IR-u-nu-latn", {
day: "numeric"
}), p = q().getTime(), h = a ? B(a).getTime() : 0;
return this.dayNames.map((c, g) => {
const k = S(r + g, 7);
return {
type: e,
key: "weekday" + k,
isToday: !1,
isSelected: !1,
isHeader: !0,
isDisabled: !1,
value: g,
text: this.dayNames[k]
};
}).concat(
K({
start: J(o, -S(o.getDay() - r, 7)),
end: J(i, S(s - i.getDay(), 7))
}).map((c) => ({
type: e,
key: c.toString(),
isToday: p === c.getTime(),
isSelected: h === c.getTime(),
isHeader: !1,
isDisabled: c < o || c > i,
isInCurrentMonth: c >= o && c <= i,
value: c,
text: m.format(c)
}))
);
},
hours: function({ type: e, hour: t }) {
return [...Array(24).keys()].map((r) => ({
type: e,
key: r,
value: r,
text: r + "",
isToday: !1,
isSelected: t === r,
isHeader: !1,
isDisabled: !1
}));
},
minutes: function({ type: e, minute: t }) {
return [...Array(60).keys()].map((r) => ({
type: e,
key: r,
value: r,
text: r + "",
isToday: !1,
isSelected: t === r,
isHeader: !1,
isDisabled: !1
}));
}
};
function S(e, t) {
return (e % t + t) % t;
}
export {
L as config
};