ts-time-utils 0.0.1 → 1.0.0

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 (72) hide show
  1. package/README.md +365 -1
  2. package/dist/age.d.ts +1 -10
  3. package/dist/age.d.ts.map +1 -1
  4. package/dist/constants.d.ts +2 -21
  5. package/dist/constants.d.ts.map +1 -1
  6. package/dist/constants.js +12 -13
  7. package/dist/duration.d.ts +171 -0
  8. package/dist/duration.d.ts.map +1 -0
  9. package/dist/duration.js +382 -0
  10. package/dist/esm/age.d.ts +1 -10
  11. package/dist/esm/age.d.ts.map +1 -1
  12. package/dist/esm/constants.d.ts +2 -21
  13. package/dist/esm/constants.d.ts.map +1 -1
  14. package/dist/esm/constants.js +12 -13
  15. package/dist/esm/duration.d.ts +171 -0
  16. package/dist/esm/duration.d.ts.map +1 -0
  17. package/dist/esm/duration.js +382 -0
  18. package/dist/esm/format.d.ts.map +1 -1
  19. package/dist/esm/index.d.ts +10 -6
  20. package/dist/esm/index.d.ts.map +1 -1
  21. package/dist/esm/index.js +8 -0
  22. package/dist/esm/interval.d.ts +3 -6
  23. package/dist/esm/interval.d.ts.map +1 -1
  24. package/dist/esm/locale.d.ts +94 -0
  25. package/dist/esm/locale.d.ts.map +1 -0
  26. package/dist/esm/locale.js +1087 -0
  27. package/dist/esm/performance.d.ts +2 -9
  28. package/dist/esm/performance.d.ts.map +1 -1
  29. package/dist/esm/performance.js +7 -8
  30. package/dist/esm/rangePresets.d.ts +7 -8
  31. package/dist/esm/rangePresets.d.ts.map +1 -1
  32. package/dist/esm/rangePresets.js +11 -9
  33. package/dist/esm/serialize.d.ts +73 -0
  34. package/dist/esm/serialize.d.ts.map +1 -0
  35. package/dist/esm/serialize.js +365 -0
  36. package/dist/esm/timezone.d.ts +2 -6
  37. package/dist/esm/timezone.d.ts.map +1 -1
  38. package/dist/esm/timezone.js +1 -1
  39. package/dist/esm/types.d.ts +229 -0
  40. package/dist/esm/types.d.ts.map +1 -0
  41. package/dist/esm/types.js +25 -0
  42. package/dist/esm/workingHours.d.ts +4 -13
  43. package/dist/esm/workingHours.d.ts.map +1 -1
  44. package/dist/esm/workingHours.js +3 -1
  45. package/dist/format.d.ts.map +1 -1
  46. package/dist/index.d.ts +10 -6
  47. package/dist/index.d.ts.map +1 -1
  48. package/dist/index.js +8 -0
  49. package/dist/interval.d.ts +3 -6
  50. package/dist/interval.d.ts.map +1 -1
  51. package/dist/locale.d.ts +94 -0
  52. package/dist/locale.d.ts.map +1 -0
  53. package/dist/locale.js +1087 -0
  54. package/dist/performance.d.ts +2 -9
  55. package/dist/performance.d.ts.map +1 -1
  56. package/dist/performance.js +7 -8
  57. package/dist/rangePresets.d.ts +7 -8
  58. package/dist/rangePresets.d.ts.map +1 -1
  59. package/dist/rangePresets.js +11 -9
  60. package/dist/serialize.d.ts +73 -0
  61. package/dist/serialize.d.ts.map +1 -0
  62. package/dist/serialize.js +365 -0
  63. package/dist/timezone.d.ts +2 -6
  64. package/dist/timezone.d.ts.map +1 -1
  65. package/dist/timezone.js +1 -1
  66. package/dist/types.d.ts +229 -0
  67. package/dist/types.d.ts.map +1 -0
  68. package/dist/types.js +25 -0
  69. package/dist/workingHours.d.ts +4 -13
  70. package/dist/workingHours.d.ts.map +1 -1
  71. package/dist/workingHours.js +3 -1
  72. package/package.json +39 -3
package/README.md CHANGED
@@ -8,7 +8,25 @@ A lightweight TypeScript utility library for time formatting, calculations, and
8
8
  - **⚡ Fast** - Zero dependencies, pure JavaScript functions
9
9
  - **🔧 TypeScript** - Full type safety and IntelliSense support
10
10
  - **🌳 Tree-shakable** - Import individual functions to minimize bundle size
11
- - **📚 Comprehensive** - 12 utility categories with 70+ functions
11
+ - **📚 Comprehensive** - 15 utility categories with 115+ functions
12
+
13
+ ### ⏱️ Duration utilities
14
+
15
+ - Immutable Duration class with arithmetic operations
16
+ - Create durations from various units and string formats
17
+ - Add, subtract, multiply, divide durations
18
+ - Compare durations and check relationships
19
+ - Format to human-readable strings
20
+ - Utility functions for arrays of durations
21
+
22
+ ### 💾 Serialization utilities
23
+
24
+ - Safe JSON date serialization and deserialization
25
+ - Multiple format support (ISO, epoch, object, custom)
26
+ - Automatic date reviver and replacer functions
27
+ - Timezone-aware serialization options
28
+ - Cross-platform date interchange utilities
29
+ - Validation for ISO strings and epoch timestamps
12
30
 
13
31
  ### 🎨 Format utilities
14
32
 
@@ -89,6 +107,16 @@ A lightweight TypeScript utility library for time formatting, calculations, and
89
107
  - This/last/next week, month, quarter, year
90
108
  - Rolling windows and quarter helpers
91
109
 
110
+ ### 🌍 Locale utilities
111
+
112
+ - Multi-language relative time formatting
113
+ - Locale-specific date and time formatting
114
+ - Support for 40+ locales with built-in configurations
115
+ - Auto-detection of system/browser locale
116
+ - Custom locale registration
117
+ - Internationalization (i18n) support
118
+ - **Locale conversions** - Convert between different locales and detect locale from text
119
+
92
120
  ### 🧱 Constants
93
121
 
94
122
  - Milliseconds & seconds per unit
@@ -122,10 +150,121 @@ import { createInterval, mergeIntervals } from "ts-time-utils/interval";
122
150
  import { formatInTimeZone } from "ts-time-utils/timezone";
123
151
  import { isWorkingTime, addWorkingHours } from "ts-time-utils/workingHours";
124
152
  import { today, lastNDays } from "ts-time-utils/rangePresets";
153
+ import { Duration, createDuration } from "ts-time-utils/duration";
154
+ import { serializeDate, parseJSONWithDates } from "ts-time-utils/serialize";
155
+ import {
156
+ formatRelativeTime,
157
+ formatDateLocale,
158
+ detectLocale,
159
+ } from "ts-time-utils/locale";
125
160
  ```
126
161
 
127
162
  ## 📖 Examples
128
163
 
164
+ ### Duration Utilities
165
+
166
+ ```ts
167
+ import {
168
+ Duration,
169
+ createDuration,
170
+ formatDurationString,
171
+ } from "ts-time-utils/duration";
172
+
173
+ // Create durations
174
+ const duration1 = Duration.fromHours(2.5); // 2.5 hours
175
+ const duration2 = new Duration({ hours: 1, minutes: 30 }); // 1.5 hours
176
+ const duration3 = Duration.fromString("1h 30m 45s"); // Parse from string
177
+ const duration4 = Duration.between(startDate, endDate); // From date range
178
+
179
+ // Arithmetic operations
180
+ const sum = duration1.add(duration2); // 4 hours
181
+ const diff = duration1.subtract(duration2); // 1 hour
182
+ const doubled = duration1.multiply(2); // 5 hours
183
+ const half = duration1.divide(2); // 1.25 hours
184
+
185
+ // Comparisons
186
+ duration1.equals(duration2); // false
187
+ duration1.greaterThan(duration2); // true
188
+ duration1.compareTo(duration2); // 1
189
+
190
+ // Conversions and formatting
191
+ duration1.hours; // 2.5
192
+ duration1.minutes; // 150
193
+ duration1.toString(); // "2h 30m"
194
+ formatDurationString(duration1, { long: true }); // "2 hours, 30 minutes"
195
+
196
+ // Utility functions with arrays
197
+ const durations = [duration1, duration2, duration3];
198
+ const max = maxDuration(...durations);
199
+ const total = sumDurations(...durations);
200
+ const average = averageDuration(...durations);
201
+ ```
202
+
203
+ ### Serialization Utilities
204
+
205
+ ```ts
206
+ import {
207
+ serializeDate,
208
+ deserializeDate,
209
+ parseJSONWithDates,
210
+ stringifyWithDates,
211
+ toEpochTimestamp,
212
+ fromEpochTimestamp,
213
+ toDateObject,
214
+ fromDateObject,
215
+ } from "ts-time-utils/serialize";
216
+
217
+ // Serialize dates in different formats
218
+ const date = new Date("2025-09-14T12:30:45.123Z");
219
+
220
+ const isoString = serializeDate(date, { format: "iso" }); // "2025-09-14T12:30:45.123Z"
221
+ const epochMs = serializeDate(date, { format: "epoch" }); // 1757853045123
222
+ const dateObj = serializeDate(date, { format: "object" }); // {year: 2025, month: 9, ...}
223
+ const custom = serializeDate(date, {
224
+ format: "custom",
225
+ customFormat: "YYYY-MM-DD HH:mm:ss",
226
+ }); // "2025-09-14 12:30:45"
227
+
228
+ // Deserialize from various formats
229
+ const fromISO = deserializeDate("2025-09-14T12:30:45.123Z");
230
+ const fromEpoch = deserializeDate(1757853045123);
231
+ const fromObj = deserializeDate({
232
+ year: 2025,
233
+ month: 9,
234
+ day: 14,
235
+ hour: 12,
236
+ minute: 30,
237
+ second: 45,
238
+ millisecond: 123,
239
+ });
240
+
241
+ // Safe JSON handling with automatic date conversion
242
+ const data = {
243
+ name: "User",
244
+ createdAt: new Date(),
245
+ updatedAt: new Date(),
246
+ metadata: "other data",
247
+ };
248
+
249
+ // Stringify with automatic date serialization
250
+ const jsonString = stringifyWithDates(data, ["createdAt", "updatedAt"], {
251
+ format: "epoch",
252
+ });
253
+ // {"name":"User","createdAt":1757853045123,"updatedAt":1757853045123,"metadata":"other data"}
254
+
255
+ // Parse with automatic date restoration
256
+ const parsed = parseJSONWithDates(jsonString, ["createdAt", "updatedAt"]);
257
+ // parsed.createdAt and parsed.updatedAt are Date objects
258
+
259
+ // Epoch timestamp utilities
260
+ const timestamp = toEpochTimestamp(date, "seconds"); // 1757853045
261
+ const restoredDate = fromEpochTimestamp(timestamp, "seconds");
262
+
263
+ // Date object utilities (UTC-based)
264
+ const dateObject = toDateObject(date, true); // includes timezone
265
+ const reconstructed = fromDateObject(dateObject);
266
+ ```
267
+
129
268
  ### Format Utilities
130
269
 
131
270
  ```ts
@@ -289,8 +428,198 @@ const week = thisWeek();
289
428
  const quarter = quarterRange();
290
429
  ```
291
430
 
431
+ ### Locale Utilities
432
+
433
+ ```ts
434
+ import {
435
+ formatRelativeTime,
436
+ formatDateLocale,
437
+ formatTimeLocale,
438
+ formatDateTimeLocale,
439
+ registerLocale,
440
+ getLocaleConfig,
441
+ detectLocale,
442
+ getSupportedLocales,
443
+ } from "ts-time-utils/locale";
444
+
445
+ // Relative time formatting in multiple languages
446
+ const pastDate = new Date(Date.now() - 2 * 60 * 60 * 1000); // 2 hours ago
447
+ formatRelativeTime(pastDate, { locale: "en" }); // "2 hours ago"
448
+ formatRelativeTime(pastDate, { locale: "es" }); // "hace 2 horas"
449
+ formatRelativeTime(pastDate, { locale: "fr" }); // "il y a 2 heures"
450
+ formatRelativeTime(pastDate, { locale: "de" }); // "vor 2 Stunden"
451
+ formatRelativeTime(pastDate, { locale: "nl" }); // "2 uur geleden"
452
+ formatRelativeTime(pastDate, { locale: "it" }); // "2 ore fa"
453
+ formatRelativeTime(pastDate, { locale: "zh" }); // "2小时前"
454
+ formatRelativeTime(pastDate, { locale: "ja" }); // "2時間前"
455
+ formatRelativeTime(pastDate, { locale: "fa" }); // "2 ساعت پیش"
456
+
457
+ // Future dates
458
+ const futureDate = new Date(Date.now() + 3 * 24 * 60 * 60 * 1000);
459
+ formatRelativeTime(futureDate, { locale: "en" }); // "in 3 days"
460
+ formatRelativeTime(futureDate, { locale: "es" }); // "en 3 días"
461
+ formatRelativeTime(futureDate, { locale: "nl" }); // "over 3 dagen"
462
+ formatRelativeTime(futureDate, { locale: "it" }); // "tra 3 giorni"
463
+ formatRelativeTime(futureDate, { locale: "fa" }); // "3 روز دیگر"
464
+
465
+ // Relative time options
466
+ formatRelativeTime(pastDate, {
467
+ locale: "en",
468
+ maxUnit: "days", // Don't use units larger than days
469
+ minUnit: "minutes", // Don't use units smaller than minutes
470
+ precision: 1, // Show 1 decimal place: "2.0 hours ago"
471
+ short: true, // Use abbreviated format: "2h ago"
472
+ numeric: "auto", // Use words when appropriate: "yesterday"
473
+ });
474
+
475
+ // Date formatting
476
+ const date = new Date("2024-01-15T14:30:45Z");
477
+ formatDateLocale(date, "en", "medium"); // "Jan 15, 2024"
478
+ formatDateLocale(date, "es", "medium"); // "15 ene 2024"
479
+ formatDateLocale(date, "fr", "long"); // "15 janvier 2024"
480
+ formatDateLocale(date, "de", "short"); // "15.1.2024"
481
+
482
+ // Time formatting
483
+ formatTimeLocale(date, "en", "short"); // "2:30 PM"
484
+ formatTimeLocale(date, "de", "medium"); // "14:30:45"
485
+ formatTimeLocale(date, "fr", "long"); // "14:30:45 UTC"
486
+
487
+ // Combined date and time
488
+ formatDateTimeLocale(date, "en"); // "Jan 15, 2024 2:30:45 PM"
489
+
490
+ // Auto-detect locale from browser/system
491
+ const userLocale = detectLocale(); // e.g., 'en-US' or 'fr-FR'
492
+ formatRelativeTime(pastDate, { locale: userLocale });
493
+
494
+ // Get supported locales
495
+ const locales = getSupportedLocales();
496
+ // ['en', 'es', 'fr', 'de', 'zh', 'ja', ...]
497
+
498
+ // Register custom locale
499
+ registerLocale("custom", {
500
+ locale: "custom",
501
+ dateFormats: {
502
+ short: "M/d/yyyy",
503
+ medium: "MMM d, yyyy",
504
+ long: "MMMM d, yyyy",
505
+ full: "EEEE, MMMM d, yyyy",
506
+ },
507
+ timeFormats: {
508
+ short: "h:mm a",
509
+ medium: "h:mm:ss a",
510
+ long: "h:mm:ss a z",
511
+ full: "h:mm:ss a zzzz",
512
+ },
513
+ relativeTime: {
514
+ future: "in {0}",
515
+ past: "{0} ago",
516
+ units: {
517
+ second: "sec",
518
+ seconds: "secs",
519
+ minute: "min",
520
+ minutes: "mins",
521
+ hour: "hr",
522
+ hours: "hrs",
523
+ day: "day",
524
+ days: "days",
525
+ week: "wk",
526
+ weeks: "wks",
527
+ month: "mo",
528
+ months: "mos",
529
+ year: "yr",
530
+ years: "yrs",
531
+ },
532
+ },
533
+ calendar: {
534
+ weekStartsOn: 0, // Sunday
535
+ monthNames: ["Jan", "Feb", "Mar" /* ... */],
536
+ monthNamesShort: ["J", "F", "M" /* ... */],
537
+ dayNames: ["Sun", "Mon", "Tue" /* ... */],
538
+ dayNamesShort: ["S", "M", "T" /* ... */],
539
+ },
540
+ numbers: {
541
+ decimal: ".",
542
+ thousands: ",",
543
+ },
544
+ });
545
+
546
+ // Locale Conversion Utilities
547
+ import {
548
+ convertRelativeTime,
549
+ detectLocaleFromRelativeTime,
550
+ convertFormatPattern,
551
+ convertFormattedDate,
552
+ convertRelativeTimeArray,
553
+ compareLocaleFormats,
554
+ } from "ts-time-utils/locale";
555
+
556
+ // Convert relative time between locales
557
+ convertRelativeTime("2 hours ago", "en", "es"); // "hace 2 horas"
558
+ convertRelativeTime("hace 3 días", "es", "fr"); // "il y a 3 jours"
559
+ convertRelativeTime("2h ago", "en", "de"); // "vor 2h"
560
+ convertRelativeTime("2 hours ago", "en", "nl"); // "2 uur geleden"
561
+ convertRelativeTime("2 hours ago", "en", "it"); // "2 ore fa"
562
+ convertRelativeTime("2 hours ago", "en", "fa"); // "2 ساعت پیش"
563
+ convertRelativeTime("2 ساعت پیش", "fa", "en"); // "2 hours ago"
564
+
565
+ // Detect locale from formatted text
566
+ detectLocaleFromRelativeTime("2 hours ago"); // "en"
567
+ detectLocaleFromRelativeTime("hace 2 horas"); // "es"
568
+ detectLocaleFromRelativeTime("il y a 2 heures"); // "fr"
569
+ detectLocaleFromRelativeTime("2 uur geleden"); // "nl"
570
+ detectLocaleFromRelativeTime("2 ore fa"); // "it"
571
+ detectLocaleFromRelativeTime("2 ساعت پیش"); // "fa"
572
+ detectLocaleFromRelativeTime("vor 2 Stunden"); // "de"
573
+
574
+ // Convert date format patterns between locales
575
+ convertFormatPattern("M/d/yyyy", "en", "de"); // "dd.MM.yyyy"
576
+ convertFormatPattern("MMM d, yyyy", "en", "fr", "long"); // "d MMMM yyyy"
577
+
578
+ // Convert formatted dates between locales
579
+ convertFormattedDate("Jan 15, 2024", "en", "es"); // "15 ene 2024"
580
+ convertFormattedDate("15. Januar 2024", "de", "en"); // "Jan 15, 2024"
581
+
582
+ // Bulk conversion of relative time arrays
583
+ const englishTimes = ["2 hours ago", "in 3 days", "1 week ago"];
584
+ convertRelativeTimeArray(englishTimes, "en", "es");
585
+ // ["hace 2 horas", "en 3 días", "hace 1 semana"]
586
+
587
+ // Compare format differences between locales
588
+ const comparison = compareLocaleFormats("en", "de");
589
+ console.log(comparison.dateFormats.short);
590
+ // { locale1: "M/d/yyyy", locale2: "dd.MM.yyyy" }
591
+ console.log(comparison.weekStartsOn);
592
+ // { locale1: 0, locale2: 1 } // Sunday vs Monday
593
+ ```
594
+
292
595
  ## 📊 API Reference
293
596
 
597
+ ### Duration Functions
598
+
599
+ - `Duration` class - Immutable duration with full arithmetic support
600
+ - `createDuration(input)` - Create duration from number, object, or string
601
+ - `Duration.fromHours/Minutes/Seconds/Days/Weeks(n)` - Create from specific units
602
+ - `Duration.fromString(str)` - Parse from string like "1h 30m 45s"
603
+ - `Duration.between(start, end)` - Create from date range
604
+ - `duration.add/subtract/multiply/divide()` - Arithmetic operations
605
+ - `duration.equals/greaterThan/lessThan()` - Comparison methods
606
+ - `formatDurationString(duration, options?)` - Format to readable string
607
+ - `maxDuration/minDuration(...durations)` - Find extremes
608
+ - `sumDurations/averageDuration(...durations)` - Aggregate operations
609
+
610
+ ### Serialization Functions
611
+
612
+ - `serializeDate(date, options?)` - Serialize date to various formats (ISO, epoch, object, custom)
613
+ - `deserializeDate(serialized, options?)` - Deserialize from string, number, or object
614
+ - `parseJSONWithDates(jsonString, dateKeys?, options?)` - Parse JSON with automatic date conversion
615
+ - `stringifyWithDates(obj, dateKeys?, options?)` - Stringify JSON with automatic date serialization
616
+ - `createDateReviver/createDateReplacer(dateKeys?, options?)` - Create JSON reviver/replacer functions
617
+ - `toEpochTimestamp/fromEpochTimestamp(input, precision?)` - Convert to/from epoch timestamps
618
+ - `toDateObject/fromDateObject(input)` - Convert to/from safe object representation
619
+ - `isValidISODateString/isValidEpochTimestamp(input)` - Validation utilities
620
+ - `cloneDate(date)` - Safe date cloning
621
+ - `datesEqual(date1, date2, precision?)` - Compare dates with precision control
622
+
294
623
  ### Format Functions
295
624
 
296
625
  - `formatDuration(ms, options?)` - Format milliseconds to readable duration
@@ -319,6 +648,41 @@ const quarter = quarterRange();
319
648
  - `isValidTimeString(time)` - Validate HH:MM time format
320
649
  - `isValidISOString(dateString)` - Validate ISO 8601 date string
321
650
 
651
+ ### Locale Functions
652
+
653
+ - `formatRelativeTime(date, options?)` - Format relative time with locale support
654
+ - Options: `locale`, `maxUnit`, `minUnit`, `precision`, `short`, `numeric`, `style`
655
+ - Supports 30+ locales: en, es, fr, de, it, pt, nl, sv, da, no, fi, pl, cs, sk, hu, ro, bg, hr, sl, et, lv, lt, ru, uk, tr, ar, he, hi, th, ko, zh, ja
656
+ - `formatDateLocale(date, locale?, style?)` - Format date in locale-specific format
657
+ - Styles: 'short', 'medium', 'long', 'full'
658
+ - `formatTimeLocale(date, locale?, style?)` - Format time in locale-specific format
659
+ - `formatDateTimeLocale(date, locale?, dateStyle?, timeStyle?)` - Format both date and time
660
+ - `registerLocale(locale, config)` - Register a custom locale configuration
661
+ - `getLocaleConfig(locale)` - Get configuration for a specific locale
662
+ - `detectLocale(fallback?)` - Auto-detect system/browser locale
663
+ - `getSupportedLocales()` - Get array of all supported locale codes
664
+ - `getMonthNames(locale?, short?)` - Get localized month names
665
+ - `getDayNames(locale?, short?)` - Get localized day names
666
+ - `getBestMatchingLocale(preferences, fallback?)` - Find best matching locale from preferences
667
+
668
+ #### Locale Conversion Functions
669
+
670
+ - `convertRelativeTime(text, fromLocale, toLocale)` - Convert relative time between locales
671
+ - Example: `convertRelativeTime("2 hours ago", "en", "es")` → `"hace 2 horas"`
672
+ - Example: `convertRelativeTime("2 hours ago", "en", "nl")` → `"2 uur geleden"`
673
+ - Example: `convertRelativeTime("2 hours ago", "en", "it")` → `"2 ore fa"`
674
+ - Example: `convertRelativeTime("2 hours ago", "en", "fa")` → `"2 ساعت پیش"`
675
+ - `detectLocaleFromRelativeTime(text)` - Detect locale from relative time string
676
+ - Returns most likely locale or null if detection fails
677
+ - `convertFormatPattern(pattern, fromLocale, toLocale, style?)` - Convert date format patterns
678
+ - Maps common patterns between locales or uses target locale's style
679
+ - `convertFormattedDate(formattedDate, fromLocale, toLocale, targetStyle?)` - Convert formatted dates
680
+ - Parses date in source locale and reformats in target locale
681
+ - `convertRelativeTimeArray(array, fromLocale, toLocale)` - Bulk convert relative time arrays
682
+ - Returns array with same length, null for unparseable strings
683
+ - `compareLocaleFormats(locale1, locale2)` - Compare format differences between locales
684
+ - Returns object with dateFormats, timeFormats, and weekStartsOn comparisons
685
+
322
686
  ## 🛠️ Development
323
687
 
324
688
  ```bash
package/dist/age.d.ts CHANGED
@@ -1,14 +1,5 @@
1
1
  import { TimeUnit } from './constants.js';
2
- /**
3
- * Age calculation result
4
- */
5
- export interface AgeResult {
6
- years: number;
7
- months: number;
8
- days: number;
9
- totalDays: number;
10
- totalMonths: number;
11
- }
2
+ import type { AgeResult } from './types.js';
12
3
  /**
13
4
  * Calculate detailed age from birth date
14
5
  * @param birthDate - date of birth
package/dist/age.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"age.d.ts","sourceRoot":"","sources":["../src/age.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE1C;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,SAAS,EAAE,IAAI,EAAE,aAAa,GAAE,IAAiB,GAAG,SAAS,CAyBzF;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,IAAI,EACf,IAAI,EAAE,QAAQ,EACd,aAAa,GAAE,IAAiB,GAC/B,MAAM,CAsBR;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAC1B,SAAS,EAAE,IAAI,EACf,aAAa,GAAE,IAAiB,GAC/B,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAQlD;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,IAAI,EAAE,aAAa,GAAE,IAAiB,GAAG,IAAI,CAYvF;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,IAAI,EAAE,aAAa,GAAE,IAAiB,GAAG,MAAM,CAG9F;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,SAAS,EAAE,IAAI,EAAE,aAAa,GAAE,IAAiB,GAAG,OAAO,CAQrF"}
1
+ {"version":3,"file":"age.d.ts","sourceRoot":"","sources":["../src/age.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,SAAS,EAAE,IAAI,EAAE,aAAa,GAAE,IAAiB,GAAG,SAAS,CAyBzF;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,IAAI,EACf,IAAI,EAAE,QAAQ,EACd,aAAa,GAAE,IAAiB,GAC/B,MAAM,CAsBR;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAC1B,SAAS,EAAE,IAAI,EACf,aAAa,GAAE,IAAiB,GAC/B,QAAQ,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,CAQlD;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,IAAI,EAAE,aAAa,GAAE,IAAiB,GAAG,IAAI,CAYvF;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,IAAI,EAAE,aAAa,GAAE,IAAiB,GAAG,MAAM,CAG9F;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,SAAS,EAAE,IAAI,EAAE,aAAa,GAAE,IAAiB,GAAG,OAAO,CAQrF"}
@@ -1,6 +1,7 @@
1
1
  /**
2
- * Time constants for millisecond conversions
2
+ * Constants for time conversions and utility types
3
3
  */
4
+ export type { TimeUnit, FormatOptions } from './types.js';
4
5
  export declare const MILLISECONDS_PER_SECOND = 1000;
5
6
  export declare const MILLISECONDS_PER_MINUTE: number;
6
7
  export declare const MILLISECONDS_PER_HOUR: number;
@@ -8,28 +9,8 @@ export declare const MILLISECONDS_PER_DAY: number;
8
9
  export declare const MILLISECONDS_PER_WEEK: number;
9
10
  export declare const MILLISECONDS_PER_MONTH: number;
10
11
  export declare const MILLISECONDS_PER_YEAR: number;
11
- /**
12
- * Second-based constants
13
- */
14
12
  export declare const SECONDS_PER_MINUTE = 60;
15
13
  export declare const SECONDS_PER_HOUR: number;
16
14
  export declare const SECONDS_PER_DAY: number;
17
15
  export declare const SECONDS_PER_WEEK: number;
18
- /**
19
- * Common time units
20
- */
21
- export type TimeUnit = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks' | 'months' | 'years';
22
- /**
23
- * Formatting options
24
- */
25
- export interface FormatOptions {
26
- /** Include milliseconds in output */
27
- includeMs?: boolean;
28
- /** Use short format (e.g., "1h" vs "1 hour") */
29
- short?: boolean;
30
- /** Maximum number of units to show */
31
- maxUnits?: number;
32
- /** Round to nearest unit instead of floor */
33
- round?: boolean;
34
- }
35
16
  //# sourceMappingURL=constants.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,uBAAuB,OAAO,CAAC;AAC5C,eAAO,MAAM,uBAAuB,QAA+B,CAAC;AACpE,eAAO,MAAM,qBAAqB,QAA+B,CAAC;AAClE,eAAO,MAAM,oBAAoB,QAA6B,CAAC;AAC/D,eAAO,MAAM,qBAAqB,QAA2B,CAAC;AAC9D,eAAO,MAAM,sBAAsB,QAA4B,CAAC;AAChE,eAAO,MAAM,qBAAqB,QAA6B,CAAC;AAEhE;;GAEG;AACH,eAAO,MAAM,kBAAkB,KAAK,CAAC;AACrC,eAAO,MAAM,gBAAgB,QAA0B,CAAC;AACxD,eAAO,MAAM,eAAe,QAAwB,CAAC;AACrD,eAAO,MAAM,gBAAgB,QAAsB,CAAC;AAEpD;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,cAAc,GAAG,SAAS,GAAG,SAAS,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,GAAG,QAAQ,GAAG,OAAO,CAAC;AAEhH;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,qCAAqC;IACrC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,gDAAgD;IAChD,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,sCAAsC;IACtC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,6CAA6C;IAC7C,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB"}
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,YAAY,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAG1D,eAAO,MAAM,uBAAuB,OAAO,CAAC;AAC5C,eAAO,MAAM,uBAAuB,QAAY,CAAC;AACjD,eAAO,MAAM,qBAAqB,QAAiB,CAAC;AACpD,eAAO,MAAM,oBAAoB,QAAsB,CAAC;AACxD,eAAO,MAAM,qBAAqB,QAA0B,CAAC;AAC7D,eAAO,MAAM,sBAAsB,QAA2B,CAAC;AAC/D,eAAO,MAAM,qBAAqB,QAA4B,CAAC;AAG/D,eAAO,MAAM,kBAAkB,KAAK,CAAC;AACrC,eAAO,MAAM,gBAAgB,QAAU,CAAC;AACxC,eAAO,MAAM,eAAe,QAAe,CAAC;AAC5C,eAAO,MAAM,gBAAgB,QAAmB,CAAC"}
package/dist/constants.js CHANGED
@@ -1,17 +1,16 @@
1
1
  /**
2
- * Time constants for millisecond conversions
2
+ * Constants for time conversions and utility types
3
3
  */
4
+ // Milliseconds per unit
4
5
  export const MILLISECONDS_PER_SECOND = 1000;
5
- export const MILLISECONDS_PER_MINUTE = 60 * MILLISECONDS_PER_SECOND;
6
- export const MILLISECONDS_PER_HOUR = 60 * MILLISECONDS_PER_MINUTE;
7
- export const MILLISECONDS_PER_DAY = 24 * MILLISECONDS_PER_HOUR;
8
- export const MILLISECONDS_PER_WEEK = 7 * MILLISECONDS_PER_DAY;
9
- export const MILLISECONDS_PER_MONTH = 30 * MILLISECONDS_PER_DAY; // Approximate
10
- export const MILLISECONDS_PER_YEAR = 365 * MILLISECONDS_PER_DAY; // Approximate
11
- /**
12
- * Second-based constants
13
- */
6
+ export const MILLISECONDS_PER_MINUTE = 60 * 1000;
7
+ export const MILLISECONDS_PER_HOUR = 60 * 60 * 1000;
8
+ export const MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
9
+ export const MILLISECONDS_PER_WEEK = 7 * 24 * 60 * 60 * 1000;
10
+ export const MILLISECONDS_PER_MONTH = 30 * 24 * 60 * 60 * 1000; // Approximate
11
+ export const MILLISECONDS_PER_YEAR = 365 * 24 * 60 * 60 * 1000; // Approximate
12
+ // Seconds per unit
14
13
  export const SECONDS_PER_MINUTE = 60;
15
- export const SECONDS_PER_HOUR = 60 * SECONDS_PER_MINUTE;
16
- export const SECONDS_PER_DAY = 24 * SECONDS_PER_HOUR;
17
- export const SECONDS_PER_WEEK = 7 * SECONDS_PER_DAY;
14
+ export const SECONDS_PER_HOUR = 60 * 60;
15
+ export const SECONDS_PER_DAY = 24 * 60 * 60;
16
+ export const SECONDS_PER_WEEK = 7 * 24 * 60 * 60;
@@ -0,0 +1,171 @@
1
+ import type { DurationInput, DurationComparison } from './types.js';
2
+ /**
3
+ * Represents a duration of time with arithmetic and conversion capabilities
4
+ */
5
+ export declare class Duration {
6
+ private readonly _milliseconds;
7
+ constructor(input: number | DurationInput | string);
8
+ /**
9
+ * Create Duration from milliseconds
10
+ */
11
+ static fromMilliseconds(ms: number): Duration;
12
+ /**
13
+ * Create Duration from seconds
14
+ */
15
+ static fromSeconds(seconds: number): Duration;
16
+ /**
17
+ * Create Duration from minutes
18
+ */
19
+ static fromMinutes(minutes: number): Duration;
20
+ /**
21
+ * Create Duration from hours
22
+ */
23
+ static fromHours(hours: number): Duration;
24
+ /**
25
+ * Create Duration from days
26
+ */
27
+ static fromDays(days: number): Duration;
28
+ /**
29
+ * Create Duration from weeks
30
+ */
31
+ static fromWeeks(weeks: number): Duration;
32
+ /**
33
+ * Create Duration from a string (e.g., "1h 30m", "2.5 hours", "90 seconds")
34
+ */
35
+ static fromString(str: string): Duration;
36
+ /**
37
+ * Create Duration between two dates
38
+ */
39
+ static between(start: Date, end: Date): Duration;
40
+ /**
41
+ * Get duration in milliseconds
42
+ */
43
+ get milliseconds(): number;
44
+ /**
45
+ * Get duration in seconds
46
+ */
47
+ get seconds(): number;
48
+ /**
49
+ * Get duration in minutes
50
+ */
51
+ get minutes(): number;
52
+ /**
53
+ * Get duration in hours
54
+ */
55
+ get hours(): number;
56
+ /**
57
+ * Get duration in days
58
+ */
59
+ get days(): number;
60
+ /**
61
+ * Get duration in weeks
62
+ */
63
+ get weeks(): number;
64
+ /**
65
+ * Add another duration
66
+ */
67
+ add(other: Duration | number | DurationInput): Duration;
68
+ /**
69
+ * Subtract another duration
70
+ */
71
+ subtract(other: Duration | number | DurationInput): Duration;
72
+ /**
73
+ * Multiply duration by a factor
74
+ */
75
+ multiply(factor: number): Duration;
76
+ /**
77
+ * Divide duration by a factor
78
+ */
79
+ divide(factor: number): Duration;
80
+ /**
81
+ * Get absolute duration (always positive)
82
+ */
83
+ abs(): Duration;
84
+ /**
85
+ * Negate duration
86
+ */
87
+ negate(): Duration;
88
+ /**
89
+ * Check if duration equals another
90
+ */
91
+ equals(other: Duration | number): boolean;
92
+ /**
93
+ * Check if duration is greater than another
94
+ */
95
+ greaterThan(other: Duration | number): boolean;
96
+ /**
97
+ * Check if duration is less than another
98
+ */
99
+ lessThan(other: Duration | number): boolean;
100
+ /**
101
+ * Compare with another duration (-1, 0, 1)
102
+ */
103
+ compareTo(other: Duration): DurationComparison;
104
+ /**
105
+ * Check if duration is zero
106
+ */
107
+ isZero(): boolean;
108
+ /**
109
+ * Check if duration is positive
110
+ */
111
+ isPositive(): boolean;
112
+ /**
113
+ * Check if duration is negative
114
+ */
115
+ isNegative(): boolean;
116
+ /**
117
+ * Convert to human-readable string
118
+ */
119
+ toString(): string;
120
+ /**
121
+ * Convert to detailed object representation
122
+ */
123
+ toObject(): Required<DurationInput>;
124
+ /**
125
+ * Convert to JSON representation
126
+ */
127
+ toJSON(): number;
128
+ /**
129
+ * Create Duration from JSON
130
+ */
131
+ static fromJSON(ms: number): Duration;
132
+ private calculateMilliseconds;
133
+ private parseString;
134
+ private normalizeToDuration;
135
+ }
136
+ /**
137
+ * Create a new Duration instance
138
+ */
139
+ export declare function createDuration(input: number | DurationInput | string): Duration;
140
+ /**
141
+ * Check if a value is a valid duration
142
+ */
143
+ export declare function isValidDuration(value: any): value is Duration;
144
+ /**
145
+ * Parse duration from various formats
146
+ */
147
+ export declare function parseDurationString(input: string | number | DurationInput): Duration;
148
+ /**
149
+ * Format duration to human-readable string
150
+ */
151
+ export declare function formatDurationString(duration: Duration | number, options?: {
152
+ long?: boolean;
153
+ precision?: number;
154
+ }): string;
155
+ /**
156
+ * Get the maximum duration from an array
157
+ */
158
+ export declare function maxDuration(...durations: Duration[]): Duration | null;
159
+ /**
160
+ * Get the minimum duration from an array
161
+ */
162
+ export declare function minDuration(...durations: Duration[]): Duration | null;
163
+ /**
164
+ * Sum multiple durations
165
+ */
166
+ export declare function sumDurations(...durations: Duration[]): Duration;
167
+ /**
168
+ * Get average duration from an array
169
+ */
170
+ export declare function averageDuration(...durations: Duration[]): Duration | null;
171
+ //# sourceMappingURL=duration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"duration.d.ts","sourceRoot":"","sources":["../src/duration.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAgB,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAElF;;GAEG;AACH,qBAAa,QAAQ;IACnB,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAS;gBAE3B,KAAK,EAAE,MAAM,GAAG,aAAa,GAAG,MAAM;IAUlD;;OAEG;IACH,MAAM,CAAC,gBAAgB,CAAC,EAAE,EAAE,MAAM,GAAG,QAAQ;IAI7C;;OAEG;IACH,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,QAAQ;IAI7C;;OAEG;IACH,MAAM,CAAC,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,QAAQ;IAI7C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