@dative-gpi/foundation-shared-services 1.1.23-fs-chart → 1.1.24-unit-formatter

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,66 @@
1
+ import { computed, toValue, type ComputedRef, type MaybeRefOrGetter } from "vue";
2
+ import { SI_PREFIXES } from "@dative-gpi/foundation-shared-services/config/units";
3
+
4
+ import { useAppLanguageCode } from "./app";
5
+
6
+ const NO_VALUE = "-";
7
+
8
+ const DECADES_PER_PREFIX = 3;
9
+ const SMALLEST_PREFIX_EXPONENT = Math.round(Math.log10(SI_PREFIXES[0].factor));
10
+ const BASE_PREFIX_INDEX = -SMALLEST_PREFIX_EXPONENT / DECADES_PER_PREFIX;
11
+
12
+ export interface FormattedQuantity {
13
+ value: ComputedRef<string>;
14
+ unit: ComputedRef<string>;
15
+ formatted: ComputedRef<string>;
16
+ }
17
+
18
+ export const useUnitFormat = () => {
19
+ const { languageCode } = useAppLanguageCode();
20
+
21
+ const findBestPrefix = (numericValue: number) => {
22
+ if (numericValue === 0) {
23
+ return SI_PREFIXES[BASE_PREFIX_INDEX];
24
+ }
25
+ const magnitude = Math.floor(Math.log10(Math.abs(numericValue)) / DECADES_PER_PREFIX) * DECADES_PER_PREFIX;
26
+ const index = Math.floor((magnitude - SMALLEST_PREFIX_EXPONENT) / DECADES_PER_PREFIX);
27
+ return SI_PREFIXES[Math.max(0, Math.min(index, SI_PREFIXES.length - 1))];
28
+ };
29
+
30
+ const format = (rawValue: MaybeRefOrGetter<number>,baseUnit: MaybeRefOrGetter<string>,precision?: MaybeRefOrGetter<number | undefined>): FormattedQuantity => {
31
+
32
+ const prefixed = computed(() => {
33
+ const numericValue = toValue(rawValue);
34
+ if (!isFinite(numericValue)) {
35
+ return null;
36
+ }
37
+ const prefix = findBestPrefix(numericValue);
38
+ return {
39
+ scaledValue: numericValue / prefix.factor,
40
+ unitSymbol: `${prefix.prefix}${toValue(baseUnit)}`,
41
+ };
42
+ });
43
+
44
+ const value = computed(() => {
45
+ if (prefixed.value === null) {
46
+ return NO_VALUE;
47
+ }
48
+ const decimals = toValue(precision);
49
+ const locale = languageCode.value ?? "fr-FR";
50
+ return new Intl.NumberFormat(locale, {
51
+ minimumFractionDigits: decimals,
52
+ maximumFractionDigits: decimals,
53
+ }).format(prefixed.value.scaledValue);
54
+ });
55
+
56
+ const unit = computed(() => prefixed.value?.unitSymbol ?? "");
57
+
58
+ const formatted = computed(() =>
59
+ prefixed.value === null ? NO_VALUE : `${value.value} ${unit.value}`
60
+ );
61
+
62
+ return { value, unit, formatted };
63
+ };
64
+
65
+ return { format };
66
+ };
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,13 @@
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
+ ];
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.23-fs-chart",
7
+ "version": "1.1.24-unit-formatter",
8
8
  "description": "",
9
9
  "publishConfig": {
10
10
  "access": "public"
@@ -13,14 +13,14 @@
13
13
  "author": "",
14
14
  "license": "ISC",
15
15
  "dependencies": {
16
- "@dative-gpi/foundation-shared-domain": "1.1.23-fs-chart",
16
+ "@dative-gpi/foundation-shared-domain": "1.1.24-unit-formatter",
17
17
  "@vueuse/core": "^14.0.0"
18
18
  },
19
19
  "peerDependencies": {
20
20
  "@dative-gpi/bones-ui": "^1.0.0",
21
21
  "@microsoft/signalr": "^8.0.0",
22
- "vue": "3.5.26",
22
+ "vue": "^3.4.38",
23
23
  "vue-router": "^4.3.0"
24
24
  },
25
- "gitHead": "30343150bd0bafec5b69a6de54b425034f3160ff"
25
+ "gitHead": "ac04fd5da2ebd73bfb31ff6ffc4f5ff54a872e9e"
26
26
  }
package/tools/index.ts CHANGED
@@ -1,3 +1,2 @@
1
- export * from "./collectionTools";
2
1
  export * from "./hubFactory";
3
2
  export * from "./translate";
@@ -1,18 +0,0 @@
1
- import { type AddOrUpdateCallback, type AllCallback, type DeleteCallback, type NotifyEvent } from "@dative-gpi/bones-ui";
2
-
3
- export const createCollectionHandler = <T>(
4
- onCollectionChangedHandler: AllCallback<T>,
5
- createElementHandler: (el: any) => T
6
- ) => {
7
- return (ev: NotifyEvent, el: any) => {
8
- switch (ev) {
9
- case "add":
10
- case "update":
11
- (onCollectionChangedHandler as AddOrUpdateCallback<T>)(ev, createElementHandler(el));
12
- break;
13
- case "delete":
14
- (onCollectionChangedHandler as DeleteCallback)(ev, el);
15
- break;
16
- }
17
- };
18
- };