@dative-gpi/foundation-shared-services 1.0.192 → 1.0.193-value-data-composable
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/composables/index.ts
CHANGED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { TimeComparisonType } from "@dative-gpi/foundation-shared-domain/enums";
|
|
2
|
+
import { useDateExpression } from "@dative-gpi/foundation-shared-services/composables";
|
|
3
|
+
import { MILLISECONDS_PER_DAY, MILLISECONDS_PER_WEEK, MILLISECONDS_PER_MONTH_APPROX, MILLISECONDS_PER_YEAR_APPROX } from "@dative-gpi/foundation-shared-services/config";
|
|
4
|
+
|
|
5
|
+
export const useTimeDuration = () => {
|
|
6
|
+
/**
|
|
7
|
+
* Computes the duration between two timestamps, in **milliseconds**.
|
|
8
|
+
*
|
|
9
|
+
* Returns `null` if:
|
|
10
|
+
* - `periodStart` or `periodEnd` is missing,
|
|
11
|
+
* - a timestamp cannot be converted to a number,
|
|
12
|
+
* - the end is equal to or before the start.
|
|
13
|
+
*
|
|
14
|
+
* @param periodStart Date fixe (ISO-8601) ou expression relative (ex: 'now-1h', 'now/d'), ou `undefined`.
|
|
15
|
+
* @param periodEnd Date fixe (ISO-8601) ou expression relative (ex: 'now', 'now-1d/d'), ou `undefined`.
|
|
16
|
+
* @returns Durée en millisecondes, ou `0` si l'expression est invalide ou si la fin est avant le début.
|
|
17
|
+
*/
|
|
18
|
+
const getPeriodDuration = (periodStart: string | undefined, periodEnd: string | undefined): number | null => {
|
|
19
|
+
if (!periodStart || !periodEnd) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
const { convert } = useDateExpression();
|
|
23
|
+
|
|
24
|
+
const startTimestamp = convert(periodStart);
|
|
25
|
+
const endTimestamp = convert(periodEnd);
|
|
26
|
+
|
|
27
|
+
if (isNaN(startTimestamp) || isNaN(endTimestamp)) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const durationMs = endTimestamp - startTimestamp;
|
|
32
|
+
return durationMs > 0 ? durationMs : null;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Returns the duration (in **milliseconds**) for a given {@link TimeComparisonType}.
|
|
37
|
+
*
|
|
38
|
+
* - `Daily` / `Weekly`: fixed constants.
|
|
39
|
+
* - `Monthly` / `Yearly`: **approximate** constants (not exact calendar lengths).
|
|
40
|
+
* - `SinceReference`: duration between `periodStart` and `periodEnd` (needed only for this mode).
|
|
41
|
+
* - `None` / `Absolute`: returns `null` (no implied fixed duration).
|
|
42
|
+
*
|
|
43
|
+
* @param timeframe Timeframe mode to convert into a duration.
|
|
44
|
+
* @param periodStart Optional start timestamp string (usually ISO-8601). Used only for `SinceReference`.
|
|
45
|
+
* @param periodEnd Optional end timestamp string (usually ISO-8601). Used only for `SinceReference`.
|
|
46
|
+
* @returns Duration in **milliseconds**.
|
|
47
|
+
*/
|
|
48
|
+
const getTimeframeDuration = (timeframe: TimeComparisonType, periodStart: string | null = null, periodEnd: string | null = null): number | null => {
|
|
49
|
+
switch (timeframe) {
|
|
50
|
+
case TimeComparisonType.SinceReference:
|
|
51
|
+
return getPeriodDuration(periodStart ?? undefined, periodEnd ?? undefined);
|
|
52
|
+
case TimeComparisonType.Daily:
|
|
53
|
+
return MILLISECONDS_PER_DAY;
|
|
54
|
+
case TimeComparisonType.Weekly:
|
|
55
|
+
return MILLISECONDS_PER_WEEK;
|
|
56
|
+
case TimeComparisonType.Monthly:
|
|
57
|
+
return MILLISECONDS_PER_MONTH_APPROX;
|
|
58
|
+
case TimeComparisonType.Yearly:
|
|
59
|
+
return MILLISECONDS_PER_YEAR_APPROX;
|
|
60
|
+
case TimeComparisonType.None:
|
|
61
|
+
// No timeframe selected: no duration.
|
|
62
|
+
return null;
|
|
63
|
+
case TimeComparisonType.Absolute:
|
|
64
|
+
// Absolute comparisons do not imply a fixed duration.
|
|
65
|
+
return null;
|
|
66
|
+
default:
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
getPeriodDuration,
|
|
73
|
+
getTimeframeDuration,
|
|
74
|
+
};
|
|
75
|
+
}
|
package/config/index.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./timePerPeriod";
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"url": "https://github.com/Dative-GPI/foundation-shared-ui.git"
|
|
5
5
|
},
|
|
6
6
|
"sideEffects": false,
|
|
7
|
-
"version": "1.0.
|
|
7
|
+
"version": "1.0.193-value-data-composable",
|
|
8
8
|
"description": "",
|
|
9
9
|
"publishConfig": {
|
|
10
10
|
"access": "public"
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"author": "",
|
|
14
14
|
"license": "ISC",
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@dative-gpi/foundation-shared-domain": "1.0.
|
|
16
|
+
"@dative-gpi/foundation-shared-domain": "1.0.193-value-data-composable",
|
|
17
17
|
"@vueuse/core": "^14.0.0"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
@@ -22,5 +22,5 @@
|
|
|
22
22
|
"vue": "^3.4.38",
|
|
23
23
|
"vue-router": "^4.3.0"
|
|
24
24
|
},
|
|
25
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "e6d7d1fd7a3bc060cebb335596f4d95a6ce91356"
|
|
26
26
|
}
|