@tmlmobilidade/go-types-shared 20260723.1550.37 → 20260723.1737.39
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.
|
@@ -5,9 +5,18 @@ export type CalendarDate = string & {
|
|
|
5
5
|
};
|
|
6
6
|
export declare const CalendarDateSchema: z.ZodEffects<z.ZodString, CalendarDate, string>;
|
|
7
7
|
/**
|
|
8
|
-
* This function validates if a string is a valid calendar date
|
|
9
|
-
*
|
|
10
|
-
* @param
|
|
11
|
-
* @returns The given
|
|
8
|
+
* This function validates if a string is a valid calendar date
|
|
9
|
+
* in the format `yyyy-MM-dd`.
|
|
10
|
+
* @param value The date in the format `yyyy-MM-dd` to be validated.
|
|
11
|
+
* @returns The given date as a CalendarDate string.
|
|
12
|
+
* @throws An error if the date is not a valid CalendarDate.
|
|
12
13
|
*/
|
|
13
|
-
export declare function validateCalendarDate(
|
|
14
|
+
export declare function validateCalendarDate(value: string): CalendarDate;
|
|
15
|
+
/**
|
|
16
|
+
* Transform a date in the format `yyyyMMdd`
|
|
17
|
+
* to a calendar date in the format `yyyy-MM-dd`.
|
|
18
|
+
* @param value The date in the format `yyyyMMdd` to be transformed.
|
|
19
|
+
* @returns The given date as a CalendarDate string.
|
|
20
|
+
* @throws An error if the date cannot be converted to a CalendarDate.
|
|
21
|
+
*/
|
|
22
|
+
export declare function toCalendarDate(value: number | string): CalendarDate;
|
|
@@ -7,14 +7,28 @@ export const CalendarDateSchema = z
|
|
|
7
7
|
.string()
|
|
8
8
|
.transform(validateCalendarDate);
|
|
9
9
|
/**
|
|
10
|
-
* This function validates if a string is a valid calendar date
|
|
11
|
-
*
|
|
12
|
-
* @param
|
|
13
|
-
* @returns The given
|
|
10
|
+
* This function validates if a string is a valid calendar date
|
|
11
|
+
* in the format `yyyy-MM-dd`.
|
|
12
|
+
* @param value The date in the format `yyyy-MM-dd` to be validated.
|
|
13
|
+
* @returns The given date as a CalendarDate string.
|
|
14
|
+
* @throws An error if the date is not a valid CalendarDate.
|
|
14
15
|
*/
|
|
15
|
-
export function validateCalendarDate(
|
|
16
|
-
const parsedDate = DateTime.fromFormat(
|
|
16
|
+
export function validateCalendarDate(value) {
|
|
17
|
+
const parsedDate = DateTime.fromFormat(value, CALENDAR_DATE_FORMAT);
|
|
17
18
|
if (!parsedDate.isValid)
|
|
18
|
-
throw new Error(`Invalid date format '${
|
|
19
|
-
return
|
|
19
|
+
throw new Error(`Invalid date format '${value}', expected format: ${CALENDAR_DATE_FORMAT}, explanation: ${parsedDate.invalidExplanation}`);
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Transform a date in the format `yyyyMMdd`
|
|
24
|
+
* to a calendar date in the format `yyyy-MM-dd`.
|
|
25
|
+
* @param value The date in the format `yyyyMMdd` to be transformed.
|
|
26
|
+
* @returns The given date as a CalendarDate string.
|
|
27
|
+
* @throws An error if the date cannot be converted to a CalendarDate.
|
|
28
|
+
*/
|
|
29
|
+
export function toCalendarDate(value) {
|
|
30
|
+
let formatedValue = String(value);
|
|
31
|
+
if (formatedValue.length === 8)
|
|
32
|
+
formatedValue = `${formatedValue.slice(0, 4)}-${formatedValue.slice(4, 6)}-${formatedValue.slice(6, 8)}`;
|
|
33
|
+
return validateCalendarDate(formatedValue);
|
|
20
34
|
}
|