@zag-js/i18n-utils 1.33.0 → 1.34.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.
package/dist/index.d.mts CHANGED
@@ -53,6 +53,18 @@ declare function formatNumber(v: number, locale: string, options?: Intl.NumberFo
53
53
 
54
54
  declare function formatRelativeTime(value: Date, locale: string, options?: Intl.RelativeTimeFormatOptions): string;
55
55
 
56
+ type TimeFormat = "12h" | "24h";
57
+ interface AmPmLabels {
58
+ am: string;
59
+ pm: string;
60
+ }
61
+ interface FormatTimeOptions {
62
+ format?: TimeFormat;
63
+ amPmLabels?: AmPmLabels;
64
+ withSeconds?: boolean;
65
+ }
66
+ declare function formatTime(value: string | Date, locale: string, options?: FormatTimeOptions): string | null;
67
+
56
68
  declare function isRTL(locale: string): boolean;
57
69
  declare function getLocaleDir(locale: string): "rtl" | "ltr";
58
70
 
@@ -75,4 +87,4 @@ interface LocaleOptions {
75
87
  }
76
88
  declare function trackLocale(options?: LocaleOptions): () => void;
77
89
 
78
- export { type FilterOptions, type FilterReturn, type FormatBytesOptions, type Locale, type LocaleOptions, createCollator, createFilter, formatBytes, formatDate, formatList, formatNumber, formatRelativeTime, getDefaultLocale, getLocaleDir, isRTL, trackLocale };
90
+ export { type AmPmLabels, type FilterOptions, type FilterReturn, type FormatBytesOptions, type FormatTimeOptions, type Locale, type LocaleOptions, type TimeFormat, createCollator, createFilter, formatBytes, formatDate, formatList, formatNumber, formatRelativeTime, formatTime, getDefaultLocale, getLocaleDir, isRTL, trackLocale };
package/dist/index.d.ts CHANGED
@@ -53,6 +53,18 @@ declare function formatNumber(v: number, locale: string, options?: Intl.NumberFo
53
53
 
54
54
  declare function formatRelativeTime(value: Date, locale: string, options?: Intl.RelativeTimeFormatOptions): string;
55
55
 
56
+ type TimeFormat = "12h" | "24h";
57
+ interface AmPmLabels {
58
+ am: string;
59
+ pm: string;
60
+ }
61
+ interface FormatTimeOptions {
62
+ format?: TimeFormat;
63
+ amPmLabels?: AmPmLabels;
64
+ withSeconds?: boolean;
65
+ }
66
+ declare function formatTime(value: string | Date, locale: string, options?: FormatTimeOptions): string | null;
67
+
56
68
  declare function isRTL(locale: string): boolean;
57
69
  declare function getLocaleDir(locale: string): "rtl" | "ltr";
58
70
 
@@ -75,4 +87,4 @@ interface LocaleOptions {
75
87
  }
76
88
  declare function trackLocale(options?: LocaleOptions): () => void;
77
89
 
78
- export { type FilterOptions, type FilterReturn, type FormatBytesOptions, type Locale, type LocaleOptions, createCollator, createFilter, formatBytes, formatDate, formatList, formatNumber, formatRelativeTime, getDefaultLocale, getLocaleDir, isRTL, trackLocale };
90
+ export { type AmPmLabels, type FilterOptions, type FilterReturn, type FormatBytesOptions, type FormatTimeOptions, type Locale, type LocaleOptions, type TimeFormat, createCollator, createFilter, formatBytes, formatDate, formatList, formatNumber, formatRelativeTime, formatTime, getDefaultLocale, getLocaleDir, isRTL, trackLocale };
package/dist/index.js CHANGED
@@ -379,6 +379,55 @@ function getDistance(startDate, endDate) {
379
379
  };
380
380
  }
381
381
 
382
+ // src/format-time.ts
383
+ var getTimeFormatter = i18nCache(Intl.DateTimeFormat);
384
+ function splitTimeString(timeString) {
385
+ const [hours = null, minutes = null, seconds = null] = timeString.split(":");
386
+ const parsedHours = hours === null ? null : Number(hours);
387
+ const parsedMinutes = minutes === null ? null : Number(minutes);
388
+ const parsedSeconds = seconds === null ? null : Number(seconds);
389
+ return {
390
+ hours: Number.isNaN(parsedHours) ? null : parsedHours,
391
+ minutes: Number.isNaN(parsedMinutes) ? null : parsedMinutes,
392
+ seconds: Number.isNaN(parsedSeconds) ? null : parsedSeconds
393
+ };
394
+ }
395
+ function getTimeParts(value) {
396
+ if (value instanceof Date) {
397
+ if (Number.isNaN(value.getTime())) return null;
398
+ return {
399
+ date: value,
400
+ hours: value.getHours(),
401
+ minutes: value.getMinutes(),
402
+ seconds: value.getSeconds()
403
+ };
404
+ }
405
+ const { hours, minutes, seconds } = splitTimeString(value);
406
+ if (hours === null || minutes === null) return null;
407
+ if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59) return null;
408
+ if (seconds !== null && (seconds < 0 || seconds > 59)) return null;
409
+ const date = /* @__PURE__ */ new Date(0);
410
+ date.setHours(hours, minutes, seconds ?? 0, 0);
411
+ return { date, hours, minutes, seconds: seconds ?? 0 };
412
+ }
413
+ function formatTime(value, locale, options = {}) {
414
+ const { format = "24h", amPmLabels, withSeconds = false } = options;
415
+ const parts = getTimeParts(value);
416
+ if (!parts) return null;
417
+ const formatter = getTimeFormatter(locale, {
418
+ hour: format === "24h" ? "2-digit" : "numeric",
419
+ minute: "2-digit",
420
+ second: withSeconds ? "2-digit" : void 0,
421
+ hour12: format === "12h"
422
+ });
423
+ if (format !== "12h" || !amPmLabels) {
424
+ return formatter.format(parts.date);
425
+ }
426
+ const isPm = parts.hours >= 12;
427
+ const tokens = formatter.formatToParts(parts.date);
428
+ return tokens.map((token) => token.type === "dayPeriod" ? isPm ? amPmLabels.pm : amPmLabels.am : token.value).join("");
429
+ }
430
+
382
431
  // src/is-rtl.ts
383
432
  var RTL_SCRIPTS = /* @__PURE__ */ new Set([
384
433
  "Avst",
@@ -455,6 +504,7 @@ exports.formatDate = formatDate;
455
504
  exports.formatList = formatList;
456
505
  exports.formatNumber = formatNumber;
457
506
  exports.formatRelativeTime = formatRelativeTime;
507
+ exports.formatTime = formatTime;
458
508
  exports.getDefaultLocale = getDefaultLocale;
459
509
  exports.getLocaleDir = getLocaleDir;
460
510
  exports.isRTL = isRTL;
package/dist/index.mjs CHANGED
@@ -377,6 +377,55 @@ function getDistance(startDate, endDate) {
377
377
  };
378
378
  }
379
379
 
380
+ // src/format-time.ts
381
+ var getTimeFormatter = i18nCache(Intl.DateTimeFormat);
382
+ function splitTimeString(timeString) {
383
+ const [hours = null, minutes = null, seconds = null] = timeString.split(":");
384
+ const parsedHours = hours === null ? null : Number(hours);
385
+ const parsedMinutes = minutes === null ? null : Number(minutes);
386
+ const parsedSeconds = seconds === null ? null : Number(seconds);
387
+ return {
388
+ hours: Number.isNaN(parsedHours) ? null : parsedHours,
389
+ minutes: Number.isNaN(parsedMinutes) ? null : parsedMinutes,
390
+ seconds: Number.isNaN(parsedSeconds) ? null : parsedSeconds
391
+ };
392
+ }
393
+ function getTimeParts(value) {
394
+ if (value instanceof Date) {
395
+ if (Number.isNaN(value.getTime())) return null;
396
+ return {
397
+ date: value,
398
+ hours: value.getHours(),
399
+ minutes: value.getMinutes(),
400
+ seconds: value.getSeconds()
401
+ };
402
+ }
403
+ const { hours, minutes, seconds } = splitTimeString(value);
404
+ if (hours === null || minutes === null) return null;
405
+ if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59) return null;
406
+ if (seconds !== null && (seconds < 0 || seconds > 59)) return null;
407
+ const date = /* @__PURE__ */ new Date(0);
408
+ date.setHours(hours, minutes, seconds ?? 0, 0);
409
+ return { date, hours, minutes, seconds: seconds ?? 0 };
410
+ }
411
+ function formatTime(value, locale, options = {}) {
412
+ const { format = "24h", amPmLabels, withSeconds = false } = options;
413
+ const parts = getTimeParts(value);
414
+ if (!parts) return null;
415
+ const formatter = getTimeFormatter(locale, {
416
+ hour: format === "24h" ? "2-digit" : "numeric",
417
+ minute: "2-digit",
418
+ second: withSeconds ? "2-digit" : void 0,
419
+ hour12: format === "12h"
420
+ });
421
+ if (format !== "12h" || !amPmLabels) {
422
+ return formatter.format(parts.date);
423
+ }
424
+ const isPm = parts.hours >= 12;
425
+ const tokens = formatter.formatToParts(parts.date);
426
+ return tokens.map((token) => token.type === "dayPeriod" ? isPm ? amPmLabels.pm : amPmLabels.am : token.value).join("");
427
+ }
428
+
380
429
  // src/is-rtl.ts
381
430
  var RTL_SCRIPTS = /* @__PURE__ */ new Set([
382
431
  "Avst",
@@ -446,4 +495,4 @@ function trackLocale(options = {}) {
446
495
  };
447
496
  }
448
497
 
449
- export { createCollator, createFilter, formatBytes, formatDate, formatList, formatNumber, formatRelativeTime, getDefaultLocale, getLocaleDir, isRTL, trackLocale };
498
+ export { createCollator, createFilter, formatBytes, formatDate, formatList, formatNumber, formatRelativeTime, formatTime, getDefaultLocale, getLocaleDir, isRTL, trackLocale };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/i18n-utils",
3
- "version": "1.33.0",
3
+ "version": "1.34.0",
4
4
  "description": "Interationalization utilities for Zag.js",
5
5
  "keywords": [
6
6
  "js",
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "clean-package": "../../../clean-package.config.json",
26
26
  "dependencies": {
27
- "@zag-js/dom-query": "1.33.0"
27
+ "@zag-js/dom-query": "1.34.0"
28
28
  },
29
29
  "devDependencies": {
30
30
  "clean-package": "2.2.0",