@mezzanine-ui/core 0.13.2 → 0.13.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { DateTime, Info, Interval } from 'luxon';
|
|
2
|
+
import range from 'lodash/range';
|
|
3
|
+
import chunk from 'lodash/chunk';
|
|
4
|
+
|
|
5
|
+
const CalendarMethodsLuxon = {
|
|
6
|
+
/** Get date infos */
|
|
7
|
+
getNow: () => DateTime.now().toISO(),
|
|
8
|
+
getSecond: (date) => DateTime.fromISO(date).second,
|
|
9
|
+
getMinute: (date) => DateTime.fromISO(date).minute,
|
|
10
|
+
getHour: (date) => DateTime.fromISO(date).hour,
|
|
11
|
+
getDate: (date) => DateTime.fromISO(date).day,
|
|
12
|
+
getWeekDay: (date) => DateTime.fromISO(date).weekday,
|
|
13
|
+
getMonth: (date) => DateTime.fromISO(date).month,
|
|
14
|
+
getYear: (date) => DateTime.fromISO(date).year,
|
|
15
|
+
getWeekDayNames: (locale) => Info.weekdays('narrow', { locale }),
|
|
16
|
+
getMonthShortName: (month, locale) => DateTime.now().set({ month }).toFormat('MMM', { locale }),
|
|
17
|
+
getMonthShortNames: (locale) => Info.months('short', { locale }),
|
|
18
|
+
/** Manipulate */
|
|
19
|
+
addDay: (date, diff) => DateTime.fromISO(date).plus({ day: diff }).toISO(),
|
|
20
|
+
addYear: (date, diff) => DateTime.fromISO(date).plus({ year: diff }).toISO(),
|
|
21
|
+
addMonth: (date, diff) => DateTime.fromISO(date).plus({ month: diff }).toISO(),
|
|
22
|
+
setSecond: (date, second) => DateTime.fromISO(date).set({ second }).toISO(),
|
|
23
|
+
setMinute: (date, minute) => DateTime.fromISO(date).set({ minute }).toISO(),
|
|
24
|
+
setHour: (date, hour) => DateTime.fromISO(date).set({ hour }).toISO(),
|
|
25
|
+
setMonth: (date, month) => DateTime.fromISO(date).set({ month }).toISO(),
|
|
26
|
+
setYear: (date, year) => DateTime.fromISO(date).set({ year }).toISO(),
|
|
27
|
+
setDate: (date, target) => DateTime.fromISO(date).set({ day: target }).toISO(),
|
|
28
|
+
startOf: (target, granularity) => DateTime.fromISO(target).startOf(granularity).toISO(),
|
|
29
|
+
/** Generate day calendar */
|
|
30
|
+
getCalendarGrid: (target) => {
|
|
31
|
+
const lastDateOfPrevMonth = DateTime.fromISO(target).minus({ month: 1 }).endOf('month').day;
|
|
32
|
+
const firstDayOfCurrentMonth = DateTime.fromISO(target).set({ day: 1 }).weekday;
|
|
33
|
+
const lastDateOfCurrentMonth = DateTime.fromISO(target).endOf('month').day;
|
|
34
|
+
return chunk([
|
|
35
|
+
...range(lastDateOfPrevMonth - firstDayOfCurrentMonth + 2, lastDateOfPrevMonth + 1),
|
|
36
|
+
...range(1, lastDateOfCurrentMonth + 1),
|
|
37
|
+
...range(1, 42 - lastDateOfCurrentMonth - firstDayOfCurrentMonth + 2),
|
|
38
|
+
], 7);
|
|
39
|
+
},
|
|
40
|
+
/** Compares */
|
|
41
|
+
isBefore: (target, comparison) => DateTime.fromISO(target) < DateTime.fromISO(comparison),
|
|
42
|
+
isBetween: (value, target1, target2) => Interval.fromDateTimes(DateTime.fromISO(target1), DateTime.fromISO(target2)).contains(DateTime.fromISO(value)),
|
|
43
|
+
isSameDate: (dateOne, dateTwo) => DateTime.fromISO(dateOne).hasSame(DateTime.fromISO(dateTwo), 'day'),
|
|
44
|
+
isSameWeek: (dateOne, dateTwo) => DateTime.fromISO(dateOne).hasSame(DateTime.fromISO(dateTwo), 'week'),
|
|
45
|
+
isInMonth: (target, month) => DateTime.fromISO(target).month === month,
|
|
46
|
+
isDateIncluded: (date, targets) => targets
|
|
47
|
+
.some((target) => DateTime.fromISO(date).hasSame(DateTime.fromISO(target), 'day')),
|
|
48
|
+
isWeekIncluded: (firstDateOfWeek, targets) => targets.some((target) => DateTime.fromISO(firstDateOfWeek).hasSame(DateTime.fromISO(target), 'week')),
|
|
49
|
+
isMonthIncluded: (date, targets) => targets
|
|
50
|
+
.some((target) => DateTime.fromISO(date).hasSame(DateTime.fromISO(target), 'month')),
|
|
51
|
+
isYearIncluded: (date, targets) => targets
|
|
52
|
+
.some((target) => DateTime.fromISO(date).hasSame(DateTime.fromISO(target), 'year')),
|
|
53
|
+
/** Format */
|
|
54
|
+
formatToString: (locale, date, format) => {
|
|
55
|
+
const luxonFormat = format
|
|
56
|
+
.replace(/YYYY/g, 'yyyy')
|
|
57
|
+
.replace(/YY/g, 'yy')
|
|
58
|
+
.replace(/Y/g, 'y')
|
|
59
|
+
.replace(/dddd/g, 'EEEE')
|
|
60
|
+
.replace(/ddd/g, 'EEE')
|
|
61
|
+
.replace(/dd/g, 'EEE')
|
|
62
|
+
.replace(/d/g, 'E')
|
|
63
|
+
.replace(/DDDD/g, 'ooo')
|
|
64
|
+
.replace(/DDD/g, 'o')
|
|
65
|
+
.replace(/DD/g, 'dd')
|
|
66
|
+
.replace(/D/g, 'd')
|
|
67
|
+
.replace(/e/g, 'E')
|
|
68
|
+
.replace(/ww/g, 'WW')
|
|
69
|
+
.replace(/w/g, 'W')
|
|
70
|
+
.replace(/kk/g, 'HH')
|
|
71
|
+
.replace(/k/g, 'H')
|
|
72
|
+
.replace(/gggg/g, 'kkkk')
|
|
73
|
+
.replace(/gg/g, 'kk')
|
|
74
|
+
.replace(/GGGG/g, 'kkkk')
|
|
75
|
+
.replace(/GG/g, 'kk')
|
|
76
|
+
.replace(/SSS/g, 'uuu')
|
|
77
|
+
.replace(/SS/g, 'uu')
|
|
78
|
+
.replace(/S/g, 'u')
|
|
79
|
+
.replace(/zz/g, 'ZZZZ')
|
|
80
|
+
.replace(/z/g, 'ZZZZ');
|
|
81
|
+
return (date instanceof Date ? DateTime.fromJSDate(date) : DateTime.fromISO(date))
|
|
82
|
+
.toFormat(luxonFormat, { locale });
|
|
83
|
+
},
|
|
84
|
+
/** Parse */
|
|
85
|
+
parse: (locale, text, formats) => {
|
|
86
|
+
for (let i = 0; i < formats.length; i += 1) {
|
|
87
|
+
let format = formats[i];
|
|
88
|
+
let formatText = text;
|
|
89
|
+
if (format.includes('wo') || format.includes('Wo')) {
|
|
90
|
+
format = format.replace(/wo/g, 'w').replace(/Wo/g, 'W');
|
|
91
|
+
const matchFormat = format.match(/[-YyMmDdHhSsWwGg]+/g);
|
|
92
|
+
const matchText = formatText.match(/[-\d]+/g);
|
|
93
|
+
if (matchFormat && matchText) {
|
|
94
|
+
format = matchFormat.join('');
|
|
95
|
+
formatText = matchText.join('');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const date = DateTime.fromFormat(formatText, format, { locale });
|
|
99
|
+
if (date.isValid) {
|
|
100
|
+
return date.toISO();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export { CalendarMethodsLuxon as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mezzanine-ui/core",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.4",
|
|
4
4
|
"description": "Core for mezzanine-ui",
|
|
5
5
|
"author": "Mezzanine",
|
|
6
6
|
"repository": {
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"dayjs": "^1.10.7",
|
|
27
27
|
"lodash": "^4.17.21",
|
|
28
|
+
"luxon": "^3.4.3",
|
|
28
29
|
"moment": "^2.29.1"
|
|
29
30
|
},
|
|
30
31
|
"peerDependenciesMeta": {
|
|
@@ -33,12 +34,18 @@
|
|
|
33
34
|
},
|
|
34
35
|
"moment": {
|
|
35
36
|
"optional": true
|
|
37
|
+
},
|
|
38
|
+
"luxon": {
|
|
39
|
+
"optional": true
|
|
36
40
|
}
|
|
37
41
|
},
|
|
38
42
|
"dependencies": {
|
|
39
|
-
"@mezzanine-ui/icons": "^0.13.
|
|
40
|
-
"@mezzanine-ui/system": "^0.13.
|
|
43
|
+
"@mezzanine-ui/icons": "^0.13.4",
|
|
44
|
+
"@mezzanine-ui/system": "^0.13.4",
|
|
41
45
|
"lodash": "^4.17.21",
|
|
42
46
|
"tslib": "^2.4.1"
|
|
47
|
+
},
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@types/luxon": "^3.3.2"
|
|
43
50
|
}
|
|
44
51
|
}
|