@tmlmobilidade/go-utils-dates 20261104.2315.12 → 20261105.106.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ import { type IsoWeekday, type Month } from '@tmlmobilidade/go-types-shared';
2
+ /** Localized Gregorian month labels, January first, independent of the viewer's timezone. */
3
+ export declare function getMonthOptions(locale: string, format?: 'long' | 'narrow' | 'short'): {
4
+ label: string;
5
+ value: Month;
6
+ }[];
7
+ /** ISO weekdays, Monday first. Holiday grouping belongs to each scheduling view. */
8
+ export declare function getWeekdayOptions(locale: string, format?: 'long' | 'narrow' | 'short'): {
9
+ label: string;
10
+ value: IsoWeekday;
11
+ }[];
12
+ /** Base-language month abbreviations for compact UI labels, capitalized without a final dot. */
13
+ export declare function getCompactMonthOptions(locale: string): {
14
+ label: string;
15
+ value: Month;
16
+ }[];
17
+ /**
18
+ * Compact UI weekday labels. Base-language short forms avoid regional expansion
19
+ * such as pt-PT's full "sábado" when the UI needs "Sáb". Native labels stay available above.
20
+ */
21
+ export declare function getCompactWeekdayOptions(locale: string): {
22
+ label: string;
23
+ value: IsoWeekday;
24
+ }[];
@@ -0,0 +1,37 @@
1
+ import { Info } from 'luxon';
2
+ /** Compact option labels use an initial capital and omit the final abbreviation dot. */
3
+ function formatShortCalendarLabel(label, locale) {
4
+ const value = label.replace(/\.$/u, '');
5
+ return value.charAt(0).toLocaleUpperCase(locale) + value.slice(1);
6
+ }
7
+ /** Localized Gregorian month labels, January first, independent of the viewer's timezone. */
8
+ export function getMonthOptions(locale, format = 'short') {
9
+ return Info.months(format, { locale }).map((label, index) => ({
10
+ label,
11
+ value: (index + 1),
12
+ }));
13
+ }
14
+ /** ISO weekdays, Monday first. Holiday grouping belongs to each scheduling view. */
15
+ export function getWeekdayOptions(locale, format = 'short') {
16
+ return Info.weekdays(format, { locale }).map((label, index) => ({
17
+ label,
18
+ value: (index + 1),
19
+ }));
20
+ }
21
+ /** Base-language month abbreviations for compact UI labels, capitalized without a final dot. */
22
+ export function getCompactMonthOptions(locale) {
23
+ return getMonthOptions(new Intl.Locale(locale).language).map(option => ({
24
+ ...option,
25
+ label: formatShortCalendarLabel(option.label, locale),
26
+ }));
27
+ }
28
+ /**
29
+ * Compact UI weekday labels. Base-language short forms avoid regional expansion
30
+ * such as pt-PT's full "sábado" when the UI needs "Sáb". Native labels stay available above.
31
+ */
32
+ export function getCompactWeekdayOptions(locale) {
33
+ return getWeekdayOptions(new Intl.Locale(locale).language).map(option => ({
34
+ ...option,
35
+ label: formatShortCalendarLabel(option.label, locale),
36
+ }));
37
+ }
package/dist/dates.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { type CalendarDate, DateFormat, type OperationalDateInt, type TimezoneIdentified, type UnixMilliseconds, type UnixSeconds } from '@tmlmobilidade/go-types-shared';
2
2
  import { type DateObjectUnits, type DateTimeUnit, type DurationObjectUnits } from 'luxon';
3
+ import { type DateFormatConfig } from './date-format.js';
3
4
  interface DatesConstructor {
4
5
  calendar_date: CalendarDate;
5
6
  iso: null | string;
@@ -144,7 +145,7 @@ export declare class Dates {
144
145
  * @param format The format string (see Luxon tokens documentation)
145
146
  * @returns The date as a string in the specified format
146
147
  */
147
- toLocaleString(format: DateFormat, locale?: string): string;
148
+ toLocaleString(format: DateFormat | DateFormatConfig, locale?: string): string;
148
149
  /**
149
150
  * Returns the operational date based on the provided timestamp and format.
150
151
  * @param isoDate The ISO date string to calculate the operational date.
package/dist/dates.js CHANGED
@@ -347,7 +347,7 @@ export class Dates {
347
347
  const dateTime = DateTime.fromISO(this.iso, { setZone: true });
348
348
  if (locale)
349
349
  dateTime.setLocale(locale);
350
- const dateFormatConfig = DateFormatConfigMap[format];
350
+ const dateFormatConfig = typeof format === 'string' ? DateFormatConfigMap[format] : format;
351
351
  if (!dateFormatConfig)
352
352
  throw new Error(`Invalid date format: ${format}`);
353
353
  return dateTime.toLocaleString(dateFormatConfig, { locale: locale });
@@ -0,0 +1,16 @@
1
+ import { CalendarDate, type TimezoneIdentified } from '@tmlmobilidade/go-types-shared';
2
+ import { Dates } from '../dates.js';
3
+ export declare const DEFAULT_CAL_TZ = "Europe/Lisbon";
4
+ /**
5
+ * Canonical calendar day key (civil date) in a timezone.
6
+ * This is the ONE key you should use for calendar UI, selections, holidays, event mapping, etc.
7
+ */
8
+ export declare function calendarKey(d: Dates, tz?: TimezoneIdentified): CalendarDate;
9
+ /**
10
+ * CalendarDate -> Dates at noon in the requested timezone
11
+ */
12
+ export declare function datesFromCalendarKey(key: CalendarDate, tz?: TimezoneIdentified): Dates;
13
+ /**
14
+ * Iterate calendar day keys from start to end inclusive (civil days).
15
+ */
16
+ export declare function eachCalendarDay(start: CalendarDate, end: CalendarDate, tz?: TimezoneIdentified): CalendarDate[];
@@ -0,0 +1,39 @@
1
+ /* * */
2
+ import { DateTime } from 'luxon';
3
+ import { Dates } from '../dates.js';
4
+ /* * */
5
+ export const DEFAULT_CAL_TZ = 'Europe/Lisbon';
6
+ /* * */
7
+ /* Calendar day helpers (civil days, not operational days) */
8
+ /* * */
9
+ /**
10
+ * Canonical calendar day key (civil date) in a timezone.
11
+ * This is the ONE key you should use for calendar UI, selections, holidays, event mapping, etc.
12
+ */
13
+ export function calendarKey(d, tz = DEFAULT_CAL_TZ) {
14
+ return d.setZone(tz, 'offset_only').toFormat('yyyy-MM-dd');
15
+ }
16
+ /**
17
+ * CalendarDate -> Dates at noon in the requested timezone
18
+ */
19
+ export function datesFromCalendarKey(key, tz = DEFAULT_CAL_TZ) {
20
+ return Dates.fromFormat(`${key} 12:00`, 'yyyy-MM-dd HH:mm', tz);
21
+ }
22
+ /**
23
+ * Iterate calendar day keys from start to end inclusive (civil days).
24
+ */
25
+ export function eachCalendarDay(start, end, tz = DEFAULT_CAL_TZ) {
26
+ const out = [];
27
+ let current = DateTime.fromISO(start, { zone: tz }).startOf('day');
28
+ const last = DateTime.fromISO(end, { zone: tz }).startOf('day');
29
+ if (!current.isValid || !last.isValid) {
30
+ throw new Error(`Invalid calendar range: ${start} - ${end} (${tz})`);
31
+ }
32
+ // Retain the IANA zone while stepping days so offsets follow daylight saving.
33
+ // Comparing UTC instants is safe here because both endpoints use local midnight.
34
+ while (current.toMillis() <= last.toMillis()) {
35
+ out.push(current.toFormat('yyyy-MM-dd'));
36
+ current = current.plus({ days: 1 });
37
+ }
38
+ return out;
39
+ }
@@ -0,0 +1,31 @@
1
+ import { type OperationalDateInt } from '@tmlmobilidade/go-types-shared';
2
+ /**
3
+ * Merges two date arrays and returns a sorted, unique array.
4
+ * Duplicates are automatically removed.
5
+ *
6
+ * @param existing - Existing array of operational date integers
7
+ * @param newDates - New array of operational date integers to merge
8
+ * @returns Sorted array of unique operational date integers
9
+ *
10
+ */
11
+ export declare function mergeDateArrays(existing: OperationalDateInt[], newDates: OperationalDateInt[]): OperationalDateInt[];
12
+ /**
13
+ * Removes specified dates from an array.
14
+ * Returns a sorted array with the dates removed.
15
+ *
16
+ * @param array - Original array of operational date integers
17
+ * @param datesToRemove - Array of operational date integers to remove
18
+ * @returns Sorted array with specified dates removed
19
+ *
20
+ */
21
+ export declare function removeDatesFromArray(array: OperationalDateInt[], datesToRemove: OperationalDateInt[]): OperationalDateInt[];
22
+ /**
23
+ * Finds common dates between two arrays (intersection).
24
+ * Returns a sorted array of dates that exist in both arrays.
25
+ *
26
+ * @param array1 - First array of operational date integers
27
+ * @param array2 - Second array of operational date integers
28
+ * @returns Sorted array of dates found in both arrays
29
+ *
30
+ */
31
+ export declare function findCommonDates(array1: OperationalDateInt[], array2: OperationalDateInt[]): OperationalDateInt[];
@@ -0,0 +1,41 @@
1
+ /* * */
2
+ /* * */
3
+ /**
4
+ * Merges two date arrays and returns a sorted, unique array.
5
+ * Duplicates are automatically removed.
6
+ *
7
+ * @param existing - Existing array of operational date integers
8
+ * @param newDates - New array of operational date integers to merge
9
+ * @returns Sorted array of unique operational date integers
10
+ *
11
+ */
12
+ export function mergeDateArrays(existing, newDates) {
13
+ const uniqueDates = new Set([...existing, ...newDates]);
14
+ return Array.from(uniqueDates).sort((left, right) => left - right);
15
+ }
16
+ /**
17
+ * Removes specified dates from an array.
18
+ * Returns a sorted array with the dates removed.
19
+ *
20
+ * @param array - Original array of operational date integers
21
+ * @param datesToRemove - Array of operational date integers to remove
22
+ * @returns Sorted array with specified dates removed
23
+ *
24
+ */
25
+ export function removeDatesFromArray(array, datesToRemove) {
26
+ const removeSet = new Set(datesToRemove);
27
+ return array.filter(date => !removeSet.has(date)).sort((left, right) => left - right);
28
+ }
29
+ /**
30
+ * Finds common dates between two arrays (intersection).
31
+ * Returns a sorted array of dates that exist in both arrays.
32
+ *
33
+ * @param array1 - First array of operational date integers
34
+ * @param array2 - Second array of operational date integers
35
+ * @returns Sorted array of dates found in both arrays
36
+ *
37
+ */
38
+ export function findCommonDates(array1, array2) {
39
+ const set1 = new Set(array1);
40
+ return array2.filter(date => set1.has(date)).sort((left, right) => left - right);
41
+ }
@@ -1,8 +1,9 @@
1
- import { type OperationalDateInt } from '@tmlmobilidade/go-types-shared';
1
+ import { type OperationalDateInt, type TimezoneIdentified } from '@tmlmobilidade/go-types-shared';
2
2
  /**
3
3
  * Returns an array of individual dates from a given range of operational dates.
4
- * @param start The start date of the range, in OperationalDate format.
5
- * @param end The end date of the range, in OperationalDate format.
6
- * @returns An array of OperationalDate objects representing each date in the range.
4
+ * @param start The start date of the range, in OperationalDateInt format.
5
+ * @param end The end date of the range, in OperationalDateInt format.
6
+ * @param timezone The timezone used for calendar-day iteration.
7
+ * @returns An array of OperationalDateInt objects representing each date in the range.
7
8
  */
8
- export declare function getOperationalDatesFromRange(start: OperationalDateInt, end: OperationalDateInt): OperationalDateInt[];
9
+ export declare function getOperationalDatesFromRange(start: OperationalDateInt, end: OperationalDateInt, timezone?: TimezoneIdentified): OperationalDateInt[];
@@ -1,30 +1,18 @@
1
1
  /* * */
2
- import { Dates } from '../dates.js';
2
+ import { OperationalDateIntSchema, toCalendarDate } from '@tmlmobilidade/go-types-shared';
3
+ import { DEFAULT_CAL_TZ, eachCalendarDay } from './calendar.js';
3
4
  /**
4
5
  * Returns an array of individual dates from a given range of operational dates.
5
- * @param start The start date of the range, in OperationalDate format.
6
- * @param end The end date of the range, in OperationalDate format.
7
- * @returns An array of OperationalDate objects representing each date in the range.
6
+ * @param start The start date of the range, in OperationalDateInt format.
7
+ * @param end The end date of the range, in OperationalDateInt format.
8
+ * @param timezone The timezone used for calendar-day iteration.
9
+ * @returns An array of OperationalDateInt objects representing each date in the range.
8
10
  */
9
- export function getOperationalDatesFromRange(start, end) {
10
- //
11
- //
12
- // Validate the start and end dates
11
+ export function getOperationalDatesFromRange(start, end, timezone = DEFAULT_CAL_TZ) {
13
12
  if (end < start) {
14
13
  throw new Error(`End date "${end}" must be after start date "${start}"`);
15
14
  }
16
- //
17
- // Parse the start and end dates to ensure they are in the correct format
18
- const startDate = Dates.fromOperationalDateInt(start, 'Europe/Lisbon');
19
- const endDate = Dates.fromOperationalDateInt(end, 'Europe/Lisbon');
20
- //
21
- // Create an array to hold the individual dates and iterate
22
- // from the start date to the end date, adding each date to the array
23
- const dates = [];
24
- let current = startDate;
25
- while (current.operational_date_int <= endDate.operational_date_int) {
26
- dates.push(current.operational_date_int);
27
- current = current.plus({ days: 1 });
28
- }
29
- return dates;
15
+ // These are date labels, not instants: walk calendar days without applying a cutoff.
16
+ return eachCalendarDay(toCalendarDate(start), toCalendarDate(end), timezone)
17
+ .map(date => OperationalDateIntSchema.parse(date));
30
18
  }
@@ -1,3 +1,6 @@
1
+ export * from './calendar.js';
2
+ export * from './date-arrays.js';
1
3
  export * from './dates-from-range.js';
2
4
  export * from './from-operational-date-time-to-unix-milliseconds.js';
5
+ export * from './operational-date-range.js';
3
6
  export * from './split-time-intervals.js';
@@ -1,3 +1,6 @@
1
+ export * from './calendar.js';
2
+ export * from './date-arrays.js';
1
3
  export * from './dates-from-range.js';
2
4
  export * from './from-operational-date-time-to-unix-milliseconds.js';
5
+ export * from './operational-date-range.js';
3
6
  export * from './split-time-intervals.js';
@@ -0,0 +1,20 @@
1
+ import { type OperationalDateInt, type TimezoneIdentified } from '@tmlmobilidade/go-types-shared';
2
+ /**
3
+ * Lists every operational date touched by an inclusive range of UTC instants.
4
+ * Each instant is viewed in the requested timezone before applying the 04:00 cutoff.
5
+ * Locale does not affect day membership. Existing callers default to Europe/Lisbon.
6
+ *
7
+ * @param startDate - Start instant (inclusive)
8
+ * @param endDate - End instant (inclusive)
9
+ * @param timezone - Timezone in which the operational dates are defined
10
+ * @returns Ordered operational date labels, or an empty array for a reversed range
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const start = new Date('2026-07-01T03:00:00Z'); // 04:00 Lisbon
15
+ * const end = new Date('2026-07-03T03:00:00Z');
16
+ * buildOperationalDateRange(start, end);
17
+ * // [20260701, 20260702, 20260703]
18
+ * ```
19
+ */
20
+ export declare function buildOperationalDateRange(startDate: Date, endDate: Date, timezone?: TimezoneIdentified): OperationalDateInt[];
@@ -0,0 +1,31 @@
1
+ /* * */
2
+ import { OperationalDateIntSchema, toCalendarDate } from '@tmlmobilidade/go-types-shared';
3
+ import { Dates } from '../dates.js';
4
+ import { DEFAULT_CAL_TZ, eachCalendarDay } from './calendar.js';
5
+ /**
6
+ * Lists every operational date touched by an inclusive range of UTC instants.
7
+ * Each instant is viewed in the requested timezone before applying the 04:00 cutoff.
8
+ * Locale does not affect day membership. Existing callers default to Europe/Lisbon.
9
+ *
10
+ * @param startDate - Start instant (inclusive)
11
+ * @param endDate - End instant (inclusive)
12
+ * @param timezone - Timezone in which the operational dates are defined
13
+ * @returns Ordered operational date labels, or an empty array for a reversed range
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * const start = new Date('2026-07-01T03:00:00Z'); // 04:00 Lisbon
18
+ * const end = new Date('2026-07-03T03:00:00Z');
19
+ * buildOperationalDateRange(start, end);
20
+ * // [20260701, 20260702, 20260703]
21
+ * ```
22
+ */
23
+ export function buildOperationalDateRange(startDate, endDate, timezone = DEFAULT_CAL_TZ) {
24
+ if (endDate.getTime() < startDate.getTime())
25
+ return [];
26
+ // Change the displayed zone while preserving each input's UTC instant.
27
+ const start = Dates.fromJSDate(startDate).setZone(timezone, 'offset_only').operational_date_int;
28
+ const end = Dates.fromJSDate(endDate).setZone(timezone, 'offset_only').operational_date_int;
29
+ return eachCalendarDay(toCalendarDate(start), toCalendarDate(end), timezone)
30
+ .map(date => OperationalDateIntSchema.parse(date));
31
+ }
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
+ export * from './calendar-labels.js';
1
2
  export * from './dates.js';
2
3
  export * from './helpers/index.js';
package/dist/index.js CHANGED
@@ -1,2 +1,3 @@
1
+ export * from './calendar-labels.js';
1
2
  export * from './dates.js';
2
3
  export * from './helpers/index.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/go-utils-dates",
3
- "version": "20261104.2315.12",
3
+ "version": "20261105.106.56",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"
@@ -31,9 +31,10 @@
31
31
  "types": "./dist/index.d.ts",
32
32
  "scripts": {
33
33
  "build": "tsc && resolve-tspaths",
34
- "lint": "eslint ./src/ && tsc --noEmit",
34
+ "lint": "eslint ./src ./tests && tsc -p tsconfig.check.json --noEmit",
35
35
  "lint:fix": "eslint ./src/ --fix",
36
- "watch": "tsc-watch --onSuccess 'resolve-tspaths'"
36
+ "watch": "tsc-watch --onSuccess 'resolve-tspaths'",
37
+ "test": "node --import tsx --test tests/*.test.ts"
37
38
  },
38
39
  "dependencies": {
39
40
  "@tmlmobilidade/go-types-shared": "*",
@@ -44,6 +45,7 @@
44
45
  "@types/node": "26.6.5",
45
46
  "resolve-tspaths": "0.8.23",
46
47
  "tsc-watch": "7.2.1",
47
- "typescript": "6.0.3"
48
+ "typescript": "6.0.3",
49
+ "tsx": "4.23.15"
48
50
  }
49
51
  }