@date-io/date-fns-jalali
Advanced tools
Comparing version
import { IUtils, DateIOFormats, Unit } from "@date-io/core/IUtils"; | ||
import defaultLocale from "date-fns-jalali/locale/fa-IR"; | ||
declare type Locale = typeof defaultLocale; | ||
type Locale = typeof defaultLocale; | ||
export default class DateFnsJalaliUtils implements IUtils<Date> { | ||
@@ -47,7 +47,7 @@ lib: string; | ||
setDate: (value: Date, count: number) => Date; | ||
date: (value?: string | number | Date) => Date; | ||
date: (value?: string | number | Date) => Date | null; | ||
toJsDate: (value: Date) => Date; | ||
parseISO: (isoString: string) => Date; | ||
toISO: (value: Date) => string; | ||
parse: (value: string, formatString: string) => Date; | ||
parse: (value: string, formatString: string) => Date | null; | ||
format: (date: Date, formatKey: keyof DateIOFormats) => string; | ||
@@ -60,2 +60,4 @@ formatByString: (date: Date, formatString: string) => string; | ||
isBeforeYear: (date: Date, value: Date) => boolean; | ||
isBeforeMonth(value: Date, comparing: Date): boolean; | ||
isAfterMonth(value: Date, comparing: Date): boolean; | ||
isAfterYear: (date: Date, value: Date) => boolean; | ||
@@ -62,0 +64,0 @@ isWithinRange: (date: Date, [start, end]: [Date, Date]) => boolean; |
@@ -56,3 +56,3 @@ import addSeconds from 'date-fns-jalali/addSeconds'; | ||
var defaultFormats = { | ||
const defaultFormats = { | ||
dayOfMonth: "d", | ||
@@ -98,12 +98,11 @@ fullDate: "PPP", | ||
}; | ||
var DateFnsJalaliUtils = /** @class */ (function () { | ||
function DateFnsJalaliUtils(_a) { | ||
var _this = this; | ||
var _b = _a === void 0 ? {} : _a, locale = _b.locale, formats = _b.formats; | ||
class DateFnsJalaliUtils { | ||
constructor({ locale, formats, } = {}) { | ||
this.lib = "date-fns-jalali"; | ||
// Note: date-fns input types are more lenient than this adapter, so we need to expose our more | ||
// strict signature and delegate to the more lenient signature. Otherwise, we have downstream type errors upon usage. | ||
this.is12HourCycleInCurrentLocale = function () { | ||
if (_this.locale) { | ||
return /a/.test(_this.locale.formatLong.time()); | ||
this.is12HourCycleInCurrentLocale = () => { | ||
var _a; | ||
if (this.locale) { | ||
return /a/.test((_a = this.locale.formatLong) === null || _a === void 0 ? void 0 : _a.time()); | ||
} | ||
@@ -113,141 +112,141 @@ // By default date-fns-jalali is using fa-IR locale with am/pm enabled | ||
}; | ||
this.getFormatHelperText = function (format) { | ||
this.getFormatHelperText = (format) => { | ||
var _a, _b; | ||
// @see https://github.com/date-fns/date-fns/blob/master/src/format/index.js#L31 | ||
var longFormatRegexp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g; | ||
var locale = _this.locale || defaultLocale; | ||
return format | ||
.match(longFormatRegexp) | ||
.map(function (token) { | ||
var firstCharacter = token[0]; | ||
const longFormatRegexp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g; | ||
const locale = this.locale || defaultLocale; | ||
return ((_b = (_a = format | ||
.match(longFormatRegexp)) === null || _a === void 0 ? void 0 : _a.map((token) => { | ||
const firstCharacter = token[0]; | ||
if (firstCharacter === "p" || firstCharacter === "P") { | ||
var longFormatter = longFormatters[firstCharacter]; | ||
const longFormatter = longFormatters[firstCharacter]; | ||
return longFormatter(token, locale.formatLong, {}); | ||
} | ||
return token; | ||
}) | ||
.join("") | ||
.replace(/(aaa|aa|a)/g, "(a|p)m") | ||
.toLocaleLowerCase(); | ||
}).join("").replace(/(aaa|aa|a)/g, "(a|p)m").toLocaleLowerCase()) !== null && _b !== void 0 ? _b : format); | ||
}; | ||
this.getCurrentLocaleCode = function () { | ||
this.getCurrentLocaleCode = () => { | ||
var _a; | ||
return ((_a = _this.locale) === null || _a === void 0 ? void 0 : _a.code) || "fa-IR"; | ||
return ((_a = this.locale) === null || _a === void 0 ? void 0 : _a.code) || "fa-IR"; | ||
}; | ||
this.addSeconds = function (value, count) { | ||
this.addSeconds = (value, count) => { | ||
return addSeconds(value, count); | ||
}; | ||
this.addMinutes = function (value, count) { | ||
this.addMinutes = (value, count) => { | ||
return addMinutes(value, count); | ||
}; | ||
this.addHours = function (value, count) { | ||
this.addHours = (value, count) => { | ||
return addHours(value, count); | ||
}; | ||
this.addDays = function (value, count) { | ||
this.addDays = (value, count) => { | ||
return addDays(value, count); | ||
}; | ||
this.addWeeks = function (value, count) { | ||
this.addWeeks = (value, count) => { | ||
return addWeeks(value, count); | ||
}; | ||
this.addMonths = function (value, count) { | ||
this.addMonths = (value, count) => { | ||
return addMonths(value, count); | ||
}; | ||
this.addYears = function (value, count) { | ||
this.addYears = (value, count) => { | ||
return addYears(value, count); | ||
}; | ||
this.isValid = function (value) { | ||
return isValid(_this.date(value)); | ||
this.isValid = (value) => { | ||
return isValid(this.date(value)); | ||
}; | ||
this.getDiff = function (value, comparing, unit) { | ||
this.getDiff = (value, comparing, unit) => { | ||
var _a; | ||
// we output 0 if the compare date is string and parsing is not valid | ||
const dateToCompare = (_a = this.date(comparing)) !== null && _a !== void 0 ? _a : value; | ||
switch (unit) { | ||
case "years": | ||
return differenceInYears(value, _this.date(comparing)); | ||
return differenceInYears(value, dateToCompare); | ||
case "quarters": | ||
return differenceInQuarters(value, _this.date(comparing)); | ||
return differenceInQuarters(value, dateToCompare); | ||
case "months": | ||
return differenceInMonths(value, _this.date(comparing)); | ||
return differenceInMonths(value, dateToCompare); | ||
case "weeks": | ||
return differenceInWeeks(value, _this.date(comparing)); | ||
return differenceInWeeks(value, dateToCompare); | ||
case "days": | ||
return differenceInDays(value, _this.date(comparing)); | ||
return differenceInDays(value, dateToCompare); | ||
case "hours": | ||
return differenceInHours(value, _this.date(comparing)); | ||
return differenceInHours(value, dateToCompare); | ||
case "minutes": | ||
return differenceInMinutes(value, _this.date(comparing)); | ||
return differenceInMinutes(value, dateToCompare); | ||
case "seconds": | ||
return differenceInSeconds(value, _this.date(comparing)); | ||
return differenceInSeconds(value, dateToCompare); | ||
default: { | ||
return differenceInMilliseconds(value, _this.date(comparing)); | ||
return differenceInMilliseconds(value, dateToCompare); | ||
} | ||
} | ||
}; | ||
this.isAfter = function (value, comparing) { | ||
this.isAfter = (value, comparing) => { | ||
return isAfter(value, comparing); | ||
}; | ||
this.isBefore = function (value, comparing) { | ||
this.isBefore = (value, comparing) => { | ||
return isBefore(value, comparing); | ||
}; | ||
this.startOfDay = function (value) { | ||
this.startOfDay = (value) => { | ||
return startOfDay(value); | ||
}; | ||
this.endOfDay = function (value) { | ||
this.endOfDay = (value) => { | ||
return endOfDay(value); | ||
}; | ||
this.getHours = function (value) { | ||
this.getHours = (value) => { | ||
return getHours(value); | ||
}; | ||
this.setHours = function (value, count) { | ||
this.setHours = (value, count) => { | ||
return setHours(value, count); | ||
}; | ||
this.setMinutes = function (value, count) { | ||
this.setMinutes = (value, count) => { | ||
return setMinutes(value, count); | ||
}; | ||
this.getSeconds = function (value) { | ||
this.getSeconds = (value) => { | ||
return getSeconds(value); | ||
}; | ||
this.setSeconds = function (value, count) { | ||
this.setSeconds = (value, count) => { | ||
return setSeconds(value, count); | ||
}; | ||
this.isSameDay = function (value, comparing) { | ||
this.isSameDay = (value, comparing) => { | ||
return isSameDay(value, comparing); | ||
}; | ||
this.isSameMonth = function (value, comparing) { | ||
this.isSameMonth = (value, comparing) => { | ||
return isSameMonth(value, comparing); | ||
}; | ||
this.isSameYear = function (value, comparing) { | ||
this.isSameYear = (value, comparing) => { | ||
return isSameYear(value, comparing); | ||
}; | ||
this.isSameHour = function (value, comparing) { | ||
this.isSameHour = (value, comparing) => { | ||
return isSameHour(value, comparing); | ||
}; | ||
this.startOfYear = function (value) { | ||
this.startOfYear = (value) => { | ||
return startOfYear(value); | ||
}; | ||
this.endOfYear = function (value) { | ||
this.endOfYear = (value) => { | ||
return endOfYear(value); | ||
}; | ||
this.startOfMonth = function (value) { | ||
this.startOfMonth = (value) => { | ||
return startOfMonth(value); | ||
}; | ||
this.endOfMonth = function (value) { | ||
this.endOfMonth = (value) => { | ||
return endOfMonth(value); | ||
}; | ||
this.startOfWeek = function (value) { | ||
return startOfWeek(value, { locale: _this.locale }); | ||
this.startOfWeek = (value) => { | ||
return startOfWeek(value, { locale: this.locale }); | ||
}; | ||
this.endOfWeek = function (value) { | ||
return endOfWeek(value, { locale: _this.locale }); | ||
this.endOfWeek = (value) => { | ||
return endOfWeek(value, { locale: this.locale }); | ||
}; | ||
this.getYear = function (value) { | ||
this.getYear = (value) => { | ||
return getYear(value); | ||
}; | ||
this.setYear = function (value, count) { | ||
this.setYear = (value, count) => { | ||
return setYear(value, count); | ||
}; | ||
this.getDate = function (value) { | ||
this.getDate = (value) => { | ||
return getDate(value); | ||
}; | ||
this.setDate = function (value, count) { | ||
this.setDate = (value, count) => { | ||
return setDate(value, count); | ||
}; | ||
this.date = function (value) { | ||
this.date = (value) => { | ||
if (typeof value === "undefined") { | ||
@@ -261,24 +260,24 @@ return new Date(); | ||
}; | ||
this.toJsDate = function (value) { | ||
this.toJsDate = (value) => { | ||
return value; | ||
}; | ||
this.parseISO = function (isoString) { | ||
this.parseISO = (isoString) => { | ||
return parseISO(isoString); | ||
}; | ||
this.toISO = function (value) { | ||
this.toISO = (value) => { | ||
return formatISO(value, { format: "extended" }); | ||
}; | ||
this.parse = function (value, formatString) { | ||
this.parse = (value, formatString) => { | ||
if (value === "") { | ||
return null; | ||
} | ||
return dateFnsParse(value, formatString, new Date(), { locale: _this.locale }); | ||
return dateFnsParse(value, formatString, new Date(), { locale: this.locale }); | ||
}; | ||
this.format = function (date, formatKey) { | ||
return _this.formatByString(date, _this.formats[formatKey]); | ||
this.format = (date, formatKey) => { | ||
return this.formatByString(date, this.formats[formatKey]); | ||
}; | ||
this.formatByString = function (date, formatString) { | ||
return format(date, formatString, { locale: _this.locale }); | ||
this.formatByString = (date, formatString) => { | ||
return format(date, formatString, { locale: this.locale }); | ||
}; | ||
this.isEqual = function (date, comparing) { | ||
this.isEqual = (date, comparing) => { | ||
if (date === null && comparing === null) { | ||
@@ -289,37 +288,39 @@ return true; | ||
}; | ||
this.isNull = function (date) { | ||
this.isNull = (date) => { | ||
return date === null; | ||
}; | ||
this.isAfterDay = function (date, value) { | ||
this.isAfterDay = (date, value) => { | ||
return isAfter(date, endOfDay(value)); | ||
}; | ||
this.isBeforeDay = function (date, value) { | ||
this.isBeforeDay = (date, value) => { | ||
return isBefore(date, startOfDay(value)); | ||
}; | ||
this.isBeforeYear = function (date, value) { | ||
this.isBeforeYear = (date, value) => { | ||
return isBefore(date, startOfYear(value)); | ||
}; | ||
this.isAfterYear = function (date, value) { | ||
this.isAfterYear = (date, value) => { | ||
return isAfter(date, endOfYear(value)); | ||
}; | ||
this.isWithinRange = function (date, _a) { | ||
var start = _a[0], end = _a[1]; | ||
return isWithinInterval(date, { start: start, end: end }); | ||
this.isWithinRange = (date, [start, end]) => { | ||
return isWithinInterval(date, { start, end }); | ||
}; | ||
this.formatNumber = function (numberToFormat) { | ||
return numberToFormat.replace(/\d/g, function (match) { return symbolMap[match]; }).replace(/,/g, "،"); | ||
this.formatNumber = (numberToFormat) => { | ||
return (numberToFormat | ||
// safe to cast since we are matching only digits | ||
.replace(/\d/g, (match) => symbolMap[match]) | ||
.replace(/,/g, "،")); | ||
}; | ||
this.getMinutes = function (date) { | ||
this.getMinutes = (date) => { | ||
return getMinutes(date); | ||
}; | ||
this.getMonth = function (date) { | ||
this.getMonth = (date) => { | ||
return getMonth(date); | ||
}; | ||
this.getDaysInMonth = function (date) { | ||
this.getDaysInMonth = (date) => { | ||
return getDaysInMonth(date); | ||
}; | ||
this.setMonth = function (date, count) { | ||
this.setMonth = (date, count) => { | ||
return setMonth(date, count); | ||
}; | ||
this.getMeridiemText = function (ampm) { | ||
this.getMeridiemText = (ampm) => { | ||
if (ampm === "am") { | ||
@@ -330,38 +331,38 @@ return "ق.ظ."; | ||
}; | ||
this.getNextMonth = function (date) { | ||
this.getNextMonth = (date) => { | ||
return addMonths(date, 1); | ||
}; | ||
this.getPreviousMonth = function (date) { | ||
this.getPreviousMonth = (date) => { | ||
return addMonths(date, -1); | ||
}; | ||
this.getMonthArray = function (date) { | ||
var firstMonth = startOfYear(date); | ||
var monthArray = [firstMonth]; | ||
this.getMonthArray = (date) => { | ||
const firstMonth = startOfYear(date); | ||
const monthArray = [firstMonth]; | ||
while (monthArray.length < 12) { | ||
var prevMonth = monthArray[monthArray.length - 1]; | ||
monthArray.push(_this.getNextMonth(prevMonth)); | ||
const prevMonth = monthArray[monthArray.length - 1]; | ||
monthArray.push(this.getNextMonth(prevMonth)); | ||
} | ||
return monthArray; | ||
}; | ||
this.mergeDateAndTime = function (date, time) { | ||
return _this.setSeconds(_this.setMinutes(_this.setHours(date, _this.getHours(time)), _this.getMinutes(time)), _this.getSeconds(time)); | ||
this.mergeDateAndTime = (date, time) => { | ||
return this.setSeconds(this.setMinutes(this.setHours(date, this.getHours(time)), this.getMinutes(time)), this.getSeconds(time)); | ||
}; | ||
this.getWeekdays = function () { | ||
var now = new Date(); | ||
this.getWeekdays = () => { | ||
const now = new Date(); | ||
return eachDayOfInterval({ | ||
start: startOfWeek(now, { locale: _this.locale }), | ||
end: endOfWeek(now, { locale: _this.locale }), | ||
}).map(function (day) { return _this.formatByString(day, "EEEEEE"); }); | ||
start: startOfWeek(now, { locale: this.locale }), | ||
end: endOfWeek(now, { locale: this.locale }), | ||
}).map((day) => this.formatByString(day, "EEEEEE")); | ||
}; | ||
this.getWeekArray = function (date) { | ||
var start = startOfWeek(startOfMonth(date), { locale: _this.locale }); | ||
var end = endOfWeek(endOfMonth(date), { locale: _this.locale }); | ||
var count = 0; | ||
var current = start; | ||
var nestedWeeks = []; | ||
var lastDay = null; | ||
this.getWeekArray = (date) => { | ||
const start = startOfWeek(startOfMonth(date), { locale: this.locale }); | ||
const end = endOfWeek(endOfMonth(date), { locale: this.locale }); | ||
let count = 0; | ||
let current = start; | ||
const nestedWeeks = []; | ||
let lastDay = null; | ||
while (isBefore(current, end)) { | ||
var weekNumber = Math.floor(count / 7); | ||
const weekNumber = Math.floor(count / 7); | ||
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || []; | ||
var day = getDay(current); | ||
const day = getDay(current); | ||
if (lastDay !== day) { | ||
@@ -376,7 +377,7 @@ lastDay = day; | ||
}; | ||
this.getYearRange = function (start, end) { | ||
var startDate = startOfYear(start); | ||
var endDate = endOfYear(end); | ||
var years = []; | ||
var current = startDate; | ||
this.getYearRange = (start, end) => { | ||
const startDate = startOfYear(start); | ||
const endDate = endOfYear(end); | ||
const years = []; | ||
let current = startDate; | ||
while (isBefore(current, endDate)) { | ||
@@ -391,5 +392,10 @@ years.push(current); | ||
} | ||
return DateFnsJalaliUtils; | ||
}()); | ||
isBeforeMonth(value, comparing) { | ||
return isBefore(value, startOfMonth(comparing)); | ||
} | ||
isAfterMonth(value, comparing) { | ||
return isAfter(value, startOfMonth(comparing)); | ||
} | ||
} | ||
export { DateFnsJalaliUtils as default }; |
@@ -115,3 +115,3 @@ 'use strict'; | ||
var defaultFormats = { | ||
const defaultFormats = { | ||
dayOfMonth: "d", | ||
@@ -157,12 +157,11 @@ fullDate: "PPP", | ||
}; | ||
var DateFnsJalaliUtils = /** @class */ (function () { | ||
function DateFnsJalaliUtils(_a) { | ||
var _this = this; | ||
var _b = _a === void 0 ? {} : _a, locale = _b.locale, formats = _b.formats; | ||
class DateFnsJalaliUtils { | ||
constructor({ locale, formats, } = {}) { | ||
this.lib = "date-fns-jalali"; | ||
// Note: date-fns input types are more lenient than this adapter, so we need to expose our more | ||
// strict signature and delegate to the more lenient signature. Otherwise, we have downstream type errors upon usage. | ||
this.is12HourCycleInCurrentLocale = function () { | ||
if (_this.locale) { | ||
return /a/.test(_this.locale.formatLong.time()); | ||
this.is12HourCycleInCurrentLocale = () => { | ||
var _a; | ||
if (this.locale) { | ||
return /a/.test((_a = this.locale.formatLong) === null || _a === void 0 ? void 0 : _a.time()); | ||
} | ||
@@ -172,141 +171,141 @@ // By default date-fns-jalali is using fa-IR locale with am/pm enabled | ||
}; | ||
this.getFormatHelperText = function (format) { | ||
this.getFormatHelperText = (format) => { | ||
var _a, _b; | ||
// @see https://github.com/date-fns/date-fns/blob/master/src/format/index.js#L31 | ||
var longFormatRegexp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g; | ||
var locale = _this.locale || defaultLocale__default["default"]; | ||
return format | ||
.match(longFormatRegexp) | ||
.map(function (token) { | ||
var firstCharacter = token[0]; | ||
const longFormatRegexp = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g; | ||
const locale = this.locale || defaultLocale__default["default"]; | ||
return ((_b = (_a = format | ||
.match(longFormatRegexp)) === null || _a === void 0 ? void 0 : _a.map((token) => { | ||
const firstCharacter = token[0]; | ||
if (firstCharacter === "p" || firstCharacter === "P") { | ||
var longFormatter = longFormatters__default["default"][firstCharacter]; | ||
const longFormatter = longFormatters__default["default"][firstCharacter]; | ||
return longFormatter(token, locale.formatLong, {}); | ||
} | ||
return token; | ||
}) | ||
.join("") | ||
.replace(/(aaa|aa|a)/g, "(a|p)m") | ||
.toLocaleLowerCase(); | ||
}).join("").replace(/(aaa|aa|a)/g, "(a|p)m").toLocaleLowerCase()) !== null && _b !== void 0 ? _b : format); | ||
}; | ||
this.getCurrentLocaleCode = function () { | ||
this.getCurrentLocaleCode = () => { | ||
var _a; | ||
return ((_a = _this.locale) === null || _a === void 0 ? void 0 : _a.code) || "fa-IR"; | ||
return ((_a = this.locale) === null || _a === void 0 ? void 0 : _a.code) || "fa-IR"; | ||
}; | ||
this.addSeconds = function (value, count) { | ||
this.addSeconds = (value, count) => { | ||
return addSeconds__default["default"](value, count); | ||
}; | ||
this.addMinutes = function (value, count) { | ||
this.addMinutes = (value, count) => { | ||
return addMinutes__default["default"](value, count); | ||
}; | ||
this.addHours = function (value, count) { | ||
this.addHours = (value, count) => { | ||
return addHours__default["default"](value, count); | ||
}; | ||
this.addDays = function (value, count) { | ||
this.addDays = (value, count) => { | ||
return addDays__default["default"](value, count); | ||
}; | ||
this.addWeeks = function (value, count) { | ||
this.addWeeks = (value, count) => { | ||
return addWeeks__default["default"](value, count); | ||
}; | ||
this.addMonths = function (value, count) { | ||
this.addMonths = (value, count) => { | ||
return addMonths__default["default"](value, count); | ||
}; | ||
this.addYears = function (value, count) { | ||
this.addYears = (value, count) => { | ||
return addYears__default["default"](value, count); | ||
}; | ||
this.isValid = function (value) { | ||
return isValid__default["default"](_this.date(value)); | ||
this.isValid = (value) => { | ||
return isValid__default["default"](this.date(value)); | ||
}; | ||
this.getDiff = function (value, comparing, unit) { | ||
this.getDiff = (value, comparing, unit) => { | ||
var _a; | ||
// we output 0 if the compare date is string and parsing is not valid | ||
const dateToCompare = (_a = this.date(comparing)) !== null && _a !== void 0 ? _a : value; | ||
switch (unit) { | ||
case "years": | ||
return differenceInYears__default["default"](value, _this.date(comparing)); | ||
return differenceInYears__default["default"](value, dateToCompare); | ||
case "quarters": | ||
return differenceInQuarters__default["default"](value, _this.date(comparing)); | ||
return differenceInQuarters__default["default"](value, dateToCompare); | ||
case "months": | ||
return differenceInMonths__default["default"](value, _this.date(comparing)); | ||
return differenceInMonths__default["default"](value, dateToCompare); | ||
case "weeks": | ||
return differenceInWeeks__default["default"](value, _this.date(comparing)); | ||
return differenceInWeeks__default["default"](value, dateToCompare); | ||
case "days": | ||
return differenceInDays__default["default"](value, _this.date(comparing)); | ||
return differenceInDays__default["default"](value, dateToCompare); | ||
case "hours": | ||
return differenceInHours__default["default"](value, _this.date(comparing)); | ||
return differenceInHours__default["default"](value, dateToCompare); | ||
case "minutes": | ||
return differenceInMinutes__default["default"](value, _this.date(comparing)); | ||
return differenceInMinutes__default["default"](value, dateToCompare); | ||
case "seconds": | ||
return differenceInSeconds__default["default"](value, _this.date(comparing)); | ||
return differenceInSeconds__default["default"](value, dateToCompare); | ||
default: { | ||
return differenceInMilliseconds__default["default"](value, _this.date(comparing)); | ||
return differenceInMilliseconds__default["default"](value, dateToCompare); | ||
} | ||
} | ||
}; | ||
this.isAfter = function (value, comparing) { | ||
this.isAfter = (value, comparing) => { | ||
return isAfter__default["default"](value, comparing); | ||
}; | ||
this.isBefore = function (value, comparing) { | ||
this.isBefore = (value, comparing) => { | ||
return isBefore__default["default"](value, comparing); | ||
}; | ||
this.startOfDay = function (value) { | ||
this.startOfDay = (value) => { | ||
return startOfDay__default["default"](value); | ||
}; | ||
this.endOfDay = function (value) { | ||
this.endOfDay = (value) => { | ||
return endOfDay__default["default"](value); | ||
}; | ||
this.getHours = function (value) { | ||
this.getHours = (value) => { | ||
return getHours__default["default"](value); | ||
}; | ||
this.setHours = function (value, count) { | ||
this.setHours = (value, count) => { | ||
return setHours__default["default"](value, count); | ||
}; | ||
this.setMinutes = function (value, count) { | ||
this.setMinutes = (value, count) => { | ||
return setMinutes__default["default"](value, count); | ||
}; | ||
this.getSeconds = function (value) { | ||
this.getSeconds = (value) => { | ||
return getSeconds__default["default"](value); | ||
}; | ||
this.setSeconds = function (value, count) { | ||
this.setSeconds = (value, count) => { | ||
return setSeconds__default["default"](value, count); | ||
}; | ||
this.isSameDay = function (value, comparing) { | ||
this.isSameDay = (value, comparing) => { | ||
return isSameDay__default["default"](value, comparing); | ||
}; | ||
this.isSameMonth = function (value, comparing) { | ||
this.isSameMonth = (value, comparing) => { | ||
return isSameMonth__default["default"](value, comparing); | ||
}; | ||
this.isSameYear = function (value, comparing) { | ||
this.isSameYear = (value, comparing) => { | ||
return isSameYear__default["default"](value, comparing); | ||
}; | ||
this.isSameHour = function (value, comparing) { | ||
this.isSameHour = (value, comparing) => { | ||
return isSameHour__default["default"](value, comparing); | ||
}; | ||
this.startOfYear = function (value) { | ||
this.startOfYear = (value) => { | ||
return startOfYear__default["default"](value); | ||
}; | ||
this.endOfYear = function (value) { | ||
this.endOfYear = (value) => { | ||
return endOfYear__default["default"](value); | ||
}; | ||
this.startOfMonth = function (value) { | ||
this.startOfMonth = (value) => { | ||
return startOfMonth__default["default"](value); | ||
}; | ||
this.endOfMonth = function (value) { | ||
this.endOfMonth = (value) => { | ||
return endOfMonth__default["default"](value); | ||
}; | ||
this.startOfWeek = function (value) { | ||
return startOfWeek__default["default"](value, { locale: _this.locale }); | ||
this.startOfWeek = (value) => { | ||
return startOfWeek__default["default"](value, { locale: this.locale }); | ||
}; | ||
this.endOfWeek = function (value) { | ||
return endOfWeek__default["default"](value, { locale: _this.locale }); | ||
this.endOfWeek = (value) => { | ||
return endOfWeek__default["default"](value, { locale: this.locale }); | ||
}; | ||
this.getYear = function (value) { | ||
this.getYear = (value) => { | ||
return getYear__default["default"](value); | ||
}; | ||
this.setYear = function (value, count) { | ||
this.setYear = (value, count) => { | ||
return setYear__default["default"](value, count); | ||
}; | ||
this.getDate = function (value) { | ||
this.getDate = (value) => { | ||
return getDate__default["default"](value); | ||
}; | ||
this.setDate = function (value, count) { | ||
this.setDate = (value, count) => { | ||
return setDate__default["default"](value, count); | ||
}; | ||
this.date = function (value) { | ||
this.date = (value) => { | ||
if (typeof value === "undefined") { | ||
@@ -320,24 +319,24 @@ return new Date(); | ||
}; | ||
this.toJsDate = function (value) { | ||
this.toJsDate = (value) => { | ||
return value; | ||
}; | ||
this.parseISO = function (isoString) { | ||
this.parseISO = (isoString) => { | ||
return parseISO__default["default"](isoString); | ||
}; | ||
this.toISO = function (value) { | ||
this.toISO = (value) => { | ||
return formatISO__default["default"](value, { format: "extended" }); | ||
}; | ||
this.parse = function (value, formatString) { | ||
this.parse = (value, formatString) => { | ||
if (value === "") { | ||
return null; | ||
} | ||
return dateFnsParse__default["default"](value, formatString, new Date(), { locale: _this.locale }); | ||
return dateFnsParse__default["default"](value, formatString, new Date(), { locale: this.locale }); | ||
}; | ||
this.format = function (date, formatKey) { | ||
return _this.formatByString(date, _this.formats[formatKey]); | ||
this.format = (date, formatKey) => { | ||
return this.formatByString(date, this.formats[formatKey]); | ||
}; | ||
this.formatByString = function (date, formatString) { | ||
return format__default["default"](date, formatString, { locale: _this.locale }); | ||
this.formatByString = (date, formatString) => { | ||
return format__default["default"](date, formatString, { locale: this.locale }); | ||
}; | ||
this.isEqual = function (date, comparing) { | ||
this.isEqual = (date, comparing) => { | ||
if (date === null && comparing === null) { | ||
@@ -348,37 +347,39 @@ return true; | ||
}; | ||
this.isNull = function (date) { | ||
this.isNull = (date) => { | ||
return date === null; | ||
}; | ||
this.isAfterDay = function (date, value) { | ||
this.isAfterDay = (date, value) => { | ||
return isAfter__default["default"](date, endOfDay__default["default"](value)); | ||
}; | ||
this.isBeforeDay = function (date, value) { | ||
this.isBeforeDay = (date, value) => { | ||
return isBefore__default["default"](date, startOfDay__default["default"](value)); | ||
}; | ||
this.isBeforeYear = function (date, value) { | ||
this.isBeforeYear = (date, value) => { | ||
return isBefore__default["default"](date, startOfYear__default["default"](value)); | ||
}; | ||
this.isAfterYear = function (date, value) { | ||
this.isAfterYear = (date, value) => { | ||
return isAfter__default["default"](date, endOfYear__default["default"](value)); | ||
}; | ||
this.isWithinRange = function (date, _a) { | ||
var start = _a[0], end = _a[1]; | ||
return isWithinInterval__default["default"](date, { start: start, end: end }); | ||
this.isWithinRange = (date, [start, end]) => { | ||
return isWithinInterval__default["default"](date, { start, end }); | ||
}; | ||
this.formatNumber = function (numberToFormat) { | ||
return numberToFormat.replace(/\d/g, function (match) { return symbolMap[match]; }).replace(/,/g, "،"); | ||
this.formatNumber = (numberToFormat) => { | ||
return (numberToFormat | ||
// safe to cast since we are matching only digits | ||
.replace(/\d/g, (match) => symbolMap[match]) | ||
.replace(/,/g, "،")); | ||
}; | ||
this.getMinutes = function (date) { | ||
this.getMinutes = (date) => { | ||
return getMinutes__default["default"](date); | ||
}; | ||
this.getMonth = function (date) { | ||
this.getMonth = (date) => { | ||
return getMonth__default["default"](date); | ||
}; | ||
this.getDaysInMonth = function (date) { | ||
this.getDaysInMonth = (date) => { | ||
return getDaysInMonth__default["default"](date); | ||
}; | ||
this.setMonth = function (date, count) { | ||
this.setMonth = (date, count) => { | ||
return setMonth__default["default"](date, count); | ||
}; | ||
this.getMeridiemText = function (ampm) { | ||
this.getMeridiemText = (ampm) => { | ||
if (ampm === "am") { | ||
@@ -389,38 +390,38 @@ return "ق.ظ."; | ||
}; | ||
this.getNextMonth = function (date) { | ||
this.getNextMonth = (date) => { | ||
return addMonths__default["default"](date, 1); | ||
}; | ||
this.getPreviousMonth = function (date) { | ||
this.getPreviousMonth = (date) => { | ||
return addMonths__default["default"](date, -1); | ||
}; | ||
this.getMonthArray = function (date) { | ||
var firstMonth = startOfYear__default["default"](date); | ||
var monthArray = [firstMonth]; | ||
this.getMonthArray = (date) => { | ||
const firstMonth = startOfYear__default["default"](date); | ||
const monthArray = [firstMonth]; | ||
while (monthArray.length < 12) { | ||
var prevMonth = monthArray[monthArray.length - 1]; | ||
monthArray.push(_this.getNextMonth(prevMonth)); | ||
const prevMonth = monthArray[monthArray.length - 1]; | ||
monthArray.push(this.getNextMonth(prevMonth)); | ||
} | ||
return monthArray; | ||
}; | ||
this.mergeDateAndTime = function (date, time) { | ||
return _this.setSeconds(_this.setMinutes(_this.setHours(date, _this.getHours(time)), _this.getMinutes(time)), _this.getSeconds(time)); | ||
this.mergeDateAndTime = (date, time) => { | ||
return this.setSeconds(this.setMinutes(this.setHours(date, this.getHours(time)), this.getMinutes(time)), this.getSeconds(time)); | ||
}; | ||
this.getWeekdays = function () { | ||
var now = new Date(); | ||
this.getWeekdays = () => { | ||
const now = new Date(); | ||
return eachDayOfInterval__default["default"]({ | ||
start: startOfWeek__default["default"](now, { locale: _this.locale }), | ||
end: endOfWeek__default["default"](now, { locale: _this.locale }), | ||
}).map(function (day) { return _this.formatByString(day, "EEEEEE"); }); | ||
start: startOfWeek__default["default"](now, { locale: this.locale }), | ||
end: endOfWeek__default["default"](now, { locale: this.locale }), | ||
}).map((day) => this.formatByString(day, "EEEEEE")); | ||
}; | ||
this.getWeekArray = function (date) { | ||
var start = startOfWeek__default["default"](startOfMonth__default["default"](date), { locale: _this.locale }); | ||
var end = endOfWeek__default["default"](endOfMonth__default["default"](date), { locale: _this.locale }); | ||
var count = 0; | ||
var current = start; | ||
var nestedWeeks = []; | ||
var lastDay = null; | ||
this.getWeekArray = (date) => { | ||
const start = startOfWeek__default["default"](startOfMonth__default["default"](date), { locale: this.locale }); | ||
const end = endOfWeek__default["default"](endOfMonth__default["default"](date), { locale: this.locale }); | ||
let count = 0; | ||
let current = start; | ||
const nestedWeeks = []; | ||
let lastDay = null; | ||
while (isBefore__default["default"](current, end)) { | ||
var weekNumber = Math.floor(count / 7); | ||
const weekNumber = Math.floor(count / 7); | ||
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || []; | ||
var day = getDay__default["default"](current); | ||
const day = getDay__default["default"](current); | ||
if (lastDay !== day) { | ||
@@ -435,7 +436,7 @@ lastDay = day; | ||
}; | ||
this.getYearRange = function (start, end) { | ||
var startDate = startOfYear__default["default"](start); | ||
var endDate = endOfYear__default["default"](end); | ||
var years = []; | ||
var current = startDate; | ||
this.getYearRange = (start, end) => { | ||
const startDate = startOfYear__default["default"](start); | ||
const endDate = endOfYear__default["default"](end); | ||
const years = []; | ||
let current = startDate; | ||
while (isBefore__default["default"](current, endDate)) { | ||
@@ -450,5 +451,10 @@ years.push(current); | ||
} | ||
return DateFnsJalaliUtils; | ||
}()); | ||
isBeforeMonth(value, comparing) { | ||
return isBefore__default["default"](value, startOfMonth__default["default"](comparing)); | ||
} | ||
isAfterMonth(value, comparing) { | ||
return isAfter__default["default"](value, startOfMonth__default["default"](comparing)); | ||
} | ||
} | ||
module.exports = DateFnsJalaliUtils; |
{ | ||
"name": "@date-io/date-fns-jalali", | ||
"version": "2.16.0", | ||
"version": "2.17.0", | ||
"description": "Abstraction over common javascript date management libraries", | ||
@@ -43,3 +43,3 @@ "main": "build/index.js", | ||
"dependencies": { | ||
"@date-io/core": "^2.16.0" | ||
"@date-io/core": "^2.17.0" | ||
}, | ||
@@ -49,5 +49,5 @@ "devDependencies": { | ||
"rollup": "^2.0.2", | ||
"typescript": "^3.7.2" | ||
"typescript": "^5.0.0" | ||
}, | ||
"gitHead": "e9eddac85299013881e8bf005f5030fdf46d4756" | ||
"gitHead": "e497a04c456f04211c8b0877d002c85e72ea98a2" | ||
} |
@@ -54,2 +54,3 @@ import addSeconds from "date-fns-jalali/addSeconds"; | ||
import isWithinInterval from "date-fns-jalali/isWithinInterval"; | ||
// @ts-ignore | ||
import longFormatters from "date-fns-jalali/_lib/format/longFormatters"; | ||
@@ -120,3 +121,3 @@ import defaultLocale from "date-fns-jalali/locale/fa-IR"; | ||
if (this.locale) { | ||
return /a/.test(this.locale.formatLong.time()); | ||
return /a/.test(this.locale.formatLong?.time()); | ||
} | ||
@@ -132,15 +133,18 @@ | ||
const locale = this.locale || defaultLocale; | ||
return format | ||
.match(longFormatRegexp) | ||
.map((token) => { | ||
const firstCharacter = token[0]; | ||
if (firstCharacter === "p" || firstCharacter === "P") { | ||
const longFormatter = longFormatters[firstCharacter]; | ||
return longFormatter(token, locale.formatLong, {}); | ||
} | ||
return token; | ||
}) | ||
.join("") | ||
.replace(/(aaa|aa|a)/g, "(a|p)m") | ||
.toLocaleLowerCase(); | ||
return ( | ||
format | ||
.match(longFormatRegexp) | ||
?.map((token) => { | ||
const firstCharacter = token[0]; | ||
if (firstCharacter === "p" || firstCharacter === "P") { | ||
const longFormatter = longFormatters[firstCharacter]; | ||
return longFormatter(token, locale.formatLong, {}); | ||
} | ||
return token; | ||
}) | ||
.join("") | ||
.replace(/(aaa|aa|a)/g, "(a|p)m") | ||
.toLocaleLowerCase() ?? format | ||
); | ||
}; | ||
@@ -185,21 +189,23 @@ | ||
public getDiff = (value: Date, comparing: Date | string, unit?: Unit) => { | ||
// we output 0 if the compare date is string and parsing is not valid | ||
const dateToCompare = this.date(comparing) ?? value; | ||
switch (unit) { | ||
case "years": | ||
return differenceInYears(value, this.date(comparing)); | ||
return differenceInYears(value, dateToCompare); | ||
case "quarters": | ||
return differenceInQuarters(value, this.date(comparing)); | ||
return differenceInQuarters(value, dateToCompare); | ||
case "months": | ||
return differenceInMonths(value, this.date(comparing)); | ||
return differenceInMonths(value, dateToCompare); | ||
case "weeks": | ||
return differenceInWeeks(value, this.date(comparing)); | ||
return differenceInWeeks(value, dateToCompare); | ||
case "days": | ||
return differenceInDays(value, this.date(comparing)); | ||
return differenceInDays(value, dateToCompare); | ||
case "hours": | ||
return differenceInHours(value, this.date(comparing)); | ||
return differenceInHours(value, dateToCompare); | ||
case "minutes": | ||
return differenceInMinutes(value, this.date(comparing)); | ||
return differenceInMinutes(value, dateToCompare); | ||
case "seconds": | ||
return differenceInSeconds(value, this.date(comparing)); | ||
return differenceInSeconds(value, dateToCompare); | ||
default: { | ||
return differenceInMilliseconds(value, this.date(comparing)); | ||
return differenceInMilliseconds(value, dateToCompare); | ||
} | ||
@@ -365,2 +371,10 @@ } | ||
public isBeforeMonth(value: Date, comparing: Date): boolean { | ||
return isBefore(value, startOfMonth(comparing)); | ||
} | ||
public isAfterMonth(value: Date, comparing: Date): boolean { | ||
return isAfter(value, startOfMonth(comparing)); | ||
} | ||
public isAfterYear = (date: Date, value: Date) => { | ||
@@ -375,3 +389,8 @@ return isAfter(date, endOfYear(value)); | ||
public formatNumber = (numberToFormat: string) => { | ||
return numberToFormat.replace(/\d/g, (match) => symbolMap[match]).replace(/,/g, "،"); | ||
return ( | ||
numberToFormat | ||
// safe to cast since we are matching only digits | ||
.replace(/\d/g, (match) => symbolMap[match as unknown as keyof typeof symbolMap]) | ||
.replace(/,/g, "،") | ||
); | ||
}; | ||
@@ -378,0 +397,0 @@ |
56775
1.03%1335
2.3%Updated