@dative-gpi/foundation-shared-services 1.1.24-widget-2 → 1.1.25

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.
@@ -6,4 +6,5 @@ export * from "./useFiles";
6
6
  export * from "./useFoundationShared";
7
7
  export * from "./useRouting";
8
8
  export * from "./useDateExpression";
9
- export * from "./useTimeDuration";
9
+ export * from "./useTimeDuration";
10
+ export * from "./useUnitFormat";
@@ -0,0 +1,63 @@
1
+ import { computed, toValue, type MaybeRefOrGetter } from "vue";
2
+ import { SI_PREFIXES, DECADES_PER_PREFIX, NO_VALUE, SMALLEST_PREFIX_EXPONENT } from "@dative-gpi/foundation-shared-services/config";
3
+ import { type FormattedQuantity } from "@dative-gpi/foundation-shared-domain/models";
4
+
5
+ import { useAppLanguageCode } from "./app";
6
+
7
+ export const useUnitFormat = (precision?: MaybeRefOrGetter<number | undefined>) => {
8
+ const { languageCode } = useAppLanguageCode();
9
+
10
+ const numberFormatter = computed(() => {
11
+ const decimals = toValue(precision);
12
+ const locale = languageCode.value ?? "fr-FR";
13
+
14
+ return new Intl.NumberFormat(locale, {
15
+ minimumFractionDigits: decimals,
16
+ maximumFractionDigits: decimals,
17
+ });
18
+ });
19
+
20
+ const format = (rawValue: MaybeRefOrGetter<number>, baseUnit: MaybeRefOrGetter<string>): FormattedQuantity => {
21
+
22
+ const quantityWithPrefix = computed(() => {
23
+ const numericValue = toValue(rawValue);
24
+ if (!Number.isFinite(numericValue)) {
25
+ return null;
26
+ }
27
+ const valueExponent = numericValue === 0 ? 0 : Math.floor(Math.log10(Math.abs(numericValue)));
28
+
29
+ const prefixExponent = Math.floor(valueExponent / DECADES_PER_PREFIX) * DECADES_PER_PREFIX;
30
+
31
+ const prefixIndex = (prefixExponent - SMALLEST_PREFIX_EXPONENT) / DECADES_PER_PREFIX;
32
+
33
+ const clampedPrefixIndex = Math.max(0, Math.min(prefixIndex, SI_PREFIXES.length - 1));
34
+
35
+ const prefix = SI_PREFIXES[clampedPrefixIndex];
36
+
37
+ return {
38
+ scaledValue: numericValue / prefix.factor,
39
+ unitSymbol: `${prefix.prefix}${toValue(baseUnit)}`,
40
+ };
41
+ });
42
+
43
+ const formattedValue = computed(() => {
44
+ if (quantityWithPrefix.value === null) {
45
+ return NO_VALUE;
46
+ }
47
+
48
+ return numberFormatter.value.format(quantityWithPrefix.value.scaledValue);
49
+ });
50
+
51
+ const unit = computed(() => quantityWithPrefix.value?.unitSymbol ?? "");
52
+
53
+ const formattedText = computed(() =>
54
+ quantityWithPrefix.value === null
55
+ ? NO_VALUE
56
+ : `${formattedValue.value} ${unit.value}`
57
+ );
58
+
59
+ return { formattedValue, unit, formattedText };
60
+ };
61
+
62
+ return { format };
63
+ };
package/config/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./literals";
2
2
  export * from "./timeDuration";
3
+ export * from "./units";
3
4
  export * from "./urls";
@@ -0,0 +1 @@
1
+ export * from "./unitPrefixes";
@@ -0,0 +1,19 @@
1
+ import { UnitPrefix } from "@dative-gpi/foundation-shared-domain/enums/units";
2
+
3
+ export const SI_PREFIXES = [
4
+ { prefix: UnitPrefix.Nano, factor: 1e-9 },
5
+ { prefix: UnitPrefix.Micro, factor: 1e-6 },
6
+ { prefix: UnitPrefix.Milli, factor: 1e-3 },
7
+ { prefix: UnitPrefix.None, factor: 1 },
8
+ { prefix: UnitPrefix.Kilo, factor: 1e3 },
9
+ { prefix: UnitPrefix.Mega, factor: 1e6 },
10
+ { prefix: UnitPrefix.Giga, factor: 1e9 },
11
+ { prefix: UnitPrefix.Tera, factor: 1e12 },
12
+ { prefix: UnitPrefix.Peta, factor: 1e15 },
13
+ ];
14
+
15
+ export const NO_VALUE = "-";
16
+
17
+ export const DECADES_PER_PREFIX = 3;
18
+ export const SMALLEST_PREFIX_EXPONENT = Math.round(Math.log10(SI_PREFIXES[0].factor));
19
+ export const BASE_PREFIX_INDEX = -SMALLEST_PREFIX_EXPONENT / DECADES_PER_PREFIX;
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.1.24-widget-2",
7
+ "version": "1.1.25",
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.1.24-widget-2",
16
+ "@dative-gpi/foundation-shared-domain": "1.1.25",
17
17
  "@vueuse/core": "^14.0.0"
18
18
  },
19
19
  "peerDependencies": {
@@ -22,5 +22,5 @@
22
22
  "vue": "3.5.26",
23
23
  "vue-router": "^4.3.0"
24
24
  },
25
- "gitHead": "e3b64258974077d06ddad77bc4133e4e5aa08c31"
25
+ "gitHead": "446431fe0419f110f86f25393e424eee6f859cde"
26
26
  }