react-np-datepicker
Version:
Integrate Nepali Datepicker in your React app easily.
635 lines (578 loc) • 16.9 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import moment from 'moment';
import { base_ad, base_bs, end_bs, calendar_data, en, ne } from './constant';
var BSMoment = /*#__PURE__*/function () {
function BSMoment() {
var date = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : base_bs;
_classCallCheck(this, BSMoment);
if (!date.year || !date.month || !date.day) {
var year = null;
var month = null;
var day = null;
if (typeof date === 'string') {
var dateSplit = null;
if (date.includes('-')) dateSplit = date.split('-');else if (date.includes('/')) dateSplit = date.split('/');
if (dateSplit) {
year = parseInt(dateSplit[0]);
month = parseInt(dateSplit[1]);
day = parseInt(dateSplit[2]);
}
}
if (typeof date === 'object') {
year = parseInt(date._date.year);
month = parseInt(date._date.month);
day = parseInt(date._date.day);
} // console.log(date)
date = {
year: year,
month: month,
day: day
};
}
if (BSMoment.isLess(date, base_bs)) {
date = base_bs;
} else if (BSMoment.isMore(date, end_bs)) {
date = end_bs;
}
this._date = date;
this._diff = {
day: this._diffDay.bind(this),
month: this._diffMonth.bind(this),
year: this._diffYear.bind(this)
};
this._add = {
day: this._addDays.bind(this),
month: this._addMonth.bind(this),
year: this._addYear.bind(this)
};
this._subtract = {
day: this._subtractDays.bind(this),
month: this._subtractMonth.bind(this),
year: this._subtractYear.bind(this)
};
this._startOf = {
day: this._startOfDay.bind(this),
month: this._startOfMonth.bind(this),
year: this._startOfYear.bind(this)
};
this._endOf = {
day: this._endOfDay.bind(this),
month: this._endOfMonth.bind(this),
year: this._endOfYear.bind(this)
};
}
_createClass(BSMoment, [{
key: "_toDate",
value: function _toDate(date) {
return new BSMoment(date);
}
}, {
key: "_addDays",
value: function _addDays() {
var count = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var _this$_date = this._date,
year = _this$_date.year,
month = _this$_date.month,
day = _this$_date.day;
day += count;
while (calendar_data[year] && day > calendar_data[year][month - 1]) {
day -= calendar_data[year][month - 1];
month++;
if (month > 12) {
year++;
month = 1;
}
}
return this._toDate({
year: year,
month: month,
day: day
});
}
}, {
key: "_addMonth",
value: function _addMonth() {
var count = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var _this$_date2 = this._date,
year = _this$_date2.year,
month = _this$_date2.month,
day = _this$_date2.day;
month += count;
while (month > 12) {
month -= 12;
year++;
}
if (calendar_data[year]) {
var totalDaysInNewMonth = calendar_data[year][month - 1];
if (day > totalDaysInNewMonth) {
day = totalDaysInNewMonth;
}
}
return this._toDate({
year: year,
month: month,
day: day
});
}
}, {
key: "_addYear",
value: function _addYear(count) {
var _this$_date3 = this._date,
year = _this$_date3.year,
month = _this$_date3.month,
day = _this$_date3.day;
year += count;
if (calendar_data[year]) {
var totalDaysInNewMonth = calendar_data[year][month - 1];
if (day > totalDaysInNewMonth) {
day = totalDaysInNewMonth;
}
}
return this._toDate({
year: year,
month: month,
day: day
});
}
}, {
key: "_subtractDays",
value: function _subtractDays() {
var count = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var _this$_date4 = this._date,
year = _this$_date4.year,
month = _this$_date4.month,
day = _this$_date4.day;
while (count >= day) {
count -= day;
month--;
if (month < 1) {
year--;
month = 1;
}
if (calendar_data[year]) {
day = calendar_data[year][month - 1];
}
}
return this._toDate({
year: year,
month: month,
day: day
});
}
}, {
key: "_subtractMonth",
value: function _subtractMonth() {
var count = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var _this$_date5 = this._date,
year = _this$_date5.year,
month = _this$_date5.month,
day = _this$_date5.day;
while (count >= month) {
count -= month;
year--;
month = 12;
}
month -= count;
if (calendar_data[year]) {
var totalDaysInNewMonth = calendar_data[year][month - 1];
if (day > totalDaysInNewMonth) {
day = totalDaysInNewMonth;
}
}
return this._toDate({
year: year,
month: month,
day: day
});
}
}, {
key: "_subtractYear",
value: function _subtractYear(count) {
var _this$_date6 = this._date,
year = _this$_date6.year,
month = _this$_date6.month,
day = _this$_date6.day;
year -= count;
if (calendar_data[year]) {
var totalDaysInNewMonth = calendar_data[year][month - 1];
if (day > totalDaysInNewMonth) {
day = totalDaysInNewMonth;
}
}
return this._toDate({
year: year,
month: month,
day: day
});
}
}, {
key: "_startOfYear",
value: function _startOfYear() {
var _this$_date7 = this._date,
year = _this$_date7.year,
month = _this$_date7.month,
day = _this$_date7.day;
month = 1;
day = 1;
return this._toDate({
year: year,
month: month,
day: day
});
}
}, {
key: "_startOfMonth",
value: function _startOfMonth() {
var _this$_date8 = this._date,
year = _this$_date8.year,
month = _this$_date8.month,
day = _this$_date8.day;
day = 1;
return this._toDate({
year: year,
month: month,
day: day
});
}
}, {
key: "_startOfDay",
value: function _startOfDay() {
return this._toDate(_objectSpread({}, this._date));
}
}, {
key: "_endOfYear",
value: function _endOfYear() {
var _this$_date9 = this._date,
year = _this$_date9.year,
month = _this$_date9.month,
day = _this$_date9.day;
month = 12;
day = parseInt(calendar_data[year][11], 10);
return this._toDate({
year: year,
month: month,
day: day
});
}
}, {
key: "_endOfMonth",
value: function _endOfMonth() {
var _this$_date10 = this._date,
year = _this$_date10.year,
month = _this$_date10.month,
day = _this$_date10.day;
day = parseInt(calendar_data[year][month - 1], 10);
return this._toDate({
year: year,
month: month,
day: day
});
}
}, {
key: "_endOfDay",
value: function _endOfDay() {
return this._toDate(_objectSpread({}, this._date));
}
}, {
key: "_diffDay",
value: function _diffDay(start, end) {
var count = 0;
for (var i = start.year; i <= end.year; i++) {
count += calendar_data[i][12];
}
for (var _i = 0; _i < start.month - 1; _i++) {
count -= calendar_data[start.year][_i];
}
for (var _i2 = end.month - 1; _i2 < 12; _i2++) {
count -= calendar_data[end.year][_i2];
}
count -= start.day;
count += end.day;
return count;
}
}, {
key: "_diffMonth",
value: function _diffMonth(start, end) {
return (end.year - start.year) * 12 + end.month - start.month;
}
}, {
key: "_diffYear",
value: function _diffYear(start, end) {
return end.year - start.year;
}
}, {
key: "year",
value: function year(_year) {
if (_year) {
var _this$_date11 = this._date,
month = _this$_date11.month,
day = _this$_date11.day;
return this._toDate({
year: _year,
day: day,
month: month
});
}
return this._date.year;
}
}, {
key: "month",
value: function month(_month) {
if (_month) {
var _this$_date12 = this._date,
year = _this$_date12.year,
day = _this$_date12.day;
return this._toDate({
year: year,
day: day,
month: _month
});
}
return this._date.month;
}
}, {
key: "date",
value: function date(day) {
if (day) {
var _this$_date13 = this._date,
year = _this$_date13.year,
month = _this$_date13.month;
return this._toDate({
year: year,
month: month,
day: day
});
}
return this._date.day;
}
}, {
key: "getMonthName",
value: function getMonthName() {
return ne.monthsName[this._date.month - 1]; // return en.monthsName[this._date.month - 1];
}
}, {
key: "getDay",
value: function getDay() {
return this.toAD().getDay();
}
}, {
key: "now",
value: function now() {
var adToday = moment(),
adBase = moment("".concat(base_ad.year, "/").concat(base_ad.month, "/").concat(base_ad.day), 'YYYY/MM/DD'),
diffInDays = adToday.diff(adBase, 'day');
return this.add(diffInDays, 'day');
}
}, {
key: "add",
value: function add(count) {
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'day';
return this._add[type](count);
}
}, {
key: "subtract",
value: function subtract(count) {
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'day';
return this._subtract[type](count);
}
}, {
key: "startOf",
value: function startOf() {
var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'day';
return this._startOf[type]();
}
}, {
key: "endOf",
value: function endOf() {
var type = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'day';
return this._endOf[type]();
}
}, {
key: "diff",
value: function diff(dateObj) {
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'day';
var inc = false,
start = this._date,
end = dateObj._date || dateObj;
if (start.year > end.year) {
inc = true;
} else if (start.year === end.year && start.month > end.month) {
inc = true;
} else if (start.year === end.year && start.month === end.month && start.day > end.day) {
inc = true;
}
if (inc) {
start = dateObj._date || dateObj;
end = this._date;
}
return this._diff[type](start, end);
}
}, {
key: "toAD",
value: function toAD() {
var daysCount = this.diff(base_bs),
adBase = moment("".concat(base_ad.year, "/").concat(base_ad.month, "/").concat(base_ad.day), 'YYYY/MM/DD');
return adBase.add(daysCount, 'day');
}
}, {
key: "weekday",
value: function weekday(count) {
var adDate = this.toAD().weekday(count);
return BSMoment.toBS(adDate);
}
}, {
key: "isBefore",
value: function isBefore(date) {
var _ref = date._date || date,
year = _ref.year,
month = _ref.month,
day = _ref.day;
if (this._date.year < year) {
return true;
} else if (this._date.year === year && this._date.month < month) {
return true;
} else if (this._date.year === year && this._date.month === month && this._date.day < day) {
return true;
}
}
}, {
key: "isSameOrBefore",
value: function isSameOrBefore(date) {
var _ref2 = date._date || date,
year = _ref2.year,
month = _ref2.month,
day = _ref2.day;
if (this._date.year < year) {
return true;
} else if (this._date.year === year && this._date.month < month) {
return true;
} else if (this._date.year === year && this._date.month === month && this._date.day <= day) {
return true;
}
}
}, {
key: "isAfter",
value: function isAfter(date) {
var _ref3 = date._date || date,
year = _ref3.year,
month = _ref3.month,
day = _ref3.day;
if (this._date.year > year) {
return true;
} else if (this._date.year === year && this._date.month > month) {
return true;
} else if (this._date.year === year && this._date.month === month && this._date.day > day) {
return true;
}
}
}, {
key: "isSame",
value: function isSame(date) {
var _ref4 = date._date || date,
year = _ref4.year,
month = _ref4.month,
day = _ref4.day;
if (this._date.year === year && this._date.month === month && this._date.day === day) {
return true;
}
}
}, {
key: "isSameOrAfter",
value: function isSameOrAfter(date) {
var _ref5 = date._date || date,
year = _ref5.year,
month = _ref5.month,
day = _ref5.day;
if (this._date.year > year) {
return true;
} else if (this._date.year === year && this._date.month > month) {
return true;
} else if (this._date.year === year && this._date.month === month && this._date.day >= day) {
return true;
}
}
}, {
key: "format",
value: function format(formatString) {
var _this$_date14 = this._date,
year = _this$_date14.year,
month = _this$_date14.month,
day = _this$_date14.day;
return formatString.replace('YYYY', year).replace('YY', year.toString().substr(2, 2)).replace('MMM', en.monthsName[month - 1]).replace('MM', month <= 9 ? "0".concat(month) : month).replace('DD', day <= 9 ? "0".concat(day) : day).replace('D', day).replace('M', month);
}
}], [{
key: "range",
value: function range(start, end) {
var type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'day';
var items = [],
i;
for (i = start; i.isBefore(end_bs) && i.isSameOrBefore(end); i = i.add(1, type)) {
items.push(i);
}
;
if (end.isSame(end_bs)) {
items.push(end);
}
return items;
}
}, {
key: "toBS",
value: function toBS(adDate) {
var momentDate = adDate ? moment(adDate, 'YYYY/MM/DD') : moment(),
adBase = moment("".concat(base_ad.year, "/").concat(base_ad.month, "/").concat(base_ad.day), 'YYYY/MM/DD'),
daysCount = momentDate.diff(adBase, 'day');
return new BSMoment(base_bs).add(daysCount, 'day');
}
}, {
key: "monthsShortName",
value: function monthsShortName() {
return ne.monthsShortName; // return en.monthsShortName;
}
}, {
key: "isLess",
value: function isLess(a, b) {
var year = a.year,
month = a.month,
day = a.day;
if (year < b.year) {
return true;
} else if (year === b.year && month < b.month) {
return true;
} else if (year === b.year && month === b.month && day < b.day) {
return true;
}
}
}, {
key: "isEqual",
value: function isEqual(a, b) {
var year = a.year,
month = a.month,
day = a.day;
if (year === b.year) {
return true;
} else if (year === b.year && month === b.month) {
return true;
} else if (year === b.year && month === b.month && day === b.day) {
return true;
}
}
}, {
key: "isMore",
value: function isMore(a, b) {
var year = a.year,
month = a.month,
day = a.day;
if (year > b.year) {
return true;
} else if (year === b.year && month > b.month) {
return true;
} else if (year === b.year && month === b.month && day > b.day) {
return true;
}
}
}]);
return BSMoment;
}();
export { BSMoment as default };