@loykin/datetime-range 0.0.2 → 0.0.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/dist/index.cjs +13 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +13 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -227,6 +227,7 @@ declare function toDisplayString(value: DateTimeRangeValue, options?: {
|
|
|
227
227
|
* toUrlString({ type: 'relative', relativeNow: true }) // "now"
|
|
228
228
|
* toUrlString({ type: 'relative', relativeValue: '5', relativeFormat: 'Minutes ago' }) // "now-5m"
|
|
229
229
|
* toUrlString({ type: 'absolute', absoluteValue: new Date() }) // "1709654400000"
|
|
230
|
+
* toUrlString({ type: 'absolute' }) // "now" (no date to serialize)
|
|
230
231
|
*/
|
|
231
232
|
declare function toUrlString(value: DateTimeRangeValue): string;
|
|
232
233
|
/**
|
|
@@ -235,6 +236,7 @@ declare function toUrlString(value: DateTimeRangeValue): string;
|
|
|
235
236
|
* fromUrlString("now") // { type: 'relative', relativeNow: true }
|
|
236
237
|
* fromUrlString("now-5m") // { type: 'relative', relativeValue: '5', relativeFormat: 'Minutes ago' }
|
|
237
238
|
* fromUrlString("1234567890000") // { type: 'absolute', absoluteValue: Date }
|
|
239
|
+
* fromUrlString("123oops") // { type: 'relative', relativeNow: true } (unparseable input)
|
|
238
240
|
*/
|
|
239
241
|
declare function fromUrlString(urlString: string): DateTimeRangeValue;
|
|
240
242
|
/** Creates a relative "now" value. */
|
package/dist/index.d.ts
CHANGED
|
@@ -227,6 +227,7 @@ declare function toDisplayString(value: DateTimeRangeValue, options?: {
|
|
|
227
227
|
* toUrlString({ type: 'relative', relativeNow: true }) // "now"
|
|
228
228
|
* toUrlString({ type: 'relative', relativeValue: '5', relativeFormat: 'Minutes ago' }) // "now-5m"
|
|
229
229
|
* toUrlString({ type: 'absolute', absoluteValue: new Date() }) // "1709654400000"
|
|
230
|
+
* toUrlString({ type: 'absolute' }) // "now" (no date to serialize)
|
|
230
231
|
*/
|
|
231
232
|
declare function toUrlString(value: DateTimeRangeValue): string;
|
|
232
233
|
/**
|
|
@@ -235,6 +236,7 @@ declare function toUrlString(value: DateTimeRangeValue): string;
|
|
|
235
236
|
* fromUrlString("now") // { type: 'relative', relativeNow: true }
|
|
236
237
|
* fromUrlString("now-5m") // { type: 'relative', relativeValue: '5', relativeFormat: 'Minutes ago' }
|
|
237
238
|
* fromUrlString("1234567890000") // { type: 'absolute', absoluteValue: Date }
|
|
239
|
+
* fromUrlString("123oops") // { type: 'relative', relativeNow: true } (unparseable input)
|
|
238
240
|
*/
|
|
239
241
|
declare function fromUrlString(urlString: string): DateTimeRangeValue;
|
|
240
242
|
/** Creates a relative "now" value. */
|
package/dist/index.js
CHANGED
|
@@ -928,17 +928,20 @@ function getSegmentValue(date, key) {
|
|
|
928
928
|
return date.getSeconds();
|
|
929
929
|
}
|
|
930
930
|
}
|
|
931
|
+
function daysInMonth(year, monthIndex) {
|
|
932
|
+
return new Date(year, monthIndex + 1, 0).getDate();
|
|
933
|
+
}
|
|
931
934
|
function applySegment(date, key, value) {
|
|
932
935
|
const d = new Date(date);
|
|
933
936
|
switch (key) {
|
|
934
937
|
case "year":
|
|
935
|
-
d.setFullYear(value);
|
|
938
|
+
d.setFullYear(value, d.getMonth(), Math.min(d.getDate(), daysInMonth(value, d.getMonth())));
|
|
936
939
|
break;
|
|
937
940
|
case "month":
|
|
938
|
-
d.setMonth(value - 1);
|
|
941
|
+
d.setMonth(value - 1, Math.min(d.getDate(), daysInMonth(d.getFullYear(), value - 1)));
|
|
939
942
|
break;
|
|
940
943
|
case "day":
|
|
941
|
-
d.setDate(value);
|
|
944
|
+
d.setDate(Math.min(value, daysInMonth(d.getFullYear(), d.getMonth())));
|
|
942
945
|
break;
|
|
943
946
|
case "hour":
|
|
944
947
|
d.setHours(value);
|
|
@@ -1174,8 +1177,9 @@ function toDisplayString(value, options) {
|
|
|
1174
1177
|
}
|
|
1175
1178
|
function toUrlString(value) {
|
|
1176
1179
|
if (value.type === "absolute") {
|
|
1177
|
-
|
|
1178
|
-
|
|
1180
|
+
if (!value.absoluteValue) return "now";
|
|
1181
|
+
const time = new Date(value.absoluteValue).getTime();
|
|
1182
|
+
return Number.isNaN(time) ? "now" : time.toString();
|
|
1179
1183
|
}
|
|
1180
1184
|
if (value.relativeNow) return "now";
|
|
1181
1185
|
if (typeof value.relativeValue === "string" && !value.relativeFormat) {
|
|
@@ -1217,8 +1221,10 @@ function fromUrlString(urlString) {
|
|
|
1217
1221
|
}
|
|
1218
1222
|
return { type: "relative", relativeNow: true };
|
|
1219
1223
|
}
|
|
1220
|
-
|
|
1221
|
-
|
|
1224
|
+
if (/^-?\d+$/.test(urlString)) {
|
|
1225
|
+
const date = new Date(Number(urlString));
|
|
1226
|
+
if (!Number.isNaN(date.getTime())) return { type: "absolute", absoluteValue: date };
|
|
1227
|
+
}
|
|
1222
1228
|
return { type: "relative", relativeNow: true };
|
|
1223
1229
|
}
|
|
1224
1230
|
function relativeNow() {
|