date-vir 4.0.2 → 4.1.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.
|
@@ -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);
|
|
@@ -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);
|