@zag-js/i18n-utils 1.34.1 → 1.35.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 (53) hide show
  1. package/dist/cache.d.mts +4 -0
  2. package/dist/cache.d.ts +4 -0
  3. package/dist/cache.js +41 -0
  4. package/dist/cache.mjs +16 -0
  5. package/dist/collator.d.mts +3 -0
  6. package/dist/collator.d.ts +3 -0
  7. package/dist/collator.js +34 -0
  8. package/dist/collator.mjs +9 -0
  9. package/dist/filter.d.mts +11 -0
  10. package/dist/filter.d.ts +11 -0
  11. package/dist/filter.js +73 -0
  12. package/dist/filter.mjs +48 -0
  13. package/dist/format-bytes.d.mts +32 -0
  14. package/dist/format-bytes.d.ts +32 -0
  15. package/dist/format-bytes.js +54 -0
  16. package/dist/format-bytes.mjs +29 -0
  17. package/dist/format-date.d.mts +7 -0
  18. package/dist/format-date.d.ts +7 -0
  19. package/dist/format-date.js +262 -0
  20. package/dist/format-date.mjs +237 -0
  21. package/dist/format-list.d.mts +3 -0
  22. package/dist/format-list.d.ts +3 -0
  23. package/dist/format-list.js +35 -0
  24. package/dist/format-list.mjs +10 -0
  25. package/dist/format-number.d.mts +3 -0
  26. package/dist/format-number.d.ts +3 -0
  27. package/dist/format-number.js +35 -0
  28. package/dist/format-number.mjs +10 -0
  29. package/dist/format-relative-time.d.mts +3 -0
  30. package/dist/format-relative-time.d.ts +3 -0
  31. package/dist/format-relative-time.js +65 -0
  32. package/dist/format-relative-time.mjs +40 -0
  33. package/dist/format-time.d.mts +13 -0
  34. package/dist/format-time.d.ts +13 -0
  35. package/dist/format-time.js +77 -0
  36. package/dist/format-time.mjs +52 -0
  37. package/dist/index.d.mts +11 -90
  38. package/dist/index.d.ts +11 -90
  39. package/dist/index.js +53 -509
  40. package/dist/index.mjs +17 -497
  41. package/dist/is-rtl.d.mts +4 -0
  42. package/dist/is-rtl.d.ts +4 -0
  43. package/dist/is-rtl.js +77 -0
  44. package/dist/is-rtl.mjs +51 -0
  45. package/dist/locale.d.mts +13 -0
  46. package/dist/locale.d.ts +13 -0
  47. package/dist/locale.js +42 -0
  48. package/dist/locale.mjs +17 -0
  49. package/dist/track-locale.d.mts +10 -0
  50. package/dist/track-locale.d.ts +10 -0
  51. package/dist/track-locale.js +43 -0
  52. package/dist/track-locale.mjs +18 -0
  53. package/package.json +2 -2
@@ -0,0 +1,40 @@
1
+ // src/format-relative-time.ts
2
+ import { i18nCache } from "./cache.mjs";
3
+ var getRelativeTimeFormatter = i18nCache(Intl.RelativeTimeFormat);
4
+ function formatRelativeTime(value, locale, options = {}) {
5
+ const rtf = getRelativeTimeFormatter(locale, options);
6
+ const now = /* @__PURE__ */ new Date();
7
+ const diff = getDistance(now, value);
8
+ if (diff.years > 0) return rtf.format(diff.years * diff.sign, "year");
9
+ if (diff.months > 0) return rtf.format(diff.months * diff.sign, "month");
10
+ if (diff.weeks > 0) return rtf.format(diff.weeks * diff.sign, "week");
11
+ if (diff.days > 0) return rtf.format(diff.days * diff.sign, "day");
12
+ if (diff.hours > 0) return rtf.format(diff.hours * diff.sign, "hour");
13
+ if (diff.minutes > 0) return rtf.format(diff.minutes * diff.sign, "minute");
14
+ return rtf.format(diff.seconds * diff.sign, "second");
15
+ }
16
+ var SECOND_TO_MS = 1e3;
17
+ var MINUTE_TO_MS = 1e3 * 60;
18
+ var HOUR_TO_MS = 1e3 * 60 * 60;
19
+ var DAY_TO_MS = 1e3 * 60 * 60 * 24;
20
+ var WEEK_TO_MS = 1e3 * 60 * 60 * 24 * 7;
21
+ var MONTH_TO_MS = 1e3 * 60 * 60 * 24 * 30;
22
+ var YEAR_TO_MS = 1e3 * 60 * 60 * 24 * 365;
23
+ function getDistance(startDate, endDate) {
24
+ const endTime = endDate.getTime();
25
+ const startTime = startDate.getTime();
26
+ const distance = Math.abs(endTime - startTime);
27
+ return {
28
+ sign: Math.sign(endTime - startTime),
29
+ days: Math.floor(distance / DAY_TO_MS),
30
+ hours: Math.floor(distance % DAY_TO_MS / HOUR_TO_MS),
31
+ minutes: Math.floor(distance % HOUR_TO_MS / MINUTE_TO_MS),
32
+ seconds: Math.floor(distance % MINUTE_TO_MS / SECOND_TO_MS),
33
+ weeks: Math.floor(distance / WEEK_TO_MS),
34
+ months: Math.floor(distance / MONTH_TO_MS),
35
+ years: Math.floor(distance / YEAR_TO_MS)
36
+ };
37
+ }
38
+ export {
39
+ formatRelativeTime
40
+ };
@@ -0,0 +1,13 @@
1
+ type TimeFormat = "12h" | "24h";
2
+ interface AmPmLabels {
3
+ am: string;
4
+ pm: string;
5
+ }
6
+ interface FormatTimeOptions {
7
+ format?: TimeFormat;
8
+ amPmLabels?: AmPmLabels;
9
+ withSeconds?: boolean;
10
+ }
11
+ declare function formatTime(value: string | Date, locale: string, options?: FormatTimeOptions): string | null;
12
+
13
+ export { type AmPmLabels, type FormatTimeOptions, type TimeFormat, formatTime };
@@ -0,0 +1,13 @@
1
+ type TimeFormat = "12h" | "24h";
2
+ interface AmPmLabels {
3
+ am: string;
4
+ pm: string;
5
+ }
6
+ interface FormatTimeOptions {
7
+ format?: TimeFormat;
8
+ amPmLabels?: AmPmLabels;
9
+ withSeconds?: boolean;
10
+ }
11
+ declare function formatTime(value: string | Date, locale: string, options?: FormatTimeOptions): string | null;
12
+
13
+ export { type AmPmLabels, type FormatTimeOptions, type TimeFormat, formatTime };
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/format-time.ts
21
+ var format_time_exports = {};
22
+ __export(format_time_exports, {
23
+ formatTime: () => formatTime
24
+ });
25
+ module.exports = __toCommonJS(format_time_exports);
26
+ var import_cache = require("./cache.cjs");
27
+ var getTimeFormatter = (0, import_cache.i18nCache)(Intl.DateTimeFormat);
28
+ function splitTimeString(timeString) {
29
+ const [hours = null, minutes = null, seconds = null] = timeString.split(":");
30
+ const parsedHours = hours === null ? null : Number(hours);
31
+ const parsedMinutes = minutes === null ? null : Number(minutes);
32
+ const parsedSeconds = seconds === null ? null : Number(seconds);
33
+ return {
34
+ hours: Number.isNaN(parsedHours) ? null : parsedHours,
35
+ minutes: Number.isNaN(parsedMinutes) ? null : parsedMinutes,
36
+ seconds: Number.isNaN(parsedSeconds) ? null : parsedSeconds
37
+ };
38
+ }
39
+ function getTimeParts(value) {
40
+ if (value instanceof Date) {
41
+ if (Number.isNaN(value.getTime())) return null;
42
+ return {
43
+ date: value,
44
+ hours: value.getHours(),
45
+ minutes: value.getMinutes(),
46
+ seconds: value.getSeconds()
47
+ };
48
+ }
49
+ const { hours, minutes, seconds } = splitTimeString(value);
50
+ if (hours === null || minutes === null) return null;
51
+ if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59) return null;
52
+ if (seconds !== null && (seconds < 0 || seconds > 59)) return null;
53
+ const date = /* @__PURE__ */ new Date(0);
54
+ date.setHours(hours, minutes, seconds ?? 0, 0);
55
+ return { date, hours, minutes, seconds: seconds ?? 0 };
56
+ }
57
+ function formatTime(value, locale, options = {}) {
58
+ const { format = "24h", amPmLabels, withSeconds = false } = options;
59
+ const parts = getTimeParts(value);
60
+ if (!parts) return null;
61
+ const formatter = getTimeFormatter(locale, {
62
+ hour: format === "24h" ? "2-digit" : "numeric",
63
+ minute: "2-digit",
64
+ second: withSeconds ? "2-digit" : void 0,
65
+ hour12: format === "12h"
66
+ });
67
+ if (format !== "12h" || !amPmLabels) {
68
+ return formatter.format(parts.date);
69
+ }
70
+ const isPm = parts.hours >= 12;
71
+ const tokens = formatter.formatToParts(parts.date);
72
+ return tokens.map((token) => token.type === "dayPeriod" ? isPm ? amPmLabels.pm : amPmLabels.am : token.value).join("");
73
+ }
74
+ // Annotate the CommonJS export names for ESM import in node:
75
+ 0 && (module.exports = {
76
+ formatTime
77
+ });
@@ -0,0 +1,52 @@
1
+ // src/format-time.ts
2
+ import { i18nCache } from "./cache.mjs";
3
+ var getTimeFormatter = i18nCache(Intl.DateTimeFormat);
4
+ function splitTimeString(timeString) {
5
+ const [hours = null, minutes = null, seconds = null] = timeString.split(":");
6
+ const parsedHours = hours === null ? null : Number(hours);
7
+ const parsedMinutes = minutes === null ? null : Number(minutes);
8
+ const parsedSeconds = seconds === null ? null : Number(seconds);
9
+ return {
10
+ hours: Number.isNaN(parsedHours) ? null : parsedHours,
11
+ minutes: Number.isNaN(parsedMinutes) ? null : parsedMinutes,
12
+ seconds: Number.isNaN(parsedSeconds) ? null : parsedSeconds
13
+ };
14
+ }
15
+ function getTimeParts(value) {
16
+ if (value instanceof Date) {
17
+ if (Number.isNaN(value.getTime())) return null;
18
+ return {
19
+ date: value,
20
+ hours: value.getHours(),
21
+ minutes: value.getMinutes(),
22
+ seconds: value.getSeconds()
23
+ };
24
+ }
25
+ const { hours, minutes, seconds } = splitTimeString(value);
26
+ if (hours === null || minutes === null) return null;
27
+ if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59) return null;
28
+ if (seconds !== null && (seconds < 0 || seconds > 59)) return null;
29
+ const date = /* @__PURE__ */ new Date(0);
30
+ date.setHours(hours, minutes, seconds ?? 0, 0);
31
+ return { date, hours, minutes, seconds: seconds ?? 0 };
32
+ }
33
+ function formatTime(value, locale, options = {}) {
34
+ const { format = "24h", amPmLabels, withSeconds = false } = options;
35
+ const parts = getTimeParts(value);
36
+ if (!parts) return null;
37
+ const formatter = getTimeFormatter(locale, {
38
+ hour: format === "24h" ? "2-digit" : "numeric",
39
+ minute: "2-digit",
40
+ second: withSeconds ? "2-digit" : void 0,
41
+ hour12: format === "12h"
42
+ });
43
+ if (format !== "12h" || !amPmLabels) {
44
+ return formatter.format(parts.date);
45
+ }
46
+ const isPm = parts.hours >= 12;
47
+ const tokens = formatter.formatToParts(parts.date);
48
+ return tokens.map((token) => token.type === "dayPeriod" ? isPm ? amPmLabels.pm : amPmLabels.am : token.value).join("");
49
+ }
50
+ export {
51
+ formatTime
52
+ };
package/dist/index.d.mts CHANGED
@@ -1,90 +1,11 @@
1
- declare function createCollator(locale?: string, options?: Intl.CollatorOptions): Intl.Collator;
2
-
3
- interface FilterReturn {
4
- startsWith(string: string, substring: string): boolean;
5
- endsWith(string: string, substring: string): boolean;
6
- contains(string: string, substring: string): boolean;
7
- }
8
- interface FilterOptions extends Intl.CollatorOptions {
9
- locale?: string | undefined;
10
- }
11
- declare function createFilter(options?: FilterOptions): FilterReturn;
12
-
13
- interface FormatBytesOptions {
14
- /**
15
- * The number of significant digits to include in the formatted output.
16
- * @default 3
17
- */
18
- precision?: number | undefined;
19
- /**
20
- * The unit system to use for calculations.
21
- * - "binary": Uses 1024 as the base (e.g., 1 KiB = 1024 bytes)
22
- * - "decimal": Uses 1000 as the base (e.g., 1 KB = 1000 bytes)
23
- * @default "decimal"
24
- */
25
- unitSystem?: "binary" | "decimal" | undefined;
26
- /**
27
- * The type of unit to format the value as.
28
- * - "bit": Format as bits (b)
29
- * - "byte": Format as bytes (B)
30
- * @default "byte"
31
- */
32
- unit?: "bit" | "byte" | undefined;
33
- /**
34
- * The display style for the unit.
35
- * - "long": Full unit name (e.g., "kilobytes")
36
- * - "short": Abbreviated unit (e.g., "KB")
37
- * - "narrow": Compact unit (e.g., "K")
38
- * @default "short"
39
- */
40
- unitDisplay?: "long" | "short" | "narrow" | undefined;
41
- }
42
- declare const formatBytes: (bytes: number, locale?: string, options?: FormatBytesOptions) => string;
43
-
44
- /**
45
- * Formats a date using the given format string as defined in:
46
- * https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
47
- */
48
- declare function formatDate(date: Date, format: string, locale: string, timeZone?: string): string;
49
-
50
- declare function formatList(list: string[], locale: string, options?: Intl.ListFormatOptions): string;
51
-
52
- declare function formatNumber(v: number, locale: string, options?: Intl.NumberFormatOptions): string;
53
-
54
- declare function formatRelativeTime(value: Date, locale: string, options?: Intl.RelativeTimeFormatOptions): string;
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
-
68
- declare function isRTL(locale: string): boolean;
69
- declare function getLocaleDir(locale: string): "rtl" | "ltr";
70
-
71
- type Direction = "rtl" | "ltr";
72
- interface Locale {
73
- locale: string;
74
- dir: Direction;
75
- }
76
- declare global {
77
- interface Navigator {
78
- userLanguage?: string | undefined;
79
- }
80
- }
81
- declare function getDefaultLocale(): Locale;
82
-
83
- interface LocaleOptions {
84
- locale?: string | undefined;
85
- getRootNode?: (() => ShadowRoot | Document | Node) | undefined;
86
- onLocaleChange?: ((locale: Locale) => void) | undefined;
87
- }
88
- declare function trackLocale(options?: LocaleOptions): () => void;
89
-
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 };
1
+ export { createCollator } from './collator.mjs';
2
+ export { FilterOptions, FilterReturn, createFilter } from './filter.mjs';
3
+ export { FormatBytesOptions, formatBytes } from './format-bytes.mjs';
4
+ export { formatDate } from './format-date.mjs';
5
+ export { formatList } from './format-list.mjs';
6
+ export { formatNumber } from './format-number.mjs';
7
+ export { formatRelativeTime } from './format-relative-time.mjs';
8
+ export { AmPmLabels, FormatTimeOptions, TimeFormat, formatTime } from './format-time.mjs';
9
+ export { getLocaleDir, isRTL } from './is-rtl.mjs';
10
+ export { Locale, getDefaultLocale } from './locale.mjs';
11
+ export { LocaleOptions, trackLocale } from './track-locale.mjs';
package/dist/index.d.ts CHANGED
@@ -1,90 +1,11 @@
1
- declare function createCollator(locale?: string, options?: Intl.CollatorOptions): Intl.Collator;
2
-
3
- interface FilterReturn {
4
- startsWith(string: string, substring: string): boolean;
5
- endsWith(string: string, substring: string): boolean;
6
- contains(string: string, substring: string): boolean;
7
- }
8
- interface FilterOptions extends Intl.CollatorOptions {
9
- locale?: string | undefined;
10
- }
11
- declare function createFilter(options?: FilterOptions): FilterReturn;
12
-
13
- interface FormatBytesOptions {
14
- /**
15
- * The number of significant digits to include in the formatted output.
16
- * @default 3
17
- */
18
- precision?: number | undefined;
19
- /**
20
- * The unit system to use for calculations.
21
- * - "binary": Uses 1024 as the base (e.g., 1 KiB = 1024 bytes)
22
- * - "decimal": Uses 1000 as the base (e.g., 1 KB = 1000 bytes)
23
- * @default "decimal"
24
- */
25
- unitSystem?: "binary" | "decimal" | undefined;
26
- /**
27
- * The type of unit to format the value as.
28
- * - "bit": Format as bits (b)
29
- * - "byte": Format as bytes (B)
30
- * @default "byte"
31
- */
32
- unit?: "bit" | "byte" | undefined;
33
- /**
34
- * The display style for the unit.
35
- * - "long": Full unit name (e.g., "kilobytes")
36
- * - "short": Abbreviated unit (e.g., "KB")
37
- * - "narrow": Compact unit (e.g., "K")
38
- * @default "short"
39
- */
40
- unitDisplay?: "long" | "short" | "narrow" | undefined;
41
- }
42
- declare const formatBytes: (bytes: number, locale?: string, options?: FormatBytesOptions) => string;
43
-
44
- /**
45
- * Formats a date using the given format string as defined in:
46
- * https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
47
- */
48
- declare function formatDate(date: Date, format: string, locale: string, timeZone?: string): string;
49
-
50
- declare function formatList(list: string[], locale: string, options?: Intl.ListFormatOptions): string;
51
-
52
- declare function formatNumber(v: number, locale: string, options?: Intl.NumberFormatOptions): string;
53
-
54
- declare function formatRelativeTime(value: Date, locale: string, options?: Intl.RelativeTimeFormatOptions): string;
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
-
68
- declare function isRTL(locale: string): boolean;
69
- declare function getLocaleDir(locale: string): "rtl" | "ltr";
70
-
71
- type Direction = "rtl" | "ltr";
72
- interface Locale {
73
- locale: string;
74
- dir: Direction;
75
- }
76
- declare global {
77
- interface Navigator {
78
- userLanguage?: string | undefined;
79
- }
80
- }
81
- declare function getDefaultLocale(): Locale;
82
-
83
- interface LocaleOptions {
84
- locale?: string | undefined;
85
- getRootNode?: (() => ShadowRoot | Document | Node) | undefined;
86
- onLocaleChange?: ((locale: Locale) => void) | undefined;
87
- }
88
- declare function trackLocale(options?: LocaleOptions): () => void;
89
-
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 };
1
+ export { createCollator } from './collator.js';
2
+ export { FilterOptions, FilterReturn, createFilter } from './filter.js';
3
+ export { FormatBytesOptions, formatBytes } from './format-bytes.js';
4
+ export { formatDate } from './format-date.js';
5
+ export { formatList } from './format-list.js';
6
+ export { formatNumber } from './format-number.js';
7
+ export { formatRelativeTime } from './format-relative-time.js';
8
+ export { AmPmLabels, FormatTimeOptions, TimeFormat, formatTime } from './format-time.js';
9
+ export { getLocaleDir, isRTL } from './is-rtl.js';
10
+ export { Locale, getDefaultLocale } from './locale.js';
11
+ export { LocaleOptions, trackLocale } from './track-locale.js';