@bereasoftware/time-guard 2.5.1 → 2.5.3

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.
Files changed (53) hide show
  1. package/README.en.md +7 -4
  2. package/README.md +7 -4
  3. package/dist/calendars/index.es.js +1 -1
  4. package/dist/plugins/advanced-format.cjs +1 -1
  5. package/dist/plugins/advanced-format.cjs.map +1 -1
  6. package/dist/plugins/advanced-format.es.js +1 -53
  7. package/dist/plugins/advanced-format.es.js.map +1 -1
  8. package/dist/plugins/duration.cjs.map +1 -1
  9. package/dist/plugins/duration.es.js +1 -1
  10. package/dist/plugins/duration.es.js.map +1 -1
  11. package/dist/plugins/relative-time.cjs.map +1 -1
  12. package/dist/plugins/relative-time.es.js +1 -1
  13. package/dist/plugins/relative-time.es.js.map +1 -1
  14. package/dist/time-guard.cjs +3 -1
  15. package/dist/time-guard.cjs.map +1 -1
  16. package/dist/time-guard.es.js +11014 -3948
  17. package/dist/time-guard.es.js.map +1 -1
  18. package/dist/time-guard.iife.js +3 -1
  19. package/dist/time-guard.iife.js.map +1 -1
  20. package/dist/time-guard.umd.js +3 -1
  21. package/dist/time-guard.umd.js.map +1 -1
  22. package/dist/types/calendars/index.d.ts +1 -81
  23. package/dist/types/index.d.ts +1 -20
  24. package/dist/types/locales/index.d.ts +1 -17
  25. package/dist/types/plugins/advanced-format.d.ts +1 -0
  26. package/dist/types/plugins/duration.d.ts +1 -0
  27. package/dist/types/plugins/relative-time.d.ts +1 -0
  28. package/package.json +6 -7
  29. package/dist/types/adapters/temporal.adapter.d.ts +0 -66
  30. package/dist/types/calendars/calendar.manager.d.ts +0 -52
  31. package/dist/types/formatters/date.formatter.d.ts +0 -21
  32. package/dist/types/locales/additional.locale.d.ts +0 -5
  33. package/dist/types/locales/asian.locale.d.ts +0 -9
  34. package/dist/types/locales/english.locale.d.ts +0 -6
  35. package/dist/types/locales/european.locale.d.ts +0 -9
  36. package/dist/types/locales/locale.manager.d.ts +0 -42
  37. package/dist/types/locales/locales-data.d.ts +0 -17
  38. package/dist/types/locales/middle-eastern.locale.d.ts +0 -5
  39. package/dist/types/locales/nordic.locale.d.ts +0 -6
  40. package/dist/types/locales/romance.locale.d.ts +0 -7
  41. package/dist/types/locales/slavic.locale.d.ts +0 -6
  42. package/dist/types/locales/spanish.locale.d.ts +0 -5
  43. package/dist/types/plugins/advanced-format/index.d.ts +0 -47
  44. package/dist/types/plugins/duration/index.d.ts +0 -107
  45. package/dist/types/plugins/duration/types.d.ts +0 -85
  46. package/dist/types/plugins/index.d.ts +0 -10
  47. package/dist/types/plugins/manager.d.ts +0 -58
  48. package/dist/types/plugins/relative-time/index.d.ts +0 -39
  49. package/dist/types/plugins/relative-time/types.d.ts +0 -27
  50. package/dist/types/polyfill-loader.d.ts +0 -6
  51. package/dist/types/time-guard.d.ts +0 -426
  52. package/dist/types/types/index.d.ts +0 -578
  53. package/dist/types/utils/duration-locale.d.ts +0 -33
@@ -1,578 +0,0 @@
1
- /**
2
- * TimeGuard - Core Types and Interfaces
3
- * Following SOLID principles and TypeScript best practices
4
- *
5
- * Temporal types are provided natively by TypeScript (lib.esnext.temporal).
6
- */
7
- /**
8
- * Unit type for date/time operations
9
- */
10
- export type Unit = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond' | 'microsecond' | 'nanosecond';
11
- /**
12
- * Format preset strings for common patterns
13
- */
14
- export type FormatPreset = 'iso' | 'date' | 'time' | 'datetime' | 'rfc2822' | 'rfc3339' | 'utc';
15
- /**
16
- * Duration-like object for arithmetic operations
17
- */
18
- export interface IDuration {
19
- years?: number;
20
- months?: number;
21
- weeks?: number;
22
- days?: number;
23
- hours?: number;
24
- minutes?: number;
25
- seconds?: number;
26
- milliseconds?: number;
27
- microseconds?: number;
28
- nanoseconds?: number;
29
- }
30
- /**
31
- * Round options for precision control
32
- */
33
- export interface IRoundOptions {
34
- smallestUnit?: Unit;
35
- roundingMode?: 'ceil' | 'floor' | 'expand' | 'trunc' | 'halfExpand' | 'halfFloor' | 'halfCeil' | 'halfTrunc';
36
- roundingIncrement?: number;
37
- }
38
- /**
39
- * Duration options for normalizing time differences
40
- */
41
- export interface IDurationOptions {
42
- largestUnit?: 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond';
43
- smallestUnit?: 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' | 'millisecond';
44
- }
45
- /**
46
- * Diff result object that allows chaining with .as()
47
- * Example: tg1.diff(tg2).as('month')
48
- */
49
- export interface IDiffResult {
50
- as(unit: Unit): number;
51
- /**
52
- * Get breakdown in calendar mode (e.g., "2 months 5 days")
53
- * Only available when mode is 'calendar'
54
- */
55
- breakdown(): DurationParts | null;
56
- /**
57
- * Format the difference as a human-readable string
58
- * @param locale Locale code (e.g., 'en', 'es', 'fr')
59
- * @example
60
- * diff.format('en') // "2 months and 5 days"
61
- * diff.format('es') // "2 meses y 5 días"
62
- */
63
- format(locale?: string): string;
64
- /**
65
- * Get the mode used for this diff calculation
66
- */
67
- getMode(): 'calendar' | 'exact';
68
- }
69
- /**
70
- * Options for diff() method
71
- */
72
- export interface IDiffOptions {
73
- /**
74
- * Calculation mode:
75
- * - 'calendar': Returns months/days breakdown (e.g., 65 days → 2 months 5 days)
76
- * - 'exact': Returns exact time units (e.g., 65 days)
77
- * @default 'exact'
78
- */
79
- mode?: 'calendar' | 'exact';
80
- /**
81
- * Unit for diff when using 'exact' mode
82
- * @default 'millisecond'
83
- */
84
- unit?: Unit;
85
- /**
86
- * Locale for formatting output text
87
- * @default from TimeGuard instance config
88
- */
89
- locale?: string;
90
- }
91
- /**
92
- * Duration parts breakdown
93
- */
94
- export interface DurationParts {
95
- years: number;
96
- months: number;
97
- weeks: number;
98
- days: number;
99
- hours: number;
100
- minutes: number;
101
- seconds: number;
102
- milliseconds: number;
103
- }
104
- /**
105
- * Options for humanize() method
106
- */
107
- export interface IHumanizeOptions {
108
- /**
109
- * Locale code (e.g., 'en', 'es', 'fr')
110
- * @default from TimeGuard instance config
111
- */
112
- locale?: string;
113
- /**
114
- * Show full breakdown (e.g., "2 months and 5 days")
115
- * or just largest unit (e.g., "2 months")
116
- * @default false (largest unit only with Intl.RelativeTimeFormat style)
117
- */
118
- fullBreakdown?: boolean;
119
- /**
120
- * Numeric format: 'always', 'auto'
121
- * @default 'always'
122
- */
123
- numeric?: 'always' | 'auto';
124
- }
125
- /**
126
- * Duration result with humanize and total methods
127
- * Returned by until(), since(), and between()
128
- * Rich object for business logic: payments, metrics, analytics
129
- */
130
- export interface IDurationResult extends DurationParts {
131
- /**
132
- * Convert duration to human-readable string
133
- * @example
134
- * duration.humanize() // "2 months"
135
- * duration.humanize({ fullBreakdown: true, locale: 'es' }) // "2 meses y 5 días"
136
- */
137
- humanize(options?: IHumanizeOptions): string;
138
- /**
139
- * Get total duration in specified unit (date-fns style)
140
- * Perfect for business metrics: payments, analytics, calculations
141
- *
142
- * Conversion factors account for:
143
- * - Leap years (1 year = 365.25 days)
144
- * - Average month length (1 month = 30.4375 days)
145
- *
146
- * @example
147
- * duration.total('days') // 65
148
- * duration.total('months') // 2.166... (65 / 30.4375)
149
- * duration.total('hours') // 1560
150
- * duration.total('seconds') // 5616000
151
- *
152
- * Use cases:
153
- * - Billing calculations: `duration.total('days') * dailyRate`
154
- * - Performance metrics: `duration.total('milliseconds')`
155
- * - Report generation: `duration.total('months')`
156
- */
157
- total(unit: Unit): number;
158
- /**
159
- * String representation
160
- * Returns humanized format for display
161
- */
162
- toString(): string;
163
- /**
164
- * JSON representation
165
- * Returns breakdown as object for API responses
166
- */
167
- toJSON(): Record<string, number>;
168
- /**
169
- * Explain the calculation - killer feature for debugging and education
170
- * Returns detailed breakdown of how the duration was calculated
171
- *
172
- * Perfect for:
173
- * - Debugging complex date calculations
174
- * - Educational purposes (showing date math)
175
- * - Auditing time-based business logic
176
- *
177
- * @example
178
- * duration.explain()
179
- * // {
180
- * // input: ['2024-01-15', '2024-03-20'],
181
- * // steps: ['...step 1...', '...step 2...'],
182
- * // breakdown: { years: 0, months: 2, days: 5, ... },
183
- * // mode: 'exact',
184
- * // explanation: '...'
185
- * // }
186
- */
187
- explain(): IDurationExplanation;
188
- }
189
- /**
190
- * Duration explanation object for debugging and education
191
- * Provides transparent insight into calculation methodology
192
- * Perfect for: debugging, auditing, educational purposes
193
- *
194
- * @example
195
- * duration.explain()
196
- * // {
197
- * // input: ['2024-01-15', '2024-03-20'],
198
- * // steps: [
199
- * // 'Parsed dates: 2024-01-15 (day 15 of 365) to 2024-03-20 (day 80 of 365)',
200
- * // '2024 is a leap year (366 days)',
201
- * // 'February 2024 has 29 days',
202
- * // 'Month 1: 31 - 15 = 16 days remaining',
203
- * // 'Month 2: 29 days (full leap month)',
204
- * // 'Month 3: 1 - 20 = 20 days',
205
- * // 'Total: 16 + 29 + 20 = 65 days'
206
- * // ],
207
- * // breakdown: { years: 0, months: 2, weeks: 0, days: 5, ... },
208
- * // mode: 'exact',
209
- * // explanation: 'Calculated 2024-01-15 to 2024-03-20 including leap year adjustment'
210
- * // }
211
- */
212
- export interface IDurationExplanation {
213
- /**
214
- * Input dates as array of strings or formatted representations
215
- */
216
- input: string[];
217
- /**
218
- * Step-by-step calculation explanation
219
- * Each step is human-readable and educational
220
- * Useful for debugging complex date calculations
221
- */
222
- steps: string[];
223
- /**
224
- * Duration breakdown by component
225
- * Same as the DurationResult properties
226
- */
227
- breakdown: DurationParts;
228
- /**
229
- * Calculation mode
230
- * - 'exact': Uses Temporal API precise calculations
231
- * - 'estimated': Fallback for edge cases
232
- */
233
- mode: 'exact' | 'estimated';
234
- /**
235
- * Human-readable explanation of the entire calculation
236
- * Summarizes the approach and any special handling
237
- */
238
- explanation: string;
239
- /**
240
- * Locale used for explanation text
241
- * Supports internationalization of step descriptions
242
- */
243
- locale: string;
244
- /**
245
- * Leap year flags if applicable
246
- * Documents which years were leap years in the calculation
247
- */
248
- leapYearFlags?: {
249
- year: number;
250
- isLeap: boolean;
251
- daysInFebruary: number;
252
- }[];
253
- /**
254
- * Performance metadata
255
- * For monitoring calculation complexity
256
- */
257
- metadata?: {
258
- calculationTimeMs: number;
259
- precision: 'nanosecond' | 'microsecond' | 'millisecond' | 'second' | 'day';
260
- };
261
- }
262
- /**
263
- * Calendar system interface
264
- */
265
- export interface ICalendarSystem {
266
- id: string;
267
- name: string;
268
- locale?: string;
269
- getMonthName(month: number, short?: boolean): string;
270
- getWeekdayName(day: number, short?: boolean): string;
271
- isLeapYear(year: number): boolean;
272
- daysInMonth(year: number, month: number): number;
273
- daysInYear(year: number): number;
274
- }
275
- /**
276
- * Calendar manager interface
277
- */
278
- export interface ICalendarManager {
279
- register(calendar: ICalendarSystem): void;
280
- get(id: string): ICalendarSystem | undefined;
281
- list(): string[];
282
- setDefault(id: string): void;
283
- getDefault(): ICalendarSystem;
284
- }
285
- /**
286
- * Locale configuration interface
287
- */
288
- export interface ILocale {
289
- name: string;
290
- months: string[];
291
- monthsShort: string[];
292
- weekdays: string[];
293
- weekdaysShort: string[];
294
- weekdaysMin: string[];
295
- meridiem?: {
296
- am: string;
297
- pm: string;
298
- };
299
- formats?: Record<string, string>;
300
- }
301
- /**
302
- * Configuration options for TimeGuard instance
303
- */
304
- export interface ITimeGuardConfig {
305
- locale?: string;
306
- timezone?: string;
307
- strict?: boolean;
308
- }
309
- /**
310
- * Interface for date/time parsing strategy (Strategy Pattern)
311
- */
312
- export interface IDateParser {
313
- parse(input: unknown): any | null;
314
- canHandle(input: unknown): boolean;
315
- }
316
- /**
317
- * Interface for date/time formatting (Strategy Pattern)
318
- */
319
- export interface IDateFormatter {
320
- format(date: any, pattern: string): string;
321
- getPreset(preset: FormatPreset): string;
322
- }
323
- /**
324
- * Interface for locale management (Single Responsibility)
325
- */
326
- export interface ILocaleManager {
327
- setLocale(locale: string, data?: ILocale): void;
328
- getLocale(locale?: string): ILocale;
329
- listLocales(): string[];
330
- }
331
- /**
332
- * Interface for arithmetic operations
333
- */
334
- export interface IDateArithmetic {
335
- add(units: Partial<Record<Unit, number>> | IDuration): TimeGuard;
336
- subtract(units: Partial<Record<Unit, number>> | IDuration): TimeGuard;
337
- diff(other: TimeGuard): IDiffResult;
338
- diff(other: TimeGuard, unit: Unit): number;
339
- until(other: TimeGuard, options?: IDurationOptions): IDurationResult;
340
- since(other: TimeGuard, options?: IDurationOptions): IDurationResult;
341
- round(options: IRoundOptions): TimeGuard;
342
- }
343
- /**
344
- * Interface for query operations
345
- */
346
- export interface IDateQuery {
347
- isBefore(other: TimeGuard): boolean;
348
- isAfter(other: TimeGuard): boolean;
349
- isSame(other: TimeGuard, unit?: Unit): boolean;
350
- isBetween(start: TimeGuard, end: TimeGuard, unit?: Unit, inclusivity?: '[)' | '()' | '[]' | '(]'): boolean;
351
- }
352
- /**
353
- * Interface for manipulation operations
354
- */
355
- export interface IDateManipulation {
356
- clone(): TimeGuard;
357
- startOf(unit: Unit): TimeGuard;
358
- endOf(unit: Unit): TimeGuard;
359
- set(values: Partial<Record<Unit, number>>): TimeGuard;
360
- year(): number;
361
- month(): number;
362
- day(): number;
363
- hour(): number;
364
- minute(): number;
365
- second(): number;
366
- millisecond(): number;
367
- dayOfWeek(): number;
368
- dayOfYear(): number;
369
- weekOfYear(): number;
370
- daysInMonth(): number;
371
- daysInYear(): number;
372
- inLeapYear(): boolean;
373
- }
374
- /**
375
- * Interface for timezone operations
376
- */
377
- export interface ITimezoneAdapter {
378
- toTimezone(date: any, timezone: string): any;
379
- fromTimezone(date: any, targetTimezone: string): any;
380
- getOffset(timezone: string): number;
381
- }
382
- /**
383
- * Main TimeGuard interface (Facade Pattern)
384
- */
385
- export interface ITimeGuard extends IDateArithmetic, IDateQuery, IDateManipulation {
386
- /**
387
- * Get the underlying Temporal date object
388
- */
389
- toTemporal(): any;
390
- /**
391
- * Get as JavaScript Date (compatibility)
392
- */
393
- toDate(): Date;
394
- /**
395
- * Get as ISO string
396
- */
397
- toISOString(): string;
398
- /**
399
- * Get as Unix timestamp (milliseconds)
400
- */
401
- valueOf(): number;
402
- /**
403
- * Format the date with pattern or preset
404
- */
405
- format(pattern: string | FormatPreset): string;
406
- /**
407
- * Get accessor for components
408
- */
409
- get(component: Unit): number;
410
- /**
411
- * Locale of this instance
412
- */
413
- locale(): string;
414
- /**
415
- * Clone with new locale
416
- */
417
- locale(locale: string): TimeGuard;
418
- /**
419
- * Timezone info
420
- */
421
- timezone(): string | null;
422
- /**
423
- * Convert to another timezone
424
- */
425
- timezone(timezone: string): TimeGuard;
426
- /**
427
- * Get Unix timestamp in seconds
428
- */
429
- unix(): number;
430
- /**
431
- * Convert to JSON
432
- */
433
- toJSON(): string;
434
- /**
435
- * String representation
436
- */
437
- toString(): string;
438
- /**
439
- * Convert to PlainDate object
440
- */
441
- toPlainDate(): {
442
- year: number;
443
- month: number;
444
- day: number;
445
- dayOfWeek: number;
446
- };
447
- /**
448
- * Convert to PlainTime object
449
- */
450
- toPlainTime(): {
451
- hour: number;
452
- minute: number;
453
- second: number;
454
- millisecond: number;
455
- };
456
- /**
457
- * Get timezone offset (±HH:mm format or Z)
458
- */
459
- getOffset(): string;
460
- /**
461
- * Get timezone offset in nanoseconds
462
- */
463
- getOffsetNanoseconds(): number;
464
- /**
465
- * Get timezone ID
466
- */
467
- getTimeZoneId(): string | null;
468
- /**
469
- * Start of day
470
- */
471
- startOfDay(): TimeGuard;
472
- /**
473
- * End of day
474
- */
475
- endOfDay(): TimeGuard;
476
- /**
477
- * Duration from another date (inverse of until)
478
- */
479
- since(other: TimeGuard, options?: IDurationOptions): IDurationResult;
480
- /**
481
- * ISO 8601 duration string (P1Y2M3DT4H5M6S)
482
- */
483
- toDurationString(other?: TimeGuard): string;
484
- /**
485
- * Check if in past
486
- */
487
- isPast(): boolean;
488
- /**
489
- * Check if in future
490
- */
491
- isFuture(): boolean;
492
- /**
493
- * Check if today
494
- */
495
- isToday(): boolean;
496
- /**
497
- * Check if tomorrow
498
- */
499
- isTomorrow(): boolean;
500
- /**
501
- * Check if yesterday
502
- */
503
- isYesterday(): boolean;
504
- }
505
- /**
506
- * Plugin interface for extending functionality
507
- */
508
- export interface ITimeGuardPlugin {
509
- name: string;
510
- version: string;
511
- install(timeGuard: typeof TimeGuard, config?: unknown): void;
512
- }
513
- /**
514
- * Factory interface
515
- */
516
- export interface ITimeGuardFactory {
517
- create(input?: unknown, config?: ITimeGuardConfig): ITimeGuard;
518
- now(config?: ITimeGuardConfig): ITimeGuard;
519
- fromTemporal(date: any, config?: ITimeGuardConfig): ITimeGuard;
520
- }
521
- /**
522
- * Forward declaration for TimeGuard class
523
- * Implementation is in ./time-guard.ts, exported via ./index.ts
524
- */
525
- export declare class TimeGuard {
526
- constructor(input?: unknown, config?: ITimeGuardConfig);
527
- }
528
- /**
529
- * Forward declaration for DurationResult class
530
- * Implementation is in ./time-guard.ts, exported via ./index.ts
531
- */
532
- export declare class DurationResult implements IDurationResult {
533
- constructor(parts: DurationParts, locale?: string, metadata?: {
534
- startDate?: string;
535
- endDate?: string;
536
- steps?: string[];
537
- mode?: 'exact' | 'estimated';
538
- leapYearFlags?: Array<{
539
- year: number;
540
- isLeap: boolean;
541
- daysInFebruary: number;
542
- }>;
543
- calculationTimeMs?: number;
544
- });
545
- years: number;
546
- months: number;
547
- weeks: number;
548
- days: number;
549
- hours: number;
550
- minutes: number;
551
- seconds: number;
552
- milliseconds: number;
553
- humanize(options?: {
554
- locale?: string;
555
- fullBreakdown?: boolean;
556
- numeric?: 'always' | 'auto';
557
- }): string;
558
- total(unit: Unit): number;
559
- toString(): string;
560
- toJSON(): Record<string, number>;
561
- explain(): IDurationExplanation;
562
- }
563
- /**
564
- * Forward declaration for TimeRange class
565
- * Fluent API for date range operations with semantic naming
566
- * Implementation is in ./time-guard.ts, exported via ./index.ts
567
- */
568
- export declare class TimeRange {
569
- constructor(start: TimeGuard, end: TimeGuard);
570
- toDuration(): DurationResult;
571
- inMonths(): number;
572
- humanize(options?: {
573
- locale?: string;
574
- fullBreakdown?: boolean;
575
- numeric?: 'always' | 'auto';
576
- }): string;
577
- in(unit: Unit): number;
578
- }
@@ -1,33 +0,0 @@
1
- /**
2
- * Shared locale utilities for duration formatting
3
- * Extracted from DiffResult and DurationResult to avoid duplication
4
- */
5
- /**
6
- * Duration unit labels for different locales
7
- * Format: [singular, plural]
8
- */
9
- export declare const DURATION_UNIT_LABELS: Record<string, Record<string, [string, string]>>;
10
- /**
11
- * Conjunction words for joining duration parts
12
- */
13
- export declare const CONJUNCTION_LABELS: Record<string, string>;
14
- /**
15
- * Get duration unit label for a specific locale
16
- */
17
- export declare function getDurationUnitLabel(unit: string, locale: string, value: number): string;
18
- /**
19
- * Get conjunction word for a specific locale
20
- */
21
- export declare function getConjunctionLabel(locale: string): string;
22
- /**
23
- * Format a duration value with its unit label
24
- */
25
- export declare function formatDurationPart(value: number, unit: string, locale: string): string;
26
- /**
27
- * Join formatted duration parts with locale-appropriate conjunctions
28
- */
29
- export declare function joinDurationParts(parts: string[], locale: string): string;
30
- /**
31
- * Format a zero duration for display
32
- */
33
- export declare function formatZeroDuration(locale: string): string;