date-vir 4.1.0 → 5.0.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.
- package/dist/cjs/date-operations/diff-dates.js +81 -48
- package/dist/cjs/duration.js +14 -1
- package/dist/cjs/formatting/relative-string.js +3 -1
- package/dist/cjs/full-date/create-full-date.js +3 -3
- package/dist/cjs/full-date/parsing.js +2 -2
- package/dist/esm/date-operations/diff-dates.d.ts +55 -21
- package/dist/esm/date-operations/diff-dates.js +81 -47
- package/dist/esm/duration.d.ts +3 -1
- package/dist/esm/duration.js +12 -0
- package/dist/esm/formatting/relative-string.js +4 -2
- package/dist/esm/full-date/create-full-date.js +3 -3
- package/dist/esm/full-date/parsing.js +2 -2
- package/package.json +23 -27
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isDateAfter = exports.
|
|
3
|
+
exports.isDateAfter = exports.diffDates = exports.DiffType = void 0;
|
|
4
4
|
const common_1 = require("@augment-vir/common");
|
|
5
5
|
const duration_1 = require("../duration");
|
|
6
6
|
const luxon_date_time_conversion_1 = require("../full-date/luxon-date-time-conversion");
|
|
7
|
+
const accuracyPriority = {
|
|
8
|
+
longterm: 1,
|
|
9
|
+
casual: 0,
|
|
10
|
+
};
|
|
7
11
|
const conversionAccuracies = {
|
|
8
12
|
[duration_1.DurationUnit.Years]: 'longterm',
|
|
9
13
|
[duration_1.DurationUnit.Quarters]: 'longterm',
|
|
@@ -15,57 +19,86 @@ const conversionAccuracies = {
|
|
|
15
19
|
[duration_1.DurationUnit.Seconds]: 'casual',
|
|
16
20
|
[duration_1.DurationUnit.Milliseconds]: 'casual',
|
|
17
21
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
function getHighestPriorityConversionAccuracy(units) {
|
|
23
|
+
const currentConversionAccuracies = units.map((unit) => conversionAccuracies[unit]);
|
|
24
|
+
const highestPriorityAccuracy = currentConversionAccuracies.reduce((priorityAccuracy, currentAccuracy) => {
|
|
25
|
+
if (accuracyPriority[currentAccuracy] > accuracyPriority[priorityAccuracy]) {
|
|
26
|
+
return currentAccuracy;
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
return priorityAccuracy;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return highestPriorityAccuracy;
|
|
33
|
+
}
|
|
34
|
+
var DiffType;
|
|
35
|
+
(function (DiffType) {
|
|
36
|
+
/**
|
|
37
|
+
* Causes the diffDates function to output the diff split between each unit provided. To get the
|
|
38
|
+
* full duration, each output unit needs to be added together.
|
|
39
|
+
*/
|
|
40
|
+
DiffType["AdditiveUnits"] = "additive-units";
|
|
41
|
+
/**
|
|
42
|
+
* Causes the diffDates function to output the diff in each unit provided with each unit being
|
|
43
|
+
* equivalent in value. Meaning, each unit will be the same duration of time converted into that
|
|
44
|
+
* unit.
|
|
45
|
+
*/
|
|
46
|
+
DiffType["EquivalentUnits"] = "equivalent-units";
|
|
47
|
+
})(DiffType || (exports.DiffType = DiffType = {}));
|
|
48
|
+
function diffDates({ start, end, units: unitsInput, unit: unitInput, diffType: diffTypeInput, decimalCount, }) {
|
|
26
49
|
const luxonDateStart = (0, luxon_date_time_conversion_1.toLuxonDateTime)(start);
|
|
27
50
|
const luxonDateEnd = (0, luxon_date_time_conversion_1.toLuxonDateTime)(end);
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
const units = (unitsInput ?? [unitInput]);
|
|
52
|
+
const diffType = units.length === 1
|
|
53
|
+
? /**
|
|
54
|
+
* When only one unit is provided, it doesn't matter which diff type we use so lets just use this
|
|
55
|
+
* one.
|
|
56
|
+
*/
|
|
57
|
+
DiffType.EquivalentUnits
|
|
58
|
+
: diffTypeInput;
|
|
59
|
+
if (!diffType) {
|
|
60
|
+
throw new Error('Failed to diff dates: no diffType was provided.');
|
|
61
|
+
}
|
|
62
|
+
if (diffType === DiffType.AdditiveUnits) {
|
|
63
|
+
const conversionAccuracy = getHighestPriorityConversionAccuracy(units);
|
|
64
|
+
const additiveDiff = luxonDateEnd
|
|
65
|
+
.diff(luxonDateStart, (0, common_1.makeWritable)(units), {
|
|
66
|
+
conversionAccuracy,
|
|
67
|
+
})
|
|
68
|
+
.toObject();
|
|
69
|
+
return (0, duration_1.roundDuration)(additiveDiff, decimalCount);
|
|
70
|
+
}
|
|
71
|
+
else if (diffType === DiffType.EquivalentUnits) {
|
|
72
|
+
const allUnitDiffEntries = units.map((durationUnit) => {
|
|
73
|
+
const diff = luxonDateEnd
|
|
74
|
+
.diff(luxonDateStart, durationUnit, {
|
|
75
|
+
conversionAccuracy: conversionAccuracies[durationUnit],
|
|
76
|
+
})
|
|
77
|
+
.toObject();
|
|
78
|
+
const duration = diff[durationUnit];
|
|
79
|
+
/** Ignore this edge case in coverage cause its for type guarding. */
|
|
80
|
+
/* c8 ignore next 5 */
|
|
81
|
+
if (duration == undefined) {
|
|
82
|
+
throw new Error(`Internal date-vir error: failed to find duration for '${durationUnit}'`);
|
|
83
|
+
}
|
|
84
|
+
return [
|
|
85
|
+
durationUnit,
|
|
86
|
+
duration,
|
|
87
|
+
];
|
|
51
88
|
});
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return [
|
|
59
|
-
durationUnit,
|
|
60
|
-
duration,
|
|
61
|
-
];
|
|
62
|
-
});
|
|
63
|
-
const allDurations = (0, common_1.typedObjectFromEntries)(allUnitDiffEntries);
|
|
64
|
-
return allDurations;
|
|
89
|
+
const equivalentDiff = (0, common_1.typedObjectFromEntries)(allUnitDiffEntries);
|
|
90
|
+
return (0, duration_1.roundDuration)(equivalentDiff, decimalCount);
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
throw new Error(`Failed to diff dates: provided diffType is invalid: '${diffType}'`);
|
|
94
|
+
}
|
|
65
95
|
}
|
|
66
|
-
exports.
|
|
96
|
+
exports.diffDates = diffDates;
|
|
67
97
|
function isDateAfter({ relativeTo, fullDate, }) {
|
|
68
|
-
return (diffDates({
|
|
69
|
-
|
|
98
|
+
return (diffDates({
|
|
99
|
+
start: relativeTo,
|
|
100
|
+
end: fullDate,
|
|
101
|
+
unit: duration_1.DurationUnit.Milliseconds,
|
|
102
|
+
}).milliseconds >= 0);
|
|
70
103
|
}
|
|
71
104
|
exports.isDateAfter = isDateAfter;
|
package/dist/cjs/duration.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.orderedDurationUnits = exports.singularDurationUnitNames = exports.DurationUnit = void 0;
|
|
3
|
+
exports.roundDuration = exports.orderedDurationUnits = exports.singularDurationUnitNames = exports.DurationUnit = void 0;
|
|
4
|
+
const common_1 = require("@augment-vir/common");
|
|
5
|
+
const run_time_assertions_1 = require("run-time-assertions");
|
|
4
6
|
var DurationUnit;
|
|
5
7
|
(function (DurationUnit) {
|
|
6
8
|
DurationUnit["Years"] = "years";
|
|
@@ -39,3 +41,14 @@ exports.orderedDurationUnits = [
|
|
|
39
41
|
DurationUnit.Quarters,
|
|
40
42
|
DurationUnit.Years,
|
|
41
43
|
];
|
|
44
|
+
function roundDuration(duration, decimalCount) {
|
|
45
|
+
return (0, common_1.mapObjectValues)(duration, (key, value) => {
|
|
46
|
+
if (decimalCount == undefined || !(0, run_time_assertions_1.isRunTimeType)(value, 'number')) {
|
|
47
|
+
return value;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
return (0, common_1.round)({ number: value, digits: decimalCount });
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
exports.roundDuration = roundDuration;
|
|
@@ -17,9 +17,11 @@ const maxDurations = {
|
|
|
17
17
|
};
|
|
18
18
|
function toRelativeString({ fullDate, relativeTo, options = {}, }) {
|
|
19
19
|
const roundingDigits = options.decimalDigitCount || 0;
|
|
20
|
-
const allUnitDiffs = (0, diff_dates_1.
|
|
20
|
+
const allUnitDiffs = (0, diff_dates_1.diffDates)({
|
|
21
21
|
start: fullDate,
|
|
22
22
|
end: relativeTo,
|
|
23
|
+
units: duration_1.orderedDurationUnits,
|
|
24
|
+
diffType: diff_dates_1.DiffType.EquivalentUnits,
|
|
23
25
|
});
|
|
24
26
|
const unitsWithinBounds = (0, common_1.mapObjectValues)(allUnitDiffs, (durationUnit, duration) => {
|
|
25
27
|
const roundedDuration = Math.floor(Math.abs((0, common_1.round)({ digits: 1, number: duration })));
|
|
@@ -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 {
|
|
@@ -1,5 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ArrayElement } from '@augment-vir/common';
|
|
2
|
+
import { Duration, DurationUnit } from '../duration';
|
|
2
3
|
import { FullDate } from '../full-date/full-date-shape';
|
|
4
|
+
export declare enum DiffType {
|
|
5
|
+
/**
|
|
6
|
+
* Causes the diffDates function to output the diff split between each unit provided. To get the
|
|
7
|
+
* full duration, each output unit needs to be added together.
|
|
8
|
+
*/
|
|
9
|
+
AdditiveUnits = "additive-units",
|
|
10
|
+
/**
|
|
11
|
+
* Causes the diffDates function to output the diff in each unit provided with each unit being
|
|
12
|
+
* equivalent in value. Meaning, each unit will be the same duration of time converted into that
|
|
13
|
+
* unit.
|
|
14
|
+
*/
|
|
15
|
+
EquivalentUnits = "equivalent-units"
|
|
16
|
+
}
|
|
3
17
|
/**
|
|
4
18
|
* Diffs two dates and returns the diff in the specific unit.
|
|
5
19
|
*
|
|
@@ -7,25 +21,45 @@ import { FullDate } from '../full-date/full-date-shape';
|
|
|
7
21
|
* to calculate the diff. See more details here:
|
|
8
22
|
* https://moment.github.io/luxon/#/math?id=casual-vs-longterm-conversion-accuracy
|
|
9
23
|
*/
|
|
10
|
-
export declare function diffDates<const
|
|
11
|
-
start: FullDate
|
|
12
|
-
end: FullDate
|
|
13
|
-
unit
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
export declare function
|
|
25
|
-
start: FullDate
|
|
26
|
-
end: FullDate
|
|
27
|
-
|
|
24
|
+
export declare function diffDates<const ChosenDurationUnits extends DurationUnit>(params: {
|
|
25
|
+
start: Readonly<FullDate>;
|
|
26
|
+
end: Readonly<FullDate>;
|
|
27
|
+
/** The unit which the output should contain. */
|
|
28
|
+
unit: ChosenDurationUnits;
|
|
29
|
+
/**
|
|
30
|
+
* Determine the diff type calculation. See DiffType values for explanations on each one.
|
|
31
|
+
* Optional when only one unit is provided, as the diffType will not change the output either
|
|
32
|
+
* way.
|
|
33
|
+
*/
|
|
34
|
+
diffType?: DiffType | undefined;
|
|
35
|
+
/** How many decimals to round the outputs to. Leave undefined to skip rounding entirely. */
|
|
36
|
+
decimalCount?: number | undefined;
|
|
37
|
+
}): Duration<ChosenDurationUnits>;
|
|
38
|
+
export declare function diffDates<const ChosenDurationUnits extends Readonly<[DurationUnit]>>(params: {
|
|
39
|
+
start: Readonly<FullDate>;
|
|
40
|
+
end: Readonly<FullDate>;
|
|
41
|
+
/** Which units the output should contain. */
|
|
42
|
+
units: ChosenDurationUnits;
|
|
43
|
+
/**
|
|
44
|
+
* Determine the diff type calculation. See DiffType values for explanations on each one.
|
|
45
|
+
* Optional when only one unit is provided, as the diffType will not change the output either
|
|
46
|
+
* way.
|
|
47
|
+
*/
|
|
48
|
+
diffType?: DiffType | undefined;
|
|
49
|
+
/** How many decimals to round the outputs to. Leave undefined to skip rounding entirely. */
|
|
50
|
+
decimalCount?: number | undefined;
|
|
51
|
+
}): Duration<ArrayElement<ChosenDurationUnits>>;
|
|
52
|
+
export declare function diffDates<const ChosenDurationUnits extends ReadonlyArray<DurationUnit>>(params: {
|
|
53
|
+
start: Readonly<FullDate>;
|
|
54
|
+
end: Readonly<FullDate>;
|
|
55
|
+
/** Which units the output should contain. */
|
|
56
|
+
units: ChosenDurationUnits;
|
|
57
|
+
/** Determine the diff type calculation. See DiffType values for explanations on each one. */
|
|
58
|
+
diffType: DiffType;
|
|
59
|
+
/** How many decimals to round the outputs to. Leave undefined to skip rounding entirely. */
|
|
60
|
+
decimalCount?: number | undefined;
|
|
61
|
+
}): Duration<ArrayElement<ChosenDurationUnits>>;
|
|
28
62
|
export declare function isDateAfter({ relativeTo, fullDate, }: {
|
|
29
|
-
relativeTo: FullDate
|
|
30
|
-
fullDate: FullDate
|
|
63
|
+
relativeTo: Readonly<FullDate>;
|
|
64
|
+
fullDate: Readonly<FullDate>;
|
|
31
65
|
}): boolean;
|
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
import { typedObjectFromEntries } from '@augment-vir/common';
|
|
2
|
-
import { DurationUnit,
|
|
1
|
+
import { makeWritable, typedObjectFromEntries } from '@augment-vir/common';
|
|
2
|
+
import { DurationUnit, roundDuration } from '../duration';
|
|
3
3
|
import { toLuxonDateTime } from '../full-date/luxon-date-time-conversion';
|
|
4
|
+
const accuracyPriority = {
|
|
5
|
+
longterm: 1,
|
|
6
|
+
casual: 0,
|
|
7
|
+
};
|
|
4
8
|
const conversionAccuracies = {
|
|
5
9
|
[DurationUnit.Years]: 'longterm',
|
|
6
10
|
[DurationUnit.Quarters]: 'longterm',
|
|
@@ -12,54 +16,84 @@ const conversionAccuracies = {
|
|
|
12
16
|
[DurationUnit.Seconds]: 'casual',
|
|
13
17
|
[DurationUnit.Milliseconds]: 'casual',
|
|
14
18
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
function getHighestPriorityConversionAccuracy(units) {
|
|
20
|
+
const currentConversionAccuracies = units.map((unit) => conversionAccuracies[unit]);
|
|
21
|
+
const highestPriorityAccuracy = currentConversionAccuracies.reduce((priorityAccuracy, currentAccuracy) => {
|
|
22
|
+
if (accuracyPriority[currentAccuracy] > accuracyPriority[priorityAccuracy]) {
|
|
23
|
+
return currentAccuracy;
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
return priorityAccuracy;
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return highestPriorityAccuracy;
|
|
30
|
+
}
|
|
31
|
+
export var DiffType;
|
|
32
|
+
(function (DiffType) {
|
|
33
|
+
/**
|
|
34
|
+
* Causes the diffDates function to output the diff split between each unit provided. To get the
|
|
35
|
+
* full duration, each output unit needs to be added together.
|
|
36
|
+
*/
|
|
37
|
+
DiffType["AdditiveUnits"] = "additive-units";
|
|
38
|
+
/**
|
|
39
|
+
* Causes the diffDates function to output the diff in each unit provided with each unit being
|
|
40
|
+
* equivalent in value. Meaning, each unit will be the same duration of time converted into that
|
|
41
|
+
* unit.
|
|
42
|
+
*/
|
|
43
|
+
DiffType["EquivalentUnits"] = "equivalent-units";
|
|
44
|
+
})(DiffType || (DiffType = {}));
|
|
45
|
+
export function diffDates({ start, end, units: unitsInput, unit: unitInput, diffType: diffTypeInput, decimalCount, }) {
|
|
23
46
|
const luxonDateStart = toLuxonDateTime(start);
|
|
24
47
|
const luxonDateEnd = toLuxonDateTime(end);
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
48
|
+
const units = (unitsInput ?? [unitInput]);
|
|
49
|
+
const diffType = units.length === 1
|
|
50
|
+
? /**
|
|
51
|
+
* When only one unit is provided, it doesn't matter which diff type we use so lets just use this
|
|
52
|
+
* one.
|
|
53
|
+
*/
|
|
54
|
+
DiffType.EquivalentUnits
|
|
55
|
+
: diffTypeInput;
|
|
56
|
+
if (!diffType) {
|
|
57
|
+
throw new Error('Failed to diff dates: no diffType was provided.');
|
|
58
|
+
}
|
|
59
|
+
if (diffType === DiffType.AdditiveUnits) {
|
|
60
|
+
const conversionAccuracy = getHighestPriorityConversionAccuracy(units);
|
|
61
|
+
const additiveDiff = luxonDateEnd
|
|
62
|
+
.diff(luxonDateStart, makeWritable(units), {
|
|
63
|
+
conversionAccuracy,
|
|
64
|
+
})
|
|
65
|
+
.toObject();
|
|
66
|
+
return roundDuration(additiveDiff, decimalCount);
|
|
67
|
+
}
|
|
68
|
+
else if (diffType === DiffType.EquivalentUnits) {
|
|
69
|
+
const allUnitDiffEntries = units.map((durationUnit) => {
|
|
70
|
+
const diff = luxonDateEnd
|
|
71
|
+
.diff(luxonDateStart, durationUnit, {
|
|
72
|
+
conversionAccuracy: conversionAccuracies[durationUnit],
|
|
73
|
+
})
|
|
74
|
+
.toObject();
|
|
75
|
+
const duration = diff[durationUnit];
|
|
76
|
+
/** Ignore this edge case in coverage cause its for type guarding. */
|
|
77
|
+
/* c8 ignore next 5 */
|
|
78
|
+
if (duration == undefined) {
|
|
79
|
+
throw new Error(`Internal date-vir error: failed to find duration for '${durationUnit}'`);
|
|
80
|
+
}
|
|
81
|
+
return [
|
|
82
|
+
durationUnit,
|
|
83
|
+
duration,
|
|
84
|
+
];
|
|
47
85
|
});
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return [
|
|
55
|
-
durationUnit,
|
|
56
|
-
duration,
|
|
57
|
-
];
|
|
58
|
-
});
|
|
59
|
-
const allDurations = typedObjectFromEntries(allUnitDiffEntries);
|
|
60
|
-
return allDurations;
|
|
86
|
+
const equivalentDiff = typedObjectFromEntries(allUnitDiffEntries);
|
|
87
|
+
return roundDuration(equivalentDiff, decimalCount);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
throw new Error(`Failed to diff dates: provided diffType is invalid: '${diffType}'`);
|
|
91
|
+
}
|
|
61
92
|
}
|
|
62
93
|
export function isDateAfter({ relativeTo, fullDate, }) {
|
|
63
|
-
return (diffDates({
|
|
64
|
-
|
|
94
|
+
return (diffDates({
|
|
95
|
+
start: relativeTo,
|
|
96
|
+
end: fullDate,
|
|
97
|
+
unit: DurationUnit.Milliseconds,
|
|
98
|
+
}).milliseconds >= 0);
|
|
65
99
|
}
|
package/dist/esm/duration.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { RequiredAndNotNull } from '@augment-vir/common';
|
|
2
|
+
import { IsUnknown, UnionToIntersection } from 'type-fest';
|
|
2
3
|
export declare enum DurationUnit {
|
|
3
4
|
Years = "years",
|
|
4
5
|
Quarters = "quarters",
|
|
@@ -52,4 +53,5 @@ export type AllDurations = RequiredAndNotNull<AnyDuration>;
|
|
|
52
53
|
* dates, or add offsets to an existing date, or describe a single time duration. Usually only one
|
|
53
54
|
* property is set on this at any given time.
|
|
54
55
|
*/
|
|
55
|
-
export type Duration<DurationKeys extends DurationUnit
|
|
56
|
+
export type Duration<DurationKeys extends DurationUnit | unknown> = UnionToIntersection<IsUnknown<DurationKeys> extends true ? AnyDuration : DurationKeys extends DurationUnit ? Pick<AllDurations, `${DurationKeys}`> : never>;
|
|
57
|
+
export declare function roundDuration<InputDuration extends AnyDuration>(duration: Readonly<InputDuration>, decimalCount: number | undefined): InputDuration;
|
package/dist/esm/duration.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { mapObjectValues, round } from '@augment-vir/common';
|
|
2
|
+
import { isRunTimeType } from 'run-time-assertions';
|
|
1
3
|
export var DurationUnit;
|
|
2
4
|
(function (DurationUnit) {
|
|
3
5
|
DurationUnit["Years"] = "years";
|
|
@@ -36,3 +38,13 @@ export const orderedDurationUnits = [
|
|
|
36
38
|
DurationUnit.Quarters,
|
|
37
39
|
DurationUnit.Years,
|
|
38
40
|
];
|
|
41
|
+
export function roundDuration(duration, decimalCount) {
|
|
42
|
+
return mapObjectValues(duration, (key, value) => {
|
|
43
|
+
if (decimalCount == undefined || !isRunTimeType(value, 'number')) {
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
return round({ number: value, digits: decimalCount });
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { areJsonEqual, mapObjectValues, round } from '@augment-vir/common';
|
|
2
|
-
import {
|
|
2
|
+
import { DiffType, diffDates } from '../date-operations/diff-dates';
|
|
3
3
|
import { DurationUnit, orderedDurationUnits, singularDurationUnitNames } from '../duration';
|
|
4
4
|
const maxDurations = {
|
|
5
5
|
[DurationUnit.Milliseconds]: 1000,
|
|
@@ -14,9 +14,11 @@ const maxDurations = {
|
|
|
14
14
|
};
|
|
15
15
|
export function toRelativeString({ fullDate, relativeTo, options = {}, }) {
|
|
16
16
|
const roundingDigits = options.decimalDigitCount || 0;
|
|
17
|
-
const allUnitDiffs =
|
|
17
|
+
const allUnitDiffs = diffDates({
|
|
18
18
|
start: fullDate,
|
|
19
19
|
end: relativeTo,
|
|
20
|
+
units: orderedDurationUnits,
|
|
21
|
+
diffType: DiffType.EquivalentUnits,
|
|
20
22
|
});
|
|
21
23
|
const unitsWithinBounds = mapObjectValues(allUnitDiffs, (durationUnit, duration) => {
|
|
22
24
|
const roundedDuration = Math.floor(Math.abs(round({ digits: 1, number: duration })));
|
|
@@ -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": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "Easy and explicit dates and times",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"date",
|
|
@@ -41,45 +41,41 @@
|
|
|
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": "^
|
|
45
|
-
"@types/luxon": "^3.3.
|
|
44
|
+
"@augment-vir/common": "^22.1.0",
|
|
45
|
+
"@types/luxon": "^3.3.6",
|
|
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.2"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@augment-vir/browser": "^
|
|
52
|
-
"@augment-vir/browser-testing": "^
|
|
51
|
+
"@augment-vir/browser": "^22.1.0",
|
|
52
|
+
"@augment-vir/browser-testing": "^22.1.0",
|
|
53
53
|
"@open-wc/testing": "^4.0.0",
|
|
54
|
-
"@types/chai": "^4.3.
|
|
55
|
-
"@types/mocha": "^10.0.
|
|
56
|
-
"@web/dev-server-esbuild": "^1.0.
|
|
54
|
+
"@types/chai": "^4.3.11",
|
|
55
|
+
"@types/mocha": "^10.0.6",
|
|
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
|
-
"cspell": "^8.
|
|
62
|
-
"dependency-cruiser": "^15.
|
|
63
|
-
"element-vir": "^
|
|
64
|
-
"esbuild": "^0.19.
|
|
65
|
-
"istanbul-smart-text-reporter": "^1.1.
|
|
66
|
-
"markdown-code-example-inserter": "^0.3.
|
|
61
|
+
"cspell": "^8.1.0",
|
|
62
|
+
"dependency-cruiser": "^15.5.0",
|
|
63
|
+
"element-vir": "^17.0.3",
|
|
64
|
+
"esbuild": "^0.19.8",
|
|
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
|
-
"prettier-plugin-toml": "^
|
|
76
|
-
"run-time-assertions": "^0.1
|
|
77
|
-
"typedoc": "^0.25.
|
|
78
|
-
"typescript": "^5.
|
|
79
|
-
"virmator": "^11.1.
|
|
80
|
-
},
|
|
81
|
-
"overrides": {
|
|
82
|
-
"@open-wc/scoped-elements": "2.2.3",
|
|
83
|
-
"@open-wc/testing-helpers": "2.3.1"
|
|
75
|
+
"prettier-plugin-toml": "^2.0.1",
|
|
76
|
+
"run-time-assertions": "^0.2.1",
|
|
77
|
+
"typedoc": "^0.25.4",
|
|
78
|
+
"typescript": "^5.3.2",
|
|
79
|
+
"virmator": "^11.1.4"
|
|
84
80
|
}
|
|
85
81
|
}
|