date-vir 4.0.2 → 4.1.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.
- package/dist/cjs/formatting/relative-string.js +17 -2
- package/dist/cjs/full-date/create-full-date.js +3 -3
- package/dist/cjs/full-date/parsing.js +2 -2
- package/dist/esm/formatting/relative-string.d.ts +6 -0
- package/dist/esm/formatting/relative-string.js +17 -2
- package/dist/esm/full-date/create-full-date.js +3 -3
- package/dist/esm/full-date/parsing.js +2 -2
- package/package.json +16 -16
|
@@ -4,6 +4,17 @@ exports.toRelativeString = void 0;
|
|
|
4
4
|
const common_1 = require("@augment-vir/common");
|
|
5
5
|
const diff_dates_1 = require("../date-operations/diff-dates");
|
|
6
6
|
const duration_1 = require("../duration");
|
|
7
|
+
const maxDurations = {
|
|
8
|
+
[duration_1.DurationUnit.Milliseconds]: 1000,
|
|
9
|
+
[duration_1.DurationUnit.Seconds]: 60,
|
|
10
|
+
[duration_1.DurationUnit.Minutes]: 60,
|
|
11
|
+
[duration_1.DurationUnit.Hours]: 24,
|
|
12
|
+
[duration_1.DurationUnit.Days]: 30,
|
|
13
|
+
[duration_1.DurationUnit.Weeks]: 4,
|
|
14
|
+
[duration_1.DurationUnit.Months]: 12,
|
|
15
|
+
[duration_1.DurationUnit.Quarters]: 4,
|
|
16
|
+
[duration_1.DurationUnit.Years]: Infinity,
|
|
17
|
+
};
|
|
7
18
|
function toRelativeString({ fullDate, relativeTo, options = {}, }) {
|
|
8
19
|
const roundingDigits = options.decimalDigitCount || 0;
|
|
9
20
|
const allUnitDiffs = (0, diff_dates_1.diffDatesAllUnits)({
|
|
@@ -11,8 +22,12 @@ function toRelativeString({ fullDate, relativeTo, options = {}, }) {
|
|
|
11
22
|
end: relativeTo,
|
|
12
23
|
});
|
|
13
24
|
const unitsWithinBounds = (0, common_1.mapObjectValues)(allUnitDiffs, (durationUnit, duration) => {
|
|
14
|
-
const
|
|
15
|
-
|
|
25
|
+
const roundedDuration = Math.floor(Math.abs((0, common_1.round)({ digits: 1, number: duration })));
|
|
26
|
+
const isAboveZero = roundedDuration > 0;
|
|
27
|
+
const isBelowMax = options.limitUnitMax
|
|
28
|
+
? roundedDuration < maxDurations[durationUnit]
|
|
29
|
+
: true;
|
|
30
|
+
return isAboveZero && isBelowMax;
|
|
16
31
|
});
|
|
17
32
|
const unitToUse = duration_1.orderedDurationUnits.reduce((chosenUnit, currentUnit) => {
|
|
18
33
|
const isUnitAllowed = !options.blockedRelativeUnits?.includes(currentUnit);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getNowFullDate = exports.toNewTimezone = exports.createFullDate = exports.createUtcFullDate = exports.createFullDateInUserTimezone = void 0;
|
|
4
|
-
const common_1 = require("@augment-vir/common");
|
|
5
4
|
const luxon_1 = require("luxon");
|
|
5
|
+
const run_time_assertions_1 = require("run-time-assertions");
|
|
6
6
|
const timezones_1 = require("../timezone/timezones");
|
|
7
7
|
const is_valid_full_date_1 = require("./is-valid-full-date");
|
|
8
8
|
const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
|
|
@@ -58,10 +58,10 @@ function convertDateLikeToLuxonDateTime(dateLike, timezone) {
|
|
|
58
58
|
if (luxon_1.DateTime.isDateTime(dateLike)) {
|
|
59
59
|
return dateLike.setZone(timezone);
|
|
60
60
|
}
|
|
61
|
-
else if ((0,
|
|
61
|
+
else if ((0, run_time_assertions_1.isRunTimeType)(dateLike, 'number')) {
|
|
62
62
|
return luxon_1.DateTime.fromMillis(dateLike, { zone: timezones_1.utcTimezone }).setZone(timezone);
|
|
63
63
|
}
|
|
64
|
-
else if ((0,
|
|
64
|
+
else if ((0, run_time_assertions_1.isRunTimeType)(dateLike, 'string')) {
|
|
65
65
|
const dateTime = luxon_1.DateTime.fromISO(dateLike, { zone: timezone });
|
|
66
66
|
if (!dateTime.isValid) {
|
|
67
67
|
return lastDitchConversion(dateLike);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseInputElementValue = exports.parseStrangeString = void 0;
|
|
4
|
-
const common_1 = require("@augment-vir/common");
|
|
5
4
|
const luxon_1 = require("luxon");
|
|
5
|
+
const run_time_assertions_1 = require("run-time-assertions");
|
|
6
6
|
const create_full_date_1 = require("./create-full-date");
|
|
7
7
|
const luxon_date_time_conversion_1 = require("./luxon-date-time-conversion");
|
|
8
8
|
/**
|
|
@@ -35,7 +35,7 @@ function parseInputElementValue(elementOrValue, timezone) {
|
|
|
35
35
|
if (!elementOrValue) {
|
|
36
36
|
return undefined;
|
|
37
37
|
}
|
|
38
|
-
const value = (0,
|
|
38
|
+
const value = (0, run_time_assertions_1.isRunTimeType)(elementOrValue, 'string')
|
|
39
39
|
? elementOrValue
|
|
40
40
|
: elementOrValue.value;
|
|
41
41
|
try {
|
|
@@ -17,6 +17,12 @@ export type RelativeStringOptions = {
|
|
|
17
17
|
decimalDigitCount: number;
|
|
18
18
|
/** Set this to true to block the 'just now' relative string. By default, that string is allowed. */
|
|
19
19
|
blockJustNow: true;
|
|
20
|
+
/**
|
|
21
|
+
* Set to true to limit durations to their max value. Each unit's max value is its value when it
|
|
22
|
+
* turns into the subsequent unit. For example, seconds maxes out at 60, at which point it
|
|
23
|
+
* becomes a minute.
|
|
24
|
+
*/
|
|
25
|
+
limitUnitMax: true;
|
|
20
26
|
};
|
|
21
27
|
export declare function toRelativeString({ fullDate, relativeTo, options, }: {
|
|
22
28
|
fullDate: Readonly<FullDate>;
|
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
import { areJsonEqual, mapObjectValues, round } from '@augment-vir/common';
|
|
2
2
|
import { diffDatesAllUnits } from '../date-operations/diff-dates';
|
|
3
3
|
import { DurationUnit, orderedDurationUnits, singularDurationUnitNames } from '../duration';
|
|
4
|
+
const maxDurations = {
|
|
5
|
+
[DurationUnit.Milliseconds]: 1000,
|
|
6
|
+
[DurationUnit.Seconds]: 60,
|
|
7
|
+
[DurationUnit.Minutes]: 60,
|
|
8
|
+
[DurationUnit.Hours]: 24,
|
|
9
|
+
[DurationUnit.Days]: 30,
|
|
10
|
+
[DurationUnit.Weeks]: 4,
|
|
11
|
+
[DurationUnit.Months]: 12,
|
|
12
|
+
[DurationUnit.Quarters]: 4,
|
|
13
|
+
[DurationUnit.Years]: Infinity,
|
|
14
|
+
};
|
|
4
15
|
export function toRelativeString({ fullDate, relativeTo, options = {}, }) {
|
|
5
16
|
const roundingDigits = options.decimalDigitCount || 0;
|
|
6
17
|
const allUnitDiffs = diffDatesAllUnits({
|
|
@@ -8,8 +19,12 @@ export function toRelativeString({ fullDate, relativeTo, options = {}, }) {
|
|
|
8
19
|
end: relativeTo,
|
|
9
20
|
});
|
|
10
21
|
const unitsWithinBounds = mapObjectValues(allUnitDiffs, (durationUnit, duration) => {
|
|
11
|
-
const
|
|
12
|
-
|
|
22
|
+
const roundedDuration = Math.floor(Math.abs(round({ digits: 1, number: duration })));
|
|
23
|
+
const isAboveZero = roundedDuration > 0;
|
|
24
|
+
const isBelowMax = options.limitUnitMax
|
|
25
|
+
? roundedDuration < maxDurations[durationUnit]
|
|
26
|
+
: true;
|
|
27
|
+
return isAboveZero && isBelowMax;
|
|
13
28
|
});
|
|
14
29
|
const unitToUse = orderedDurationUnits.reduce((chosenUnit, currentUnit) => {
|
|
15
30
|
const isUnitAllowed = !options.blockedRelativeUnits?.includes(currentUnit);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { isRuntimeTypeOf } from '@augment-vir/common';
|
|
2
1
|
import { DateTime } from 'luxon';
|
|
2
|
+
import { isRunTimeType } from 'run-time-assertions';
|
|
3
3
|
import { userTimezone, utcTimezone } from '../timezone/timezones';
|
|
4
4
|
import { isValidFullDate } from './is-valid-full-date';
|
|
5
5
|
import { parseLuxonDateTime, toLuxonDateTime } from './luxon-date-time-conversion';
|
|
@@ -51,10 +51,10 @@ function convertDateLikeToLuxonDateTime(dateLike, timezone) {
|
|
|
51
51
|
if (DateTime.isDateTime(dateLike)) {
|
|
52
52
|
return dateLike.setZone(timezone);
|
|
53
53
|
}
|
|
54
|
-
else if (
|
|
54
|
+
else if (isRunTimeType(dateLike, 'number')) {
|
|
55
55
|
return DateTime.fromMillis(dateLike, { zone: utcTimezone }).setZone(timezone);
|
|
56
56
|
}
|
|
57
|
-
else if (
|
|
57
|
+
else if (isRunTimeType(dateLike, 'string')) {
|
|
58
58
|
const dateTime = DateTime.fromISO(dateLike, { zone: timezone });
|
|
59
59
|
if (!dateTime.isValid) {
|
|
60
60
|
return lastDitchConversion(dateLike);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { isRuntimeTypeOf } from '@augment-vir/common';
|
|
2
1
|
import { DateTime } from 'luxon';
|
|
2
|
+
import { isRunTimeType } from 'run-time-assertions';
|
|
3
3
|
import { createFullDate } from './create-full-date';
|
|
4
4
|
import { parseLuxonDateTime } from './luxon-date-time-conversion';
|
|
5
5
|
/**
|
|
@@ -31,7 +31,7 @@ export function parseInputElementValue(elementOrValue, timezone) {
|
|
|
31
31
|
if (!elementOrValue) {
|
|
32
32
|
return undefined;
|
|
33
33
|
}
|
|
34
|
-
const value =
|
|
34
|
+
const value = isRunTimeType(elementOrValue, 'string')
|
|
35
35
|
? elementOrValue
|
|
36
36
|
: elementOrValue.value;
|
|
37
37
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "date-vir",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.1",
|
|
4
4
|
"description": "Easy and explicit dates and times",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"date",
|
|
@@ -41,45 +41,45 @@
|
|
|
41
41
|
"test:watch": "web-test-runner --watch --color --config configs/web-test-runner.config.mjs"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@augment-vir/common": "^
|
|
44
|
+
"@augment-vir/common": "^22.0.0",
|
|
45
45
|
"@types/luxon": "^3.3.4",
|
|
46
46
|
"luxon": "^3.4.4",
|
|
47
|
-
"object-shape-tester": "^1.0.
|
|
48
|
-
"type-fest": "^4.
|
|
47
|
+
"object-shape-tester": "^1.0.2",
|
|
48
|
+
"type-fest": "^4.8.1"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@augment-vir/browser": "^
|
|
52
|
-
"@augment-vir/browser-testing": "^
|
|
51
|
+
"@augment-vir/browser": "^22.0.0",
|
|
52
|
+
"@augment-vir/browser-testing": "^22.0.0",
|
|
53
53
|
"@open-wc/testing": "^4.0.0",
|
|
54
54
|
"@types/chai": "^4.3.10",
|
|
55
55
|
"@types/mocha": "^10.0.4",
|
|
56
|
-
"@web/dev-server-esbuild": "^1.0.
|
|
56
|
+
"@web/dev-server-esbuild": "^1.0.1",
|
|
57
57
|
"@web/test-runner": "^0.18.0",
|
|
58
58
|
"@web/test-runner-commands": "^0.9.0",
|
|
59
59
|
"@web/test-runner-playwright": "^0.11.0",
|
|
60
60
|
"@web/test-runner-visual-regression": "^0.9.0",
|
|
61
61
|
"cspell": "^8.0.0",
|
|
62
62
|
"dependency-cruiser": "^15.3.0",
|
|
63
|
-
"element-vir": "^
|
|
64
|
-
"esbuild": "^0.19.
|
|
65
|
-
"istanbul-smart-text-reporter": "^1.1.
|
|
66
|
-
"markdown-code-example-inserter": "^0.3.
|
|
63
|
+
"element-vir": "^17.0.0",
|
|
64
|
+
"esbuild": "^0.19.6",
|
|
65
|
+
"istanbul-smart-text-reporter": "^1.1.3",
|
|
66
|
+
"markdown-code-example-inserter": "^0.3.3",
|
|
67
67
|
"npm-check-updates": "~16.12.3",
|
|
68
68
|
"prettier": "^3.1.0",
|
|
69
|
-
"prettier-plugin-interpolated-html-tags": "^1.0.
|
|
69
|
+
"prettier-plugin-interpolated-html-tags": "^1.0.3",
|
|
70
70
|
"prettier-plugin-jsdoc": "^1.1.1",
|
|
71
|
-
"prettier-plugin-multiline-arrays": "^3.0.
|
|
71
|
+
"prettier-plugin-multiline-arrays": "^3.0.1",
|
|
72
72
|
"prettier-plugin-organize-imports": "^3.2.4",
|
|
73
73
|
"prettier-plugin-packagejson": "^2.4.6",
|
|
74
74
|
"prettier-plugin-sort-json": "^3.1.0",
|
|
75
75
|
"prettier-plugin-toml": "^1.0.0",
|
|
76
|
-
"run-time-assertions": "^0.1
|
|
76
|
+
"run-time-assertions": "^0.2.1",
|
|
77
77
|
"typedoc": "^0.25.3",
|
|
78
78
|
"typescript": "^5.2.2",
|
|
79
|
-
"virmator": "^11.1.
|
|
79
|
+
"virmator": "^11.1.2"
|
|
80
80
|
},
|
|
81
81
|
"overrides": {
|
|
82
82
|
"@open-wc/scoped-elements": "2.2.3",
|
|
83
|
-
"@open-wc/testing-helpers": "2.3.
|
|
83
|
+
"@open-wc/testing-helpers": "2.3.0"
|
|
84
84
|
}
|
|
85
85
|
}
|