@gobrand/tiempo 2.2.2 → 2.2.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.
package/README.md CHANGED
@@ -120,6 +120,84 @@ console.log(iso2); // "2025-01-20T20:00:00Z"
120
120
 
121
121
  ### Formatting
122
122
 
123
+ #### `intlFormatDistance(laterDate, earlierDate, options?)`
124
+
125
+ Format the distance between two dates as a human-readable, internationalized string using `Intl.RelativeTimeFormat`. Automatically selects the most appropriate unit (seconds, minutes, hours, days, weeks, months, years) based on the distance.
126
+
127
+ **Parameters:**
128
+ - `laterDate` (Temporal.Instant | Temporal.ZonedDateTime): The later date to compare
129
+ - `earlierDate` (Temporal.Instant | Temporal.ZonedDateTime): The earlier date to compare with
130
+ - `options` (IntlFormatDistanceOptions, optional): Formatting options
131
+ - `unit` (Intl.RelativeTimeFormatUnit): Force a specific unit instead of automatic selection
132
+ - `locale` (string | string[]): Locale for formatting (default: system locale)
133
+ - `numeric` ('always' | 'auto'): Use numeric values always or allow natural language like "tomorrow" (default: 'auto')
134
+ - `style` ('long' | 'short' | 'narrow'): Formatting style (default: 'long')
135
+ - `localeMatcher` ('best fit' | 'lookup'): Locale matching algorithm
136
+
137
+ **Returns:** `string` - Formatted relative time string
138
+
139
+ **Automatic unit selection:**
140
+ - < 60 seconds → "in X seconds" / "X seconds ago"
141
+ - < 60 minutes → "in X minutes" / "X minutes ago"
142
+ - < 24 hours → "in X hours" / "X hours ago"
143
+ - < 7 days → "tomorrow" / "yesterday" / "in X days" / "X days ago"
144
+ - < 4 weeks → "next week" / "last week" / "in X weeks" / "X weeks ago"
145
+ - < 12 months → "next month" / "last month" / "in X months" / "X months ago"
146
+ - ≥ 12 months → "next year" / "last year" / "in X years" / "X years ago"
147
+
148
+ **Example:**
149
+ ```typescript
150
+ import { intlFormatDistance } from '@gobrand/tiempo';
151
+
152
+ const later = Temporal.Instant.from('2025-01-20T12:00:00Z');
153
+ const earlier = Temporal.Instant.from('2025-01-20T11:00:00Z');
154
+
155
+ // Basic usage
156
+ intlFormatDistance(later, earlier);
157
+ // => "in 1 hour"
158
+
159
+ intlFormatDistance(earlier, later);
160
+ // => "1 hour ago"
161
+
162
+ // With different locales
163
+ intlFormatDistance(later, earlier, { locale: 'es' });
164
+ // => "dentro de 1 hora"
165
+
166
+ intlFormatDistance(later, earlier, { locale: 'ja' });
167
+ // => "1 時間後"
168
+
169
+ // Force numeric format
170
+ const tomorrow = Temporal.Instant.from('2025-01-21T00:00:00Z');
171
+ const today = Temporal.Instant.from('2025-01-20T00:00:00Z');
172
+
173
+ intlFormatDistance(tomorrow, today, { numeric: 'auto' });
174
+ // => "tomorrow"
175
+
176
+ intlFormatDistance(tomorrow, today, { numeric: 'always' });
177
+ // => "in 1 day"
178
+
179
+ // Different styles
180
+ const future = Temporal.Instant.from('2027-01-20T00:00:00Z');
181
+ const now = Temporal.Instant.from('2025-01-20T00:00:00Z');
182
+
183
+ intlFormatDistance(future, now, { style: 'long' });
184
+ // => "in 2 years"
185
+
186
+ intlFormatDistance(future, now, { style: 'short' });
187
+ // => "in 2 yr."
188
+
189
+ intlFormatDistance(future, now, { style: 'narrow' });
190
+ // => "in 2y"
191
+
192
+ // Force specific units
193
+ intlFormatDistance(future, now, { unit: 'quarter' });
194
+ // => "in 8 quarters"
195
+
196
+ const dayLater = Temporal.Instant.from('2025-01-21T00:00:00Z');
197
+ intlFormatDistance(dayLater, today, { unit: 'hour' });
198
+ // => "in 24 hours"
199
+ ```
200
+
123
201
  #### `format(input, formatStr, options?)`
124
202
 
125
203
  Format a Temporal.Instant or ZonedDateTime using date-fns-like format tokens.
@@ -0,0 +1,94 @@
1
+ import {
2
+ differenceInWeeks
3
+ } from "./chunk-PVAOUYXF.js";
4
+ import {
5
+ differenceInYears
6
+ } from "./chunk-CHW2EN2O.js";
7
+ import {
8
+ differenceInDays
9
+ } from "./chunk-6IP245MS.js";
10
+ import {
11
+ differenceInHours
12
+ } from "./chunk-PIDXROVB.js";
13
+ import {
14
+ differenceInMinutes
15
+ } from "./chunk-RJY62CDU.js";
16
+ import {
17
+ differenceInMonths
18
+ } from "./chunk-BIAPE4MR.js";
19
+ import {
20
+ differenceInSeconds
21
+ } from "./chunk-ZHRMURYP.js";
22
+ import {
23
+ normalizeTemporalInput
24
+ } from "./chunk-MJSZNWCV.js";
25
+
26
+ // src/intlFormatDistance.ts
27
+ function intlFormatDistance(laterDate, earlierDate, options) {
28
+ const zoned1 = normalizeTemporalInput(laterDate);
29
+ const zoned2 = normalizeTemporalInput(earlierDate);
30
+ let unit = options?.unit;
31
+ if (!unit) {
32
+ const absSeconds = Math.abs(differenceInSeconds(zoned1, zoned2));
33
+ const absMinutes = Math.abs(differenceInMinutes(zoned1, zoned2));
34
+ const absHours = Math.abs(differenceInHours(zoned1, zoned2));
35
+ const absDays = Math.abs(differenceInDays(zoned1, zoned2));
36
+ const absWeeks = Math.abs(differenceInWeeks(zoned1, zoned2));
37
+ const absMonths = Math.abs(differenceInMonths(zoned1, zoned2));
38
+ if (absSeconds < 60) {
39
+ unit = "second";
40
+ } else if (absMinutes < 60) {
41
+ unit = "minute";
42
+ } else if (absHours < 24) {
43
+ unit = "hour";
44
+ } else if (absDays < 7) {
45
+ unit = "day";
46
+ } else if (absWeeks < 4) {
47
+ unit = "week";
48
+ } else if (absMonths < 12) {
49
+ unit = "month";
50
+ } else {
51
+ unit = "year";
52
+ }
53
+ }
54
+ let value;
55
+ switch (unit) {
56
+ case "second":
57
+ value = differenceInSeconds(zoned1, zoned2);
58
+ break;
59
+ case "minute":
60
+ value = differenceInMinutes(zoned1, zoned2);
61
+ break;
62
+ case "hour":
63
+ value = differenceInHours(zoned1, zoned2);
64
+ break;
65
+ case "day":
66
+ value = differenceInDays(zoned1, zoned2);
67
+ break;
68
+ case "week":
69
+ value = differenceInWeeks(zoned1, zoned2);
70
+ break;
71
+ case "month":
72
+ value = differenceInMonths(zoned1, zoned2);
73
+ break;
74
+ case "quarter":
75
+ value = Math.round(differenceInMonths(zoned1, zoned2) / 3);
76
+ break;
77
+ case "year":
78
+ value = differenceInYears(zoned1, zoned2);
79
+ break;
80
+ default:
81
+ value = differenceInSeconds(zoned1, zoned2);
82
+ }
83
+ const formatter = new Intl.RelativeTimeFormat(options?.locale, {
84
+ localeMatcher: options?.localeMatcher,
85
+ numeric: options?.numeric ?? "auto",
86
+ style: options?.style
87
+ });
88
+ return formatter.format(value, unit);
89
+ }
90
+
91
+ export {
92
+ intlFormatDistance
93
+ };
94
+ //# sourceMappingURL=chunk-NL5LWPJI.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/intlFormatDistance.ts"],"sourcesContent":["import type { Temporal } from '@js-temporal/polyfill';\nimport { differenceInDays } from './differenceInDays.js';\nimport { differenceInHours } from './differenceInHours.js';\nimport { differenceInMinutes } from './differenceInMinutes.js';\nimport { differenceInMonths } from './differenceInMonths.js';\nimport { differenceInSeconds } from './differenceInSeconds.js';\nimport { differenceInWeeks } from './differenceInWeeks.js';\nimport { differenceInYears } from './differenceInYears.js';\nimport { normalizeTemporalInput } from './shared/normalizeTemporalInput.js';\n\nexport interface IntlFormatDistanceOptions {\n /**\n * The unit to force formatting in. If not specified, the unit will be automatically selected.\n */\n unit?: Intl.RelativeTimeFormatUnit;\n /**\n * The locale to use for formatting. Defaults to the system locale.\n */\n locale?: string | string[];\n /**\n * The locale matching algorithm to use.\n */\n localeMatcher?: 'best fit' | 'lookup';\n /**\n * Whether to use numeric values always, or use special strings like \"yesterday\", \"tomorrow\", etc.\n * Defaults to 'auto'.\n */\n numeric?: 'always' | 'auto';\n /**\n * The formatting style to use.\n */\n style?: 'long' | 'short' | 'narrow';\n}\n\n/**\n * Formats the distance between two dates as a human-readable, internationalized string.\n *\n * The function automatically picks the most appropriate unit based on the distance between dates.\n * For example, if the distance is a few hours, it returns \"in X hours\". If the distance is a few\n * months, it returns \"in X months\".\n *\n * You can force a specific unit using the `options.unit` parameter.\n *\n * @param laterDate - The later date to compare\n * @param earlierDate - The earlier date to compare with\n * @param options - Formatting options\n * @returns The formatted distance string\n *\n * @example\n * ```typescript\n * const later = Temporal.Instant.from('2024-01-01T12:00:00Z');\n * const earlier = Temporal.Instant.from('2024-01-01T11:00:00Z');\n *\n * intlFormatDistance(later, earlier);\n * // => 'in 1 hour'\n *\n * intlFormatDistance(earlier, later);\n * // => '1 hour ago'\n * ```\n *\n * @example\n * ```typescript\n * // Force a specific unit\n * const later = Temporal.Instant.from('2025-01-01T00:00:00Z');\n * const earlier = Temporal.Instant.from('2024-01-01T00:00:00Z');\n *\n * intlFormatDistance(later, earlier, { unit: 'quarter' });\n * // => 'in 4 quarters'\n * ```\n *\n * @example\n * ```typescript\n * // Use a different locale\n * const later = Temporal.Instant.from('2024-01-01T12:00:00Z');\n * const earlier = Temporal.Instant.from('2024-01-01T11:00:00Z');\n *\n * intlFormatDistance(later, earlier, { locale: 'es' });\n * // => 'dentro de 1 hora'\n * ```\n *\n * @example\n * ```typescript\n * // Use numeric: 'always' to avoid special strings\n * const later = Temporal.Instant.from('2024-01-02T00:00:00Z');\n * const earlier = Temporal.Instant.from('2024-01-01T00:00:00Z');\n *\n * intlFormatDistance(later, earlier, { numeric: 'auto' });\n * // => 'tomorrow'\n *\n * intlFormatDistance(later, earlier, { numeric: 'always' });\n * // => 'in 1 day'\n * ```\n */\nexport function intlFormatDistance(\n laterDate: Temporal.Instant | Temporal.ZonedDateTime,\n earlierDate: Temporal.Instant | Temporal.ZonedDateTime,\n options?: IntlFormatDistanceOptions\n): string {\n const zoned1 = normalizeTemporalInput(laterDate);\n const zoned2 = normalizeTemporalInput(earlierDate);\n\n // Determine unit if not specified\n let unit = options?.unit;\n if (!unit) {\n const absSeconds = Math.abs(differenceInSeconds(zoned1, zoned2));\n const absMinutes = Math.abs(differenceInMinutes(zoned1, zoned2));\n const absHours = Math.abs(differenceInHours(zoned1, zoned2));\n const absDays = Math.abs(differenceInDays(zoned1, zoned2));\n const absWeeks = Math.abs(differenceInWeeks(zoned1, zoned2));\n const absMonths = Math.abs(differenceInMonths(zoned1, zoned2));\n\n if (absSeconds < 60) {\n unit = 'second';\n } else if (absMinutes < 60) {\n unit = 'minute';\n } else if (absHours < 24) {\n unit = 'hour';\n } else if (absDays < 7) {\n unit = 'day';\n } else if (absWeeks < 4) {\n unit = 'week';\n } else if (absMonths < 12) {\n unit = 'month';\n } else {\n unit = 'year';\n }\n }\n\n // Calculate value for the selected unit\n let value: number;\n switch (unit) {\n case 'second':\n value = differenceInSeconds(zoned1, zoned2);\n break;\n case 'minute':\n value = differenceInMinutes(zoned1, zoned2);\n break;\n case 'hour':\n value = differenceInHours(zoned1, zoned2);\n break;\n case 'day':\n value = differenceInDays(zoned1, zoned2);\n break;\n case 'week':\n value = differenceInWeeks(zoned1, zoned2);\n break;\n case 'month':\n value = differenceInMonths(zoned1, zoned2);\n break;\n case 'quarter':\n value = Math.round(differenceInMonths(zoned1, zoned2) / 3);\n break;\n case 'year':\n value = differenceInYears(zoned1, zoned2);\n break;\n default:\n // For any other unit type, try to use it directly\n value = differenceInSeconds(zoned1, zoned2);\n }\n\n // Format using Intl.RelativeTimeFormat\n const formatter = new Intl.RelativeTimeFormat(options?.locale, {\n localeMatcher: options?.localeMatcher,\n numeric: options?.numeric ?? 'auto',\n style: options?.style,\n });\n\n return formatter.format(value, unit);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AA6FO,SAAS,mBACd,WACA,aACA,SACQ;AACR,QAAM,SAAS,uBAAuB,SAAS;AAC/C,QAAM,SAAS,uBAAuB,WAAW;AAGjD,MAAI,OAAO,SAAS;AACpB,MAAI,CAAC,MAAM;AACT,UAAM,aAAa,KAAK,IAAI,oBAAoB,QAAQ,MAAM,CAAC;AAC/D,UAAM,aAAa,KAAK,IAAI,oBAAoB,QAAQ,MAAM,CAAC;AAC/D,UAAM,WAAW,KAAK,IAAI,kBAAkB,QAAQ,MAAM,CAAC;AAC3D,UAAM,UAAU,KAAK,IAAI,iBAAiB,QAAQ,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{ differenceInDays } from './differenceInDays';
27
27
  export { differenceInWeeks } from './differenceInWeeks';
28
28
  export { differenceInMonths } from './differenceInMonths';
29
29
  export { differenceInYears } from './differenceInYears';
30
+ export { intlFormatDistance, type IntlFormatDistanceOptions, } from './intlFormatDistance';
30
31
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,UAAU,CAAC;AACtD,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OA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AM,eAAe,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,uBAAuB,EAAE,MAAM,2BAA2B,CAAC;AACpE,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AACtE,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EACL,kBAAkB,EAClB,KAAK,yBAAyB,GAC/B,MAAM,sBAAsB,CAAC"}
package/dist/index.js CHANGED
@@ -1,3 +1,6 @@
1
+ import {
2
+ startOfMonth
3
+ } from "./chunk-TU2UNOOW.js";
1
4
  import {
2
5
  startOfWeek
3
6
  } from "./chunk-2WMXB7QL.js";
@@ -16,6 +19,9 @@ import {
16
19
  import {
17
20
  today
18
21
  } from "./chunk-67QHALIG.js";
22
+ import {
23
+ intlFormatDistance
24
+ } from "./chunk-NL5LWPJI.js";
19
25
  import {
20
26
  isAfter
21
27
  } from "./chunk-BBNNR2QH.js";
@@ -37,9 +43,6 @@ import {
37
43
  import {
38
44
  startOfDay
39
45
  } from "./chunk-TW5EV3DH.js";
40
- import {
41
- startOfMonth
42
- } from "./chunk-TU2UNOOW.js";
43
46
  import {
44
47
  differenceInWeeks
45
48
  } from "./chunk-PVAOUYXF.js";
@@ -103,6 +106,7 @@ export {
103
106
  endOfWeek,
104
107
  endOfYear,
105
108
  format,
109
+ intlFormatDistance,
106
110
  isAfter,
107
111
  isBefore,
108
112
  isFuture,
@@ -0,0 +1,85 @@
1
+ import type { Temporal } from '@js-temporal/polyfill';
2
+ export interface IntlFormatDistanceOptions {
3
+ /**
4
+ * The unit to force formatting in. If not specified, the unit will be automatically selected.
5
+ */
6
+ unit?: Intl.RelativeTimeFormatUnit;
7
+ /**
8
+ * The locale to use for formatting. Defaults to the system locale.
9
+ */
10
+ locale?: string | string[];
11
+ /**
12
+ * The locale matching algorithm to use.
13
+ */
14
+ localeMatcher?: 'best fit' | 'lookup';
15
+ /**
16
+ * Whether to use numeric values always, or use special strings like "yesterday", "tomorrow", etc.
17
+ * Defaults to 'auto'.
18
+ */
19
+ numeric?: 'always' | 'auto';
20
+ /**
21
+ * The formatting style to use.
22
+ */
23
+ style?: 'long' | 'short' | 'narrow';
24
+ }
25
+ /**
26
+ * Formats the distance between two dates as a human-readable, internationalized string.
27
+ *
28
+ * The function automatically picks the most appropriate unit based on the distance between dates.
29
+ * For example, if the distance is a few hours, it returns "in X hours". If the distance is a few
30
+ * months, it returns "in X months".
31
+ *
32
+ * You can force a specific unit using the `options.unit` parameter.
33
+ *
34
+ * @param laterDate - The later date to compare
35
+ * @param earlierDate - The earlier date to compare with
36
+ * @param options - Formatting options
37
+ * @returns The formatted distance string
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * const later = Temporal.Instant.from('2024-01-01T12:00:00Z');
42
+ * const earlier = Temporal.Instant.from('2024-01-01T11:00:00Z');
43
+ *
44
+ * intlFormatDistance(later, earlier);
45
+ * // => 'in 1 hour'
46
+ *
47
+ * intlFormatDistance(earlier, later);
48
+ * // => '1 hour ago'
49
+ * ```
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * // Force a specific unit
54
+ * const later = Temporal.Instant.from('2025-01-01T00:00:00Z');
55
+ * const earlier = Temporal.Instant.from('2024-01-01T00:00:00Z');
56
+ *
57
+ * intlFormatDistance(later, earlier, { unit: 'quarter' });
58
+ * // => 'in 4 quarters'
59
+ * ```
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * // Use a different locale
64
+ * const later = Temporal.Instant.from('2024-01-01T12:00:00Z');
65
+ * const earlier = Temporal.Instant.from('2024-01-01T11:00:00Z');
66
+ *
67
+ * intlFormatDistance(later, earlier, { locale: 'es' });
68
+ * // => 'dentro de 1 hora'
69
+ * ```
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * // Use numeric: 'always' to avoid special strings
74
+ * const later = Temporal.Instant.from('2024-01-02T00:00:00Z');
75
+ * const earlier = Temporal.Instant.from('2024-01-01T00:00:00Z');
76
+ *
77
+ * intlFormatDistance(later, earlier, { numeric: 'auto' });
78
+ * // => 'tomorrow'
79
+ *
80
+ * intlFormatDistance(later, earlier, { numeric: 'always' });
81
+ * // => 'in 1 day'
82
+ * ```
83
+ */
84
+ export declare function intlFormatDistance(laterDate: Temporal.Instant | Temporal.ZonedDateTime, earlierDate: Temporal.Instant | Temporal.ZonedDateTime, options?: IntlFormatDistanceOptions): string;
85
+ //# sourceMappingURL=intlFormatDistance.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"intlFormatDistance.d.ts","sourceRoot":"","sources":["../src/intlFormatDistance.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAUtD,MAAM,WAAW,yBAAyB;IACxC;;OAEG;IACH,IAAI,CAAC,EAAE,IAAI,CAAC,sBAAsB,CAAC;IACnC;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAC3B;;OAEG;IACH,aAAa,CAAC,EAAE,UAAU,GAAG,QAAQ,CAAC;IACtC;;;OAGG;IACH,OAAO,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC;IAC5B;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,GAAG,OAAO,GAAG,QAAQ,CAAC;CACrC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AACH,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,QAAQ,CAAC,OAAO,GAAG,QAAQ,CAAC,aAAa,EACpD,WAAW,EAAE,QAAQ,CAAC,OAAO,GAAG,QAAQ,CAAC,aAAa,EACtD,OAAO,CAAC,EAAE,yBAAyB,GAClC,MAAM,CAuER"}
@@ -0,0 +1,15 @@
1
+ import {
2
+ intlFormatDistance
3
+ } from "./chunk-NL5LWPJI.js";
4
+ import "./chunk-PVAOUYXF.js";
5
+ import "./chunk-CHW2EN2O.js";
6
+ import "./chunk-6IP245MS.js";
7
+ import "./chunk-PIDXROVB.js";
8
+ import "./chunk-RJY62CDU.js";
9
+ import "./chunk-BIAPE4MR.js";
10
+ import "./chunk-ZHRMURYP.js";
11
+ import "./chunk-MJSZNWCV.js";
12
+ export {
13
+ intlFormatDistance
14
+ };
15
+ //# sourceMappingURL=intlFormatDistance.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=intlFormatDistance.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"intlFormatDistance.test.d.ts","sourceRoot":"","sources":["../src/intlFormatDistance.test.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gobrand/tiempo",
3
- "version": "2.2.2",
3
+ "version": "2.2.3",
4
4
  "description": "Lightweight utility functions for converting between UTC and timezone-aware datetimes using the Temporal API",
5
5
  "private": false,
6
6
  "publishConfig": {