@superdispatch/dates 0.25.2 → 0.26.1

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.
@@ -1,67 +1,89 @@
1
1
  import { DateTime } from 'luxon';
2
2
  import { useMemo } from 'react';
3
+
4
+ /** @deprecated */
5
+
6
+ /** @deprecated */
7
+
8
+ /** @deprecated */
9
+
10
+ /** @deprecated */
11
+
12
+ /** @deprecated */
13
+
14
+ /** @deprecated */
15
+
16
+ /** @deprecated */
17
+
18
+ /** @deprecated */
19
+
20
+ /** @deprecated */
21
+
3
22
  /** @deprecated */
4
23
 
5
24
  /** @deprecated */
6
25
  function toDateTime(value) {
7
26
  return typeof value === 'number' ? DateTime.fromMillis(value) : DateTime.fromJSDate(value);
8
27
  }
9
- /** @deprecated */
10
-
11
28
 
29
+ /** @deprecated */
12
30
  export function isDate(value) {
13
31
  return value != null && value instanceof Date;
14
32
  }
15
- /** @deprecated */
16
33
 
34
+ /** @deprecated */
17
35
  export function isDateLike(value) {
18
36
  return isDate(value) || typeof value === 'number' && Number.isInteger(value);
19
37
  }
20
- /** @deprecated */
21
38
 
39
+ /** @deprecated */
22
40
  export function isValidDate(value) {
23
41
  return isDate(value) && Number.isFinite(value.getTime());
24
42
  }
25
- /** @deprecated */
26
43
 
44
+ /** @deprecated */
27
45
  function checkRange(range, validator) {
28
46
  if (!Array.isArray(range) || range.length > 2) {
29
47
  return false;
30
48
  }
31
-
32
49
  var [start, finish] = range;
33
50
  return (start == null || validator(start)) && (finish == null || validator(finish));
34
51
  }
35
- /** @deprecated */
36
-
37
52
 
53
+ /** @deprecated */
38
54
  export function isDateRange(range) {
39
55
  return checkRange(range, isDate);
40
56
  }
41
- /** @deprecated */
42
57
 
58
+ /** @deprecated */
43
59
  export function isDateRangeLike(range) {
44
60
  return checkRange(range, isDateLike);
45
61
  }
46
- /** @deprecated */
47
62
 
63
+ /** @deprecated */
48
64
  export function isValidDateRange(range) {
49
65
  return checkRange(range, isValidDate);
50
66
  }
51
- /** @deprecated */
52
67
 
68
+ /** @deprecated */
53
69
  export function toDate(value) {
54
70
  return !isDateLike(value) ? new Date(NaN) : new Date(value);
55
71
  }
56
- /** @deprecated */
57
72
 
73
+ /** @deprecated */
58
74
  export function toDateRange(range) {
59
75
  if (range == null || !isDateRangeLike(range)) {
60
76
  return [];
61
77
  }
62
-
63
78
  return range.filter(x => x != null).map(x => x == null ? undefined : toDate(x)).sort((a, b) => !isValidDate(a) ? -1 : !isValidDate(b) ? 1 : a.valueOf() - b.valueOf());
64
79
  }
80
+
81
+ /** @deprecated */
82
+
83
+ /** @deprecated */
84
+
85
+ /** @deprecated */
86
+
65
87
  /** @deprecated */
66
88
 
67
89
  /** @deprecated */
@@ -89,9 +111,8 @@ export class DateUtils {
89
111
  millisecond
90
112
  };
91
113
  }
92
- /** @deprecated */
93
-
94
114
 
115
+ /** @deprecated */
95
116
  fromObject(_ref) {
96
117
  var {
97
118
  year = 0,
@@ -102,11 +123,9 @@ export class DateUtils {
102
123
  second = 0,
103
124
  millisecond = 0
104
125
  } = _ref;
105
-
106
126
  if (Number.isNaN(year) || Number.isNaN(month) || Number.isNaN(day) || Number.isNaN(hour) || Number.isNaN(minute) || Number.isNaN(second) || Number.isNaN(millisecond)) {
107
127
  return new Date(NaN);
108
128
  }
109
-
110
129
  return DateTime.fromObject({
111
130
  year,
112
131
  month,
@@ -117,15 +136,13 @@ export class DateUtils {
117
136
  millisecond
118
137
  }).toJSDate();
119
138
  }
120
- /** @deprecated */
121
-
122
139
 
140
+ /** @deprecated */
123
141
  update(value, values) {
124
142
  return toDateTime(value).set(values).toJSDate();
125
143
  }
126
- /** @deprecated */
127
-
128
144
 
145
+ /** @deprecated */
129
146
  mergeDateAndTime(date, time) {
130
147
  var {
131
148
  hour,
@@ -133,11 +150,9 @@ export class DateUtils {
133
150
  second,
134
151
  millisecond
135
152
  } = this.toObject(time);
136
-
137
153
  if (Number.isNaN(hour) || Number.isNaN(minute) || Number.isNaN(second) || Number.isNaN(millisecond)) {
138
154
  return new Date(NaN);
139
155
  }
140
-
141
156
  return this.update(date, {
142
157
  hour,
143
158
  minute,
@@ -145,73 +160,61 @@ export class DateUtils {
145
160
  millisecond
146
161
  });
147
162
  }
148
- /** @deprecated */
149
-
150
163
 
164
+ /** @deprecated */
151
165
  startOf(value, unit) {
152
166
  return toDateTime(value).startOf(unit).toJSDate();
153
167
  }
154
- /** @deprecated */
155
-
156
168
 
169
+ /** @deprecated */
157
170
  endOf(value, unit) {
158
171
  return toDateTime(value).endOf(unit).toJSDate();
159
172
  }
160
- /** @deprecated */
161
-
162
173
 
174
+ /** @deprecated */
163
175
  plus(value, values) {
164
176
  return toDateTime(value).plus(values).toJSDate();
165
177
  }
166
- /** @deprecated */
167
-
168
178
 
179
+ /** @deprecated */
169
180
  minus(value, values) {
170
181
  return toDateTime(value).minus(values).toJSDate();
171
182
  }
172
- /** @deprecated */
173
-
174
183
 
184
+ /** @deprecated */
175
185
  isSameDate(value, compare) {
176
186
  var unit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'millisecond';
177
-
178
187
  if (value == null && compare == null) {
179
188
  return true;
180
189
  }
181
-
182
190
  if (value == null || compare == null) {
183
191
  return false;
184
192
  }
185
-
186
193
  var dateTimeValue = toDateTime(value);
187
194
  var dateTimeCompare = toDateTime(compare);
188
195
  return dateTimeValue.isValid && dateTimeCompare.isValid && dateTimeValue.startOf(unit).equals(dateTimeCompare.startOf(unit));
189
196
  }
190
- /** @deprecated */
191
-
192
197
 
198
+ /** @deprecated */
193
199
  isSameDateRange(value, compare, unit) {
194
200
  var range1 = toDateRange(value);
195
201
  var range2 = toDateRange(compare);
196
202
  return !range1.some((date, idx) => !this.isSameDate(date, range2[idx], unit));
197
203
  }
198
- /** @deprecated */
199
-
200
204
 
205
+ /** @deprecated */
201
206
  diff(value, compare, unit) {
202
207
  var valueDateTime = toDateTime(value);
203
208
  var compareDateTime = toDateTime(compare);
204
209
  return valueDateTime.diff(compareDateTime, unit).as(unit);
205
210
  }
206
- /** @deprecated */
207
-
208
211
 
212
+ /** @deprecated */
209
213
  toLocaleString(value, options) {
210
214
  return toDateTime(value).toLocaleString(options);
211
215
  }
212
- /** @deprecated */
213
-
214
216
 
217
+ /** @deprecated */
215
218
  format(value, variant) {
216
219
  return this.toLocaleString(value, variant === 'date' ? {
217
220
  day: '2-digit',
@@ -231,30 +234,24 @@ export class DateUtils {
231
234
  minute: 'numeric'
232
235
  });
233
236
  }
234
- /** @deprecated */
235
-
236
237
 
238
+ /** @deprecated */
237
239
  formatRange(value) {
238
240
  var emptyText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
239
241
  var range = toDateRange(value);
240
-
241
242
  if (!isValidDateRange(range)) {
242
243
  return 'Invalid Date Range';
243
244
  }
244
-
245
245
  var [start, finish] = range;
246
-
247
246
  if (!start) {
248
247
  return emptyText;
249
248
  }
250
-
251
249
  var startText = this.format(start, !this.isSameDate(start, finish, 'year') ? 'date' : 'shortDate');
252
250
  var finishText = !finish ? '…' : this.format(finish, 'date');
253
251
  return "".concat(startText, " - ").concat(finishText);
254
252
  }
255
- /** @deprecated */
256
-
257
253
 
254
+ /** @deprecated */
258
255
  formatRelativeTime(value) {
259
256
  var {
260
257
  style = 'long',
@@ -267,7 +264,6 @@ export class DateUtils {
267
264
  base: compareDateTime
268
265
  });
269
266
  }
270
-
271
267
  }
272
268
  export function useDateUtils() {
273
269
  return useMemo(() => new DateUtils(), []);
@@ -1,7 +1,7 @@
1
1
  import _objectSpread from "@babel/runtime/helpers/objectSpread2";
2
2
  import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
3
  var _excluded = ["variant", "fallback"],
4
- _excluded2 = ["date", "fallback"];
4
+ _excluded2 = ["date", "fallback"];
5
5
  import { renderChildren } from '@superdispatch/ui';
6
6
  import { useMemo } from 'react';
7
7
  import { useDateConfig } from "../date-config/DateConfig.js";
@@ -9,11 +9,10 @@ import { formatDate } from "../date-time-utils/DateTimeUtils.js";
9
9
  import { useDateTime } from "../use-date-time/useDateTime.js";
10
10
  export function useFormattedDate(input, _ref) {
11
11
  var {
12
- variant,
13
- fallback
14
- } = _ref,
15
- dateConfig = _objectWithoutProperties(_ref, _excluded);
16
-
12
+ variant,
13
+ fallback
14
+ } = _ref,
15
+ dateConfig = _objectWithoutProperties(_ref, _excluded);
17
16
  var config = useDateConfig(dateConfig);
18
17
  var date = useDateTime(input, config);
19
18
  return useMemo(() => formatDate(date, {
@@ -23,11 +22,10 @@ export function useFormattedDate(input, _ref) {
23
22
  }
24
23
  export function FormattedDate(_ref2) {
25
24
  var {
26
- date,
27
- fallback = 'Invalid Date'
28
- } = _ref2,
29
- options = _objectWithoutProperties(_ref2, _excluded2);
30
-
25
+ date,
26
+ fallback = 'Invalid Date'
27
+ } = _ref2,
28
+ options = _objectWithoutProperties(_ref2, _excluded2);
31
29
  var formatted = useFormattedDate(date, _objectSpread(_objectSpread({}, options), {}, {
32
30
  fallback: ''
33
31
  }));
@@ -1,7 +1,7 @@
1
1
  import _objectSpread from "@babel/runtime/helpers/objectSpread2";
2
2
  import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
3
  var _excluded = ["unit", "round", "padding", "fallback", "base"],
4
- _excluded2 = ["date", "fallback"];
4
+ _excluded2 = ["date", "fallback"];
5
5
  import { renderChildren } from '@superdispatch/ui';
6
6
  import { useMemo } from 'react';
7
7
  import { useDateConfig } from "../date-config/DateConfig.js";
@@ -9,15 +9,14 @@ import { formatRelativeTime } from "../date-time-utils/DateTimeUtils.js";
9
9
  import { useDateTime } from "../use-date-time/useDateTime.js";
10
10
  export function useFormattedRelativeTime(input) {
11
11
  var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
12
- {
13
- unit,
14
- round,
15
- padding,
16
- fallback,
17
- base: baseOption
18
- } = _ref,
19
- dateConfig = _objectWithoutProperties(_ref, _excluded);
20
-
12
+ {
13
+ unit,
14
+ round,
15
+ padding,
16
+ fallback,
17
+ base: baseOption
18
+ } = _ref,
19
+ dateConfig = _objectWithoutProperties(_ref, _excluded);
21
20
  var config = useDateConfig(dateConfig);
22
21
  var date = useDateTime(input, config);
23
22
  var baseOptionDate = useDateTime(baseOption, config);
@@ -32,11 +31,10 @@ export function useFormattedRelativeTime(input) {
32
31
  }
33
32
  export function FormattedRelativeTime(_ref2) {
34
33
  var {
35
- date,
36
- fallback = 'Invalid Date'
37
- } = _ref2,
38
- options = _objectWithoutProperties(_ref2, _excluded2);
39
-
34
+ date,
35
+ fallback = 'Invalid Date'
36
+ } = _ref2,
37
+ options = _objectWithoutProperties(_ref2, _excluded2);
40
38
  var formatted = useFormattedRelativeTime(date, _objectSpread(_objectSpread({}, options), {}, {
41
39
  fallback: ''
42
40
  }));
@@ -10,7 +10,6 @@ import { formatDate, toDatePayload } from "../date-time-utils/DateTimeUtils.js";
10
10
  import { useDateTime } from "../use-date-time/useDateTime.js";
11
11
  import { jsx as _jsx } from "react/jsx-runtime";
12
12
  var TIME_MATCH_FORMATS = ['h:mm a', 'h:mma', 'H:mm', 'h:mm', 'hmm', 'Hmm', 'h', 'H'];
13
-
14
13
  function toTimeFieldOption(date, config) {
15
14
  return {
16
15
  value: date.valueOf(),
@@ -20,52 +19,42 @@ function toTimeFieldOption(date, config) {
20
19
  pattern: new RegExp("^(".concat(TIME_MATCH_FORMATS.map(format => date.toFormat(format)).join('|'), ")"), 'i')
21
20
  };
22
21
  }
23
-
24
22
  function normalizeInputValue(inputValue) {
25
23
  return inputValue.replace(/[\s]/g, '').toLowerCase();
26
24
  }
27
-
28
25
  function makeOptions(config) {
29
26
  var options = [];
30
27
  var now = DateTime.local().startOf('day');
31
-
32
28
  for (var i = 0; i < 96; i++) {
33
29
  options.push(toTimeFieldOption(now.set({
34
30
  minute: i * 15
35
31
  }), config));
36
32
  }
37
-
38
33
  var cache = new Map();
39
34
  return [options, (_, _ref) => {
40
35
  var {
41
36
  inputValue
42
37
  } = _ref;
43
38
  var query = normalizeInputValue(inputValue);
44
-
45
39
  if (!query) {
46
40
  return options;
47
41
  }
48
-
49
42
  var filtered = cache.get(query);
50
-
51
43
  if (!filtered) {
52
44
  filtered = options.filter(option => option.pattern.test(query));
53
45
  cache.set(query, filtered);
54
46
  }
55
-
56
47
  return filtered;
57
48
  }];
58
49
  }
59
-
60
50
  export var TimeField = /*#__PURE__*/forwardRef((_ref2, ref) => {
61
51
  var {
62
- disabled,
63
- onChange,
64
- value: valueProp,
65
- format: formatProp
66
- } = _ref2,
67
- props = _objectWithoutProperties(_ref2, _excluded);
68
-
52
+ disabled,
53
+ onChange,
54
+ value: valueProp,
55
+ format: formatProp
56
+ } = _ref2,
57
+ props = _objectWithoutProperties(_ref2, _excluded);
69
58
  var config = useDateConfig({
70
59
  format: formatProp
71
60
  });
@@ -73,19 +62,15 @@ export var TimeField = /*#__PURE__*/forwardRef((_ref2, ref) => {
73
62
  var selectedOption = useMemo(() => !date.isValid ? undefined : toTimeFieldOption(date, config), [date, config]);
74
63
  var [options, filterOptions] = useMemo(() => makeOptions(config), [config]);
75
64
  var [inputValue, setInputValue] = useState('');
76
-
77
65
  function handleChange(nextValue) {
78
66
  if (onChange) {
79
67
  onChange(toDatePayload(nextValue, config));
80
68
  }
81
69
  }
82
-
83
70
  function handleType(text) {
84
71
  text = normalizeInputValue(text);
85
-
86
72
  for (var timeFormat of TIME_MATCH_FORMATS) {
87
73
  var nextDate = DateTime.fromFormat(text, timeFormat);
88
-
89
74
  if (nextDate.isValid) {
90
75
  if (onChange) {
91
76
  if (date.isValid) {
@@ -95,17 +80,13 @@ export var TimeField = /*#__PURE__*/forwardRef((_ref2, ref) => {
95
80
  day: date.day
96
81
  });
97
82
  }
98
-
99
83
  onChange(toDatePayload(nextDate, config));
100
84
  }
101
-
102
85
  return;
103
86
  }
104
87
  }
105
-
106
88
  setInputValue((selectedOption === null || selectedOption === void 0 ? void 0 : selectedOption.label) || '');
107
89
  }
108
-
109
90
  useEffect(() => {
110
91
  if (!date.isValid) {
111
92
  setInputValue('');
@@ -6,14 +6,12 @@ export function useDateTime(input, options) {
6
6
  var primitiveInput = toPrimitiveDateInput(input);
7
7
  return useMemo(() => {
8
8
  var date = parseDate(primitiveInput, config);
9
-
10
9
  if (process.env.NODE_ENV !== 'production') {
11
10
  if (!date.isValid && typeof primitiveInput === 'string') {
12
11
  // eslint-disable-next-line no-console
13
12
  console.error("[useDateTime] Failed to parse \"".concat(primitiveInput, "\" string with \"").concat(config.format, "\" format."));
14
13
  }
15
14
  }
16
-
17
15
  return date;
18
16
  }, [config, primitiveInput]);
19
17
  }