@zag-js/date-utils 1.40.0 → 1.41.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.
@@ -5,5 +5,6 @@ declare function alignStart(date: DateValue, duration: DateDuration, locale: str
5
5
  declare function alignEnd(date: DateValue, duration: DateDuration, locale: string, min?: DateValue, max?: DateValue): DateValue;
6
6
  declare function constrainStart(date: DateValue, aligned: DateValue, duration: DateDuration, locale: string, min?: DateValue, max?: DateValue): DateValue;
7
7
  declare function constrainValue<T extends DateValue>(date: T, minValue?: DateValue, maxValue?: DateValue): T;
8
+ declare function constrainSegments<T extends DateValue>(date: T, minValue?: DateValue, maxValue?: DateValue): T;
8
9
 
9
- export { alignCenter, alignEnd, alignStart, constrainStart, constrainValue };
10
+ export { alignCenter, alignEnd, alignStart, constrainSegments, constrainStart, constrainValue };
@@ -5,5 +5,6 @@ declare function alignStart(date: DateValue, duration: DateDuration, locale: str
5
5
  declare function alignEnd(date: DateValue, duration: DateDuration, locale: string, min?: DateValue, max?: DateValue): DateValue;
6
6
  declare function constrainStart(date: DateValue, aligned: DateValue, duration: DateDuration, locale: string, min?: DateValue, max?: DateValue): DateValue;
7
7
  declare function constrainValue<T extends DateValue>(date: T, minValue?: DateValue, maxValue?: DateValue): T;
8
+ declare function constrainSegments<T extends DateValue>(date: T, minValue?: DateValue, maxValue?: DateValue): T;
8
9
 
9
- export { alignCenter, alignEnd, alignStart, constrainStart, constrainValue };
10
+ export { alignCenter, alignEnd, alignStart, constrainSegments, constrainStart, constrainValue };
package/dist/constrain.js CHANGED
@@ -23,6 +23,7 @@ __export(constrain_exports, {
23
23
  alignCenter: () => alignCenter,
24
24
  alignEnd: () => alignEnd,
25
25
  alignStart: () => alignStart,
26
+ constrainSegments: () => constrainSegments,
26
27
  constrainStart: () => constrainStart,
27
28
  constrainValue: () => constrainValue
28
29
  });
@@ -99,11 +100,47 @@ function constrainValue(date, minValue, maxValue) {
99
100
  }
100
101
  return constrainedDateOnly;
101
102
  }
103
+ function constrainSegments(date, minValue, maxValue) {
104
+ const dateOnly = (0, import_date.toCalendarDate)(date);
105
+ const minOnly = minValue ? (0, import_date.toCalendarDate)(minValue) : void 0;
106
+ const maxOnly = maxValue ? (0, import_date.toCalendarDate)(maxValue) : void 0;
107
+ let result = dateOnly;
108
+ if (minOnly && result.compare(minOnly) < 0) {
109
+ if (result.year < minOnly.year) {
110
+ result = result.set({ year: minOnly.year });
111
+ }
112
+ if (result.compare(minOnly) < 0 && result.month < minOnly.month) {
113
+ result = result.set({ month: minOnly.month });
114
+ }
115
+ if (result.compare(minOnly) < 0 && result.day < minOnly.day) {
116
+ result = result.set({ day: minOnly.day });
117
+ }
118
+ }
119
+ if (maxOnly && result.compare(maxOnly) > 0) {
120
+ if (result.year > maxOnly.year) {
121
+ result = result.set({ year: maxOnly.year });
122
+ }
123
+ if (result.compare(maxOnly) > 0 && result.month > maxOnly.month) {
124
+ result = result.set({ month: maxOnly.month });
125
+ }
126
+ if (result.compare(maxOnly) > 0 && result.day > maxOnly.day) {
127
+ result = result.set({ day: maxOnly.day });
128
+ }
129
+ }
130
+ if (result.compare(dateOnly) === 0) {
131
+ return date;
132
+ }
133
+ if ("hour" in date) {
134
+ return date.set({ year: result.year, month: result.month, day: result.day });
135
+ }
136
+ return result;
137
+ }
102
138
  // Annotate the CommonJS export names for ESM import in node:
103
139
  0 && (module.exports = {
104
140
  alignCenter,
105
141
  alignEnd,
106
142
  alignStart,
143
+ constrainSegments,
107
144
  constrainStart,
108
145
  constrainValue
109
146
  });
@@ -78,10 +78,46 @@ function constrainValue(date, minValue, maxValue) {
78
78
  }
79
79
  return constrainedDateOnly;
80
80
  }
81
+ function constrainSegments(date, minValue, maxValue) {
82
+ const dateOnly = toCalendarDate(date);
83
+ const minOnly = minValue ? toCalendarDate(minValue) : void 0;
84
+ const maxOnly = maxValue ? toCalendarDate(maxValue) : void 0;
85
+ let result = dateOnly;
86
+ if (minOnly && result.compare(minOnly) < 0) {
87
+ if (result.year < minOnly.year) {
88
+ result = result.set({ year: minOnly.year });
89
+ }
90
+ if (result.compare(minOnly) < 0 && result.month < minOnly.month) {
91
+ result = result.set({ month: minOnly.month });
92
+ }
93
+ if (result.compare(minOnly) < 0 && result.day < minOnly.day) {
94
+ result = result.set({ day: minOnly.day });
95
+ }
96
+ }
97
+ if (maxOnly && result.compare(maxOnly) > 0) {
98
+ if (result.year > maxOnly.year) {
99
+ result = result.set({ year: maxOnly.year });
100
+ }
101
+ if (result.compare(maxOnly) > 0 && result.month > maxOnly.month) {
102
+ result = result.set({ month: maxOnly.month });
103
+ }
104
+ if (result.compare(maxOnly) > 0 && result.day > maxOnly.day) {
105
+ result = result.set({ day: maxOnly.day });
106
+ }
107
+ }
108
+ if (result.compare(dateOnly) === 0) {
109
+ return date;
110
+ }
111
+ if ("hour" in date) {
112
+ return date.set({ year: result.year, month: result.month, day: result.day });
113
+ }
114
+ return result;
115
+ }
81
116
  export {
82
117
  alignCenter,
83
118
  alignEnd,
84
119
  alignStart,
120
+ constrainSegments,
85
121
  constrainStart,
86
122
  constrainValue
87
123
  };
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { alignDate, alignStartDate } from './align.mjs';
2
2
  export { isDateEqual, isDateOutsideRange, isDateUnavailable, isNextRangeInvalid, isPreviousRangeInvalid } from './assertion.mjs';
3
- export { alignCenter, alignEnd, alignStart, constrainStart, constrainValue } from './constrain.mjs';
3
+ export { alignCenter, alignEnd, alignStart, constrainSegments, constrainStart, constrainValue } from './constrain.mjs';
4
4
  export { getEndDate, getUnitDuration } from './duration.mjs';
5
5
  export { formatRange, formatSelectedDate, formatVisibleRange } from './format.mjs';
6
6
  export { getDayFormatter, getMonthFormatter } from './formatter.mjs';
@@ -8,6 +8,7 @@ export { getDaysInWeek, getEndOfWeek, getMonthDays, getMonthNames, getStartOfWee
8
8
  export { YearsRange, getDecadeRange, getDefaultYearRange, getYearsRange, normalizeYear } from './date-year.mjs';
9
9
  export { getPreviousAvailableDate, getTodayDate, setCalendar, setDate } from './mutation.mjs';
10
10
  export { AdjustDateParams, AdjustDateReturn, getAdjustedDateFn, getNextPage, getNextRow, getNextSection, getPreviousPage, getPreviousRow, getPreviousSection, getSectionEnd, getSectionStart } from './pagination.mjs';
11
+ export { ensureValidCharacters, getLocaleSeparator, isValidCharacter } from './locale.mjs';
11
12
  export { parseDateString } from './parse-date.mjs';
12
13
  export { getDateRangePreset } from './preset.mjs';
13
14
  export { DateAdjustFn, DateGranularity, DateRangePreset, DateValue } from './types.mjs';
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { alignDate, alignStartDate } from './align.js';
2
2
  export { isDateEqual, isDateOutsideRange, isDateUnavailable, isNextRangeInvalid, isPreviousRangeInvalid } from './assertion.js';
3
- export { alignCenter, alignEnd, alignStart, constrainStart, constrainValue } from './constrain.js';
3
+ export { alignCenter, alignEnd, alignStart, constrainSegments, constrainStart, constrainValue } from './constrain.js';
4
4
  export { getEndDate, getUnitDuration } from './duration.js';
5
5
  export { formatRange, formatSelectedDate, formatVisibleRange } from './format.js';
6
6
  export { getDayFormatter, getMonthFormatter } from './formatter.js';
@@ -8,6 +8,7 @@ export { getDaysInWeek, getEndOfWeek, getMonthDays, getMonthNames, getStartOfWee
8
8
  export { YearsRange, getDecadeRange, getDefaultYearRange, getYearsRange, normalizeYear } from './date-year.js';
9
9
  export { getPreviousAvailableDate, getTodayDate, setCalendar, setDate } from './mutation.js';
10
10
  export { AdjustDateParams, AdjustDateReturn, getAdjustedDateFn, getNextPage, getNextRow, getNextSection, getPreviousPage, getPreviousRow, getPreviousSection, getSectionEnd, getSectionStart } from './pagination.js';
11
+ export { ensureValidCharacters, getLocaleSeparator, isValidCharacter } from './locale.js';
11
12
  export { parseDateString } from './parse-date.js';
12
13
  export { getDateRangePreset } from './preset.js';
13
14
  export { DateAdjustFn, DateGranularity, DateRangePreset, DateValue } from './types.js';
package/dist/index.js CHANGED
@@ -27,6 +27,7 @@ __reExport(index_exports, require("./date-month.js"), module.exports);
27
27
  __reExport(index_exports, require("./date-year.js"), module.exports);
28
28
  __reExport(index_exports, require("./mutation.js"), module.exports);
29
29
  __reExport(index_exports, require("./pagination.js"), module.exports);
30
+ __reExport(index_exports, require("./locale.js"), module.exports);
30
31
  __reExport(index_exports, require("./parse-date.js"), module.exports);
31
32
  __reExport(index_exports, require("./preset.js"), module.exports);
32
33
  // Annotate the CommonJS export names for ESM import in node:
@@ -41,6 +42,7 @@ __reExport(index_exports, require("./preset.js"), module.exports);
41
42
  ...require("./date-year.js"),
42
43
  ...require("./mutation.js"),
43
44
  ...require("./pagination.js"),
45
+ ...require("./locale.js"),
44
46
  ...require("./parse-date.js"),
45
47
  ...require("./preset.js")
46
48
  });
package/dist/index.mjs CHANGED
@@ -9,5 +9,6 @@ export * from "./date-month.mjs";
9
9
  export * from "./date-year.mjs";
10
10
  export * from "./mutation.mjs";
11
11
  export * from "./pagination.mjs";
12
+ export * from "./locale.mjs";
12
13
  export * from "./parse-date.mjs";
13
14
  export * from "./preset.mjs";
@@ -0,0 +1,5 @@
1
+ declare const isValidCharacter: (char: string | null, separator: string) => boolean;
2
+ declare const ensureValidCharacters: (value: string, separator: string) => string;
3
+ declare function getLocaleSeparator(locale: string): string;
4
+
5
+ export { ensureValidCharacters, getLocaleSeparator, isValidCharacter };
@@ -0,0 +1,5 @@
1
+ declare const isValidCharacter: (char: string | null, separator: string) => boolean;
2
+ declare const ensureValidCharacters: (value: string, separator: string) => string;
3
+ declare function getLocaleSeparator(locale: string): string;
4
+
5
+ export { ensureValidCharacters, getLocaleSeparator, isValidCharacter };
package/dist/locale.js ADDED
@@ -0,0 +1,50 @@
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/locale.ts
21
+ var locale_exports = {};
22
+ __export(locale_exports, {
23
+ ensureValidCharacters: () => ensureValidCharacters,
24
+ getLocaleSeparator: () => getLocaleSeparator,
25
+ isValidCharacter: () => isValidCharacter
26
+ });
27
+ module.exports = __toCommonJS(locale_exports);
28
+ var isValidCharacter = (char, separator) => {
29
+ if (!char) return true;
30
+ return /\d/.test(char) || separator.includes(char) || char.length !== 1;
31
+ };
32
+ var ensureValidCharacters = (value, separator) => {
33
+ return value.split("").filter((char) => isValidCharacter(char, separator)).join("");
34
+ };
35
+ var separatorCache = /* @__PURE__ */ new Map();
36
+ function getLocaleSeparator(locale) {
37
+ let separator = separatorCache.get(locale);
38
+ if (separator != null) return separator;
39
+ const parts = new Intl.DateTimeFormat(locale).formatToParts(/* @__PURE__ */ new Date());
40
+ const literal = parts.find((part) => part.type === "literal");
41
+ separator = literal ? literal.value : "/";
42
+ separatorCache.set(locale, separator);
43
+ return separator;
44
+ }
45
+ // Annotate the CommonJS export names for ESM import in node:
46
+ 0 && (module.exports = {
47
+ ensureValidCharacters,
48
+ getLocaleSeparator,
49
+ isValidCharacter
50
+ });
@@ -0,0 +1,23 @@
1
+ // src/locale.ts
2
+ var isValidCharacter = (char, separator) => {
3
+ if (!char) return true;
4
+ return /\d/.test(char) || separator.includes(char) || char.length !== 1;
5
+ };
6
+ var ensureValidCharacters = (value, separator) => {
7
+ return value.split("").filter((char) => isValidCharacter(char, separator)).join("");
8
+ };
9
+ var separatorCache = /* @__PURE__ */ new Map();
10
+ function getLocaleSeparator(locale) {
11
+ let separator = separatorCache.get(locale);
12
+ if (separator != null) return separator;
13
+ const parts = new Intl.DateTimeFormat(locale).formatToParts(/* @__PURE__ */ new Date());
14
+ const literal = parts.find((part) => part.type === "literal");
15
+ separator = literal ? literal.value : "/";
16
+ separatorCache.set(locale, separator);
17
+ return separator;
18
+ }
19
+ export {
20
+ ensureValidCharacters,
21
+ getLocaleSeparator,
22
+ isValidCharacter
23
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/date-utils",
3
- "version": "1.40.0",
3
+ "version": "1.41.0",
4
4
  "description": "Date utilities for zag.js",
5
5
  "keywords": [
6
6
  "js",
@@ -27,7 +27,7 @@
27
27
  "@internationalized/date": ">=3.0.0"
28
28
  },
29
29
  "devDependencies": {
30
- "@internationalized/date": "3.12.0",
30
+ "@internationalized/date": "3.12.1",
31
31
  "clean-package": "2.2.0"
32
32
  },
33
33
  "module": "dist/index.mjs",