@powerlines/plugin-date 0.12.299 → 0.12.301
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.
- package/dist/_virtual/rolldown_runtime.cjs +29 -0
- package/dist/components/date-fns.cjs +528 -0
- package/dist/components/date-fns.d.cts +14 -0
- package/dist/components/date-fns.d.cts.map +1 -0
- package/dist/components/date-fns.d.mts +14 -0
- package/dist/components/date-fns.d.mts.map +1 -0
- package/dist/components/date-fns.mjs +528 -0
- package/dist/components/date-fns.mjs.map +1 -0
- package/dist/components/dayjs.cjs +473 -0
- package/dist/components/dayjs.d.cts +14 -0
- package/dist/components/dayjs.d.cts.map +1 -0
- package/dist/components/dayjs.d.mts +14 -0
- package/dist/components/dayjs.d.mts.map +1 -0
- package/dist/components/dayjs.mjs +473 -0
- package/dist/components/dayjs.mjs.map +1 -0
- package/dist/components/index.cjs +4 -0
- package/dist/components/index.d.cts +4 -0
- package/dist/components/index.d.mts +4 -0
- package/dist/components/index.mjs +6 -0
- package/dist/components/luxon.cjs +477 -0
- package/dist/components/luxon.d.cts +14 -0
- package/dist/components/luxon.d.cts.map +1 -0
- package/dist/components/luxon.d.mts +14 -0
- package/dist/components/luxon.d.mts.map +1 -0
- package/dist/components/luxon.mjs +477 -0
- package/dist/components/luxon.mjs.map +1 -0
- package/dist/components/moment.cjs +463 -0
- package/dist/components/moment.d.cts +14 -0
- package/dist/components/moment.d.cts.map +1 -0
- package/dist/components/moment.d.mts +14 -0
- package/dist/components/moment.d.mts.map +1 -0
- package/dist/components/moment.mjs +463 -0
- package/dist/components/moment.mjs.map +1 -0
- package/dist/index.cjs +17 -2007
- package/dist/index.d.cts +8 -40
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +8 -40
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +6 -1970
- package/dist/index.mjs.map +1 -1
- package/dist/node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.cjs +43 -0
- package/dist/node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.mjs +43 -0
- package/dist/node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.mjs.map +1 -0
- package/dist/types/index.cjs +0 -3
- package/dist/types/index.d.cts +2 -3
- package/dist/types/index.d.mts +2 -3
- package/dist/types/index.mjs +0 -4
- package/dist/types/plugin.cjs +0 -1
- package/dist/types/plugin.d.cts +44 -2
- package/dist/types/plugin.d.cts.map +1 -0
- package/dist/types/plugin.d.mts +44 -2
- package/dist/types/plugin.d.mts.map +1 -0
- package/dist/types/plugin.mjs +0 -2
- package/dist/types/runtime.cjs +0 -1
- package/dist/types/runtime.d.cts +712 -2
- package/dist/types/runtime.d.cts.map +1 -0
- package/dist/types/runtime.d.mts +712 -2
- package/dist/types/runtime.d.mts.map +1 -0
- package/dist/types/runtime.mjs +0 -2
- package/package.json +5 -5
- package/dist/index-CA4VvAY_.d.mts +0 -1
- package/dist/index-CmCelmEL.d.cts +0 -1
- package/dist/plugin-CWe-29nA.d.mts +0 -44
- package/dist/plugin-CWe-29nA.d.mts.map +0 -1
- package/dist/plugin-D82ijYIk.d.cts +0 -44
- package/dist/plugin-D82ijYIk.d.cts.map +0 -1
- package/dist/plugin-DHXHjv16.cjs +0 -0
- package/dist/plugin-ufqFO5xS.mjs +0 -1
- package/dist/runtime-B7WoCmC4.mjs +0 -1
- package/dist/runtime-B8LVkjWB.d.cts +0 -712
- package/dist/runtime-B8LVkjWB.d.cts.map +0 -1
- package/dist/runtime-CiYq8JJ6.d.mts +0 -712
- package/dist/runtime-CiYq8JJ6.d.mts.map +0 -1
- package/dist/runtime-D4zTdSwv.cjs +0 -0
- package/dist/types-BReKsBV6.mjs +0 -1
- package/dist/types-CBTc19th.cjs +0 -0
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
import { getFileHeader } from "powerlines/lib/utilities/file-header";
|
|
2
|
+
|
|
3
|
+
//#region src/components/luxon.ts
|
|
4
|
+
/**
|
|
5
|
+
* Generates the Powerlines configuration file.
|
|
6
|
+
*
|
|
7
|
+
* @param context - The build context containing runtime information.
|
|
8
|
+
* @returns A string representing the configuration file content.
|
|
9
|
+
*/
|
|
10
|
+
function luxonModule(context) {
|
|
11
|
+
return `
|
|
12
|
+
/**
|
|
13
|
+
* The Date module provides utility functions for date manipulation and formatting
|
|
14
|
+
*
|
|
15
|
+
* @module ${context.config.framework}:date
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
${getFileHeader(context)}
|
|
19
|
+
|
|
20
|
+
import { DateTime, Settings, Info } from "luxon";
|
|
21
|
+
import {
|
|
22
|
+
StormDateInterface,
|
|
23
|
+
DateFormats,
|
|
24
|
+
TimeUnit
|
|
25
|
+
} from "@powerlines/plugin-date/types/runtime";
|
|
26
|
+
|
|
27
|
+
export const formats: DateFormats = {
|
|
28
|
+
dayOfMonth: "d",
|
|
29
|
+
fullDate: "DD",
|
|
30
|
+
fullDateWithWeekday: "DDDD",
|
|
31
|
+
fullDateTime: "ff",
|
|
32
|
+
fullDateTime12h: "DD, hh:mm a",
|
|
33
|
+
fullDateTime24h: "DD, T",
|
|
34
|
+
fullTime: "t",
|
|
35
|
+
fullTime12h: "hh:mm a",
|
|
36
|
+
fullTime24h: "HH:mm",
|
|
37
|
+
hours12h: "hh",
|
|
38
|
+
hours24h: "HH",
|
|
39
|
+
keyboardDate: "D",
|
|
40
|
+
keyboardDateTime: "D t",
|
|
41
|
+
keyboardDateTime12h: "D hh:mm a",
|
|
42
|
+
keyboardDateTime24h: "D T",
|
|
43
|
+
systemDateTime: "D HH:mm:ss.SSS",
|
|
44
|
+
filePathDateTime: "D_HH-mm-ss-SSS",
|
|
45
|
+
minutes: "mm",
|
|
46
|
+
seconds: "ss",
|
|
47
|
+
month: "LLLL",
|
|
48
|
+
monthAndDate: "MMMM d",
|
|
49
|
+
monthAndYear: "LLLL yyyy",
|
|
50
|
+
monthShort: "MMM",
|
|
51
|
+
weekday: "cccc",
|
|
52
|
+
weekdayShort: "ccc",
|
|
53
|
+
normalDate: "d MMMM",
|
|
54
|
+
normalDateWithWeekday: "EEE, MMM d",
|
|
55
|
+
shortDate: "MMM d",
|
|
56
|
+
year: "yyyy",
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export let locale = "${context.env.parsed.DEFAULT_LOCALE || context.workspaceConfig.locale ? `${context.env.parsed.DEFAULT_LOCALE || context.workspaceConfig.locale}` : "en-US"}";
|
|
60
|
+
|
|
61
|
+
export const type = "luxon";
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Creates a date object using the provided value.
|
|
65
|
+
*
|
|
66
|
+
* @remarks
|
|
67
|
+
* This function creates a date object using the provided value. If the value is \`null\`, it returns \`null\`. If the value is \`undefined\`, it returns the current date. If the value is a string, it parses the string as a date. If the value is a number, it treats it as a timestamp. If the value is a date object, it returns the date object.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* \`\`\`ts
|
|
71
|
+
* import { createDate } from "storm:date";
|
|
72
|
+
*
|
|
73
|
+
* const date = createDate("2023-10-01");
|
|
74
|
+
* console.log(date.format("YYYY-MM-DD")); // Outputs: 2023-10-01
|
|
75
|
+
* \`\`\`
|
|
76
|
+
*
|
|
77
|
+
* @param value - The value to create the date object from.
|
|
78
|
+
* @returns A date object or \`null\` if the value is \`null\`.
|
|
79
|
+
*/
|
|
80
|
+
export function createDate<
|
|
81
|
+
TArg extends unknown = undefined,
|
|
82
|
+
TResultingDate extends unknown = TArg extends null
|
|
83
|
+
? null
|
|
84
|
+
: TArg extends undefined
|
|
85
|
+
? DateTime
|
|
86
|
+
: DateTime | null
|
|
87
|
+
>(value?: TArg): TResultingDate {
|
|
88
|
+
if (typeof value === "undefined") {
|
|
89
|
+
return DateTime.local() as TResultingDate;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (value === null) {
|
|
93
|
+
return null as TResultingDate;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (typeof value === "string") {
|
|
97
|
+
return DateTime.fromJSDate(new Date(value), { locale }) as TResultingDate;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (DateTime.isDateTime(value)) {
|
|
101
|
+
return value as TResultingDate;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (value instanceof Date) {
|
|
105
|
+
return DateTime.fromJSDate(value, { locale }) as TResultingDate;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/* istanbul ignore next */
|
|
109
|
+
return DateTime.local() as TResultingDate;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function toJsDate(value: DateTime) {
|
|
113
|
+
return value.toJSDate();
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export function parseISO(isoString: string) {
|
|
117
|
+
return DateTime.fromISO(isoString);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export function toISO(value: DateTime) {
|
|
121
|
+
return value.toISO({ format: "extended" });
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export function parse(value: string, formatString: string) {
|
|
125
|
+
if (value === "") {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return DateTime.fromFormat(value, formatString, { locale });
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/* istanbul ignore next */
|
|
133
|
+
export function is12HourCycleInCurrentLocale() {
|
|
134
|
+
if (typeof Intl === "undefined" || typeof Intl.DateTimeFormat === "undefined") {
|
|
135
|
+
return true; // Luxon defaults to en-US if Intl not found
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return Boolean(
|
|
139
|
+
new Intl.DateTimeFormat(this.locale, { hour: "numeric" })?.resolvedOptions()?.hour12
|
|
140
|
+
);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export function getFormatHelperText(format: string) {
|
|
144
|
+
// Unfortunately there is no way for luxon to retrieve readable formats from localized format
|
|
145
|
+
return "";
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/* istanbul ignore next */
|
|
149
|
+
export function getCurrentLocaleCode() {
|
|
150
|
+
return this.locale || Settings.defaultLocale;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export function addSeconds(date: DateTime, count: number) {
|
|
154
|
+
return date.plus({ seconds: count });
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
export function addMinutes(date: DateTime, count: number) {
|
|
158
|
+
return date.plus({ minutes: count });
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export function addHours(date: DateTime, count: number) {
|
|
162
|
+
return date.plus({ hours: count });
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export function addDays(date: DateTime, count: number) {
|
|
166
|
+
return date.plus({ days: count });
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export function addWeeks(date: DateTime, count: number) {
|
|
170
|
+
return date.plus({ weeks: count });
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
export function addMonths(date: DateTime, count: number) {
|
|
174
|
+
return date.plus({ months: count });
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export function addYears(date: DateTime, count: number) {
|
|
178
|
+
return date.plus({ years: count });
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
export function isValid(value: any) {
|
|
182
|
+
if (DateTime.isDateTime(value)) {
|
|
183
|
+
return value.isValid;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (value === null) {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return createDate(value)?.isValid ?? false;
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
export function isEqual(value: any, comparing: any) {
|
|
194
|
+
if (value === null && comparing === null) {
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// make sure that null will not be passed to createDate
|
|
199
|
+
if (value === null || comparing === null) {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (!createDate(comparing)) {
|
|
204
|
+
/* istanbul ignore next */
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return createDate(value)?.equals(createDate(comparing) as DateTime) ?? false;
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
export function isSameDay(date: DateTime, comparing: DateTime) {
|
|
212
|
+
return date.hasSame(comparing, "day");
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
export function isSameMonth(date: DateTime, comparing: DateTime) {
|
|
216
|
+
return date.hasSame(comparing, "month");
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
export function isSameYear(date: DateTime, comparing: DateTime) {
|
|
220
|
+
return date.hasSame(comparing, "year");
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
export function isSameHour(date: DateTime, comparing: DateTime) {
|
|
224
|
+
return date.hasSame(comparing, "hour");
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
export function isAfter(value: DateTime, comparing: DateTime) {
|
|
228
|
+
return value > comparing;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
export function isBefore(value: DateTime, comparing: DateTime) {
|
|
232
|
+
return value < comparing;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
export function isBeforeDay(value: DateTime, comparing: DateTime) {
|
|
236
|
+
const diff = value.diff(comparing.startOf("day"), "days").toObject();
|
|
237
|
+
return diff.days! < 0;
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
export function isAfterDay(value: DateTime, comparing: DateTime) {
|
|
241
|
+
const diff = value.diff(comparing.endOf("day"), "days").toObject();
|
|
242
|
+
return diff.days! > 0;
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
export function isBeforeMonth(value: DateTime, comparing: DateTime) {
|
|
246
|
+
const diff = value.diff(comparing.startOf("month"), "months").toObject();
|
|
247
|
+
return diff.months! < 0;
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
export function isAfterMonth(value: DateTime, comparing: DateTime) {
|
|
251
|
+
const diff = value.diff(comparing.startOf("month"), "months").toObject();
|
|
252
|
+
return diff.months! > 0;
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
export function isBeforeYear(value: DateTime, comparing: DateTime) {
|
|
256
|
+
const diff = value.diff(comparing.startOf("year"), "years").toObject();
|
|
257
|
+
return diff.years! < 0;
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
export function isAfterYear(value: DateTime, comparing: DateTime) {
|
|
261
|
+
const diff = value.diff(comparing.endOf("year"), "years").toObject();
|
|
262
|
+
return diff.years! > 0;
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
export function getDiff(value: DateTime, comparing: DateTime | string, unit?: TimeUnit) {
|
|
266
|
+
if (typeof comparing === "string") {
|
|
267
|
+
comparing = DateTime.fromJSDate(new Date(comparing));
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (!comparing.isValid) {
|
|
271
|
+
return 0;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
if (unit) {
|
|
275
|
+
return Math.floor(value.diff(comparing).as(unit));
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return value.diff(comparing).as("millisecond");
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
export function startOfDay(value: DateTime) {
|
|
282
|
+
return value.startOf("day");
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
export function endOfDay(value: DateTime) {
|
|
286
|
+
return value.endOf("day");
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
export function format(date: DateTime, formatKey: keyof DateFormats) {
|
|
290
|
+
return formatByString(date, formats[formatKey]);
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
export function formatByString(date: DateTime, format: string) {
|
|
294
|
+
return date.setLocale(locale).toFormat(format);
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
export function formatNumber(numberToFormat: string) {
|
|
298
|
+
return numberToFormat;
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
export function getHours(value: DateTime) {
|
|
302
|
+
return value.get("hour");
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
export function setHours(value: DateTime, count: number) {
|
|
306
|
+
return value.set({ hour: count });
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
export function getMinutes(value: DateTime) {
|
|
310
|
+
return value.get("minute");
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
export function setMinutes(value: DateTime, count: number) {
|
|
314
|
+
return value.set({ minute: count });
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
export function getSeconds(value: DateTime) {
|
|
318
|
+
return value.get("second");
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
export function setSeconds(value: DateTime, count: number) {
|
|
322
|
+
return value.set({ second: count });
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
export function getWeek(value: DateTime) {
|
|
326
|
+
return value.get("weekNumber");
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
export function getMonth(value: DateTime) {
|
|
330
|
+
// See https://github.com/moment/luxon/blob/master/docs/moment.md#major-functional-differences
|
|
331
|
+
return value.get("month") - 1;
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
export function getDaysInMonth(value: DateTime) {
|
|
335
|
+
return value.daysInMonth;
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
export function setMonth(value: DateTime, count: number) {
|
|
339
|
+
return value.set({ month: count + 1 });
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
export function getYear(value: DateTime) {
|
|
343
|
+
return value.get("year");
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
export function setYear(value: DateTime, year: number) {
|
|
347
|
+
return value.set({ year });
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
export function getDate(value: DateTime) {
|
|
351
|
+
return value.get("day");
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
export function setDate(value: DateTime, day: number) {
|
|
355
|
+
return value.set({ day });
|
|
356
|
+
};
|
|
357
|
+
|
|
358
|
+
export function mergeDateAndTime(date: DateTime, time: DateTime) {
|
|
359
|
+
return date.set({
|
|
360
|
+
second: time.second,
|
|
361
|
+
hour: time.hour,
|
|
362
|
+
minute: time.minute,
|
|
363
|
+
});
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
export function startOfYear(value: DateTime) {
|
|
367
|
+
return value.startOf("year");
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
export function endOfYear(value: DateTime) {
|
|
371
|
+
return value.endOf("year");
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
export function startOfMonth(value: DateTime) {
|
|
375
|
+
return value.startOf("month");
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
export function endOfMonth(value: DateTime) {
|
|
379
|
+
return value.endOf("month");
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
export function startOfWeek(value: DateTime) {
|
|
383
|
+
return value.startOf("week");
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
export function endOfWeek(value: DateTime) {
|
|
387
|
+
return value.endOf("week");
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
export function getNextMonth(value: DateTime) {
|
|
391
|
+
return value.plus({ months: 1 });
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
export function getPreviousMonth(value: DateTime) {
|
|
395
|
+
return value.minus({ months: 1 });
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
export function getMonthArray(date: DateTime) {
|
|
399
|
+
const firstMonth = date.startOf("year");
|
|
400
|
+
const monthArray = [firstMonth];
|
|
401
|
+
|
|
402
|
+
while (monthArray.length < 12) {
|
|
403
|
+
const prevMonth = monthArray[monthArray.length - 1];
|
|
404
|
+
monthArray.push(this.getNextMonth(prevMonth));
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return monthArray;
|
|
408
|
+
};
|
|
409
|
+
|
|
410
|
+
export function getWeekdays() {
|
|
411
|
+
return Info.weekdaysFormat("short", { locale });
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
export function getWeekArray(date: DateTime) {
|
|
415
|
+
const { days } = date
|
|
416
|
+
.endOf("month")
|
|
417
|
+
.endOf("week")
|
|
418
|
+
.diff(date.startOf("month").startOf("week"), "days")
|
|
419
|
+
.toObject();
|
|
420
|
+
|
|
421
|
+
const weeks: DateTime[][] = [];
|
|
422
|
+
new Array<number>(Math.round(days!))
|
|
423
|
+
.fill(0)
|
|
424
|
+
.map((_, i) => i)
|
|
425
|
+
.map((day) => date.startOf("month").startOf("week").plus({ days: day }))
|
|
426
|
+
.forEach((v, i) => {
|
|
427
|
+
if (i === 0 || (i % 7 === 0 && i > 6)) {
|
|
428
|
+
weeks.push([v]);
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
weeks[weeks.length - 1].push(v);
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
return weeks;
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
export function getYearRange(start: DateTime, end: DateTime) {
|
|
439
|
+
const startDate = start.startOf("year");
|
|
440
|
+
const endDate = end.endOf("year");
|
|
441
|
+
|
|
442
|
+
let current = startDate;
|
|
443
|
+
const years: DateTime[] = [];
|
|
444
|
+
|
|
445
|
+
while (current < endDate) {
|
|
446
|
+
years.push(current);
|
|
447
|
+
current = current.plus({ year: 1 });
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
return years;
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
export function getMeridiemText(meridiem: "am" | "pm") {
|
|
454
|
+
return Info.meridiems({ locale }).find(
|
|
455
|
+
(v) => v.toLowerCase() === meridiem.toLowerCase()
|
|
456
|
+
)!;
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
export function isNull(date: DateTime | null) {
|
|
460
|
+
return date === null;
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
export function isWithinRange(date: DateTime, [start, end]: [DateTime, DateTime]) {
|
|
464
|
+
return (
|
|
465
|
+
date.equals(start) ||
|
|
466
|
+
date.equals(end) ||
|
|
467
|
+
(isAfter(date, start) && isBefore(date, end))
|
|
468
|
+
);
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
`;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
//#endregion
|
|
476
|
+
export { luxonModule };
|
|
477
|
+
//# sourceMappingURL=luxon.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"luxon.mjs","names":[],"sources":["../../src/components/luxon.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { getFileHeader } from \"powerlines/lib/utilities/file-header\";\nimport { DatePluginContext } from \"../types/plugin\";\n\n/**\n * Generates the Powerlines configuration file.\n *\n * @param context - The build context containing runtime information.\n * @returns A string representing the configuration file content.\n */\nexport function luxonModule(context: DatePluginContext) {\n return `\n/**\n * The Date module provides utility functions for date manipulation and formatting\n *\n * @module ${context.config.framework}:date\n */\n\n${getFileHeader(context)}\n\nimport { DateTime, Settings, Info } from \"luxon\";\nimport {\n StormDateInterface,\n DateFormats,\n TimeUnit\n} from \"@powerlines/plugin-date/types/runtime\";\n\nexport const formats: DateFormats = {\n dayOfMonth: \"d\",\n fullDate: \"DD\",\n fullDateWithWeekday: \"DDDD\",\n fullDateTime: \"ff\",\n fullDateTime12h: \"DD, hh:mm a\",\n fullDateTime24h: \"DD, T\",\n fullTime: \"t\",\n fullTime12h: \"hh:mm a\",\n fullTime24h: \"HH:mm\",\n hours12h: \"hh\",\n hours24h: \"HH\",\n keyboardDate: \"D\",\n keyboardDateTime: \"D t\",\n keyboardDateTime12h: \"D hh:mm a\",\n keyboardDateTime24h: \"D T\",\n systemDateTime: \"D HH:mm:ss.SSS\",\n filePathDateTime: \"D_HH-mm-ss-SSS\",\n minutes: \"mm\",\n seconds: \"ss\",\n month: \"LLLL\",\n monthAndDate: \"MMMM d\",\n monthAndYear: \"LLLL yyyy\",\n monthShort: \"MMM\",\n weekday: \"cccc\",\n weekdayShort: \"ccc\",\n normalDate: \"d MMMM\",\n normalDateWithWeekday: \"EEE, MMM d\",\n shortDate: \"MMM d\",\n year: \"yyyy\",\n};\n\nexport let locale = \"${\n context.env.parsed.DEFAULT_LOCALE || context.workspaceConfig.locale\n ? `${context.env.parsed.DEFAULT_LOCALE || context.workspaceConfig.locale}`\n : \"en-US\"\n }\";\n\nexport const type = \"luxon\";\n\n/**\n * Creates a date object using the provided value.\n *\n * @remarks\n * This function creates a date object using the provided value. If the value is \\`null\\`, it returns \\`null\\`. If the value is \\`undefined\\`, it returns the current date. If the value is a string, it parses the string as a date. If the value is a number, it treats it as a timestamp. If the value is a date object, it returns the date object.\n *\n * @example\n * \\`\\`\\`ts\n * import { createDate } from \"storm:date\";\n *\n * const date = createDate(\"2023-10-01\");\n * console.log(date.format(\"YYYY-MM-DD\")); // Outputs: 2023-10-01\n * \\`\\`\\`\n *\n * @param value - The value to create the date object from.\n * @returns A date object or \\`null\\` if the value is \\`null\\`.\n */\nexport function createDate<\n TArg extends unknown = undefined,\n TResultingDate extends unknown = TArg extends null\n ? null\n : TArg extends undefined\n ? DateTime\n : DateTime | null\n>(value?: TArg): TResultingDate {\n if (typeof value === \"undefined\") {\n return DateTime.local() as TResultingDate;\n }\n\n if (value === null) {\n return null as TResultingDate;\n }\n\n if (typeof value === \"string\") {\n return DateTime.fromJSDate(new Date(value), { locale }) as TResultingDate;\n }\n\n if (DateTime.isDateTime(value)) {\n return value as TResultingDate;\n }\n\n if (value instanceof Date) {\n return DateTime.fromJSDate(value, { locale }) as TResultingDate;\n }\n\n /* istanbul ignore next */\n return DateTime.local() as TResultingDate;\n}\n\nexport function toJsDate(value: DateTime) {\n return value.toJSDate();\n};\n\nexport function parseISO(isoString: string) {\n return DateTime.fromISO(isoString);\n};\n\nexport function toISO(value: DateTime) {\n return value.toISO({ format: \"extended\" });\n};\n\nexport function parse(value: string, formatString: string) {\n if (value === \"\") {\n return null;\n }\n\n return DateTime.fromFormat(value, formatString, { locale });\n};\n\n/* istanbul ignore next */\nexport function is12HourCycleInCurrentLocale() {\n if (typeof Intl === \"undefined\" || typeof Intl.DateTimeFormat === \"undefined\") {\n return true; // Luxon defaults to en-US if Intl not found\n }\n\n return Boolean(\n new Intl.DateTimeFormat(this.locale, { hour: \"numeric\" })?.resolvedOptions()?.hour12\n );\n};\n\nexport function getFormatHelperText(format: string) {\n // Unfortunately there is no way for luxon to retrieve readable formats from localized format\n return \"\";\n};\n\n/* istanbul ignore next */\nexport function getCurrentLocaleCode() {\n return this.locale || Settings.defaultLocale;\n};\n\nexport function addSeconds(date: DateTime, count: number) {\n return date.plus({ seconds: count });\n};\n\nexport function addMinutes(date: DateTime, count: number) {\n return date.plus({ minutes: count });\n};\n\nexport function addHours(date: DateTime, count: number) {\n return date.plus({ hours: count });\n};\n\nexport function addDays(date: DateTime, count: number) {\n return date.plus({ days: count });\n};\n\nexport function addWeeks(date: DateTime, count: number) {\n return date.plus({ weeks: count });\n};\n\nexport function addMonths(date: DateTime, count: number) {\n return date.plus({ months: count });\n};\n\nexport function addYears(date: DateTime, count: number) {\n return date.plus({ years: count });\n};\n\nexport function isValid(value: any) {\n if (DateTime.isDateTime(value)) {\n return value.isValid;\n }\n\n if (value === null) {\n return false;\n }\n\n return createDate(value)?.isValid ?? false;\n};\n\nexport function isEqual(value: any, comparing: any) {\n if (value === null && comparing === null) {\n return true;\n }\n\n // make sure that null will not be passed to createDate\n if (value === null || comparing === null) {\n return false;\n }\n\n if (!createDate(comparing)) {\n /* istanbul ignore next */\n return false;\n }\n\n return createDate(value)?.equals(createDate(comparing) as DateTime) ?? false;\n};\n\nexport function isSameDay(date: DateTime, comparing: DateTime) {\n return date.hasSame(comparing, \"day\");\n};\n\nexport function isSameMonth(date: DateTime, comparing: DateTime) {\n return date.hasSame(comparing, \"month\");\n};\n\nexport function isSameYear(date: DateTime, comparing: DateTime) {\n return date.hasSame(comparing, \"year\");\n};\n\nexport function isSameHour(date: DateTime, comparing: DateTime) {\n return date.hasSame(comparing, \"hour\");\n};\n\nexport function isAfter(value: DateTime, comparing: DateTime) {\n return value > comparing;\n};\n\nexport function isBefore(value: DateTime, comparing: DateTime) {\n return value < comparing;\n};\n\nexport function isBeforeDay(value: DateTime, comparing: DateTime) {\n const diff = value.diff(comparing.startOf(\"day\"), \"days\").toObject();\n return diff.days! < 0;\n};\n\nexport function isAfterDay(value: DateTime, comparing: DateTime) {\n const diff = value.diff(comparing.endOf(\"day\"), \"days\").toObject();\n return diff.days! > 0;\n};\n\nexport function isBeforeMonth(value: DateTime, comparing: DateTime) {\n const diff = value.diff(comparing.startOf(\"month\"), \"months\").toObject();\n return diff.months! < 0;\n};\n\nexport function isAfterMonth(value: DateTime, comparing: DateTime) {\n const diff = value.diff(comparing.startOf(\"month\"), \"months\").toObject();\n return diff.months! > 0;\n};\n\nexport function isBeforeYear(value: DateTime, comparing: DateTime) {\n const diff = value.diff(comparing.startOf(\"year\"), \"years\").toObject();\n return diff.years! < 0;\n};\n\nexport function isAfterYear(value: DateTime, comparing: DateTime) {\n const diff = value.diff(comparing.endOf(\"year\"), \"years\").toObject();\n return diff.years! > 0;\n};\n\nexport function getDiff(value: DateTime, comparing: DateTime | string, unit?: TimeUnit) {\n if (typeof comparing === \"string\") {\n comparing = DateTime.fromJSDate(new Date(comparing));\n }\n\n if (!comparing.isValid) {\n return 0;\n }\n\n if (unit) {\n return Math.floor(value.diff(comparing).as(unit));\n }\n\n return value.diff(comparing).as(\"millisecond\");\n};\n\nexport function startOfDay(value: DateTime) {\n return value.startOf(\"day\");\n};\n\nexport function endOfDay(value: DateTime) {\n return value.endOf(\"day\");\n};\n\nexport function format(date: DateTime, formatKey: keyof DateFormats) {\n return formatByString(date, formats[formatKey]);\n};\n\nexport function formatByString(date: DateTime, format: string) {\n return date.setLocale(locale).toFormat(format);\n};\n\nexport function formatNumber(numberToFormat: string) {\n return numberToFormat;\n};\n\nexport function getHours(value: DateTime) {\n return value.get(\"hour\");\n};\n\nexport function setHours(value: DateTime, count: number) {\n return value.set({ hour: count });\n};\n\nexport function getMinutes(value: DateTime) {\n return value.get(\"minute\");\n};\n\nexport function setMinutes(value: DateTime, count: number) {\n return value.set({ minute: count });\n};\n\nexport function getSeconds(value: DateTime) {\n return value.get(\"second\");\n};\n\nexport function setSeconds(value: DateTime, count: number) {\n return value.set({ second: count });\n};\n\nexport function getWeek(value: DateTime) {\n return value.get(\"weekNumber\");\n};\n\nexport function getMonth(value: DateTime) {\n // See https://github.com/moment/luxon/blob/master/docs/moment.md#major-functional-differences\n return value.get(\"month\") - 1;\n};\n\nexport function getDaysInMonth(value: DateTime) {\n return value.daysInMonth;\n};\n\nexport function setMonth(value: DateTime, count: number) {\n return value.set({ month: count + 1 });\n};\n\nexport function getYear(value: DateTime) {\n return value.get(\"year\");\n};\n\nexport function setYear(value: DateTime, year: number) {\n return value.set({ year });\n};\n\nexport function getDate(value: DateTime) {\n return value.get(\"day\");\n};\n\nexport function setDate(value: DateTime, day: number) {\n return value.set({ day });\n};\n\nexport function mergeDateAndTime(date: DateTime, time: DateTime) {\n return date.set({\n second: time.second,\n hour: time.hour,\n minute: time.minute,\n });\n};\n\nexport function startOfYear(value: DateTime) {\n return value.startOf(\"year\");\n};\n\nexport function endOfYear(value: DateTime) {\n return value.endOf(\"year\");\n};\n\nexport function startOfMonth(value: DateTime) {\n return value.startOf(\"month\");\n};\n\nexport function endOfMonth(value: DateTime) {\n return value.endOf(\"month\");\n};\n\nexport function startOfWeek(value: DateTime) {\n return value.startOf(\"week\");\n};\n\nexport function endOfWeek(value: DateTime) {\n return value.endOf(\"week\");\n};\n\nexport function getNextMonth(value: DateTime) {\n return value.plus({ months: 1 });\n};\n\nexport function getPreviousMonth(value: DateTime) {\n return value.minus({ months: 1 });\n};\n\nexport function getMonthArray(date: DateTime) {\n const firstMonth = date.startOf(\"year\");\n const monthArray = [firstMonth];\n\n while (monthArray.length < 12) {\n const prevMonth = monthArray[monthArray.length - 1];\n monthArray.push(this.getNextMonth(prevMonth));\n }\n\n return monthArray;\n};\n\nexport function getWeekdays() {\n return Info.weekdaysFormat(\"short\", { locale });\n};\n\nexport function getWeekArray(date: DateTime) {\n const { days } = date\n .endOf(\"month\")\n .endOf(\"week\")\n .diff(date.startOf(\"month\").startOf(\"week\"), \"days\")\n .toObject();\n\n const weeks: DateTime[][] = [];\n new Array<number>(Math.round(days!))\n .fill(0)\n .map((_, i) => i)\n .map((day) => date.startOf(\"month\").startOf(\"week\").plus({ days: day }))\n .forEach((v, i) => {\n if (i === 0 || (i % 7 === 0 && i > 6)) {\n weeks.push([v]);\n return;\n }\n\n weeks[weeks.length - 1].push(v);\n });\n\n return weeks;\n};\n\nexport function getYearRange(start: DateTime, end: DateTime) {\n const startDate = start.startOf(\"year\");\n const endDate = end.endOf(\"year\");\n\n let current = startDate;\n const years: DateTime[] = [];\n\n while (current < endDate) {\n years.push(current);\n current = current.plus({ year: 1 });\n }\n\n return years;\n};\n\nexport function getMeridiemText(meridiem: \"am\" | \"pm\") {\n return Info.meridiems({ locale }).find(\n (v) => v.toLowerCase() === meridiem.toLowerCase()\n )!;\n};\n\nexport function isNull(date: DateTime | null) {\n return date === null;\n};\n\nexport function isWithinRange(date: DateTime, [start, end]: [DateTime, DateTime]) {\n return (\n date.equals(start) ||\n date.equals(end) ||\n (isAfter(date, start) && isBefore(date, end))\n );\n};\n\n\n`;\n}\n"],"mappings":";;;;;;;;;AA2BA,SAAgB,YAAY,SAA4B;AACtD,QAAO;;;;aAII,QAAQ,OAAO,UAAU;;;EAGpC,cAAc,QAAQ,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uBA0CrB,QAAQ,IAAI,OAAO,kBAAkB,QAAQ,gBAAgB,SACzD,GAAG,QAAQ,IAAI,OAAO,kBAAkB,QAAQ,gBAAgB,WAChE,QACL"}
|