@dative-gpi/foundation-shared-services 1.0.128-fix-mobile → 1.0.128-mountain-alpha
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
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { ImageDetails, type ImageDetailsDTO } from "@dative-gpi/foundation-shared-domain/models";
|
|
2
1
|
import { ComposableFactory, ServiceFactory } from "@dative-gpi/bones-ui/core";
|
|
2
|
+
import type { ImageDetailsDTO } from "@dative-gpi/foundation-shared-domain/models";
|
|
3
|
+
import { ImageDetails } from "@dative-gpi/foundation-shared-domain/models";
|
|
3
4
|
|
|
4
5
|
import { IMAGE_URL } from "../../config/urls";
|
|
5
6
|
|
|
@@ -43,6 +43,14 @@ export const useDateFormat = () => {
|
|
|
43
43
|
return date.toLocaleString(languageCode.value, { ...OPTIONS.dayMonthLongOnly, timeZone: timeZone.value });
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
const epochToDayMonthShortOnly = (value: number | null | undefined): string => {
|
|
47
|
+
if (value == null || !isFinite(value)) {
|
|
48
|
+
return "";
|
|
49
|
+
}
|
|
50
|
+
const date = new Date(value);
|
|
51
|
+
return date.toLocaleString(languageCode.value, { ...OPTIONS.dayMonthShortOnly, timeZone: timeZone.value });
|
|
52
|
+
}
|
|
53
|
+
|
|
46
54
|
const epochToShortDateFormat = (value: number | null | undefined): string => {
|
|
47
55
|
if (value == null || !isFinite(value)) {
|
|
48
56
|
return "";
|
|
@@ -74,6 +82,14 @@ export const useDateFormat = () => {
|
|
|
74
82
|
return date.toLocaleString(languageCode.value, { ...OPTIONS.shortTime, timeZone: timeZone.value });
|
|
75
83
|
};
|
|
76
84
|
|
|
85
|
+
const epochToMonthShortTimeFormat = (value: number | null | undefined): string => {
|
|
86
|
+
if (value == null || !isFinite(value)) {
|
|
87
|
+
return "";
|
|
88
|
+
}
|
|
89
|
+
const date = new Date(value);
|
|
90
|
+
return date.toLocaleString(languageCode.value, { ...OPTIONS.monthShortTime, timeZone: timeZone.value });
|
|
91
|
+
};
|
|
92
|
+
|
|
77
93
|
const epochToLocalDayStart = (value: number | null | undefined): number => {
|
|
78
94
|
if (value == null || !isFinite(value)) {
|
|
79
95
|
return 0;
|
|
@@ -209,6 +225,8 @@ export const useDateFormat = () => {
|
|
|
209
225
|
epochToMonthYearOnlyFormat,
|
|
210
226
|
epochToShortDateFormat,
|
|
211
227
|
epochToShortTimeFormat,
|
|
228
|
+
epochToDayMonthShortOnly,
|
|
229
|
+
epochToMonthShortTimeFormat,
|
|
212
230
|
epochToShortTimeOnlyFormat,
|
|
213
231
|
epochToTimeOnlyFormat,
|
|
214
232
|
epochToWeekNumber,
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { useAppTimeZone } from './app/useAppTimeZone';
|
|
2
|
+
import { useDateFormat } from './useDateFormat';
|
|
3
|
+
|
|
4
|
+
export const useTermFieldDate = () => {
|
|
5
|
+
const { todayToEpoch } = useDateFormat();
|
|
6
|
+
const { getUserOffset } = useAppTimeZone();
|
|
7
|
+
|
|
8
|
+
const EXPRESSION = /(?:(?:([-\+])(\d*))?(\w+))?(?:\/(\w))?/g;
|
|
9
|
+
const NOW = 'now';
|
|
10
|
+
|
|
11
|
+
const convert = (value: string, variables: { [key: string]: number | string } = {}): number => {
|
|
12
|
+
//Try to convert the value to a date
|
|
13
|
+
const date = new Date(value);
|
|
14
|
+
if (!isNaN(date.getTime())) {
|
|
15
|
+
return date.getTime() + getUserOffset(date.getTime());
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const currentVariables = {
|
|
19
|
+
...variables,
|
|
20
|
+
[NOW]: todayToEpoch()
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
return applyFormula(value, currentVariables);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const applyFormula = (expression: string, variables: { [key: string]: number | string }): number => {
|
|
27
|
+
expression = expression.replace(/\s/g, '');
|
|
28
|
+
const matches = expression.matchAll(EXPRESSION);
|
|
29
|
+
|
|
30
|
+
if (!matches) {
|
|
31
|
+
console.error('Invalid expression');
|
|
32
|
+
return NaN;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let timestamp = 0;
|
|
36
|
+
|
|
37
|
+
// Retrieving group in order are: operator (+ or -), number, unit, and special (variable)
|
|
38
|
+
matches.forEach(match => {
|
|
39
|
+
const operator = match[1];
|
|
40
|
+
const number = match[2];
|
|
41
|
+
const unit = match[3];
|
|
42
|
+
const special = match[4];
|
|
43
|
+
|
|
44
|
+
if (unit) {
|
|
45
|
+
timestamp = applyUnit(timestamp, operator, number, unit, variables);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (special) {
|
|
49
|
+
timestamp = applySpecial(timestamp, special);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
return timestamp;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const applyUnit = (timestamp: number, operator: string, number: string, unit: string, variables: { [key: string]: number | string }): number => {
|
|
57
|
+
let factor = operator === '-' ? -1 : 1;
|
|
58
|
+
factor *= Number.isNaN(parseInt(number)) ? 1 : parseInt(number);
|
|
59
|
+
|
|
60
|
+
switch (unit) {
|
|
61
|
+
case 's':
|
|
62
|
+
return timestamp + factor * 1000;
|
|
63
|
+
case 'm':
|
|
64
|
+
return timestamp + factor * 60 * 1000;
|
|
65
|
+
case 'h':
|
|
66
|
+
return timestamp + factor * 60 * 60 * 1000;
|
|
67
|
+
case 'd':
|
|
68
|
+
return new Date(timestamp).setDate(new Date(timestamp).getDate() + factor)
|
|
69
|
+
case 'w':
|
|
70
|
+
return new Date(timestamp).setDate(new Date(timestamp).getDate() + factor * 7);
|
|
71
|
+
case 'M':
|
|
72
|
+
return new Date(timestamp).setMonth(new Date(timestamp).getMonth() + factor);
|
|
73
|
+
case 'y':
|
|
74
|
+
return new Date(timestamp).setFullYear(new Date(timestamp).getFullYear() + factor);
|
|
75
|
+
default:
|
|
76
|
+
if(variables[unit]) {
|
|
77
|
+
const variableValue = getVariableValue(unit, variables);
|
|
78
|
+
return timestamp + factor * variableValue;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
console.error(`Invalid unit expression, unit ${unit} is not defined in the variables`);
|
|
82
|
+
return timestamp;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const getVariableValue = (unit: string, variables: { [key: string]: number | string }): number => {
|
|
88
|
+
if(variables[unit]) {
|
|
89
|
+
const variableValue = parseInt(variables[unit].toString());
|
|
90
|
+
if (typeof variables[unit] === 'number') {
|
|
91
|
+
return variableValue;
|
|
92
|
+
} else if (!isNaN(variableValue)) {
|
|
93
|
+
return variableValue;
|
|
94
|
+
} else {
|
|
95
|
+
const variableExpression = variables[unit].toString();
|
|
96
|
+
const variableValue = applyFormula(variableExpression, variables);
|
|
97
|
+
if (!isNaN(variableValue)) {
|
|
98
|
+
return variableValue;
|
|
99
|
+
} else {
|
|
100
|
+
console.error(`Invalid unit expression, unit ${unit} is not defined in the variables`);
|
|
101
|
+
return 0;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
console.error(`Invalid unit expression, unit ${unit} is not defined in the variables`);
|
|
107
|
+
return 0;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const applySpecial = (timestamp: number, special: string): number => {
|
|
112
|
+
switch (special) {
|
|
113
|
+
case "h":
|
|
114
|
+
return new Date(timestamp).setMinutes(0, 0, 0);
|
|
115
|
+
case "d":
|
|
116
|
+
return new Date(timestamp).setHours(0, 0, 0, 0);
|
|
117
|
+
case "w":
|
|
118
|
+
const date = new Date(timestamp);
|
|
119
|
+
const day = date.getDay();
|
|
120
|
+
const diff = date.getDate() - day + (day === 0 ? -6 : 1);
|
|
121
|
+
return new Date(date.setDate(diff)).setHours(0, 0, 0, 0);
|
|
122
|
+
case "M":
|
|
123
|
+
const dateM = new Date(timestamp);
|
|
124
|
+
return new Date(dateM.setMonth(dateM.getMonth(), 1)).setHours(0, 0, 0, 0);
|
|
125
|
+
case "y":
|
|
126
|
+
const dateY = new Date(timestamp);
|
|
127
|
+
return new Date(dateY.setMonth(0, 1)).setHours(0, 0, 0, 0);
|
|
128
|
+
default:
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
return timestamp;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
convert
|
|
136
|
+
}
|
|
137
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dative-gpi/foundation-shared-services",
|
|
3
3
|
"sideEffects": false,
|
|
4
|
-
"version": "1.0.128-
|
|
4
|
+
"version": "1.0.128-mountain-alpha",
|
|
5
5
|
"description": "",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public"
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"author": "",
|
|
11
11
|
"license": "ISC",
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@dative-gpi/foundation-shared-domain": "1.0.128-
|
|
13
|
+
"@dative-gpi/foundation-shared-domain": "1.0.128-mountain-alpha"
|
|
14
14
|
},
|
|
15
15
|
"peerDependencies": {
|
|
16
16
|
"@dative-gpi/bones-ui": "^1.0.0",
|
|
@@ -18,5 +18,5 @@
|
|
|
18
18
|
"vue": "^3.4.38",
|
|
19
19
|
"vue-router": "^4.3.0"
|
|
20
20
|
},
|
|
21
|
-
"gitHead": "
|
|
21
|
+
"gitHead": "37cef6036141cc79839c97f8ffc124c024e5486a"
|
|
22
22
|
}
|