@date-io/dayjs
Advanced tools
Comparing version 2.16.0 to 2.17.0
@@ -9,4 +9,4 @@ import defaultDayjs from "dayjs"; | ||
} | ||
declare type Dayjs = defaultDayjs.Dayjs; | ||
declare type Constructor<TDate extends Dayjs> = (...args: Parameters<typeof defaultDayjs>) => TDate; | ||
type Dayjs = defaultDayjs.Dayjs; | ||
type Constructor<TDate extends Dayjs> = (...args: Parameters<typeof defaultDayjs>) => TDate; | ||
export default class DayjsUtils<TDate extends Dayjs = Dayjs> implements IUtils<TDate> { | ||
@@ -24,8 +24,8 @@ rawDayJsInstance: typeof defaultDayjs; | ||
toISO: (value: Dayjs) => string; | ||
parse: (value: any, format: string) => TDate; | ||
date: (value?: any) => TDate; | ||
parse: (value: any, format: string) => TDate | null; | ||
date: (value?: any) => TDate | null; | ||
toJsDate: (value: Dayjs) => Date; | ||
isValid: (value: any) => boolean; | ||
isNull: (date: Dayjs | null) => boolean; | ||
getDiff: (date: TDate, comparing: TDate, units?: Unit) => number; | ||
getDiff: (date: TDate, comparing: TDate | string, units?: Unit) => number; | ||
isAfter: (date: TDate, value: TDate) => boolean; | ||
@@ -35,2 +35,4 @@ isBefore: (date: TDate, value: TDate) => boolean; | ||
isBeforeDay: (date: Dayjs, value: Dayjs) => boolean; | ||
isAfterMonth: (date: Dayjs, value: Dayjs) => boolean; | ||
isBeforeMonth: (date: Dayjs, value: Dayjs) => boolean; | ||
isBeforeYear: (date: Dayjs, value: Dayjs) => boolean; | ||
@@ -37,0 +39,0 @@ isAfterYear: (date: Dayjs, value: Dayjs) => boolean; |
@@ -9,10 +9,4 @@ import defaultDayjs from 'dayjs'; | ||
defaultDayjs.extend(isBetweenPlugin); | ||
var withLocale = function (dayjs, locale) { return (!locale ? dayjs : function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return dayjs.apply(void 0, args).locale(locale); | ||
}); }; | ||
var defaultFormats = { | ||
const withLocale = (dayjs, locale) => (!locale ? dayjs : (...args) => dayjs(...args).locale(locale)); | ||
const defaultFormats = { | ||
normalDateWithWeekday: "ddd, MMM D", | ||
@@ -46,21 +40,19 @@ normalDate: "D MMMM", | ||
}; | ||
var DayjsUtils = /** @class */ (function () { | ||
function DayjsUtils(_a) { | ||
var _this = this; | ||
var _b = _a === void 0 ? {} : _a, locale = _b.locale, formats = _b.formats, instance = _b.instance; | ||
class DayjsUtils { | ||
constructor({ locale, formats, instance } = {}) { | ||
this.lib = "dayjs"; | ||
this.is12HourCycleInCurrentLocale = function () { | ||
var _a, _b; | ||
this.is12HourCycleInCurrentLocale = () => { | ||
var _a, _b, _c; | ||
/* istanbul ignore next */ | ||
return /A|a/.test((_b = (_a = _this.rawDayJsInstance.Ls[_this.locale || "en"]) === null || _a === void 0 ? void 0 : _a.formats) === null || _b === void 0 ? void 0 : _b.LT); | ||
return /A|a/.test((_c = (_b = (_a = this.rawDayJsInstance.Ls[this.locale || "en"]) === null || _a === void 0 ? void 0 : _a.formats) === null || _b === void 0 ? void 0 : _b.LT) !== null && _c !== void 0 ? _c : ""); | ||
}; | ||
this.getCurrentLocaleCode = function () { | ||
return _this.locale || "en"; | ||
this.getCurrentLocaleCode = () => { | ||
return this.locale || "en"; | ||
}; | ||
this.getFormatHelperText = function (format) { | ||
this.getFormatHelperText = (format) => { | ||
var _a, _b; | ||
// @see https://github.com/iamkun/dayjs/blob/dev/src/plugin/localizedFormat/index.js | ||
var localFormattingTokens = /(\[[^\[]*\])|(\\)?(LTS|LT|LL?L?L?)|./g; | ||
return format | ||
.match(localFormattingTokens) | ||
.map(function (token) { | ||
return ((_b = (_a = format | ||
.match(localFormattingTokens)) === null || _a === void 0 ? void 0 : _a.map((token) => { | ||
var _a, _b; | ||
@@ -70,77 +62,86 @@ var firstCharacter = token[0]; | ||
/* istanbul ignore next */ | ||
return (_b = (_a = _this.rawDayJsInstance.Ls[_this.locale || "en"]) === null || _a === void 0 ? void 0 : _a.formats[token]) !== null && _b !== void 0 ? _b : token; | ||
return ((_b = (_a = this.rawDayJsInstance.Ls[this.locale || "en"]) === null || _a === void 0 ? void 0 : _a.formats[token]) !== null && _b !== void 0 ? _b : token); | ||
} | ||
return token; | ||
}) | ||
.join("") | ||
.replace(/a/gi, "(a|p)m") | ||
.toLocaleLowerCase(); | ||
}).join("").replace(/a/gi, "(a|p)m").toLocaleLowerCase()) !== null && _b !== void 0 ? _b : format); | ||
}; | ||
this.parseISO = function (isoString) { | ||
return _this.dayjs(isoString); | ||
this.parseISO = (isoString) => { | ||
return this.dayjs(isoString); | ||
}; | ||
this.toISO = function (value) { | ||
this.toISO = (value) => { | ||
return value.toISOString(); | ||
}; | ||
this.parse = function (value, format) { | ||
this.parse = (value, format) => { | ||
if (value === "") { | ||
return null; | ||
} | ||
return _this.dayjs(value, format, _this.locale, true); | ||
return this.dayjs(value, format, this.locale, true); | ||
}; | ||
this.date = function (value) { | ||
this.date = (value) => { | ||
if (value === null) { | ||
return null; | ||
} | ||
return _this.dayjs(value); | ||
return this.dayjs(value); | ||
}; | ||
this.toJsDate = function (value) { | ||
this.toJsDate = (value) => { | ||
return value.toDate(); | ||
}; | ||
this.isValid = function (value) { | ||
return _this.dayjs(value).isValid(); | ||
this.isValid = (value) => { | ||
return this.dayjs(value).isValid(); | ||
}; | ||
this.isNull = function (date) { | ||
this.isNull = (date) => { | ||
return date === null; | ||
}; | ||
this.getDiff = function (date, comparing, units) { | ||
this.getDiff = (date, comparing, units) => { | ||
if (typeof comparing === "string") { | ||
comparing = this.dayjs(comparing); | ||
} | ||
if (!comparing.isValid()) { | ||
return 0; | ||
} | ||
return date.diff(comparing, units); | ||
}; | ||
this.isAfter = function (date, value) { | ||
this.isAfter = (date, value) => { | ||
return date.isAfter(value); | ||
}; | ||
this.isBefore = function (date, value) { | ||
this.isBefore = (date, value) => { | ||
return date.isBefore(value); | ||
}; | ||
this.isAfterDay = function (date, value) { | ||
this.isAfterDay = (date, value) => { | ||
return date.isAfter(value, "day"); | ||
}; | ||
this.isBeforeDay = function (date, value) { | ||
this.isBeforeDay = (date, value) => { | ||
return date.isBefore(value, "day"); | ||
}; | ||
this.isBeforeYear = function (date, value) { | ||
this.isAfterMonth = (date, value) => { | ||
return date.isAfter(value, "month"); | ||
}; | ||
this.isBeforeMonth = (date, value) => { | ||
return date.isBefore(value, "month"); | ||
}; | ||
this.isBeforeYear = (date, value) => { | ||
return date.isBefore(value, "year"); | ||
}; | ||
this.isAfterYear = function (date, value) { | ||
this.isAfterYear = (date, value) => { | ||
return date.isAfter(value, "year"); | ||
}; | ||
this.startOfDay = function (date) { | ||
this.startOfDay = (date) => { | ||
return date.startOf("day"); | ||
}; | ||
this.endOfDay = function (date) { | ||
this.endOfDay = (date) => { | ||
return date.endOf("day"); | ||
}; | ||
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 _this.dayjs(date).format(formatString); | ||
this.formatByString = (date, formatString) => { | ||
return this.dayjs(date).format(formatString); | ||
}; | ||
this.formatNumber = function (numberToFormat) { | ||
this.formatNumber = (numberToFormat) => { | ||
return numberToFormat; | ||
}; | ||
this.getHours = function (date) { | ||
this.getHours = (date) => { | ||
return date.hour(); | ||
}; | ||
this.addSeconds = function (date, count) { | ||
this.addSeconds = (date, count) => { | ||
return count < 0 | ||
@@ -150,3 +151,3 @@ ? date.subtract(Math.abs(count), "second") | ||
}; | ||
this.addMinutes = function (date, count) { | ||
this.addMinutes = (date, count) => { | ||
return count < 0 | ||
@@ -156,3 +157,3 @@ ? date.subtract(Math.abs(count), "minute") | ||
}; | ||
this.addHours = function (date, count) { | ||
this.addHours = (date, count) => { | ||
return count < 0 | ||
@@ -162,3 +163,3 @@ ? date.subtract(Math.abs(count), "hour") | ||
}; | ||
this.addDays = function (date, count) { | ||
this.addDays = (date, count) => { | ||
return count < 0 | ||
@@ -168,3 +169,3 @@ ? date.subtract(Math.abs(count), "day") | ||
}; | ||
this.addWeeks = function (date, count) { | ||
this.addWeeks = (date, count) => { | ||
return count < 0 | ||
@@ -174,3 +175,3 @@ ? date.subtract(Math.abs(count), "week") | ||
}; | ||
this.addMonths = function (date, count) { | ||
this.addMonths = (date, count) => { | ||
return count < 0 | ||
@@ -180,3 +181,3 @@ ? date.subtract(Math.abs(count), "month") | ||
}; | ||
this.addYears = function (date, count) { | ||
this.addYears = (date, count) => { | ||
return count < 0 | ||
@@ -186,109 +187,107 @@ ? date.subtract(Math.abs(count), "year") | ||
}; | ||
this.setMonth = function (date, count) { | ||
this.setMonth = (date, count) => { | ||
return date.set("month", count); | ||
}; | ||
this.setHours = function (date, count) { | ||
this.setHours = (date, count) => { | ||
return date.set("hour", count); | ||
}; | ||
this.getMinutes = function (date) { | ||
this.getMinutes = (date) => { | ||
return date.minute(); | ||
}; | ||
this.setMinutes = function (date, count) { | ||
this.setMinutes = (date, count) => { | ||
return date.set("minute", count); | ||
}; | ||
this.getSeconds = function (date) { | ||
this.getSeconds = (date) => { | ||
return date.second(); | ||
}; | ||
this.setSeconds = function (date, count) { | ||
this.setSeconds = (date, count) => { | ||
return date.set("second", count); | ||
}; | ||
this.getMonth = function (date) { | ||
this.getMonth = (date) => { | ||
return date.month(); | ||
}; | ||
this.getDate = function (date) { | ||
this.getDate = (date) => { | ||
return date.date(); | ||
}; | ||
this.setDate = function (date, count) { | ||
this.setDate = (date, count) => { | ||
return date.set("date", count); | ||
}; | ||
this.getDaysInMonth = function (date) { | ||
this.getDaysInMonth = (date) => { | ||
return date.daysInMonth(); | ||
}; | ||
this.isSameDay = function (date, comparing) { | ||
this.isSameDay = (date, comparing) => { | ||
return date.isSame(comparing, "day"); | ||
}; | ||
this.isSameMonth = function (date, comparing) { | ||
this.isSameMonth = (date, comparing) => { | ||
return date.isSame(comparing, "month"); | ||
}; | ||
this.isSameYear = function (date, comparing) { | ||
this.isSameYear = (date, comparing) => { | ||
return date.isSame(comparing, "year"); | ||
}; | ||
this.isSameHour = function (date, comparing) { | ||
this.isSameHour = (date, comparing) => { | ||
return date.isSame(comparing, "hour"); | ||
}; | ||
this.getMeridiemText = function (ampm) { | ||
this.getMeridiemText = (ampm) => { | ||
return ampm === "am" ? "AM" : "PM"; | ||
}; | ||
this.startOfYear = function (date) { | ||
this.startOfYear = (date) => { | ||
return date.startOf("year"); | ||
}; | ||
this.endOfYear = function (date) { | ||
this.endOfYear = (date) => { | ||
return date.endOf("year"); | ||
}; | ||
this.startOfMonth = function (date) { | ||
this.startOfMonth = (date) => { | ||
return date.startOf("month"); | ||
}; | ||
this.endOfMonth = function (date) { | ||
this.endOfMonth = (date) => { | ||
return date.endOf("month"); | ||
}; | ||
this.startOfWeek = function (date) { | ||
this.startOfWeek = (date) => { | ||
return date.startOf("week"); | ||
}; | ||
this.endOfWeek = function (date) { | ||
this.endOfWeek = (date) => { | ||
return date.endOf("week"); | ||
}; | ||
this.getNextMonth = function (date) { | ||
this.getNextMonth = (date) => { | ||
return date.add(1, "month"); | ||
}; | ||
this.getPreviousMonth = function (date) { | ||
this.getPreviousMonth = (date) => { | ||
return date.subtract(1, "month"); | ||
}; | ||
this.getMonthArray = function (date) { | ||
var firstMonth = date.startOf("year"); | ||
var monthArray = [firstMonth]; | ||
this.getMonthArray = (date) => { | ||
const firstMonth = date.startOf("year"); | ||
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.getYear = function (date) { | ||
this.getYear = (date) => { | ||
return date.year(); | ||
}; | ||
this.setYear = function (date, year) { | ||
this.setYear = (date, year) => { | ||
return date.set("year", year); | ||
}; | ||
this.mergeDateAndTime = function (date, time) { | ||
this.mergeDateAndTime = (date, time) => { | ||
return date.hour(time.hour()).minute(time.minute()).second(time.second()); | ||
}; | ||
this.getWeekdays = function () { | ||
var start = _this.dayjs().startOf("week"); | ||
return [0, 1, 2, 3, 4, 5, 6].map(function (diff) { | ||
return _this.formatByString(start.add(diff, "day"), "dd"); | ||
}); | ||
this.getWeekdays = () => { | ||
const start = this.dayjs().startOf("week"); | ||
return [0, 1, 2, 3, 4, 5, 6].map((diff) => this.formatByString(start.add(diff, "day"), "dd")); | ||
}; | ||
this.isEqual = function (value, comparing) { | ||
this.isEqual = (value, comparing) => { | ||
if (value === null && comparing === null) { | ||
return true; | ||
} | ||
return _this.dayjs(value).isSame(comparing); | ||
return this.dayjs(value).isSame(comparing); | ||
}; | ||
this.getWeekArray = function (date) { | ||
var start = _this.dayjs(date).startOf("month").startOf("week"); | ||
var end = _this.dayjs(date).endOf("month").endOf("week"); | ||
var count = 0; | ||
var current = start; | ||
var nestedWeeks = []; | ||
this.getWeekArray = (date) => { | ||
const start = this.dayjs(date).startOf("month").startOf("week"); | ||
const end = this.dayjs(date).endOf("month").endOf("week"); | ||
let count = 0; | ||
let current = start; | ||
const nestedWeeks = []; | ||
while (current.isBefore(end)) { | ||
var weekNumber = Math.floor(count / 7); | ||
const weekNumber = Math.floor(count / 7); | ||
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || []; | ||
@@ -301,7 +300,7 @@ nestedWeeks[weekNumber].push(current); | ||
}; | ||
this.getYearRange = function (start, end) { | ||
var startDate = _this.dayjs(start).startOf("year"); | ||
var endDate = _this.dayjs(end).endOf("year"); | ||
var years = []; | ||
var current = startDate; | ||
this.getYearRange = (start, end) => { | ||
const startDate = this.dayjs(start).startOf("year"); | ||
const endDate = this.dayjs(end).endOf("year"); | ||
const years = []; | ||
let current = startDate; | ||
while (current.isBefore(endDate)) { | ||
@@ -313,4 +312,3 @@ years.push(current); | ||
}; | ||
this.isWithinRange = function (date, _a) { | ||
var start = _a[0], end = _a[1]; | ||
this.isWithinRange = (date, [start, end]) => { | ||
return date.isBetween(start, end, null, "[]"); | ||
@@ -323,5 +321,4 @@ }; | ||
} | ||
return DayjsUtils; | ||
}()); | ||
} | ||
export { DayjsUtils as default }; |
@@ -18,10 +18,4 @@ 'use strict'; | ||
defaultDayjs__default["default"].extend(isBetweenPlugin__default["default"]); | ||
var withLocale = function (dayjs, locale) { return (!locale ? dayjs : function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return dayjs.apply(void 0, args).locale(locale); | ||
}); }; | ||
var defaultFormats = { | ||
const withLocale = (dayjs, locale) => (!locale ? dayjs : (...args) => dayjs(...args).locale(locale)); | ||
const defaultFormats = { | ||
normalDateWithWeekday: "ddd, MMM D", | ||
@@ -55,21 +49,19 @@ normalDate: "D MMMM", | ||
}; | ||
var DayjsUtils = /** @class */ (function () { | ||
function DayjsUtils(_a) { | ||
var _this = this; | ||
var _b = _a === void 0 ? {} : _a, locale = _b.locale, formats = _b.formats, instance = _b.instance; | ||
class DayjsUtils { | ||
constructor({ locale, formats, instance } = {}) { | ||
this.lib = "dayjs"; | ||
this.is12HourCycleInCurrentLocale = function () { | ||
var _a, _b; | ||
this.is12HourCycleInCurrentLocale = () => { | ||
var _a, _b, _c; | ||
/* istanbul ignore next */ | ||
return /A|a/.test((_b = (_a = _this.rawDayJsInstance.Ls[_this.locale || "en"]) === null || _a === void 0 ? void 0 : _a.formats) === null || _b === void 0 ? void 0 : _b.LT); | ||
return /A|a/.test((_c = (_b = (_a = this.rawDayJsInstance.Ls[this.locale || "en"]) === null || _a === void 0 ? void 0 : _a.formats) === null || _b === void 0 ? void 0 : _b.LT) !== null && _c !== void 0 ? _c : ""); | ||
}; | ||
this.getCurrentLocaleCode = function () { | ||
return _this.locale || "en"; | ||
this.getCurrentLocaleCode = () => { | ||
return this.locale || "en"; | ||
}; | ||
this.getFormatHelperText = function (format) { | ||
this.getFormatHelperText = (format) => { | ||
var _a, _b; | ||
// @see https://github.com/iamkun/dayjs/blob/dev/src/plugin/localizedFormat/index.js | ||
var localFormattingTokens = /(\[[^\[]*\])|(\\)?(LTS|LT|LL?L?L?)|./g; | ||
return format | ||
.match(localFormattingTokens) | ||
.map(function (token) { | ||
return ((_b = (_a = format | ||
.match(localFormattingTokens)) === null || _a === void 0 ? void 0 : _a.map((token) => { | ||
var _a, _b; | ||
@@ -79,77 +71,86 @@ var firstCharacter = token[0]; | ||
/* istanbul ignore next */ | ||
return (_b = (_a = _this.rawDayJsInstance.Ls[_this.locale || "en"]) === null || _a === void 0 ? void 0 : _a.formats[token]) !== null && _b !== void 0 ? _b : token; | ||
return ((_b = (_a = this.rawDayJsInstance.Ls[this.locale || "en"]) === null || _a === void 0 ? void 0 : _a.formats[token]) !== null && _b !== void 0 ? _b : token); | ||
} | ||
return token; | ||
}) | ||
.join("") | ||
.replace(/a/gi, "(a|p)m") | ||
.toLocaleLowerCase(); | ||
}).join("").replace(/a/gi, "(a|p)m").toLocaleLowerCase()) !== null && _b !== void 0 ? _b : format); | ||
}; | ||
this.parseISO = function (isoString) { | ||
return _this.dayjs(isoString); | ||
this.parseISO = (isoString) => { | ||
return this.dayjs(isoString); | ||
}; | ||
this.toISO = function (value) { | ||
this.toISO = (value) => { | ||
return value.toISOString(); | ||
}; | ||
this.parse = function (value, format) { | ||
this.parse = (value, format) => { | ||
if (value === "") { | ||
return null; | ||
} | ||
return _this.dayjs(value, format, _this.locale, true); | ||
return this.dayjs(value, format, this.locale, true); | ||
}; | ||
this.date = function (value) { | ||
this.date = (value) => { | ||
if (value === null) { | ||
return null; | ||
} | ||
return _this.dayjs(value); | ||
return this.dayjs(value); | ||
}; | ||
this.toJsDate = function (value) { | ||
this.toJsDate = (value) => { | ||
return value.toDate(); | ||
}; | ||
this.isValid = function (value) { | ||
return _this.dayjs(value).isValid(); | ||
this.isValid = (value) => { | ||
return this.dayjs(value).isValid(); | ||
}; | ||
this.isNull = function (date) { | ||
this.isNull = (date) => { | ||
return date === null; | ||
}; | ||
this.getDiff = function (date, comparing, units) { | ||
this.getDiff = (date, comparing, units) => { | ||
if (typeof comparing === "string") { | ||
comparing = this.dayjs(comparing); | ||
} | ||
if (!comparing.isValid()) { | ||
return 0; | ||
} | ||
return date.diff(comparing, units); | ||
}; | ||
this.isAfter = function (date, value) { | ||
this.isAfter = (date, value) => { | ||
return date.isAfter(value); | ||
}; | ||
this.isBefore = function (date, value) { | ||
this.isBefore = (date, value) => { | ||
return date.isBefore(value); | ||
}; | ||
this.isAfterDay = function (date, value) { | ||
this.isAfterDay = (date, value) => { | ||
return date.isAfter(value, "day"); | ||
}; | ||
this.isBeforeDay = function (date, value) { | ||
this.isBeforeDay = (date, value) => { | ||
return date.isBefore(value, "day"); | ||
}; | ||
this.isBeforeYear = function (date, value) { | ||
this.isAfterMonth = (date, value) => { | ||
return date.isAfter(value, "month"); | ||
}; | ||
this.isBeforeMonth = (date, value) => { | ||
return date.isBefore(value, "month"); | ||
}; | ||
this.isBeforeYear = (date, value) => { | ||
return date.isBefore(value, "year"); | ||
}; | ||
this.isAfterYear = function (date, value) { | ||
this.isAfterYear = (date, value) => { | ||
return date.isAfter(value, "year"); | ||
}; | ||
this.startOfDay = function (date) { | ||
this.startOfDay = (date) => { | ||
return date.startOf("day"); | ||
}; | ||
this.endOfDay = function (date) { | ||
this.endOfDay = (date) => { | ||
return date.endOf("day"); | ||
}; | ||
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 _this.dayjs(date).format(formatString); | ||
this.formatByString = (date, formatString) => { | ||
return this.dayjs(date).format(formatString); | ||
}; | ||
this.formatNumber = function (numberToFormat) { | ||
this.formatNumber = (numberToFormat) => { | ||
return numberToFormat; | ||
}; | ||
this.getHours = function (date) { | ||
this.getHours = (date) => { | ||
return date.hour(); | ||
}; | ||
this.addSeconds = function (date, count) { | ||
this.addSeconds = (date, count) => { | ||
return count < 0 | ||
@@ -159,3 +160,3 @@ ? date.subtract(Math.abs(count), "second") | ||
}; | ||
this.addMinutes = function (date, count) { | ||
this.addMinutes = (date, count) => { | ||
return count < 0 | ||
@@ -165,3 +166,3 @@ ? date.subtract(Math.abs(count), "minute") | ||
}; | ||
this.addHours = function (date, count) { | ||
this.addHours = (date, count) => { | ||
return count < 0 | ||
@@ -171,3 +172,3 @@ ? date.subtract(Math.abs(count), "hour") | ||
}; | ||
this.addDays = function (date, count) { | ||
this.addDays = (date, count) => { | ||
return count < 0 | ||
@@ -177,3 +178,3 @@ ? date.subtract(Math.abs(count), "day") | ||
}; | ||
this.addWeeks = function (date, count) { | ||
this.addWeeks = (date, count) => { | ||
return count < 0 | ||
@@ -183,3 +184,3 @@ ? date.subtract(Math.abs(count), "week") | ||
}; | ||
this.addMonths = function (date, count) { | ||
this.addMonths = (date, count) => { | ||
return count < 0 | ||
@@ -189,3 +190,3 @@ ? date.subtract(Math.abs(count), "month") | ||
}; | ||
this.addYears = function (date, count) { | ||
this.addYears = (date, count) => { | ||
return count < 0 | ||
@@ -195,109 +196,107 @@ ? date.subtract(Math.abs(count), "year") | ||
}; | ||
this.setMonth = function (date, count) { | ||
this.setMonth = (date, count) => { | ||
return date.set("month", count); | ||
}; | ||
this.setHours = function (date, count) { | ||
this.setHours = (date, count) => { | ||
return date.set("hour", count); | ||
}; | ||
this.getMinutes = function (date) { | ||
this.getMinutes = (date) => { | ||
return date.minute(); | ||
}; | ||
this.setMinutes = function (date, count) { | ||
this.setMinutes = (date, count) => { | ||
return date.set("minute", count); | ||
}; | ||
this.getSeconds = function (date) { | ||
this.getSeconds = (date) => { | ||
return date.second(); | ||
}; | ||
this.setSeconds = function (date, count) { | ||
this.setSeconds = (date, count) => { | ||
return date.set("second", count); | ||
}; | ||
this.getMonth = function (date) { | ||
this.getMonth = (date) => { | ||
return date.month(); | ||
}; | ||
this.getDate = function (date) { | ||
this.getDate = (date) => { | ||
return date.date(); | ||
}; | ||
this.setDate = function (date, count) { | ||
this.setDate = (date, count) => { | ||
return date.set("date", count); | ||
}; | ||
this.getDaysInMonth = function (date) { | ||
this.getDaysInMonth = (date) => { | ||
return date.daysInMonth(); | ||
}; | ||
this.isSameDay = function (date, comparing) { | ||
this.isSameDay = (date, comparing) => { | ||
return date.isSame(comparing, "day"); | ||
}; | ||
this.isSameMonth = function (date, comparing) { | ||
this.isSameMonth = (date, comparing) => { | ||
return date.isSame(comparing, "month"); | ||
}; | ||
this.isSameYear = function (date, comparing) { | ||
this.isSameYear = (date, comparing) => { | ||
return date.isSame(comparing, "year"); | ||
}; | ||
this.isSameHour = function (date, comparing) { | ||
this.isSameHour = (date, comparing) => { | ||
return date.isSame(comparing, "hour"); | ||
}; | ||
this.getMeridiemText = function (ampm) { | ||
this.getMeridiemText = (ampm) => { | ||
return ampm === "am" ? "AM" : "PM"; | ||
}; | ||
this.startOfYear = function (date) { | ||
this.startOfYear = (date) => { | ||
return date.startOf("year"); | ||
}; | ||
this.endOfYear = function (date) { | ||
this.endOfYear = (date) => { | ||
return date.endOf("year"); | ||
}; | ||
this.startOfMonth = function (date) { | ||
this.startOfMonth = (date) => { | ||
return date.startOf("month"); | ||
}; | ||
this.endOfMonth = function (date) { | ||
this.endOfMonth = (date) => { | ||
return date.endOf("month"); | ||
}; | ||
this.startOfWeek = function (date) { | ||
this.startOfWeek = (date) => { | ||
return date.startOf("week"); | ||
}; | ||
this.endOfWeek = function (date) { | ||
this.endOfWeek = (date) => { | ||
return date.endOf("week"); | ||
}; | ||
this.getNextMonth = function (date) { | ||
this.getNextMonth = (date) => { | ||
return date.add(1, "month"); | ||
}; | ||
this.getPreviousMonth = function (date) { | ||
this.getPreviousMonth = (date) => { | ||
return date.subtract(1, "month"); | ||
}; | ||
this.getMonthArray = function (date) { | ||
var firstMonth = date.startOf("year"); | ||
var monthArray = [firstMonth]; | ||
this.getMonthArray = (date) => { | ||
const firstMonth = date.startOf("year"); | ||
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.getYear = function (date) { | ||
this.getYear = (date) => { | ||
return date.year(); | ||
}; | ||
this.setYear = function (date, year) { | ||
this.setYear = (date, year) => { | ||
return date.set("year", year); | ||
}; | ||
this.mergeDateAndTime = function (date, time) { | ||
this.mergeDateAndTime = (date, time) => { | ||
return date.hour(time.hour()).minute(time.minute()).second(time.second()); | ||
}; | ||
this.getWeekdays = function () { | ||
var start = _this.dayjs().startOf("week"); | ||
return [0, 1, 2, 3, 4, 5, 6].map(function (diff) { | ||
return _this.formatByString(start.add(diff, "day"), "dd"); | ||
}); | ||
this.getWeekdays = () => { | ||
const start = this.dayjs().startOf("week"); | ||
return [0, 1, 2, 3, 4, 5, 6].map((diff) => this.formatByString(start.add(diff, "day"), "dd")); | ||
}; | ||
this.isEqual = function (value, comparing) { | ||
this.isEqual = (value, comparing) => { | ||
if (value === null && comparing === null) { | ||
return true; | ||
} | ||
return _this.dayjs(value).isSame(comparing); | ||
return this.dayjs(value).isSame(comparing); | ||
}; | ||
this.getWeekArray = function (date) { | ||
var start = _this.dayjs(date).startOf("month").startOf("week"); | ||
var end = _this.dayjs(date).endOf("month").endOf("week"); | ||
var count = 0; | ||
var current = start; | ||
var nestedWeeks = []; | ||
this.getWeekArray = (date) => { | ||
const start = this.dayjs(date).startOf("month").startOf("week"); | ||
const end = this.dayjs(date).endOf("month").endOf("week"); | ||
let count = 0; | ||
let current = start; | ||
const nestedWeeks = []; | ||
while (current.isBefore(end)) { | ||
var weekNumber = Math.floor(count / 7); | ||
const weekNumber = Math.floor(count / 7); | ||
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || []; | ||
@@ -310,7 +309,7 @@ nestedWeeks[weekNumber].push(current); | ||
}; | ||
this.getYearRange = function (start, end) { | ||
var startDate = _this.dayjs(start).startOf("year"); | ||
var endDate = _this.dayjs(end).endOf("year"); | ||
var years = []; | ||
var current = startDate; | ||
this.getYearRange = (start, end) => { | ||
const startDate = this.dayjs(start).startOf("year"); | ||
const endDate = this.dayjs(end).endOf("year"); | ||
const years = []; | ||
let current = startDate; | ||
while (current.isBefore(endDate)) { | ||
@@ -322,4 +321,3 @@ years.push(current); | ||
}; | ||
this.isWithinRange = function (date, _a) { | ||
var start = _a[0], end = _a[1]; | ||
this.isWithinRange = (date, [start, end]) => { | ||
return date.isBetween(start, end, null, "[]"); | ||
@@ -332,5 +330,4 @@ }; | ||
} | ||
return DayjsUtils; | ||
}()); | ||
} | ||
module.exports = DayjsUtils; |
{ | ||
"name": "@date-io/dayjs", | ||
"version": "2.16.0", | ||
"version": "2.17.0", | ||
"description": "Abstraction over common javascript date management libraries", | ||
@@ -17,3 +17,3 @@ "main": "build/index.js", | ||
"dependencies": { | ||
"@date-io/core": "^2.16.0" | ||
"@date-io/core": "^2.17.0" | ||
}, | ||
@@ -23,3 +23,3 @@ "devDependencies": { | ||
"rollup": "^2.0.2", | ||
"typescript": "^3.7.2" | ||
"typescript": "^5.0.0" | ||
}, | ||
@@ -51,3 +51,3 @@ "scripts": { | ||
"license": "MIT", | ||
"gitHead": "e9eddac85299013881e8bf005f5030fdf46d4756" | ||
"gitHead": "e497a04c456f04211c8b0877d002c85e72ea98a2" | ||
} |
@@ -22,3 +22,2 @@ import defaultDayjs, { QUnitType } from "dayjs"; | ||
) => TDate; | ||
const withLocale = <TDate extends Dayjs>( | ||
@@ -76,3 +75,3 @@ dayjs: any, | ||
/* istanbul ignore next */ | ||
return /A|a/.test(this.rawDayJsInstance.Ls[this.locale || "en"]?.formats?.LT); | ||
return /A|a/.test(this.rawDayJsInstance.Ls[this.locale || "en"]?.formats?.LT ?? ""); | ||
}; | ||
@@ -87,15 +86,22 @@ | ||
var localFormattingTokens = /(\[[^\[]*\])|(\\)?(LTS|LT|LL?L?L?)|./g; | ||
return format | ||
.match(localFormattingTokens) | ||
.map((token) => { | ||
var firstCharacter = token[0]; | ||
if (firstCharacter === "L") { | ||
/* istanbul ignore next */ | ||
return this.rawDayJsInstance.Ls[this.locale || "en"]?.formats[token] ?? token; | ||
} | ||
return token; | ||
}) | ||
.join("") | ||
.replace(/a/gi, "(a|p)m") | ||
.toLocaleLowerCase(); | ||
return ( | ||
format | ||
.match(localFormattingTokens) | ||
?.map((token) => { | ||
var firstCharacter = token[0]; | ||
if (firstCharacter === "L") { | ||
/* istanbul ignore next */ | ||
return ( | ||
this.rawDayJsInstance.Ls[this.locale || "en"]?.formats[ | ||
token as keyof ILocale["formats"] | ||
] ?? token | ||
); | ||
} | ||
return token; | ||
}) | ||
.join("") | ||
.replace(/a/gi, "(a|p)m") | ||
.toLocaleLowerCase() ?? format | ||
); | ||
}; | ||
@@ -139,3 +145,11 @@ | ||
public getDiff = (date: TDate, comparing: TDate, units?: Unit) => { | ||
public getDiff = (date: TDate, comparing: TDate | string, units?: Unit) => { | ||
if (typeof comparing === "string") { | ||
comparing = this.dayjs(comparing); | ||
} | ||
if (!comparing.isValid()) { | ||
return 0; | ||
} | ||
return date.diff(comparing, units as QUnitType); | ||
@@ -160,2 +174,10 @@ }; | ||
public isAfterMonth = (date: Dayjs, value: Dayjs) => { | ||
return date.isAfter(value, "month"); | ||
}; | ||
public isBeforeMonth = (date: Dayjs, value: Dayjs) => { | ||
return date.isBefore(value, "month"); | ||
}; | ||
public isBeforeYear = (date: Dayjs, value: Dayjs) => { | ||
@@ -162,0 +184,0 @@ return date.isBefore(value, "year"); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40998
1060
Updated@date-io/core@^2.17.0