date-vir 4.0.0 → 4.0.2
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,12 +5,13 @@ 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
7
|
function toRelativeString({ fullDate, relativeTo, options = {}, }) {
|
|
8
|
+
const roundingDigits = options.decimalDigitCount || 0;
|
|
8
9
|
const allUnitDiffs = (0, diff_dates_1.diffDatesAllUnits)({
|
|
9
10
|
start: fullDate,
|
|
10
11
|
end: relativeTo,
|
|
11
12
|
});
|
|
12
13
|
const unitsWithinBounds = (0, common_1.mapObjectValues)(allUnitDiffs, (durationUnit, duration) => {
|
|
13
|
-
const isAboveZero = Math.floor(Math.abs(duration)) > 0;
|
|
14
|
+
const isAboveZero = Math.floor(Math.abs((0, common_1.round)({ digits: 1, number: duration }))) > 0;
|
|
14
15
|
return isAboveZero;
|
|
15
16
|
});
|
|
16
17
|
const unitToUse = duration_1.orderedDurationUnits.reduce((chosenUnit, currentUnit) => {
|
|
@@ -24,18 +25,19 @@ function toRelativeString({ fullDate, relativeTo, options = {}, }) {
|
|
|
24
25
|
}, undefined);
|
|
25
26
|
const shouldUseJustNow = options.blockJustNow
|
|
26
27
|
? false
|
|
27
|
-
: (
|
|
28
|
+
: (0, common_1.areJsonEqual)(fullDate, relativeTo) ||
|
|
29
|
+
(unitToUse === duration_1.DurationUnit.Minutes && allUnitDiffs.minutes < 2) ||
|
|
28
30
|
(unitToUse === duration_1.DurationUnit.Seconds && allUnitDiffs.seconds < 11) ||
|
|
29
31
|
(unitToUse === duration_1.DurationUnit.Milliseconds && allUnitDiffs.milliseconds < 710);
|
|
30
32
|
/** Some short circuits. */
|
|
31
|
-
if (
|
|
32
|
-
return undefined;
|
|
33
|
-
}
|
|
34
|
-
else if (shouldUseJustNow) {
|
|
33
|
+
if (shouldUseJustNow) {
|
|
35
34
|
return 'just now';
|
|
36
35
|
}
|
|
36
|
+
else if (unitToUse == undefined) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
37
39
|
const duration = allUnitDiffs[unitToUse];
|
|
38
|
-
const roundedAbsoluteDuration = Math.abs((0, common_1.round)({ digits:
|
|
40
|
+
const roundedAbsoluteDuration = Math.abs((0, common_1.round)({ digits: roundingDigits, number: duration }));
|
|
39
41
|
const isDurationSingular = roundedAbsoluteDuration === 1;
|
|
40
42
|
const unitName = [
|
|
41
43
|
duration_1.singularDurationUnitNames[unitToUse],
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { mapObjectValues, round } from '@augment-vir/common';
|
|
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
4
|
export function toRelativeString({ fullDate, relativeTo, options = {}, }) {
|
|
5
|
+
const roundingDigits = options.decimalDigitCount || 0;
|
|
5
6
|
const allUnitDiffs = diffDatesAllUnits({
|
|
6
7
|
start: fullDate,
|
|
7
8
|
end: relativeTo,
|
|
8
9
|
});
|
|
9
10
|
const unitsWithinBounds = mapObjectValues(allUnitDiffs, (durationUnit, duration) => {
|
|
10
|
-
const isAboveZero = Math.floor(Math.abs(duration)) > 0;
|
|
11
|
+
const isAboveZero = Math.floor(Math.abs(round({ digits: 1, number: duration }))) > 0;
|
|
11
12
|
return isAboveZero;
|
|
12
13
|
});
|
|
13
14
|
const unitToUse = orderedDurationUnits.reduce((chosenUnit, currentUnit) => {
|
|
@@ -21,18 +22,19 @@ export function toRelativeString({ fullDate, relativeTo, options = {}, }) {
|
|
|
21
22
|
}, undefined);
|
|
22
23
|
const shouldUseJustNow = options.blockJustNow
|
|
23
24
|
? false
|
|
24
|
-
: (
|
|
25
|
+
: areJsonEqual(fullDate, relativeTo) ||
|
|
26
|
+
(unitToUse === DurationUnit.Minutes && allUnitDiffs.minutes < 2) ||
|
|
25
27
|
(unitToUse === DurationUnit.Seconds && allUnitDiffs.seconds < 11) ||
|
|
26
28
|
(unitToUse === DurationUnit.Milliseconds && allUnitDiffs.milliseconds < 710);
|
|
27
29
|
/** Some short circuits. */
|
|
28
|
-
if (
|
|
29
|
-
return undefined;
|
|
30
|
-
}
|
|
31
|
-
else if (shouldUseJustNow) {
|
|
30
|
+
if (shouldUseJustNow) {
|
|
32
31
|
return 'just now';
|
|
33
32
|
}
|
|
33
|
+
else if (unitToUse == undefined) {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
34
36
|
const duration = allUnitDiffs[unitToUse];
|
|
35
|
-
const roundedAbsoluteDuration = Math.abs(round({ digits:
|
|
37
|
+
const roundedAbsoluteDuration = Math.abs(round({ digits: roundingDigits, number: duration }));
|
|
36
38
|
const isDurationSingular = roundedAbsoluteDuration === 1;
|
|
37
39
|
const unitName = [
|
|
38
40
|
singularDurationUnitNames[unitToUse],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "date-vir",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.2",
|
|
4
4
|
"description": "Easy and explicit dates and times",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"date",
|
|
@@ -77,5 +77,9 @@
|
|
|
77
77
|
"typedoc": "^0.25.3",
|
|
78
78
|
"typescript": "^5.2.2",
|
|
79
79
|
"virmator": "^11.1.0"
|
|
80
|
+
},
|
|
81
|
+
"overrides": {
|
|
82
|
+
"@open-wc/scoped-elements": "2.2.3",
|
|
83
|
+
"@open-wc/testing-helpers": "2.3.1"
|
|
80
84
|
}
|
|
81
85
|
}
|