@powerlines/plugin-date 0.12.279 → 0.12.280
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/index-CA4VvAY_.d.mts +1 -0
- package/dist/index-CmCelmEL.d.cts +1 -0
- package/dist/index.cjs +2007 -18
- package/dist/index.d.cts +42 -9
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +42 -9
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1972 -7
- package/dist/index.mjs.map +1 -0
- package/dist/plugin-CWe-29nA.d.mts +44 -0
- package/dist/plugin-CWe-29nA.d.mts.map +1 -0
- package/dist/plugin-D82ijYIk.d.cts +44 -0
- package/dist/plugin-D82ijYIk.d.cts.map +1 -0
- package/dist/plugin-DHXHjv16.cjs +0 -0
- package/dist/plugin-ufqFO5xS.mjs +1 -0
- package/dist/runtime-B7WoCmC4.mjs +1 -0
- package/dist/runtime-B8LVkjWB.d.cts +712 -0
- package/dist/runtime-B8LVkjWB.d.cts.map +1 -0
- package/dist/runtime-CiYq8JJ6.d.mts +712 -0
- package/dist/runtime-CiYq8JJ6.d.mts.map +1 -0
- package/dist/runtime-D4zTdSwv.cjs +0 -0
- package/dist/types/index.cjs +3 -0
- package/dist/types/index.d.cts +3 -2
- package/dist/types/index.d.mts +3 -2
- package/dist/types/index.mjs +4 -0
- package/dist/types/plugin.cjs +1 -0
- package/dist/types/plugin.d.cts +1 -42
- package/dist/types/plugin.d.mts +1 -42
- package/dist/types/plugin.mjs +2 -0
- package/dist/types/runtime.cjs +1 -0
- package/dist/types/runtime.d.cts +1 -710
- package/dist/types/runtime.d.mts +1 -710
- package/dist/types/runtime.mjs +2 -0
- package/dist/types-BReKsBV6.mjs +1 -0
- package/dist/types-CBTc19th.cjs +0 -0
- package/package.json +5 -5
- package/dist/_virtual/rolldown_runtime.cjs +0 -29
- package/dist/components/date-fns.cjs +0 -528
- package/dist/components/date-fns.d.cts +0 -13
- package/dist/components/date-fns.d.mts +0 -13
- package/dist/components/date-fns.mjs +0 -527
- package/dist/components/dayjs.cjs +0 -473
- package/dist/components/dayjs.d.cts +0 -13
- package/dist/components/dayjs.d.mts +0 -13
- package/dist/components/dayjs.mjs +0 -472
- package/dist/components/index.cjs +0 -4
- package/dist/components/index.d.cts +0 -4
- package/dist/components/index.d.mts +0 -4
- package/dist/components/index.mjs +0 -6
- package/dist/components/luxon.cjs +0 -477
- package/dist/components/luxon.d.cts +0 -13
- package/dist/components/luxon.d.mts +0 -13
- package/dist/components/luxon.mjs +0 -476
- package/dist/components/moment.cjs +0 -463
- package/dist/components/moment.d.cts +0 -13
- package/dist/components/moment.d.mts +0 -13
- package/dist/components/moment.mjs +0 -462
package/dist/index.cjs
CHANGED
|
@@ -1,15 +1,2004 @@
|
|
|
1
1
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
12
|
+
key = keys[i];
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
14
|
+
__defProp(to, key, {
|
|
15
|
+
get: ((k) => from[k]).bind(null, key),
|
|
16
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
24
|
+
value: mod,
|
|
25
|
+
enumerable: true
|
|
26
|
+
}) : target, mod));
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
require('./plugin-DHXHjv16.cjs');
|
|
30
|
+
require('./runtime-D4zTdSwv.cjs');
|
|
31
|
+
require('./types-CBTc19th.cjs');
|
|
8
32
|
let __powerlines_plugin_env = require("@powerlines/plugin-env");
|
|
9
|
-
__powerlines_plugin_env =
|
|
10
|
-
let
|
|
11
|
-
|
|
33
|
+
__powerlines_plugin_env = __toESM(__powerlines_plugin_env);
|
|
34
|
+
let powerlines_lib_utilities_file_header = require("powerlines/lib/utilities/file-header");
|
|
35
|
+
|
|
36
|
+
//#region ../../node_modules/.pnpm/defu@6.1.4/node_modules/defu/dist/defu.mjs
|
|
37
|
+
function isPlainObject(value) {
|
|
38
|
+
if (value === null || typeof value !== "object") return false;
|
|
39
|
+
const prototype = Object.getPrototypeOf(value);
|
|
40
|
+
if (prototype !== null && prototype !== Object.prototype && Object.getPrototypeOf(prototype) !== null) return false;
|
|
41
|
+
if (Symbol.iterator in value) return false;
|
|
42
|
+
if (Symbol.toStringTag in value) return Object.prototype.toString.call(value) === "[object Module]";
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
function _defu(baseObject, defaults, namespace = ".", merger) {
|
|
46
|
+
if (!isPlainObject(defaults)) return _defu(baseObject, {}, namespace, merger);
|
|
47
|
+
const object = Object.assign({}, defaults);
|
|
48
|
+
for (const key in baseObject) {
|
|
49
|
+
if (key === "__proto__" || key === "constructor") continue;
|
|
50
|
+
const value = baseObject[key];
|
|
51
|
+
if (value === null || value === void 0) continue;
|
|
52
|
+
if (merger && merger(object, key, value, namespace)) continue;
|
|
53
|
+
if (Array.isArray(value) && Array.isArray(object[key])) object[key] = [...value, ...object[key]];
|
|
54
|
+
else if (isPlainObject(value) && isPlainObject(object[key])) object[key] = _defu(value, object[key], (namespace ? `${namespace}.` : "") + key.toString(), merger);
|
|
55
|
+
else object[key] = value;
|
|
56
|
+
}
|
|
57
|
+
return object;
|
|
58
|
+
}
|
|
59
|
+
function createDefu(merger) {
|
|
60
|
+
return (...arguments_) => arguments_.reduce((p, c) => _defu(p, c, "", merger), {});
|
|
61
|
+
}
|
|
62
|
+
const defu = createDefu();
|
|
63
|
+
const defuFn = createDefu((object, key, currentValue) => {
|
|
64
|
+
if (object[key] !== void 0 && typeof currentValue === "function") {
|
|
65
|
+
object[key] = currentValue(object[key]);
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
const defuArrayFn = createDefu((object, key, currentValue) => {
|
|
70
|
+
if (Array.isArray(object[key]) && typeof currentValue === "function") {
|
|
71
|
+
object[key] = currentValue(object[key]);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region src/components/date-fns.ts
|
|
78
|
+
/**
|
|
79
|
+
* Generates the Powerlines configuration file.
|
|
80
|
+
*
|
|
81
|
+
* @param context - The build context containing runtime information.
|
|
82
|
+
* @returns A string representing the configuration file content.
|
|
83
|
+
*/
|
|
84
|
+
function dateFnsModule(context) {
|
|
85
|
+
return `
|
|
86
|
+
/**
|
|
87
|
+
* The Date module provides utility functions for date manipulation and formatting
|
|
88
|
+
*
|
|
89
|
+
* @module ${context.config.framework}:date
|
|
90
|
+
*/
|
|
91
|
+
|
|
92
|
+
${(0, powerlines_lib_utilities_file_header.getFileHeader)(context)}
|
|
93
|
+
|
|
94
|
+
import { DateFormats, TimeUnit } from "@powerlines/plugin-date/types/runtime";
|
|
95
|
+
import { addDays as dateFnsAddDays } from "date-fns/addDays";
|
|
96
|
+
import { addHours as dateFnsAddHours } from "date-fns/addHours";
|
|
97
|
+
import { addMinutes as dateFnsAddMinutes } from "date-fns/addMinutes";
|
|
98
|
+
import { addMonths as dateFnsAddMonths } from "date-fns/addMonths";
|
|
99
|
+
import { addSeconds as dateFnsAddSeconds } from "date-fns/addSeconds";
|
|
100
|
+
import { addWeeks as dateFnsAddWeeks } from "date-fns/addWeeks";
|
|
101
|
+
import { addYears as dateFnsAddYears } from "date-fns/addYears";
|
|
102
|
+
import { differenceInDays } from "date-fns/differenceInDays";
|
|
103
|
+
import { differenceInHours } from "date-fns/differenceInHours";
|
|
104
|
+
import { differenceInMilliseconds } from "date-fns/differenceInMilliseconds";
|
|
105
|
+
import { differenceInMinutes } from "date-fns/differenceInMinutes";
|
|
106
|
+
import { differenceInMonths } from "date-fns/differenceInMonths";
|
|
107
|
+
import { differenceInQuarters } from "date-fns/differenceInQuarters";
|
|
108
|
+
import { differenceInSeconds } from "date-fns/differenceInSeconds";
|
|
109
|
+
import { differenceInWeeks } from "date-fns/differenceInWeeks";
|
|
110
|
+
import { differenceInYears } from "date-fns/differenceInYears";
|
|
111
|
+
import { eachDayOfInterval } from "date-fns/eachDayOfInterval";
|
|
112
|
+
import { endOfDay as dateFnsEndOfDay } from "date-fns/endOfDay";
|
|
113
|
+
import { endOfMonth as dateFnsEndOfMonth } from "date-fns/endOfMonth";
|
|
114
|
+
import { endOfWeek as dateFnsEndOfWeek } from "date-fns/endOfWeek";
|
|
115
|
+
import { endOfYear as dateFnsEndOfYear } from "date-fns/endOfYear";
|
|
116
|
+
import { format as dateFnsFormat, longFormatters } from "date-fns/format";
|
|
117
|
+
import { formatISO } from "date-fns/formatISO";
|
|
118
|
+
import { getDate as dateFnsGetDate } from "date-fns/getDate";
|
|
119
|
+
import { getDay as dateFnsGetDay } from "date-fns/getDay";
|
|
120
|
+
import { getDaysInMonth as dateFnsGetDaysInMonth } from "date-fns/getDaysInMonth";
|
|
121
|
+
import { getHours as dateFnsGetHours } from "date-fns/getHours";
|
|
122
|
+
import { getMinutes as dateFnsGetMinutes } from "date-fns/getMinutes";
|
|
123
|
+
import { getMonth as dateFnsGetMonth } from "date-fns/getMonth";
|
|
124
|
+
import { getSeconds as dateFnsGetSeconds } from "date-fns/getSeconds";
|
|
125
|
+
import { getWeek as dateFnsGetWeek } from "date-fns/getWeek";
|
|
126
|
+
import { getYear as dateFnsGetYear } from "date-fns/getYear";
|
|
127
|
+
import { isAfter as dateFnsIsAfter } from "date-fns/isAfter";
|
|
128
|
+
import { isBefore as dateFnsIsBefore } from "date-fns/isBefore";
|
|
129
|
+
import { isEqual as dateFnsIsEqual } from "date-fns/isEqual";
|
|
130
|
+
import { isSameDay as dateFnsIsSameDay } from "date-fns/isSameDay";
|
|
131
|
+
import { isSameHour as dateFnsIsSameHour } from "date-fns/isSameHour";
|
|
132
|
+
import { isSameMonth as dateFnsIsSameMonth } from "date-fns/isSameMonth";
|
|
133
|
+
import { isSameYear as dateFnsIsSameYear } from "date-fns/isSameYear";
|
|
134
|
+
import { isValid as dateFnsIsValid } from "date-fns/isValid";
|
|
135
|
+
import { isWithinInterval } from "date-fns/isWithinInterval";
|
|
136
|
+
import { Locale } from "date-fns/locale";
|
|
137
|
+
import { ${context.env.parsed.DEFAULT_LOCALE?.replaceAll("-", "") || "enUS"} as defaultLocale } from "date-fns/locale/${context.env.parsed.DEFAULT_LOCALE || "en-US"}";
|
|
138
|
+
import { parse as dateFnsParse } from "date-fns/parse";
|
|
139
|
+
import { parseISO as dateFnsParseISO } from "date-fns/parseISO";
|
|
140
|
+
import { setDate as dateFnsSetDate } from "date-fns/setDate";
|
|
141
|
+
import { setHours as dateFnsSetHours } from "date-fns/setHours";
|
|
142
|
+
import { setMinutes as dateFnsSetMinutes } from "date-fns/setMinutes";
|
|
143
|
+
import { setMonth as dateFnsSetMonth } from "date-fns/setMonth";
|
|
144
|
+
import { setSeconds as dateFnsSetSeconds } from "date-fns/setSeconds";
|
|
145
|
+
import { setYear as dateFnsSetYear } from "date-fns/setYear";
|
|
146
|
+
import { startOfDay as dateFnsStartOfDay } from "date-fns/startOfDay";
|
|
147
|
+
import { startOfMonth as dateFnsStartOfMonth } from "date-fns/startOfMonth";
|
|
148
|
+
import { startOfWeek as dateFnsStartOfWeek } from "date-fns/startOfWeek";
|
|
149
|
+
import { startOfYear as dateFnsStartOfYear } from "date-fns/startOfYear";
|
|
150
|
+
|
|
151
|
+
export const formats: DateFormats = {
|
|
152
|
+
dayOfMonth: "d",
|
|
153
|
+
fullDate: "PP",
|
|
154
|
+
fullDateWithWeekday: "PPPP",
|
|
155
|
+
fullDateTime: "PP p",
|
|
156
|
+
fullDateTime12h: "PP hh:mm aa",
|
|
157
|
+
fullDateTime24h: "PP HH:mm",
|
|
158
|
+
fullTime: "p",
|
|
159
|
+
fullTime12h: "hh:mm aa",
|
|
160
|
+
fullTime24h: "HH:mm",
|
|
161
|
+
hours12h: "hh",
|
|
162
|
+
hours24h: "HH",
|
|
163
|
+
keyboardDate: "P",
|
|
164
|
+
keyboardDateTime: "P p",
|
|
165
|
+
keyboardDateTime12h: "P hh:mm aa",
|
|
166
|
+
keyboardDateTime24h: "P HH:mm",
|
|
167
|
+
systemDateTime: "P HH:mm:ss.SSS",
|
|
168
|
+
filePathDateTime: "P_HH-mm-ss-SSS",
|
|
169
|
+
minutes: "mm",
|
|
170
|
+
month: "LLLL",
|
|
171
|
+
monthAndDate: "MMMM d",
|
|
172
|
+
monthAndYear: "LLLL yyyy",
|
|
173
|
+
monthShort: "MMM",
|
|
174
|
+
weekday: "EEEE",
|
|
175
|
+
weekdayShort: "EEE",
|
|
176
|
+
normalDate: "d MMMM",
|
|
177
|
+
normalDateWithWeekday: "EEE, MMM d",
|
|
178
|
+
seconds: "ss",
|
|
179
|
+
shortDate: "MMM d",
|
|
180
|
+
year: "yyyy"
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export const locale: Locale = defaultLocale;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Creates a date object using the provided value.
|
|
187
|
+
*
|
|
188
|
+
* @remarks
|
|
189
|
+
* 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.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* \`\`\`ts
|
|
193
|
+
* import { createDate } from "storm:date";
|
|
194
|
+
*
|
|
195
|
+
* const date = createDate("2023-10-01");
|
|
196
|
+
* console.log(date.format("YYYY-MM-DD")); // Outputs: 2023-10-01
|
|
197
|
+
* \`\`\`
|
|
198
|
+
*
|
|
199
|
+
* @param value - The value to create the date object from.
|
|
200
|
+
* @returns A date object or \`null\` if the value is \`null\`.
|
|
201
|
+
*/
|
|
202
|
+
export function createDate<
|
|
203
|
+
TArg = undefined,
|
|
204
|
+
TResultingDate = TArg extends null
|
|
205
|
+
? null
|
|
206
|
+
: TArg extends undefined
|
|
207
|
+
? Date
|
|
208
|
+
: Date | null
|
|
209
|
+
>(value?: TArg): TResultingDate {
|
|
210
|
+
if (typeof value === "undefined") {
|
|
211
|
+
return new Date() as TResultingDate;
|
|
212
|
+
}
|
|
213
|
+
if (value === null) {
|
|
214
|
+
return null as TResultingDate;
|
|
215
|
+
}
|
|
216
|
+
return new Date(value as string | number) as TResultingDate;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export const type = "date-fns";
|
|
220
|
+
|
|
221
|
+
// Note: date-fns input types are more lenient than this adapter, so we need to expose our more
|
|
222
|
+
// strict signature and delegate to the more lenient signature. Otherwise, we have downstream type errors upon usage.
|
|
223
|
+
export function is12HourCycleInCurrentLocale(): boolean {
|
|
224
|
+
if (locale) {
|
|
225
|
+
return /a/.test(locale.formatLong?.time({}));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// By default date-fns is using en-US locale with am/pm enabled
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function getFormatHelperText(format: string) {
|
|
233
|
+
// @see https://github.com/date-fns/date-fns/blob/master/src/format/index.js#L31
|
|
234
|
+
const longFormatRegexp = /P+p+|P+|p+|''|'(?:''|[^'])+(?:'|$)|./g;
|
|
235
|
+
const _locale = locale || defaultLocale;
|
|
236
|
+
|
|
237
|
+
return (
|
|
238
|
+
format
|
|
239
|
+
.match(longFormatRegexp)
|
|
240
|
+
?.map(token => {
|
|
241
|
+
const firstCharacter = token[0];
|
|
242
|
+
if (firstCharacter === "p" || firstCharacter === "P") {
|
|
243
|
+
const longFormatter = longFormatters[firstCharacter]!;
|
|
244
|
+
|
|
245
|
+
return longFormatter(token, _locale.formatLong);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return token;
|
|
249
|
+
})
|
|
250
|
+
.join("")
|
|
251
|
+
.replace(/aaa|aa|a/g, "(a|p)m")
|
|
252
|
+
.toLocaleLowerCase() ?? format
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function parseISO(isoString: string) {
|
|
257
|
+
return dateFnsParseISO(isoString);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export function toISO(value: Date) {
|
|
261
|
+
return formatISO(value, {
|
|
262
|
+
format: "extended"
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function getCurrentLocaleCode() {
|
|
267
|
+
return locale?.code || $storm.env.DEFAULT_LOCALE;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function addSeconds(value: Date, count: number) {
|
|
271
|
+
return dateFnsAddSeconds(value, count);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function addMinutes(value: Date, count: number) {
|
|
275
|
+
return dateFnsAddMinutes(value, count);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export function addHours(value: Date, count: number) {
|
|
279
|
+
return dateFnsAddHours(value, count);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export function addDays(value: Date, count: number) {
|
|
283
|
+
return dateFnsAddDays(value, count);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export function addWeeks(value: Date, count: number) {
|
|
287
|
+
return dateFnsAddWeeks(value, count);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
export function addMonths(value: Date, count: number) {
|
|
291
|
+
return dateFnsAddMonths(value, count);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function addYears(value: Date, count: number) {
|
|
295
|
+
return dateFnsAddYears(value, count);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function isValid(value: any) {
|
|
299
|
+
return dateFnsIsValid(createDate(value));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export function getDiff(
|
|
303
|
+
value: Date,
|
|
304
|
+
comparing: Date | string,
|
|
305
|
+
unit?: TimeUnit
|
|
306
|
+
) {
|
|
307
|
+
// we output 0 if the compare date is string and parsing is not valid
|
|
308
|
+
const dateToCompare = createDate(comparing) ?? value;
|
|
309
|
+
if (!isValid(dateToCompare)) {
|
|
310
|
+
return 0;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
switch (unit) {
|
|
314
|
+
case "years":
|
|
315
|
+
return differenceInYears(value, dateToCompare);
|
|
316
|
+
case "quarters":
|
|
317
|
+
return differenceInQuarters(value, dateToCompare);
|
|
318
|
+
case "months":
|
|
319
|
+
return differenceInMonths(value, dateToCompare);
|
|
320
|
+
case "weeks":
|
|
321
|
+
return differenceInWeeks(value, dateToCompare);
|
|
322
|
+
case "days":
|
|
323
|
+
return differenceInDays(value, dateToCompare);
|
|
324
|
+
case "hours":
|
|
325
|
+
return differenceInHours(value, dateToCompare);
|
|
326
|
+
case "minutes":
|
|
327
|
+
return differenceInMinutes(value, dateToCompare);
|
|
328
|
+
case "seconds":
|
|
329
|
+
return differenceInSeconds(value, dateToCompare);
|
|
330
|
+
case "milliseconds":
|
|
331
|
+
case undefined:
|
|
332
|
+
default:
|
|
333
|
+
return differenceInMilliseconds(value, dateToCompare);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
export function isAfter(value: Date, comparing: Date) {
|
|
338
|
+
return dateFnsIsAfter(value, comparing);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
export function isBefore(value: Date, comparing: Date) {
|
|
342
|
+
return dateFnsIsBefore(value, comparing);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export function startOfDay(value: Date) {
|
|
346
|
+
return dateFnsStartOfDay(value);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export function endOfDay(value: Date) {
|
|
350
|
+
return dateFnsEndOfDay(value);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export function getHours(value: Date) {
|
|
354
|
+
return dateFnsGetHours(value);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
export function setHours(value: Date, count: number) {
|
|
358
|
+
return dateFnsSetHours(value, count);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export function setMinutes(value: Date, count: number) {
|
|
362
|
+
return dateFnsSetMinutes(value, count);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export function getSeconds(value: Date) {
|
|
366
|
+
return dateFnsGetSeconds(value);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export function setSeconds(value: Date, count: number) {
|
|
370
|
+
return dateFnsSetSeconds(value, count);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export function isSameDay(value: Date, comparing: Date) {
|
|
374
|
+
return dateFnsIsSameDay(value, comparing);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export function isSameMonth(value: Date, comparing: Date) {
|
|
378
|
+
return dateFnsIsSameMonth(value, comparing);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
export function isSameYear(value: Date, comparing: Date) {
|
|
382
|
+
return dateFnsIsSameYear(value, comparing);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export function isSameHour(value: Date, comparing: Date) {
|
|
386
|
+
return dateFnsIsSameHour(value, comparing);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export function startOfYear(value: Date) {
|
|
390
|
+
return dateFnsStartOfYear(value);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export function endOfYear(value: Date) {
|
|
394
|
+
return dateFnsEndOfYear(value);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export function startOfMonth(value: Date) {
|
|
398
|
+
return dateFnsStartOfMonth(value);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export function endOfMonth(value: Date) {
|
|
402
|
+
return dateFnsEndOfMonth(value);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
export function startOfWeek(value: Date) {
|
|
406
|
+
return dateFnsStartOfWeek(value, {
|
|
407
|
+
locale
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export function endOfWeek(value: Date) {
|
|
412
|
+
return dateFnsEndOfWeek(value, {
|
|
413
|
+
locale
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
export function getYear(value: Date) {
|
|
418
|
+
return dateFnsGetYear(value);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
export function setYear(value: Date, count: number) {
|
|
422
|
+
return dateFnsSetYear(value, count);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export function toJsDate(value: Date) {
|
|
426
|
+
return value;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
export function parse(value: string, formatString: string) {
|
|
430
|
+
if (value === "") {
|
|
431
|
+
return null;
|
|
432
|
+
}
|
|
433
|
+
return dateFnsParse(value, formatString, new Date(), {
|
|
434
|
+
locale
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export function format(date: Date, formatKey: keyof DateFormats) {
|
|
439
|
+
return formatByString(date, formats[formatKey]);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
export function formatByString(date: Date, formatString: string) {
|
|
443
|
+
return dateFnsFormat(date, formatString, {
|
|
444
|
+
locale
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
export function isEqual(date: any, comparing: any) {
|
|
449
|
+
if (date === null && comparing === null) {
|
|
450
|
+
return true;
|
|
451
|
+
}
|
|
452
|
+
return dateFnsIsEqual(date, comparing);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
export function isNull(date: Date) {
|
|
456
|
+
return date === null;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
export function isAfterDay(date: Date, value: Date) {
|
|
460
|
+
return isAfter(date, endOfDay(value));
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
export function isBeforeDay(date: Date, value: Date) {
|
|
464
|
+
return isBefore(date, startOfDay(value));
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export function isBeforeYear(date: Date, value: Date) {
|
|
468
|
+
return isBefore(date, startOfYear(value));
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
export function isBeforeMonth(value: Date, comparing: Date): boolean {
|
|
472
|
+
return isBefore(value, startOfMonth(comparing));
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
export function isAfterMonth(value: Date, comparing: Date): boolean {
|
|
476
|
+
return isAfter(value, startOfMonth(comparing));
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
export function isAfterYear(date: Date, value: Date) {
|
|
480
|
+
return isAfter(date, endOfYear(value));
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
export function isWithinRange(date: Date, [start, end]: [Date, Date]) {
|
|
484
|
+
return isWithinInterval(date, {
|
|
485
|
+
start,
|
|
486
|
+
end
|
|
487
|
+
});
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
export function formatNumber(numberToFormat: string) {
|
|
491
|
+
return numberToFormat;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
export function getMinutes(date: Date) {
|
|
495
|
+
return dateFnsGetMinutes(date);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
export function getDate(date: Date) {
|
|
499
|
+
return dateFnsGetDate(date);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
export function setDate(date: Date, count: number) {
|
|
503
|
+
return dateFnsSetDate(date, count);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
export function getWeek(date: Date) {
|
|
507
|
+
return dateFnsGetWeek(date);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
export function getMonth(date: Date) {
|
|
511
|
+
return dateFnsGetMonth(date);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
export function getDaysInMonth(date: Date) {
|
|
515
|
+
return dateFnsGetDaysInMonth(date);
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
export function setMonth(date: Date, count: number) {
|
|
519
|
+
return dateFnsSetMonth(date, count);
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
export function getMeridiemText(meridiem: "am" | "pm") {
|
|
523
|
+
return meridiem === "am" ? "AM" : "PM";
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
export function getNextMonth(date: Date) {
|
|
527
|
+
return addMonths(date, 1);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
export function getPreviousMonth(date: Date) {
|
|
531
|
+
return addMonths(date, -1);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
export function getMonthArray(date: Date) {
|
|
535
|
+
const firstMonth = startOfYear(date);
|
|
536
|
+
const monthArray = [firstMonth];
|
|
537
|
+
|
|
538
|
+
while (monthArray.length < 12) {
|
|
539
|
+
const prevMonth = monthArray[monthArray.length - 1]!;
|
|
540
|
+
monthArray.push(getNextMonth(prevMonth));
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
return monthArray;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
export function mergeDateAndTime(date: Date, time: Date) {
|
|
547
|
+
return setSeconds(
|
|
548
|
+
setMinutes(setHours(date, getHours(time)), getMinutes(time)),
|
|
549
|
+
getSeconds(time)
|
|
550
|
+
);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
export function getWeekdays() {
|
|
554
|
+
const now = new Date();
|
|
555
|
+
|
|
556
|
+
return eachDayOfInterval({
|
|
557
|
+
start: startOfWeek(now),
|
|
558
|
+
end: endOfWeek(now)
|
|
559
|
+
}).map(day => formatByString(day, "EEEEEE"));
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
export function getWeekArray(date: Date) {
|
|
563
|
+
const start = startOfWeek(startOfMonth(date));
|
|
564
|
+
const end = endOfWeek(endOfMonth(date));
|
|
565
|
+
let count = 0;
|
|
566
|
+
let current = start;
|
|
567
|
+
const nestedWeeks: Date[][] = [];
|
|
568
|
+
let lastDay = null as null | number;
|
|
569
|
+
while (isBefore(current, end)) {
|
|
570
|
+
const weekNumber = Math.floor(count / 7);
|
|
571
|
+
nestedWeeks[weekNumber] ??= [];
|
|
572
|
+
const day = dateFnsGetDay(current);
|
|
573
|
+
if (lastDay !== day) {
|
|
574
|
+
lastDay = day;
|
|
575
|
+
nestedWeeks[weekNumber].push(current);
|
|
576
|
+
count += 1;
|
|
577
|
+
}
|
|
578
|
+
current = addDays(current, 1);
|
|
579
|
+
}
|
|
580
|
+
return nestedWeeks;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
export function getYearRange(start: Date, end: Date) {
|
|
584
|
+
const startDate = startOfYear(start);
|
|
585
|
+
const endDate = endOfYear(end);
|
|
586
|
+
const years: Date[] = [];
|
|
587
|
+
|
|
588
|
+
let current = startDate;
|
|
589
|
+
while (isBefore(current, endDate)) {
|
|
590
|
+
years.push(current);
|
|
591
|
+
current = addYears(current, 1);
|
|
592
|
+
}
|
|
593
|
+
return years;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
`;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
//#endregion
|
|
601
|
+
//#region src/components/dayjs.ts
|
|
602
|
+
/**
|
|
603
|
+
* Generates the Powerlines configuration file.
|
|
604
|
+
*
|
|
605
|
+
* @param context - The build context containing runtime information.
|
|
606
|
+
* @returns A string representing the configuration file content.
|
|
607
|
+
*/
|
|
608
|
+
function dayjsModule(context) {
|
|
609
|
+
return `
|
|
610
|
+
/**
|
|
611
|
+
* The Date module provides utility functions for date manipulation and formatting
|
|
612
|
+
*
|
|
613
|
+
* @module ${context.config.framework}:date
|
|
614
|
+
*/
|
|
615
|
+
|
|
616
|
+
${(0, powerlines_lib_utilities_file_header.getFileHeader)(context)}
|
|
617
|
+
|
|
618
|
+
import defaultDayjs, { QUnitType } from "dayjs";
|
|
619
|
+
import customParseFormatPlugin from "dayjs/plugin/customParseFormat";
|
|
620
|
+
import localizedFormatPlugin from "dayjs/plugin/localizedFormat";
|
|
621
|
+
import isBetweenPlugin from "dayjs/plugin/isBetween";
|
|
622
|
+
import weekOfYear from "dayjs/plugin/weekOfYear";
|
|
623
|
+
import {
|
|
624
|
+
StormDateInterface,
|
|
625
|
+
DateFormats,
|
|
626
|
+
TimeUnit
|
|
627
|
+
} from "@powerlines/plugin-date/types/runtime";
|
|
628
|
+
|
|
629
|
+
defaultDayjs.extend(customParseFormatPlugin);
|
|
630
|
+
defaultDayjs.extend(localizedFormatPlugin);
|
|
631
|
+
defaultDayjs.extend(isBetweenPlugin);
|
|
632
|
+
defaultDayjs.extend(weekOfYear);
|
|
633
|
+
|
|
634
|
+
interface Opts {
|
|
635
|
+
locale?: string;
|
|
636
|
+
/** Make sure that your dayjs instance extends customParseFormat and advancedFormat */
|
|
637
|
+
instance?: typeof defaultDayjs;
|
|
638
|
+
formats?: Partial<DateFormats>;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
type Dayjs = defaultDayjs.Dayjs;
|
|
642
|
+
type Constructor<TDate extends Dayjs> = (
|
|
643
|
+
...args: Parameters<typeof defaultDayjs>
|
|
644
|
+
) => TDate;
|
|
645
|
+
const withLocale = <TDate extends Dayjs>(
|
|
646
|
+
dayjs: any,
|
|
647
|
+
locale?: string
|
|
648
|
+
): Constructor<TDate> => (!locale ? dayjs : (...args) => dayjs(...args).locale(locale));
|
|
649
|
+
|
|
650
|
+
export const formats: DateFormats = {
|
|
651
|
+
normalDateWithWeekday: "ddd, MMM D",
|
|
652
|
+
normalDate: "D MMMM",
|
|
653
|
+
shortDate: "MMM D",
|
|
654
|
+
monthAndDate: "MMMM D",
|
|
655
|
+
dayOfMonth: "D",
|
|
656
|
+
year: "YYYY",
|
|
657
|
+
month: "MMMM",
|
|
658
|
+
monthShort: "MMM",
|
|
659
|
+
monthAndYear: "MMMM YYYY",
|
|
660
|
+
weekday: "dddd",
|
|
661
|
+
weekdayShort: "ddd",
|
|
662
|
+
minutes: "mm",
|
|
663
|
+
hours12h: "hh",
|
|
664
|
+
hours24h: "HH",
|
|
665
|
+
seconds: "ss",
|
|
666
|
+
fullTime: "LT",
|
|
667
|
+
fullTime12h: "hh:mm A",
|
|
668
|
+
fullTime24h: "HH:mm",
|
|
669
|
+
fullDate: "ll",
|
|
670
|
+
fullDateWithWeekday: "dddd, LL",
|
|
671
|
+
fullDateTime: "lll",
|
|
672
|
+
fullDateTime12h: "ll hh:mm A",
|
|
673
|
+
fullDateTime24h: "ll HH:mm",
|
|
674
|
+
keyboardDate: "L",
|
|
675
|
+
keyboardDateTime: "L LT",
|
|
676
|
+
keyboardDateTime12h: "L hh:mm A",
|
|
677
|
+
keyboardDateTime24h: "L HH:mm",
|
|
678
|
+
systemDateTime: "L HH:mm:ss.SSS",
|
|
679
|
+
filePathDateTime: "L_HH-mm-ss-SSS"
|
|
680
|
+
};
|
|
681
|
+
|
|
682
|
+
export let locale = "${context.env.parsed.DEFAULT_LOCALE || context.workspaceConfig.locale ? `${context.env.parsed.DEFAULT_LOCALE || context.workspaceConfig.locale}` : "en-US"}";
|
|
683
|
+
|
|
684
|
+
export const dayjs = withLocale(defaultDayjs, locale);
|
|
685
|
+
|
|
686
|
+
export const type = "dayjs";
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* Creates a date object using the provided value.
|
|
690
|
+
*
|
|
691
|
+
* @remarks
|
|
692
|
+
* 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.
|
|
693
|
+
*
|
|
694
|
+
* @example
|
|
695
|
+
* \`\`\`ts
|
|
696
|
+
* import { createDate } from "storm:date";
|
|
697
|
+
*
|
|
698
|
+
* const date = createDate("2023-10-01");
|
|
699
|
+
* console.log(date.format("YYYY-MM-DD")); // Outputs: 2023-10-01
|
|
700
|
+
* \`\`\`
|
|
701
|
+
*
|
|
702
|
+
* @param value - The value to create the date object from.
|
|
703
|
+
* @returns A date object or \`null\` if the value is \`null\`.
|
|
704
|
+
*/
|
|
705
|
+
export function createDate<
|
|
706
|
+
TArg extends unknown = undefined,
|
|
707
|
+
TResultingDate extends unknown = TArg extends null
|
|
708
|
+
? null
|
|
709
|
+
: TArg extends undefined
|
|
710
|
+
? TDate
|
|
711
|
+
: TDate | null
|
|
712
|
+
>(value?: TArg): TResultingDate {
|
|
713
|
+
if (value === null) {
|
|
714
|
+
return null as TResultingDate;
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
return dayjs(value as any) as unknown as TResultingDate;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
export function is12HourCycleInCurrentLocale() {
|
|
721
|
+
/* istanbul ignore next */
|
|
722
|
+
return /A|a/.test(dayjs.Ls[dayjs.locale() || $storm.env.DEFAULT_LOCALE]?.formats?.LT ?? "");
|
|
723
|
+
};
|
|
724
|
+
|
|
725
|
+
export function getCurrentLocaleCode() {
|
|
726
|
+
return dayjs.locale() || $storm.env.DEFAULT_LOCALE;
|
|
727
|
+
};
|
|
728
|
+
|
|
729
|
+
export function getFormatHelperText(format: string) {
|
|
730
|
+
// @see https://github.com/iamkun/dayjs/blob/dev/src/plugin/localizedFormat/index.js
|
|
731
|
+
const localFormattingTokens = /(\[[^\[]*\])|(\\)?(LTS|LT|LL?L?L?)|./g;
|
|
732
|
+
|
|
733
|
+
return (
|
|
734
|
+
format
|
|
735
|
+
.match(localFormattingTokens)
|
|
736
|
+
?.map((token) => {
|
|
737
|
+
const firstCharacter = token[0];
|
|
738
|
+
if (firstCharacter === "L") {
|
|
739
|
+
/* istanbul ignore next */
|
|
740
|
+
return (
|
|
741
|
+
dayjs.Ls[dayjs.locale() || $storm.env.DEFAULT_LOCALE]?.formats[
|
|
742
|
+
token as keyof ILocale["formats"]
|
|
743
|
+
] ?? token
|
|
744
|
+
);
|
|
745
|
+
}
|
|
746
|
+
return token;
|
|
747
|
+
})
|
|
748
|
+
.join("")
|
|
749
|
+
.replace(/a/gi, "(a|p)m")
|
|
750
|
+
.toLocaleLowerCase() ?? format
|
|
751
|
+
);
|
|
752
|
+
};
|
|
753
|
+
|
|
754
|
+
export function parseISO(isoString: string) {
|
|
755
|
+
return dayjs(isoString);
|
|
756
|
+
};
|
|
757
|
+
|
|
758
|
+
export function toISO(value: Dayjs) {
|
|
759
|
+
return value.toISOString();
|
|
760
|
+
};
|
|
761
|
+
|
|
762
|
+
export function parse(value: any, format: string) {
|
|
763
|
+
if (value === "") {
|
|
764
|
+
return null;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
return dayjs(value, format, dayjs.locale(), true);
|
|
768
|
+
};
|
|
769
|
+
|
|
770
|
+
export function toJsDate(value: Dayjs) {
|
|
771
|
+
return value.toDate();
|
|
772
|
+
};
|
|
773
|
+
|
|
774
|
+
export function isValid(value: any) {
|
|
775
|
+
return dayjs(value).isValid();
|
|
776
|
+
};
|
|
777
|
+
|
|
778
|
+
export function isNull(date: Dayjs | null) {
|
|
779
|
+
return date === null;
|
|
780
|
+
};
|
|
781
|
+
|
|
782
|
+
export function getDiff(date: Dayjs, comparing: Dayjs | string, units?: Unit) {
|
|
783
|
+
if (typeof comparing === "string") {
|
|
784
|
+
comparing = dayjs(comparing);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
if (!comparing.isValid()) {
|
|
788
|
+
return 0;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
return date.diff(comparing, units as QUnitType);
|
|
792
|
+
};
|
|
793
|
+
|
|
794
|
+
export function isAfter(date: Dayjs, value: Dayjs) {
|
|
795
|
+
return date.isAfter(value);
|
|
796
|
+
};
|
|
797
|
+
|
|
798
|
+
export function isBefore(date: Dayjs, value: Dayjs) {
|
|
799
|
+
return date.isBefore(value);
|
|
800
|
+
};
|
|
801
|
+
|
|
802
|
+
export function isAfterDay(date: Dayjs, value: Dayjs) {
|
|
803
|
+
return date.isAfter(value, "day");
|
|
804
|
+
};
|
|
805
|
+
|
|
806
|
+
export function isBeforeDay(date: Dayjs, value: Dayjs) {
|
|
807
|
+
return date.isBefore(value, "day");
|
|
808
|
+
};
|
|
809
|
+
|
|
810
|
+
export function isAfterMonth(date: Dayjs, value: Dayjs) {
|
|
811
|
+
return date.isAfter(value, "month");
|
|
812
|
+
};
|
|
813
|
+
|
|
814
|
+
export function isBeforeMonth(date: Dayjs, value: Dayjs) {
|
|
815
|
+
return date.isBefore(value, "month");
|
|
816
|
+
};
|
|
817
|
+
|
|
818
|
+
export function isBeforeYear(date: Dayjs, value: Dayjs) {
|
|
819
|
+
return date.isBefore(value, "year");
|
|
820
|
+
};
|
|
821
|
+
|
|
822
|
+
export function isAfterYear(date: Dayjs, value: Dayjs) {
|
|
823
|
+
return date.isAfter(value, "year");
|
|
824
|
+
};
|
|
825
|
+
|
|
826
|
+
export function startOfDay(date: Dayjs) {
|
|
827
|
+
return date.startOf("day");
|
|
828
|
+
};
|
|
829
|
+
|
|
830
|
+
export function endOfDay(date: Dayjs) {
|
|
831
|
+
return date.endOf("day");
|
|
832
|
+
};
|
|
833
|
+
|
|
834
|
+
export function format(date: Dayjs, formatKey: keyof DateFormats) {
|
|
835
|
+
return formatByString(date, formats[formatKey]);
|
|
836
|
+
};
|
|
837
|
+
|
|
838
|
+
export function formatByString(date: Dayjs, formatString: string) {
|
|
839
|
+
return dayjs(date).format(formatString);
|
|
840
|
+
};
|
|
841
|
+
|
|
842
|
+
export function formatNumber(numberToFormat: string) {
|
|
843
|
+
return numberToFormat;
|
|
844
|
+
};
|
|
845
|
+
|
|
846
|
+
export function getHours(date: Dayjs) {
|
|
847
|
+
return date.hour();
|
|
848
|
+
};
|
|
849
|
+
|
|
850
|
+
export function addSeconds(date: Dayjs, count: number) {
|
|
851
|
+
return count < 0
|
|
852
|
+
? (date.subtract(Math.abs(count), "second") as Dayjs)
|
|
853
|
+
: (date.add(count, "second") as Dayjs);
|
|
854
|
+
};
|
|
855
|
+
|
|
856
|
+
export function addMinutes(date: Dayjs, count: number) {
|
|
857
|
+
return count < 0
|
|
858
|
+
? (date.subtract(Math.abs(count), "minute") as Dayjs)
|
|
859
|
+
: (date.add(count, "minute") as Dayjs);
|
|
860
|
+
};
|
|
861
|
+
|
|
862
|
+
export function addHours(date: Dayjs, count: number) {
|
|
863
|
+
return count < 0
|
|
864
|
+
? (date.subtract(Math.abs(count), "hour") as Dayjs)
|
|
865
|
+
: (date.add(count, "hour") as Dayjs);
|
|
866
|
+
};
|
|
867
|
+
|
|
868
|
+
export function addDays(date: Dayjs, count: number) {
|
|
869
|
+
return count < 0
|
|
870
|
+
? (date.subtract(Math.abs(count), "day") as Dayjs)
|
|
871
|
+
: (date.add(count, "day") as Dayjs);
|
|
872
|
+
};
|
|
873
|
+
|
|
874
|
+
export function addWeeks(date: Dayjs, count: number) {
|
|
875
|
+
return count < 0
|
|
876
|
+
? (date.subtract(Math.abs(count), "week") as Dayjs)
|
|
877
|
+
: (date.add(count, "week") as Dayjs);
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
export function addMonths(date: Dayjs, count: number) {
|
|
881
|
+
return count < 0
|
|
882
|
+
? (date.subtract(Math.abs(count), "month") as Dayjs)
|
|
883
|
+
: (date.add(count, "month") as Dayjs);
|
|
884
|
+
};
|
|
885
|
+
|
|
886
|
+
export function addYears(date: Dayjs, count: number) {
|
|
887
|
+
return count < 0
|
|
888
|
+
? (date.subtract(Math.abs(count), "year") as Dayjs)
|
|
889
|
+
: (date.add(count, "year") as Dayjs);
|
|
890
|
+
};
|
|
891
|
+
|
|
892
|
+
export function setMonth(date: Dayjs, count: number) {
|
|
893
|
+
return date.set("month", count) as Dayjs;
|
|
894
|
+
};
|
|
895
|
+
|
|
896
|
+
export function setHours(date: Dayjs, count: number) {
|
|
897
|
+
return date.set("hour", count) as Dayjs;
|
|
898
|
+
};
|
|
899
|
+
|
|
900
|
+
export function getMinutes(date: Dayjs) {
|
|
901
|
+
return date.minute();
|
|
902
|
+
};
|
|
903
|
+
|
|
904
|
+
export function setMinutes(date: Dayjs, count: number) {
|
|
905
|
+
return date.set("minute", count) as Dayjs;
|
|
906
|
+
};
|
|
907
|
+
|
|
908
|
+
export function getSeconds(date: Dayjs) {
|
|
909
|
+
return date.second();
|
|
910
|
+
};
|
|
911
|
+
|
|
912
|
+
export function setSeconds(date: Dayjs, count: number) {
|
|
913
|
+
return date.set("second", count) as Dayjs;
|
|
914
|
+
};
|
|
915
|
+
|
|
916
|
+
export function getWeek(date: Dayjs) {
|
|
917
|
+
return date.week();
|
|
918
|
+
};
|
|
919
|
+
|
|
920
|
+
export function getMonth(date: Dayjs) {
|
|
921
|
+
return date.month();
|
|
922
|
+
};
|
|
923
|
+
|
|
924
|
+
export function getDate(date: Dayjs) {
|
|
925
|
+
return date.date();
|
|
926
|
+
};
|
|
927
|
+
|
|
928
|
+
export function setDate(date: Dayjs, count: number) {
|
|
929
|
+
return date.set("date", count) as Dayjs;
|
|
930
|
+
};
|
|
931
|
+
|
|
932
|
+
export function getDaysInMonth(date: Dayjs) {
|
|
933
|
+
return date.daysInMonth();
|
|
934
|
+
};
|
|
935
|
+
|
|
936
|
+
export function isSameDay(date: Dayjs, comparing: Dayjs) {
|
|
937
|
+
return date.isSame(comparing, "day");
|
|
938
|
+
};
|
|
939
|
+
|
|
940
|
+
export function isSameMonth(date: Dayjs, comparing: Dayjs) {
|
|
941
|
+
return date.isSame(comparing, "month");
|
|
942
|
+
};
|
|
943
|
+
|
|
944
|
+
export function isSameYear(date: Dayjs, comparing: Dayjs) {
|
|
945
|
+
return date.isSame(comparing, "year");
|
|
946
|
+
};
|
|
947
|
+
|
|
948
|
+
export function isSameHour(date: Dayjs, comparing: Dayjs) {
|
|
949
|
+
return date.isSame(comparing, "hour");
|
|
950
|
+
};
|
|
951
|
+
|
|
952
|
+
export function getMeridiemText(meridiem: "am" | "pm") {
|
|
953
|
+
return meridiem === "am" ? "AM" : "PM";
|
|
954
|
+
};
|
|
955
|
+
|
|
956
|
+
export function startOfYear(date: Dayjs) {
|
|
957
|
+
return date.startOf("year") as Dayjs;
|
|
958
|
+
};
|
|
12
959
|
|
|
960
|
+
export function endOfYear(date: Dayjs) {
|
|
961
|
+
return date.endOf("year") as Dayjs;
|
|
962
|
+
};
|
|
963
|
+
|
|
964
|
+
export function startOfMonth(date: Dayjs) {
|
|
965
|
+
return date.startOf("month") as Dayjs;
|
|
966
|
+
};
|
|
967
|
+
|
|
968
|
+
export function endOfMonth(date: Dayjs) {
|
|
969
|
+
return date.endOf("month") as Dayjs;
|
|
970
|
+
};
|
|
971
|
+
|
|
972
|
+
export function startOfWeek(date: Dayjs) {
|
|
973
|
+
return date.startOf("week") as Dayjs;
|
|
974
|
+
};
|
|
975
|
+
|
|
976
|
+
export function endOfWeek(date: Dayjs) {
|
|
977
|
+
return date.endOf("week") as Dayjs;
|
|
978
|
+
};
|
|
979
|
+
|
|
980
|
+
export function getNextMonth(date: Dayjs) {
|
|
981
|
+
return date.add(1, "month") as Dayjs;
|
|
982
|
+
};
|
|
983
|
+
|
|
984
|
+
export function getPreviousMonth(date: Dayjs) {
|
|
985
|
+
return date.subtract(1, "month") as Dayjs;
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
export function getMonthArray(date: Dayjs) {
|
|
989
|
+
const firstMonth = date.startOf("year") as Dayjs;
|
|
990
|
+
const monthArray = [firstMonth];
|
|
991
|
+
|
|
992
|
+
while (monthArray.length < 12) {
|
|
993
|
+
const prevMonth = monthArray[monthArray.length - 1];
|
|
994
|
+
monthArray.push(getNextMonth(prevMonth));
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
return monthArray;
|
|
998
|
+
};
|
|
999
|
+
|
|
1000
|
+
export function getYear(date: Dayjs) {
|
|
1001
|
+
return date.year();
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
export function setYear(date: Dayjs, year: number) {
|
|
1005
|
+
return date.set("year", year) as Dayjs;
|
|
1006
|
+
};
|
|
1007
|
+
|
|
1008
|
+
export function mergeDateAndTime(date: Dayjs, time: Dayjs) {
|
|
1009
|
+
return date.hour(time.hour()).minute(time.minute()).second(time.second()) as Dayjs;
|
|
1010
|
+
};
|
|
1011
|
+
|
|
1012
|
+
export function getWeekdays() {
|
|
1013
|
+
const start = dayjs().startOf("week");
|
|
1014
|
+
return [0, 1, 2, 3, 4, 5, 6].map((diff) =>
|
|
1015
|
+
formatByString(start.add(diff, "day"), "dd")
|
|
1016
|
+
);
|
|
1017
|
+
};
|
|
1018
|
+
|
|
1019
|
+
export function isEqual(value: any, comparing: any) {
|
|
1020
|
+
if (value === null && comparing === null) {
|
|
1021
|
+
return true;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
return dayjs(value).isSame(comparing);
|
|
1025
|
+
};
|
|
1026
|
+
|
|
1027
|
+
export function getWeekArray(date: Dayjs) {
|
|
1028
|
+
const start = dayjs(date).startOf("month").startOf("week") as Dayjs;
|
|
1029
|
+
const end = dayjs(date).endOf("month").endOf("week") as Dayjs;
|
|
1030
|
+
|
|
1031
|
+
let count = 0;
|
|
1032
|
+
let current = start;
|
|
1033
|
+
const nestedWeeks: Dayjs[][] = [];
|
|
1034
|
+
|
|
1035
|
+
while (current.isBefore(end)) {
|
|
1036
|
+
const weekNumber = Math.floor(count / 7);
|
|
1037
|
+
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || [];
|
|
1038
|
+
nestedWeeks[weekNumber].push(current);
|
|
1039
|
+
|
|
1040
|
+
current = current.add(1, "day") as TDate;
|
|
1041
|
+
count += 1;
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
return nestedWeeks;
|
|
1045
|
+
};
|
|
1046
|
+
|
|
1047
|
+
export function getYearRange(start: Dayjs, end: Dayjs) {
|
|
1048
|
+
const startDate = start.startOf("year");
|
|
1049
|
+
const endDate = end.endOf("year");
|
|
1050
|
+
const years: Dayjs[] = [];
|
|
1051
|
+
|
|
1052
|
+
let current = startDate;
|
|
1053
|
+
while (current.isBefore(endDate)) {
|
|
1054
|
+
years.push(current);
|
|
1055
|
+
current = current.add(1, "year");
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
return years;
|
|
1059
|
+
};
|
|
1060
|
+
|
|
1061
|
+
export function isWithinRange(date: Dayjs, [start, end]: [Dayjs, Dayjs]) {
|
|
1062
|
+
return date.isBetween(start, end, null, "[]");
|
|
1063
|
+
};
|
|
1064
|
+
|
|
1065
|
+
|
|
1066
|
+
`;
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
//#endregion
|
|
1070
|
+
//#region src/components/luxon.ts
|
|
1071
|
+
/**
|
|
1072
|
+
* Generates the Powerlines configuration file.
|
|
1073
|
+
*
|
|
1074
|
+
* @param context - The build context containing runtime information.
|
|
1075
|
+
* @returns A string representing the configuration file content.
|
|
1076
|
+
*/
|
|
1077
|
+
function luxonModule(context) {
|
|
1078
|
+
return `
|
|
1079
|
+
/**
|
|
1080
|
+
* The Date module provides utility functions for date manipulation and formatting
|
|
1081
|
+
*
|
|
1082
|
+
* @module ${context.config.framework}:date
|
|
1083
|
+
*/
|
|
1084
|
+
|
|
1085
|
+
${(0, powerlines_lib_utilities_file_header.getFileHeader)(context)}
|
|
1086
|
+
|
|
1087
|
+
import { DateTime, Settings, Info } from "luxon";
|
|
1088
|
+
import {
|
|
1089
|
+
StormDateInterface,
|
|
1090
|
+
DateFormats,
|
|
1091
|
+
TimeUnit
|
|
1092
|
+
} from "@powerlines/plugin-date/types/runtime";
|
|
1093
|
+
|
|
1094
|
+
export const formats: DateFormats = {
|
|
1095
|
+
dayOfMonth: "d",
|
|
1096
|
+
fullDate: "DD",
|
|
1097
|
+
fullDateWithWeekday: "DDDD",
|
|
1098
|
+
fullDateTime: "ff",
|
|
1099
|
+
fullDateTime12h: "DD, hh:mm a",
|
|
1100
|
+
fullDateTime24h: "DD, T",
|
|
1101
|
+
fullTime: "t",
|
|
1102
|
+
fullTime12h: "hh:mm a",
|
|
1103
|
+
fullTime24h: "HH:mm",
|
|
1104
|
+
hours12h: "hh",
|
|
1105
|
+
hours24h: "HH",
|
|
1106
|
+
keyboardDate: "D",
|
|
1107
|
+
keyboardDateTime: "D t",
|
|
1108
|
+
keyboardDateTime12h: "D hh:mm a",
|
|
1109
|
+
keyboardDateTime24h: "D T",
|
|
1110
|
+
systemDateTime: "D HH:mm:ss.SSS",
|
|
1111
|
+
filePathDateTime: "D_HH-mm-ss-SSS",
|
|
1112
|
+
minutes: "mm",
|
|
1113
|
+
seconds: "ss",
|
|
1114
|
+
month: "LLLL",
|
|
1115
|
+
monthAndDate: "MMMM d",
|
|
1116
|
+
monthAndYear: "LLLL yyyy",
|
|
1117
|
+
monthShort: "MMM",
|
|
1118
|
+
weekday: "cccc",
|
|
1119
|
+
weekdayShort: "ccc",
|
|
1120
|
+
normalDate: "d MMMM",
|
|
1121
|
+
normalDateWithWeekday: "EEE, MMM d",
|
|
1122
|
+
shortDate: "MMM d",
|
|
1123
|
+
year: "yyyy",
|
|
1124
|
+
};
|
|
1125
|
+
|
|
1126
|
+
export let locale = "${context.env.parsed.DEFAULT_LOCALE || context.workspaceConfig.locale ? `${context.env.parsed.DEFAULT_LOCALE || context.workspaceConfig.locale}` : "en-US"}";
|
|
1127
|
+
|
|
1128
|
+
export const type = "luxon";
|
|
1129
|
+
|
|
1130
|
+
/**
|
|
1131
|
+
* Creates a date object using the provided value.
|
|
1132
|
+
*
|
|
1133
|
+
* @remarks
|
|
1134
|
+
* 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.
|
|
1135
|
+
*
|
|
1136
|
+
* @example
|
|
1137
|
+
* \`\`\`ts
|
|
1138
|
+
* import { createDate } from "storm:date";
|
|
1139
|
+
*
|
|
1140
|
+
* const date = createDate("2023-10-01");
|
|
1141
|
+
* console.log(date.format("YYYY-MM-DD")); // Outputs: 2023-10-01
|
|
1142
|
+
* \`\`\`
|
|
1143
|
+
*
|
|
1144
|
+
* @param value - The value to create the date object from.
|
|
1145
|
+
* @returns A date object or \`null\` if the value is \`null\`.
|
|
1146
|
+
*/
|
|
1147
|
+
export function createDate<
|
|
1148
|
+
TArg extends unknown = undefined,
|
|
1149
|
+
TResultingDate extends unknown = TArg extends null
|
|
1150
|
+
? null
|
|
1151
|
+
: TArg extends undefined
|
|
1152
|
+
? DateTime
|
|
1153
|
+
: DateTime | null
|
|
1154
|
+
>(value?: TArg): TResultingDate {
|
|
1155
|
+
if (typeof value === "undefined") {
|
|
1156
|
+
return DateTime.local() as TResultingDate;
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
if (value === null) {
|
|
1160
|
+
return null as TResultingDate;
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
if (typeof value === "string") {
|
|
1164
|
+
return DateTime.fromJSDate(new Date(value), { locale }) as TResultingDate;
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
if (DateTime.isDateTime(value)) {
|
|
1168
|
+
return value as TResultingDate;
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
if (value instanceof Date) {
|
|
1172
|
+
return DateTime.fromJSDate(value, { locale }) as TResultingDate;
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
/* istanbul ignore next */
|
|
1176
|
+
return DateTime.local() as TResultingDate;
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
export function toJsDate(value: DateTime) {
|
|
1180
|
+
return value.toJSDate();
|
|
1181
|
+
};
|
|
1182
|
+
|
|
1183
|
+
export function parseISO(isoString: string) {
|
|
1184
|
+
return DateTime.fromISO(isoString);
|
|
1185
|
+
};
|
|
1186
|
+
|
|
1187
|
+
export function toISO(value: DateTime) {
|
|
1188
|
+
return value.toISO({ format: "extended" });
|
|
1189
|
+
};
|
|
1190
|
+
|
|
1191
|
+
export function parse(value: string, formatString: string) {
|
|
1192
|
+
if (value === "") {
|
|
1193
|
+
return null;
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
return DateTime.fromFormat(value, formatString, { locale });
|
|
1197
|
+
};
|
|
1198
|
+
|
|
1199
|
+
/* istanbul ignore next */
|
|
1200
|
+
export function is12HourCycleInCurrentLocale() {
|
|
1201
|
+
if (typeof Intl === "undefined" || typeof Intl.DateTimeFormat === "undefined") {
|
|
1202
|
+
return true; // Luxon defaults to en-US if Intl not found
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
return Boolean(
|
|
1206
|
+
new Intl.DateTimeFormat(this.locale, { hour: "numeric" })?.resolvedOptions()?.hour12
|
|
1207
|
+
);
|
|
1208
|
+
};
|
|
1209
|
+
|
|
1210
|
+
export function getFormatHelperText(format: string) {
|
|
1211
|
+
// Unfortunately there is no way for luxon to retrieve readable formats from localized format
|
|
1212
|
+
return "";
|
|
1213
|
+
};
|
|
1214
|
+
|
|
1215
|
+
/* istanbul ignore next */
|
|
1216
|
+
export function getCurrentLocaleCode() {
|
|
1217
|
+
return this.locale || Settings.defaultLocale;
|
|
1218
|
+
};
|
|
1219
|
+
|
|
1220
|
+
export function addSeconds(date: DateTime, count: number) {
|
|
1221
|
+
return date.plus({ seconds: count });
|
|
1222
|
+
};
|
|
1223
|
+
|
|
1224
|
+
export function addMinutes(date: DateTime, count: number) {
|
|
1225
|
+
return date.plus({ minutes: count });
|
|
1226
|
+
};
|
|
1227
|
+
|
|
1228
|
+
export function addHours(date: DateTime, count: number) {
|
|
1229
|
+
return date.plus({ hours: count });
|
|
1230
|
+
};
|
|
1231
|
+
|
|
1232
|
+
export function addDays(date: DateTime, count: number) {
|
|
1233
|
+
return date.plus({ days: count });
|
|
1234
|
+
};
|
|
1235
|
+
|
|
1236
|
+
export function addWeeks(date: DateTime, count: number) {
|
|
1237
|
+
return date.plus({ weeks: count });
|
|
1238
|
+
};
|
|
1239
|
+
|
|
1240
|
+
export function addMonths(date: DateTime, count: number) {
|
|
1241
|
+
return date.plus({ months: count });
|
|
1242
|
+
};
|
|
1243
|
+
|
|
1244
|
+
export function addYears(date: DateTime, count: number) {
|
|
1245
|
+
return date.plus({ years: count });
|
|
1246
|
+
};
|
|
1247
|
+
|
|
1248
|
+
export function isValid(value: any) {
|
|
1249
|
+
if (DateTime.isDateTime(value)) {
|
|
1250
|
+
return value.isValid;
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
if (value === null) {
|
|
1254
|
+
return false;
|
|
1255
|
+
}
|
|
1256
|
+
|
|
1257
|
+
return createDate(value)?.isValid ?? false;
|
|
1258
|
+
};
|
|
1259
|
+
|
|
1260
|
+
export function isEqual(value: any, comparing: any) {
|
|
1261
|
+
if (value === null && comparing === null) {
|
|
1262
|
+
return true;
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
// make sure that null will not be passed to createDate
|
|
1266
|
+
if (value === null || comparing === null) {
|
|
1267
|
+
return false;
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
if (!createDate(comparing)) {
|
|
1271
|
+
/* istanbul ignore next */
|
|
1272
|
+
return false;
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
return createDate(value)?.equals(createDate(comparing) as DateTime) ?? false;
|
|
1276
|
+
};
|
|
1277
|
+
|
|
1278
|
+
export function isSameDay(date: DateTime, comparing: DateTime) {
|
|
1279
|
+
return date.hasSame(comparing, "day");
|
|
1280
|
+
};
|
|
1281
|
+
|
|
1282
|
+
export function isSameMonth(date: DateTime, comparing: DateTime) {
|
|
1283
|
+
return date.hasSame(comparing, "month");
|
|
1284
|
+
};
|
|
1285
|
+
|
|
1286
|
+
export function isSameYear(date: DateTime, comparing: DateTime) {
|
|
1287
|
+
return date.hasSame(comparing, "year");
|
|
1288
|
+
};
|
|
1289
|
+
|
|
1290
|
+
export function isSameHour(date: DateTime, comparing: DateTime) {
|
|
1291
|
+
return date.hasSame(comparing, "hour");
|
|
1292
|
+
};
|
|
1293
|
+
|
|
1294
|
+
export function isAfter(value: DateTime, comparing: DateTime) {
|
|
1295
|
+
return value > comparing;
|
|
1296
|
+
};
|
|
1297
|
+
|
|
1298
|
+
export function isBefore(value: DateTime, comparing: DateTime) {
|
|
1299
|
+
return value < comparing;
|
|
1300
|
+
};
|
|
1301
|
+
|
|
1302
|
+
export function isBeforeDay(value: DateTime, comparing: DateTime) {
|
|
1303
|
+
const diff = value.diff(comparing.startOf("day"), "days").toObject();
|
|
1304
|
+
return diff.days! < 0;
|
|
1305
|
+
};
|
|
1306
|
+
|
|
1307
|
+
export function isAfterDay(value: DateTime, comparing: DateTime) {
|
|
1308
|
+
const diff = value.diff(comparing.endOf("day"), "days").toObject();
|
|
1309
|
+
return diff.days! > 0;
|
|
1310
|
+
};
|
|
1311
|
+
|
|
1312
|
+
export function isBeforeMonth(value: DateTime, comparing: DateTime) {
|
|
1313
|
+
const diff = value.diff(comparing.startOf("month"), "months").toObject();
|
|
1314
|
+
return diff.months! < 0;
|
|
1315
|
+
};
|
|
1316
|
+
|
|
1317
|
+
export function isAfterMonth(value: DateTime, comparing: DateTime) {
|
|
1318
|
+
const diff = value.diff(comparing.startOf("month"), "months").toObject();
|
|
1319
|
+
return diff.months! > 0;
|
|
1320
|
+
};
|
|
1321
|
+
|
|
1322
|
+
export function isBeforeYear(value: DateTime, comparing: DateTime) {
|
|
1323
|
+
const diff = value.diff(comparing.startOf("year"), "years").toObject();
|
|
1324
|
+
return diff.years! < 0;
|
|
1325
|
+
};
|
|
1326
|
+
|
|
1327
|
+
export function isAfterYear(value: DateTime, comparing: DateTime) {
|
|
1328
|
+
const diff = value.diff(comparing.endOf("year"), "years").toObject();
|
|
1329
|
+
return diff.years! > 0;
|
|
1330
|
+
};
|
|
1331
|
+
|
|
1332
|
+
export function getDiff(value: DateTime, comparing: DateTime | string, unit?: TimeUnit) {
|
|
1333
|
+
if (typeof comparing === "string") {
|
|
1334
|
+
comparing = DateTime.fromJSDate(new Date(comparing));
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
if (!comparing.isValid) {
|
|
1338
|
+
return 0;
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
if (unit) {
|
|
1342
|
+
return Math.floor(value.diff(comparing).as(unit));
|
|
1343
|
+
}
|
|
1344
|
+
|
|
1345
|
+
return value.diff(comparing).as("millisecond");
|
|
1346
|
+
};
|
|
1347
|
+
|
|
1348
|
+
export function startOfDay(value: DateTime) {
|
|
1349
|
+
return value.startOf("day");
|
|
1350
|
+
};
|
|
1351
|
+
|
|
1352
|
+
export function endOfDay(value: DateTime) {
|
|
1353
|
+
return value.endOf("day");
|
|
1354
|
+
};
|
|
1355
|
+
|
|
1356
|
+
export function format(date: DateTime, formatKey: keyof DateFormats) {
|
|
1357
|
+
return formatByString(date, formats[formatKey]);
|
|
1358
|
+
};
|
|
1359
|
+
|
|
1360
|
+
export function formatByString(date: DateTime, format: string) {
|
|
1361
|
+
return date.setLocale(locale).toFormat(format);
|
|
1362
|
+
};
|
|
1363
|
+
|
|
1364
|
+
export function formatNumber(numberToFormat: string) {
|
|
1365
|
+
return numberToFormat;
|
|
1366
|
+
};
|
|
1367
|
+
|
|
1368
|
+
export function getHours(value: DateTime) {
|
|
1369
|
+
return value.get("hour");
|
|
1370
|
+
};
|
|
1371
|
+
|
|
1372
|
+
export function setHours(value: DateTime, count: number) {
|
|
1373
|
+
return value.set({ hour: count });
|
|
1374
|
+
};
|
|
1375
|
+
|
|
1376
|
+
export function getMinutes(value: DateTime) {
|
|
1377
|
+
return value.get("minute");
|
|
1378
|
+
};
|
|
1379
|
+
|
|
1380
|
+
export function setMinutes(value: DateTime, count: number) {
|
|
1381
|
+
return value.set({ minute: count });
|
|
1382
|
+
};
|
|
1383
|
+
|
|
1384
|
+
export function getSeconds(value: DateTime) {
|
|
1385
|
+
return value.get("second");
|
|
1386
|
+
};
|
|
1387
|
+
|
|
1388
|
+
export function setSeconds(value: DateTime, count: number) {
|
|
1389
|
+
return value.set({ second: count });
|
|
1390
|
+
};
|
|
1391
|
+
|
|
1392
|
+
export function getWeek(value: DateTime) {
|
|
1393
|
+
return value.get("weekNumber");
|
|
1394
|
+
};
|
|
1395
|
+
|
|
1396
|
+
export function getMonth(value: DateTime) {
|
|
1397
|
+
// See https://github.com/moment/luxon/blob/master/docs/moment.md#major-functional-differences
|
|
1398
|
+
return value.get("month") - 1;
|
|
1399
|
+
};
|
|
1400
|
+
|
|
1401
|
+
export function getDaysInMonth(value: DateTime) {
|
|
1402
|
+
return value.daysInMonth;
|
|
1403
|
+
};
|
|
1404
|
+
|
|
1405
|
+
export function setMonth(value: DateTime, count: number) {
|
|
1406
|
+
return value.set({ month: count + 1 });
|
|
1407
|
+
};
|
|
1408
|
+
|
|
1409
|
+
export function getYear(value: DateTime) {
|
|
1410
|
+
return value.get("year");
|
|
1411
|
+
};
|
|
1412
|
+
|
|
1413
|
+
export function setYear(value: DateTime, year: number) {
|
|
1414
|
+
return value.set({ year });
|
|
1415
|
+
};
|
|
1416
|
+
|
|
1417
|
+
export function getDate(value: DateTime) {
|
|
1418
|
+
return value.get("day");
|
|
1419
|
+
};
|
|
1420
|
+
|
|
1421
|
+
export function setDate(value: DateTime, day: number) {
|
|
1422
|
+
return value.set({ day });
|
|
1423
|
+
};
|
|
1424
|
+
|
|
1425
|
+
export function mergeDateAndTime(date: DateTime, time: DateTime) {
|
|
1426
|
+
return date.set({
|
|
1427
|
+
second: time.second,
|
|
1428
|
+
hour: time.hour,
|
|
1429
|
+
minute: time.minute,
|
|
1430
|
+
});
|
|
1431
|
+
};
|
|
1432
|
+
|
|
1433
|
+
export function startOfYear(value: DateTime) {
|
|
1434
|
+
return value.startOf("year");
|
|
1435
|
+
};
|
|
1436
|
+
|
|
1437
|
+
export function endOfYear(value: DateTime) {
|
|
1438
|
+
return value.endOf("year");
|
|
1439
|
+
};
|
|
1440
|
+
|
|
1441
|
+
export function startOfMonth(value: DateTime) {
|
|
1442
|
+
return value.startOf("month");
|
|
1443
|
+
};
|
|
1444
|
+
|
|
1445
|
+
export function endOfMonth(value: DateTime) {
|
|
1446
|
+
return value.endOf("month");
|
|
1447
|
+
};
|
|
1448
|
+
|
|
1449
|
+
export function startOfWeek(value: DateTime) {
|
|
1450
|
+
return value.startOf("week");
|
|
1451
|
+
};
|
|
1452
|
+
|
|
1453
|
+
export function endOfWeek(value: DateTime) {
|
|
1454
|
+
return value.endOf("week");
|
|
1455
|
+
};
|
|
1456
|
+
|
|
1457
|
+
export function getNextMonth(value: DateTime) {
|
|
1458
|
+
return value.plus({ months: 1 });
|
|
1459
|
+
};
|
|
1460
|
+
|
|
1461
|
+
export function getPreviousMonth(value: DateTime) {
|
|
1462
|
+
return value.minus({ months: 1 });
|
|
1463
|
+
};
|
|
1464
|
+
|
|
1465
|
+
export function getMonthArray(date: DateTime) {
|
|
1466
|
+
const firstMonth = date.startOf("year");
|
|
1467
|
+
const monthArray = [firstMonth];
|
|
1468
|
+
|
|
1469
|
+
while (monthArray.length < 12) {
|
|
1470
|
+
const prevMonth = monthArray[monthArray.length - 1];
|
|
1471
|
+
monthArray.push(this.getNextMonth(prevMonth));
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
return monthArray;
|
|
1475
|
+
};
|
|
1476
|
+
|
|
1477
|
+
export function getWeekdays() {
|
|
1478
|
+
return Info.weekdaysFormat("short", { locale });
|
|
1479
|
+
};
|
|
1480
|
+
|
|
1481
|
+
export function getWeekArray(date: DateTime) {
|
|
1482
|
+
const { days } = date
|
|
1483
|
+
.endOf("month")
|
|
1484
|
+
.endOf("week")
|
|
1485
|
+
.diff(date.startOf("month").startOf("week"), "days")
|
|
1486
|
+
.toObject();
|
|
1487
|
+
|
|
1488
|
+
const weeks: DateTime[][] = [];
|
|
1489
|
+
new Array<number>(Math.round(days!))
|
|
1490
|
+
.fill(0)
|
|
1491
|
+
.map((_, i) => i)
|
|
1492
|
+
.map((day) => date.startOf("month").startOf("week").plus({ days: day }))
|
|
1493
|
+
.forEach((v, i) => {
|
|
1494
|
+
if (i === 0 || (i % 7 === 0 && i > 6)) {
|
|
1495
|
+
weeks.push([v]);
|
|
1496
|
+
return;
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
weeks[weeks.length - 1].push(v);
|
|
1500
|
+
});
|
|
1501
|
+
|
|
1502
|
+
return weeks;
|
|
1503
|
+
};
|
|
1504
|
+
|
|
1505
|
+
export function getYearRange(start: DateTime, end: DateTime) {
|
|
1506
|
+
const startDate = start.startOf("year");
|
|
1507
|
+
const endDate = end.endOf("year");
|
|
1508
|
+
|
|
1509
|
+
let current = startDate;
|
|
1510
|
+
const years: DateTime[] = [];
|
|
1511
|
+
|
|
1512
|
+
while (current < endDate) {
|
|
1513
|
+
years.push(current);
|
|
1514
|
+
current = current.plus({ year: 1 });
|
|
1515
|
+
}
|
|
1516
|
+
|
|
1517
|
+
return years;
|
|
1518
|
+
};
|
|
1519
|
+
|
|
1520
|
+
export function getMeridiemText(meridiem: "am" | "pm") {
|
|
1521
|
+
return Info.meridiems({ locale }).find(
|
|
1522
|
+
(v) => v.toLowerCase() === meridiem.toLowerCase()
|
|
1523
|
+
)!;
|
|
1524
|
+
};
|
|
1525
|
+
|
|
1526
|
+
export function isNull(date: DateTime | null) {
|
|
1527
|
+
return date === null;
|
|
1528
|
+
};
|
|
1529
|
+
|
|
1530
|
+
export function isWithinRange(date: DateTime, [start, end]: [DateTime, DateTime]) {
|
|
1531
|
+
return (
|
|
1532
|
+
date.equals(start) ||
|
|
1533
|
+
date.equals(end) ||
|
|
1534
|
+
(isAfter(date, start) && isBefore(date, end))
|
|
1535
|
+
);
|
|
1536
|
+
};
|
|
1537
|
+
|
|
1538
|
+
|
|
1539
|
+
`;
|
|
1540
|
+
}
|
|
1541
|
+
|
|
1542
|
+
//#endregion
|
|
1543
|
+
//#region src/components/moment.ts
|
|
1544
|
+
/**
|
|
1545
|
+
* Generates the Powerlines configuration file.
|
|
1546
|
+
*
|
|
1547
|
+
* @param context - The build context containing runtime information.
|
|
1548
|
+
* @returns A string representing the configuration file content.
|
|
1549
|
+
*/
|
|
1550
|
+
function momentModule(context) {
|
|
1551
|
+
return `
|
|
1552
|
+
/**
|
|
1553
|
+
* The Date module provides utility functions for date manipulation and formatting
|
|
1554
|
+
*
|
|
1555
|
+
* @module ${context.config.framework}:date
|
|
1556
|
+
*/
|
|
1557
|
+
|
|
1558
|
+
${(0, powerlines_lib_utilities_file_header.getFileHeader)(context)}
|
|
1559
|
+
|
|
1560
|
+
import defaultMoment, { LongDateFormatKey } from "moment";
|
|
1561
|
+
import {
|
|
1562
|
+
StormDateInterface,
|
|
1563
|
+
DateFormats,
|
|
1564
|
+
TimeUnit
|
|
1565
|
+
} from "@powerlines/plugin-date/types/runtime";
|
|
1566
|
+
|
|
1567
|
+
type Moment = defaultMoment.Moment;
|
|
1568
|
+
|
|
1569
|
+
export const formats: DateFormats = {
|
|
1570
|
+
normalDateWithWeekday: "ddd, MMM D",
|
|
1571
|
+
normalDate: "D MMMM",
|
|
1572
|
+
shortDate: "MMM D",
|
|
1573
|
+
monthAndDate: "MMMM D",
|
|
1574
|
+
dayOfMonth: "D",
|
|
1575
|
+
year: "YYYY",
|
|
1576
|
+
month: "MMMM",
|
|
1577
|
+
monthShort: "MMM",
|
|
1578
|
+
monthAndYear: "MMMM YYYY",
|
|
1579
|
+
weekday: "dddd",
|
|
1580
|
+
weekdayShort: "ddd",
|
|
1581
|
+
minutes: "mm",
|
|
1582
|
+
hours12h: "hh",
|
|
1583
|
+
hours24h: "HH",
|
|
1584
|
+
seconds: "ss",
|
|
1585
|
+
fullTime: "LT",
|
|
1586
|
+
fullTime12h: "hh:mm A",
|
|
1587
|
+
fullTime24h: "HH:mm",
|
|
1588
|
+
fullDate: "ll",
|
|
1589
|
+
fullDateWithWeekday: "dddd, LL",
|
|
1590
|
+
fullDateTime: "lll",
|
|
1591
|
+
fullDateTime12h: "ll hh:mm A",
|
|
1592
|
+
fullDateTime24h: "ll HH:mm",
|
|
1593
|
+
keyboardDate: "L",
|
|
1594
|
+
keyboardDateTime: "L LT",
|
|
1595
|
+
keyboardDateTime12h: "L hh:mm A",
|
|
1596
|
+
keyboardDateTime24h: "L HH:mm",
|
|
1597
|
+
systemDateTime: "L HH:mm:ss.SSS",
|
|
1598
|
+
filePathDateTime: "L_HH-mm-ss-SSS"
|
|
1599
|
+
};
|
|
1600
|
+
|
|
1601
|
+
export let locale = "${context.env.parsed.DEFAULT_LOCALE || context.workspaceConfig.locale ? `${context.env.parsed.DEFAULT_LOCALE || context.workspaceConfig.locale}` : "en-US"}";
|
|
1602
|
+
|
|
1603
|
+
export const type = "moment";
|
|
1604
|
+
|
|
1605
|
+
const moment = defaultMoment;
|
|
1606
|
+
|
|
1607
|
+
/**
|
|
1608
|
+
* Creates a date object using the provided value.
|
|
1609
|
+
*
|
|
1610
|
+
* @remarks
|
|
1611
|
+
* 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.
|
|
1612
|
+
*
|
|
1613
|
+
* @example
|
|
1614
|
+
* \`\`\`ts
|
|
1615
|
+
* import { createDate } from "storm:date";
|
|
1616
|
+
*
|
|
1617
|
+
* const date = createDate("2023-10-01");
|
|
1618
|
+
* console.log(date.format("YYYY-MM-DD")); // Outputs: 2023-10-01
|
|
1619
|
+
* \`\`\`
|
|
1620
|
+
*
|
|
1621
|
+
* @param value - The value to create the date object from.
|
|
1622
|
+
* @returns A date object or \`null\` if the value is \`null\`.
|
|
1623
|
+
*/
|
|
1624
|
+
export function createDate<
|
|
1625
|
+
TArg extends unknown = undefined,
|
|
1626
|
+
TResultingDate extends unknown = TArg extends null
|
|
1627
|
+
? null
|
|
1628
|
+
: TArg extends undefined
|
|
1629
|
+
? defaultMoment.Moment
|
|
1630
|
+
: defaultMoment.Moment | null
|
|
1631
|
+
>(value?: TArg): TResultingDate {
|
|
1632
|
+
if (value === null) {
|
|
1633
|
+
return null as TResultingDate;
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
const _moment = moment(value);
|
|
1637
|
+
if (locale) {
|
|
1638
|
+
_moment.locale(locale);
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
return _moment as TResultingDate;
|
|
1642
|
+
}
|
|
1643
|
+
|
|
1644
|
+
|
|
1645
|
+
export function is12HourCycleInCurrentLocale(): boolean {
|
|
1646
|
+
return /A|a/.test(
|
|
1647
|
+
moment.localeData(getCurrentLocaleCode()).longDateFormat("LT")
|
|
1648
|
+
);
|
|
1649
|
+
};
|
|
1650
|
+
|
|
1651
|
+
export function getFormatHelperText(format: string) {
|
|
1652
|
+
// @see https://github.com/moment/moment/blob/develop/src/lib/format/format.js#L6
|
|
1653
|
+
const localFormattingTokens = /(\[[^\[]*\])|(\\)?(LTS|LT|LL?L?L?|l{1,4})|./g;
|
|
1654
|
+
|
|
1655
|
+
return (
|
|
1656
|
+
format
|
|
1657
|
+
.match(localFormattingTokens)
|
|
1658
|
+
?.map((token) => {
|
|
1659
|
+
const firstCharacter = token[0];
|
|
1660
|
+
if (firstCharacter === "L" || firstCharacter === ";") {
|
|
1661
|
+
return moment
|
|
1662
|
+
.localeData(getCurrentLocaleCode())
|
|
1663
|
+
.longDateFormat(token as LongDateFormatKey);
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
return token;
|
|
1667
|
+
})
|
|
1668
|
+
.join("")
|
|
1669
|
+
.replace(/a/gi, "(a|p)m")
|
|
1670
|
+
.toLocaleLowerCase() ?? format
|
|
1671
|
+
);
|
|
1672
|
+
};
|
|
1673
|
+
|
|
1674
|
+
export function getCurrentLocaleCode() {
|
|
1675
|
+
return locale || moment.locale();
|
|
1676
|
+
};
|
|
1677
|
+
|
|
1678
|
+
export function parseISO(isoString: string) {
|
|
1679
|
+
return moment(isoString, true);
|
|
1680
|
+
};
|
|
1681
|
+
|
|
1682
|
+
export function toISO(value: Moment) {
|
|
1683
|
+
return value.toISOString();
|
|
1684
|
+
};
|
|
1685
|
+
|
|
1686
|
+
export function parse(value: string, format: string) {
|
|
1687
|
+
if (value === "") {
|
|
1688
|
+
return null;
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
if (locale) {
|
|
1692
|
+
return moment(value, format, locale, true);
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
return moment(value, format, true);
|
|
1696
|
+
};
|
|
1697
|
+
|
|
1698
|
+
export function toJsDate(value: Moment) {
|
|
1699
|
+
return value.toDate();
|
|
1700
|
+
};
|
|
1701
|
+
|
|
1702
|
+
export function isValid(value: any) {
|
|
1703
|
+
return moment(value).isValid();
|
|
1704
|
+
};
|
|
1705
|
+
|
|
1706
|
+
export function isNull(date: Moment) {
|
|
1707
|
+
return date === null;
|
|
1708
|
+
};
|
|
1709
|
+
|
|
1710
|
+
export function getDiff(date: Moment, comparing: Moment | string, unit?: TimeUnit) {
|
|
1711
|
+
if (!moment(comparing).isValid()) {
|
|
1712
|
+
return 0;
|
|
1713
|
+
}
|
|
1714
|
+
|
|
1715
|
+
return date.diff(comparing, unit);
|
|
1716
|
+
};
|
|
1717
|
+
|
|
1718
|
+
export function isAfter(date: Moment, value: Moment) {
|
|
1719
|
+
return date.isAfter(value);
|
|
1720
|
+
};
|
|
1721
|
+
|
|
1722
|
+
export function isBefore(date: Moment, value: Moment) {
|
|
1723
|
+
return date.isBefore(value);
|
|
1724
|
+
};
|
|
1725
|
+
|
|
1726
|
+
export function isAfterDay(date: Moment, value: Moment) {
|
|
1727
|
+
return date.isAfter(value, "day");
|
|
1728
|
+
};
|
|
1729
|
+
|
|
1730
|
+
export function isBeforeDay(date: Moment, value: Moment) {
|
|
1731
|
+
return date.isBefore(value, "day");
|
|
1732
|
+
};
|
|
1733
|
+
|
|
1734
|
+
export function isBeforeMonth(date: Moment, value: Moment) {
|
|
1735
|
+
return date.isBefore(value, "month");
|
|
1736
|
+
};
|
|
1737
|
+
|
|
1738
|
+
export function isAfterMonth(date: Moment, value: Moment) {
|
|
1739
|
+
return date.isAfter(value, "month");
|
|
1740
|
+
};
|
|
1741
|
+
|
|
1742
|
+
export function isBeforeYear(date: Moment, value: Moment) {
|
|
1743
|
+
return date.isBefore(value, "year");
|
|
1744
|
+
};
|
|
1745
|
+
|
|
1746
|
+
export function isAfterYear(date: Moment, value: Moment) {
|
|
1747
|
+
return date.isAfter(value, "year");
|
|
1748
|
+
};
|
|
1749
|
+
|
|
1750
|
+
export function startOfDay(date: Moment) {
|
|
1751
|
+
return date.clone().startOf("day");
|
|
1752
|
+
};
|
|
1753
|
+
|
|
1754
|
+
export function endOfDay(date: Moment) {
|
|
1755
|
+
return date.clone().endOf("day");
|
|
1756
|
+
};
|
|
1757
|
+
|
|
1758
|
+
export function format(date: Moment, formatKey: keyof DateFormats) {
|
|
1759
|
+
return formatByString(date, formats[formatKey]);
|
|
1760
|
+
};
|
|
1761
|
+
|
|
1762
|
+
export function formatByString(date: Moment, formatString: string) {
|
|
1763
|
+
const clonedDate = date.clone();
|
|
1764
|
+
if (locale) {
|
|
1765
|
+
clonedDate.locale(locale);
|
|
1766
|
+
}
|
|
1767
|
+
return clonedDate.format(formatString);
|
|
1768
|
+
};
|
|
1769
|
+
|
|
1770
|
+
export function formatNumber(numberToFormat: string) {
|
|
1771
|
+
return numberToFormat;
|
|
1772
|
+
};
|
|
1773
|
+
|
|
1774
|
+
export function getHours(date: Moment) {
|
|
1775
|
+
return date.get("hours");
|
|
1776
|
+
};
|
|
1777
|
+
|
|
1778
|
+
export function addSeconds(date: Moment, count: number) {
|
|
1779
|
+
return count < 0
|
|
1780
|
+
? date.clone().subtract(Math.abs(count), "seconds")
|
|
1781
|
+
: date.clone().add(count, "seconds");
|
|
1782
|
+
};
|
|
1783
|
+
|
|
1784
|
+
export function addMinutes(date: Moment, count: number) {
|
|
1785
|
+
return count < 0
|
|
1786
|
+
? date.clone().subtract(Math.abs(count), "minutes")
|
|
1787
|
+
: date.clone().add(count, "minutes");
|
|
1788
|
+
};
|
|
1789
|
+
|
|
1790
|
+
export function addHours(date: Moment, count: number) {
|
|
1791
|
+
return count < 0
|
|
1792
|
+
? date.clone().subtract(Math.abs(count), "hours")
|
|
1793
|
+
: date.clone().add(count, "hours");
|
|
1794
|
+
};
|
|
1795
|
+
|
|
1796
|
+
export function addDays(date: Moment, count: number) {
|
|
1797
|
+
return count < 0
|
|
1798
|
+
? date.clone().subtract(Math.abs(count), "days")
|
|
1799
|
+
: date.clone().add(count, "days");
|
|
1800
|
+
};
|
|
1801
|
+
|
|
1802
|
+
export function addWeeks(date: Moment, count: number) {
|
|
1803
|
+
return count < 0
|
|
1804
|
+
? date.clone().subtract(Math.abs(count), "weeks")
|
|
1805
|
+
: date.clone().add(count, "weeks");
|
|
1806
|
+
};
|
|
1807
|
+
|
|
1808
|
+
export function addMonths(date: Moment, count: number) {
|
|
1809
|
+
return count < 0
|
|
1810
|
+
? date.clone().subtract(Math.abs(count), "months")
|
|
1811
|
+
: date.clone().add(count, "months");
|
|
1812
|
+
};
|
|
1813
|
+
|
|
1814
|
+
export function addYears(date: Moment, count: number) {
|
|
1815
|
+
return count < 0
|
|
1816
|
+
? date.clone().subtract(Math.abs(count), "years")
|
|
1817
|
+
: date.clone().add(count, "years");
|
|
1818
|
+
};
|
|
1819
|
+
|
|
1820
|
+
export function setHours(date: Moment, count: number) {
|
|
1821
|
+
return date.clone().hours(count);
|
|
1822
|
+
};
|
|
1823
|
+
|
|
1824
|
+
export function getMinutes(date: Moment) {
|
|
1825
|
+
return date.get("minutes");
|
|
1826
|
+
};
|
|
1827
|
+
|
|
1828
|
+
export function setMinutes(date: Moment, count: number) {
|
|
1829
|
+
return date.clone().minutes(count);
|
|
1830
|
+
};
|
|
1831
|
+
|
|
1832
|
+
export function getSeconds(date: Moment) {
|
|
1833
|
+
return date.get("seconds");
|
|
1834
|
+
};
|
|
1835
|
+
|
|
1836
|
+
export function setSeconds(date: Moment, count: number) {
|
|
1837
|
+
return date.clone().seconds(count);
|
|
1838
|
+
};
|
|
1839
|
+
|
|
1840
|
+
export function getWeek(date: Moment) {
|
|
1841
|
+
return date.get("week");
|
|
1842
|
+
};
|
|
1843
|
+
|
|
1844
|
+
export function getMonth(date: Moment) {
|
|
1845
|
+
return date.get("month");
|
|
1846
|
+
};
|
|
1847
|
+
|
|
1848
|
+
export function getDaysInMonth(date: Moment) {
|
|
1849
|
+
return date.daysInMonth();
|
|
1850
|
+
};
|
|
1851
|
+
|
|
1852
|
+
export function isSameDay(date: Moment, comparing: Moment) {
|
|
1853
|
+
return date.isSame(comparing, "day");
|
|
1854
|
+
};
|
|
1855
|
+
|
|
1856
|
+
export function isSameMonth(date: Moment, comparing: Moment) {
|
|
1857
|
+
return date.isSame(comparing, "month");
|
|
1858
|
+
};
|
|
1859
|
+
|
|
1860
|
+
export function isSameYear(date: Moment, comparing: Moment) {
|
|
1861
|
+
return date.isSame(comparing, "year");
|
|
1862
|
+
};
|
|
1863
|
+
|
|
1864
|
+
export function isSameHour(date: Moment, comparing: Moment) {
|
|
1865
|
+
return date.isSame(comparing, "hour");
|
|
1866
|
+
};
|
|
1867
|
+
|
|
1868
|
+
export function setMonth(date: Moment, count: number) {
|
|
1869
|
+
return date.clone().month(count);
|
|
1870
|
+
};
|
|
1871
|
+
|
|
1872
|
+
export function getMeridiemText(meridiem: "am" | "pm") {
|
|
1873
|
+
if (is12HourCycleInCurrentLocale()) {
|
|
1874
|
+
// AM/PM translation only possible in those who have 12 hour cycle in locale.
|
|
1875
|
+
return moment
|
|
1876
|
+
.localeData(getCurrentLocaleCode())
|
|
1877
|
+
.meridiem(meridiem === "am" ? 0 : 13, 0, false);
|
|
1878
|
+
}
|
|
1879
|
+
|
|
1880
|
+
return meridiem === "am" ? "AM" : "PM"; // fallback for de, ru, ...etc
|
|
1881
|
+
};
|
|
1882
|
+
|
|
1883
|
+
export function startOfYear(date: Moment) {
|
|
1884
|
+
return date.clone().startOf("year");
|
|
1885
|
+
};
|
|
1886
|
+
|
|
1887
|
+
export function endOfYear(date: Moment) {
|
|
1888
|
+
return date.clone().endOf("year");
|
|
1889
|
+
};
|
|
1890
|
+
|
|
1891
|
+
export function startOfMonth(date: Moment) {
|
|
1892
|
+
return date.clone().startOf("month");
|
|
1893
|
+
};
|
|
1894
|
+
|
|
1895
|
+
export function endOfMonth(date: Moment) {
|
|
1896
|
+
return date.clone().endOf("month");
|
|
1897
|
+
};
|
|
1898
|
+
|
|
1899
|
+
export function startOfWeek(date: Moment) {
|
|
1900
|
+
return date.clone().startOf("week");
|
|
1901
|
+
};
|
|
1902
|
+
|
|
1903
|
+
export function endOfWeek(date: Moment) {
|
|
1904
|
+
return date.clone().endOf("week");
|
|
1905
|
+
};
|
|
1906
|
+
|
|
1907
|
+
export function getNextMonth(date: Moment) {
|
|
1908
|
+
return date.clone().add(1, "month");
|
|
1909
|
+
};
|
|
1910
|
+
|
|
1911
|
+
export function getPreviousMonth(date: Moment) {
|
|
1912
|
+
return date.clone().subtract(1, "month");
|
|
1913
|
+
};
|
|
1914
|
+
|
|
1915
|
+
export function getMonthArray(date: Moment) {
|
|
1916
|
+
const firstMonth = date.clone().startOf("year");
|
|
1917
|
+
const monthArray = [firstMonth];
|
|
1918
|
+
|
|
1919
|
+
while (monthArray.length < 12) {
|
|
1920
|
+
const prevMonth = monthArray[monthArray.length - 1];
|
|
1921
|
+
monthArray.push(getNextMonth(prevMonth));
|
|
1922
|
+
}
|
|
1923
|
+
|
|
1924
|
+
return monthArray;
|
|
1925
|
+
};
|
|
1926
|
+
|
|
1927
|
+
export function getYear(date: Moment) {
|
|
1928
|
+
return date.get("year");
|
|
1929
|
+
};
|
|
1930
|
+
|
|
1931
|
+
export function setYear(date: Moment, year: number) {
|
|
1932
|
+
return date.clone().set("year", year);
|
|
1933
|
+
};
|
|
1934
|
+
|
|
1935
|
+
export function getDate(date: Moment) {
|
|
1936
|
+
return date.get("date");
|
|
1937
|
+
};
|
|
1938
|
+
|
|
1939
|
+
export function setDate(date: Moment, day: number) {
|
|
1940
|
+
return date.clone().set("date", day);
|
|
1941
|
+
};
|
|
1942
|
+
|
|
1943
|
+
export function mergeDateAndTime(date: Moment, time: Moment) {
|
|
1944
|
+
return date.hour(time.hour()).minute(time.minute()).second(time.second());
|
|
1945
|
+
};
|
|
1946
|
+
|
|
1947
|
+
export function getWeekdays() {
|
|
1948
|
+
return moment.weekdaysShort(true);
|
|
1949
|
+
};
|
|
1950
|
+
|
|
1951
|
+
export function isEqual(value: any, comparing: any) {
|
|
1952
|
+
if (value === null && comparing === null) {
|
|
1953
|
+
return true;
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
return moment(value).isSame(comparing);
|
|
1957
|
+
};
|
|
1958
|
+
|
|
1959
|
+
export function getWeekArray(date: Moment) {
|
|
1960
|
+
const start = date.clone().startOf("month").startOf("week");
|
|
1961
|
+
const end = date.clone().endOf("month").endOf("week");
|
|
1962
|
+
|
|
1963
|
+
let count = 0;
|
|
1964
|
+
let current = start;
|
|
1965
|
+
const nestedWeeks: Moment[][] = [];
|
|
1966
|
+
|
|
1967
|
+
while (current.isBefore(end)) {
|
|
1968
|
+
const weekNumber = Math.floor(count / 7);
|
|
1969
|
+
nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || [];
|
|
1970
|
+
nestedWeeks[weekNumber].push(current);
|
|
1971
|
+
|
|
1972
|
+
current = current.clone().add(1, "day");
|
|
1973
|
+
count += 1;
|
|
1974
|
+
}
|
|
1975
|
+
|
|
1976
|
+
return nestedWeeks;
|
|
1977
|
+
};
|
|
1978
|
+
|
|
1979
|
+
export function getYearRange(start: Moment, end: Moment) {
|
|
1980
|
+
const startDate = moment(start).startOf("year");
|
|
1981
|
+
const endDate = moment(end).endOf("year");
|
|
1982
|
+
const years: Moment[] = [];
|
|
1983
|
+
|
|
1984
|
+
let current = startDate;
|
|
1985
|
+
while (current.isBefore(endDate)) {
|
|
1986
|
+
years.push(current);
|
|
1987
|
+
current = current.clone().add(1, "year");
|
|
1988
|
+
}
|
|
1989
|
+
|
|
1990
|
+
return years;
|
|
1991
|
+
};
|
|
1992
|
+
|
|
1993
|
+
export function isWithinRange(date: Moment, [start, end]: [Moment, Moment]) {
|
|
1994
|
+
return date.isBetween(start, end, null, "[]");
|
|
1995
|
+
};
|
|
1996
|
+
|
|
1997
|
+
|
|
1998
|
+
`;
|
|
1999
|
+
}
|
|
2000
|
+
|
|
2001
|
+
//#endregion
|
|
13
2002
|
//#region src/index.ts
|
|
14
2003
|
/**
|
|
15
2004
|
* A Powerlines plugin to assist in developing other Powerlines plugins.
|
|
@@ -19,7 +2008,7 @@ function plugin(options = {}) {
|
|
|
19
2008
|
name: "date",
|
|
20
2009
|
async config() {
|
|
21
2010
|
this.debug("Providing default configuration for the Powerlines `date` build plugin.");
|
|
22
|
-
const config = { date:
|
|
2011
|
+
const config = { date: defu(options, { type: "date-fns" }) };
|
|
23
2012
|
if (!config.date.type || ![
|
|
24
2013
|
"date-fns",
|
|
25
2014
|
"dayjs",
|
|
@@ -41,17 +2030,17 @@ function plugin(options = {}) {
|
|
|
41
2030
|
let dateModule;
|
|
42
2031
|
switch (this.config.date.type) {
|
|
43
2032
|
case "dayjs":
|
|
44
|
-
dateModule =
|
|
2033
|
+
dateModule = dayjsModule;
|
|
45
2034
|
break;
|
|
46
2035
|
case "luxon":
|
|
47
|
-
dateModule =
|
|
2036
|
+
dateModule = luxonModule;
|
|
48
2037
|
break;
|
|
49
2038
|
case "moment":
|
|
50
|
-
dateModule =
|
|
2039
|
+
dateModule = momentModule;
|
|
51
2040
|
break;
|
|
52
2041
|
case "date-fns":
|
|
53
2042
|
default:
|
|
54
|
-
dateModule =
|
|
2043
|
+
dateModule = dateFnsModule;
|
|
55
2044
|
break;
|
|
56
2045
|
}
|
|
57
2046
|
await this.emitBuiltin(await Promise.resolve(dateModule(this)), "date");
|
|
@@ -61,9 +2050,9 @@ function plugin(options = {}) {
|
|
|
61
2050
|
var src_default = plugin;
|
|
62
2051
|
|
|
63
2052
|
//#endregion
|
|
64
|
-
exports.dateFnsModule =
|
|
65
|
-
exports.dayjsModule =
|
|
2053
|
+
exports.dateFnsModule = dateFnsModule;
|
|
2054
|
+
exports.dayjsModule = dayjsModule;
|
|
66
2055
|
exports.default = src_default;
|
|
67
|
-
exports.luxonModule =
|
|
68
|
-
exports.momentModule =
|
|
2056
|
+
exports.luxonModule = luxonModule;
|
|
2057
|
+
exports.momentModule = momentModule;
|
|
69
2058
|
exports.plugin = plugin;
|