346 lines
11 KiB
JavaScript
346 lines
11 KiB
JavaScript
"use strict";
|
|
var __create = Object.create;
|
|
var __defProp = Object.defineProperty;
|
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
var __getProtoOf = Object.getPrototypeOf;
|
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
var __spreadValues = (a, b) => {
|
|
for (var prop in b || (b = {}))
|
|
if (__hasOwnProp.call(b, prop))
|
|
__defNormalProp(a, prop, b[prop]);
|
|
if (__getOwnPropSymbols)
|
|
for (var prop of __getOwnPropSymbols(b)) {
|
|
if (__propIsEnum.call(b, prop))
|
|
__defNormalProp(a, prop, b[prop]);
|
|
}
|
|
return a;
|
|
};
|
|
var __export = (target, all) => {
|
|
for (var name in all)
|
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
};
|
|
var __copyProps = (to, from, except, desc) => {
|
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
for (let key of __getOwnPropNames(from))
|
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
}
|
|
return to;
|
|
};
|
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
// If the importer is in node compatibility mode or this is not an ESM
|
|
// file that has been converted to a CommonJS file using a Babel-
|
|
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
mod
|
|
));
|
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
|
|
// src/index.ts
|
|
var index_exports = {};
|
|
__export(index_exports, {
|
|
default: () => index_default
|
|
});
|
|
module.exports = __toCommonJS(index_exports);
|
|
var import_fa = __toESM(require("dayjs/locale/fa"));
|
|
var import_jalaali_js = require("jalaali-js");
|
|
|
|
// src/constant.ts
|
|
var REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})[-/]?(\d{0,2})(.*)?$/;
|
|
var REGEX_FORMAT = /\[.*?\]|jY{2,4}|jM{1,4}|jD{1,2}|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g;
|
|
var DATE = "date";
|
|
var D = "day";
|
|
var M = "month";
|
|
var Y = "year";
|
|
var W = "week";
|
|
var FORMAT_DEFAULT = "YYYY-MM-DDTHH:mm:ssZ";
|
|
var fa = {
|
|
jmonths: "\u0641\u0631\u0648\u0631\u062F\u06CC\u0646_\u0627\u0631\u062F\u06CC\u0628\u0647\u0634\u062A_\u062E\u0631\u062F\u0627\u062F_\u062A\u06CC\u0631_\u0645\u0631\u062F\u0627\u062F_\u0634\u0647\u0631\u06CC\u0648\u0631_\u0645\u0647\u0631_\u0622\u0628\u0627\u0646_\u0622\u0630\u0631_\u062F\u06CC_\u0628\u0647\u0645\u0646_\u0627\u0633\u0641\u0646\u062F".split("_")
|
|
};
|
|
|
|
// src/index.ts
|
|
var plugin = (option, dayjsClass, dayjsFactory) => {
|
|
const proto = dayjsClass.prototype;
|
|
const factory = dayjsFactory;
|
|
const U = proto.$utils();
|
|
const $isJalali = (v) => v.$C === "jalali";
|
|
const $prettyUnit = U.prettyUnit || U.p;
|
|
const $isUndefined = U.isUndefined || U.u;
|
|
const $padStart = U.padStart || U.s;
|
|
const $monthDiff = U.monthDiff || U.m;
|
|
const $absFloor = U.absFloor || U.a;
|
|
const wrapperOfTruth = (action) => function(...args) {
|
|
const unsure = action.bind(this)(...args);
|
|
unsure.$C = this.$C;
|
|
if (unsure.isJalali()) {
|
|
unsure.InitJalali();
|
|
}
|
|
return unsure;
|
|
};
|
|
proto.startOf = wrapperOfTruth(proto.startOf);
|
|
proto.endOf = wrapperOfTruth(proto.endOf);
|
|
proto.add = wrapperOfTruth(proto.add);
|
|
proto.subtract = wrapperOfTruth(proto.subtract);
|
|
proto.set = wrapperOfTruth(proto.set);
|
|
const oldParse = proto.parse;
|
|
const oldInit = proto.init;
|
|
const oldStartOf = proto.startOf;
|
|
const old$Set = proto.$set;
|
|
const oldAdd = proto.add;
|
|
const oldFormat = proto.format;
|
|
const oldDiff = proto.diff;
|
|
const oldYear = proto.year;
|
|
const oldMonth = proto.month;
|
|
const oldDate = proto.date;
|
|
const oldDaysInMonth = proto.daysInMonth;
|
|
const oldToArray = proto.toArray;
|
|
factory.$C = "gregory";
|
|
factory.$fdow = 6;
|
|
factory.calendar = function(calendar) {
|
|
factory.$C = calendar;
|
|
return factory;
|
|
};
|
|
proto.calendar = function(calendar) {
|
|
const that = this.clone();
|
|
that.$C = calendar;
|
|
if (that.isJalali()) {
|
|
that.InitJalali();
|
|
}
|
|
return that;
|
|
};
|
|
proto.isJalali = function() {
|
|
return $isJalali(this);
|
|
};
|
|
factory.en = factory.en || {};
|
|
factory.en.jmonths = "Farvardin_Ordibehesht_Khordaad_Tir_Mordaad_Shahrivar_Mehr_Aabaan_Aazar_Dey_Bahman_Esfand".split("_");
|
|
factory.locale("fa", __spreadValues(__spreadValues({}, import_fa.default), fa), true);
|
|
const wrapper = function(date, instance) {
|
|
return factory(date, {
|
|
locale: instance.$L,
|
|
utc: instance.$u,
|
|
calendar: instance.$C
|
|
});
|
|
};
|
|
proto.init = function(cfg = {}) {
|
|
oldInit.bind(this)(cfg);
|
|
if (this.isJalali()) {
|
|
this.InitJalali();
|
|
}
|
|
};
|
|
proto.parse = function(cfg) {
|
|
let reg = null;
|
|
this.$C = cfg.calendar || this.$C || factory.$C;
|
|
if (cfg.jalali && typeof cfg.date === "string" && /.*[^Z]$/i.test(cfg.date)) {
|
|
reg = cfg.date.match(REGEX_PARSE);
|
|
if (reg) {
|
|
const { gy: y, gm: m, gd: d } = (0, import_jalaali_js.toGregorian)(
|
|
parseInt(reg[1], 10),
|
|
parseInt(reg[2], 10),
|
|
parseInt(reg[3] || "1", 10)
|
|
);
|
|
cfg.date = `${y}-${m}-${d}${reg[4] || ""}`;
|
|
}
|
|
}
|
|
return oldParse.bind(this)(cfg);
|
|
};
|
|
proto.InitJalali = function() {
|
|
const { jy, jm, jd } = (0, import_jalaali_js.toJalaali)(this.$y, this.$M + 1, this.$D);
|
|
this.$jy = jy;
|
|
this.$jM = jm - 1;
|
|
this.$jD = jd;
|
|
};
|
|
const instanceFactory = ({ d, m, y }, context) => {
|
|
const year = y != null ? y : context.$jy;
|
|
const { gy, gm, gd } = (0, import_jalaali_js.toGregorian)(year, m + 1, d);
|
|
return wrapper(new Date(gy, gm - 1, gd), context);
|
|
};
|
|
proto.startOf = function(units, startOf = true) {
|
|
const def = () => oldStartOf.bind(this)(units, startOf);
|
|
if (!$isJalali(this)) {
|
|
return def();
|
|
}
|
|
const unit = $prettyUnit(units);
|
|
const WModifier = (this.$W + (7 - factory.$fdow)) % 7;
|
|
switch (unit) {
|
|
case Y:
|
|
return startOf ? instanceFactory({ d: 1, m: 0 }, this) : instanceFactory({ d: 0, m: 0, y: (this.$jy || 0) + 1 }, this);
|
|
case M:
|
|
return startOf ? instanceFactory({ d: 1, m: this.$jM }, this) : instanceFactory({
|
|
d: 0,
|
|
m: ((this.$jM || 0) + 1) % 12,
|
|
y: (this.$jy || 0) + Math.floor(((this.$jM || 0) + 1) / 12)
|
|
}, this);
|
|
case W:
|
|
return startOf ? instanceFactory({ d: (this.$jD || 0) - WModifier, m: this.$jM }, this) : instanceFactory({ d: (this.$jD || 0) + (6 - WModifier), m: this.$jM }, this);
|
|
default:
|
|
return def();
|
|
}
|
|
};
|
|
proto.$set = function(units, value) {
|
|
const def = () => old$Set.bind(this)(units, value);
|
|
if (!$isJalali(this)) {
|
|
return def();
|
|
}
|
|
const unit = $prettyUnit(units);
|
|
const d = this.$jD || 1;
|
|
const m = this.$jM || 0;
|
|
const y = this.$jy || 0;
|
|
switch (unit) {
|
|
case DATE:
|
|
case D:
|
|
return instanceFactory({ d: value, m, y }, this);
|
|
case M:
|
|
return instanceFactory({ d, m: value, y }, this);
|
|
case Y:
|
|
return instanceFactory({ d, m, y: value }, this);
|
|
default:
|
|
return def();
|
|
}
|
|
};
|
|
proto.add = function(number, units) {
|
|
const def = () => oldAdd.bind(this)(number, units);
|
|
if (!$isJalali(this)) {
|
|
return def();
|
|
}
|
|
const num = Number(number);
|
|
const defaultUnit = "d";
|
|
const unit = units ? $prettyUnit(units) : defaultUnit;
|
|
const addInstanceFactory = (u, n) => {
|
|
const date = this.set("date", 1).set(u, n + num);
|
|
return date.set("date", Math.min(this.$jD || 0, date.daysInMonth()));
|
|
};
|
|
if (["M", "month"].indexOf(unit) > -1) {
|
|
const n = (this.$jM || 0) + num;
|
|
const y = n < 0 ? -Math.ceil(-n / 12) : Math.floor(n / 12);
|
|
const d = this.$jD || 0;
|
|
const x = this.set("day", 1).add(y, "year").set("month", n - y * 12);
|
|
return x.set("day", Math.min(x.daysInMonth(), d));
|
|
}
|
|
if (["y", "year"].indexOf(unit) > -1) {
|
|
return addInstanceFactory("year", this.$jy || 0);
|
|
}
|
|
if (["d", "day"].indexOf(unit) > -1) {
|
|
const date = new Date(this.$d);
|
|
date.setDate(date.getDate() + num);
|
|
return wrapper(date, this);
|
|
}
|
|
return def();
|
|
};
|
|
proto.format = function(formatStr, localeObject) {
|
|
if (!$isJalali(this)) {
|
|
return oldFormat.bind(this)(formatStr || "");
|
|
}
|
|
const str = formatStr || FORMAT_DEFAULT;
|
|
const locale = localeObject || this.$locale();
|
|
const { jmonths } = locale || { jmonths: [] };
|
|
return str.replace(REGEX_FORMAT, (match) => {
|
|
var _a;
|
|
if (match.indexOf("[") > -1) return match.replace(/\[|\]/g, "");
|
|
const jy = this.$jy || 0;
|
|
const jm = this.$jM || 0;
|
|
const jd = this.$jD || 0;
|
|
switch (match) {
|
|
case "YY":
|
|
return String(jy).slice(-2);
|
|
case "YYYY":
|
|
return String(jy);
|
|
case "M":
|
|
return String(jm + 1);
|
|
case "MM":
|
|
return $padStart(jm + 1, 2, "0");
|
|
case "MMM":
|
|
return ((_a = jmonths[jm]) == null ? void 0 : _a.slice(0, 3)) || "";
|
|
case "MMMM":
|
|
return jmonths[jm] || "";
|
|
case "D":
|
|
return String(jd);
|
|
case "DD":
|
|
return $padStart(jd, 2, "0");
|
|
default:
|
|
return oldFormat.bind(this)(match);
|
|
}
|
|
});
|
|
};
|
|
proto.diff = function(input, units, float) {
|
|
const def = () => oldDiff.bind(this)(input, units, float);
|
|
if (!$isJalali(this)) {
|
|
return def();
|
|
}
|
|
const unit = units ? $prettyUnit(units) : void 0;
|
|
const that = factory(input);
|
|
let result = $monthDiff(this, that);
|
|
switch (unit) {
|
|
case Y:
|
|
result /= 12;
|
|
break;
|
|
case M:
|
|
break;
|
|
default:
|
|
return def();
|
|
}
|
|
return float ? result : $absFloor(result);
|
|
};
|
|
proto.$g = function(input, get, set) {
|
|
if ($isUndefined(input)) return this[get];
|
|
return this.set(set, input);
|
|
};
|
|
proto.year = function(input) {
|
|
if (!$isJalali(this)) {
|
|
return oldYear.bind(this)(input);
|
|
}
|
|
if (typeof input === "undefined") {
|
|
return this.$jy || 0;
|
|
}
|
|
return this.set("year", input);
|
|
};
|
|
proto.month = function(input) {
|
|
if (!$isJalali(this)) {
|
|
return oldMonth.bind(this)(input);
|
|
}
|
|
if (typeof input === "undefined") {
|
|
return this.$jM || 0;
|
|
}
|
|
return this.set("month", input);
|
|
};
|
|
proto.date = function(input) {
|
|
if (!$isJalali(this)) {
|
|
return oldDate.bind(this)(input);
|
|
}
|
|
if (typeof input === "undefined") {
|
|
return this.$jD || 0;
|
|
}
|
|
return this.set("date", input);
|
|
};
|
|
proto.daysInMonth = function() {
|
|
if (!$isJalali(this)) {
|
|
return oldDaysInMonth.bind(this)();
|
|
}
|
|
return (0, import_jalaali_js.jalaaliMonthLength)(this.$jy, this.$jM + 1);
|
|
};
|
|
if (oldToArray) {
|
|
proto.toArray = function() {
|
|
if (!$isJalali(this)) {
|
|
return oldToArray.bind(this)();
|
|
}
|
|
return [
|
|
this.$jy || 0,
|
|
this.$jM || 0,
|
|
this.$jD || 0,
|
|
this.$H || 0,
|
|
this.$m || 0,
|
|
this.$s || 0,
|
|
this.$ms || 0
|
|
];
|
|
};
|
|
}
|
|
proto.clone = function() {
|
|
return wrapper(this.toDate(), this);
|
|
};
|
|
};
|
|
var index_default = plugin;
|
|
//# sourceMappingURL=index.js.map
|