@dative-gpi/foundation-shared-services 1.0.191-add-unit-formatter → 1.0.191-rename-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.
@@ -6,5 +6,4 @@ export * from "./useFiles";
6
6
  export * from "./useFoundationShared";
7
7
  export * from "./useRouting";
8
8
  export * from "./useDateExpression";
9
- export * from "./useTimeDuration";
10
- export * from "./useUnitFormatter";
9
+ export * from "./useTimeDuration";
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.191-add-unit-formatter",
7
+ "version": "1.0.191-rename-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.191-add-unit-formatter",
16
+ "@dative-gpi/foundation-shared-domain": "1.0.191-rename-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": "0a19e29a6ff02531f7a54c712d7fcd27c328fc27"
25
+ "gitHead": "3341fa68958f7d55ff71cec2f27edf169e59ea87"
26
26
  }
@@ -1,152 +0,0 @@
1
- import { UnitPrefix } from "@dative-gpi/foundation-shared-domain/enums/units";
2
- import { useAppLanguageCode } from "@dative-gpi/foundation-shared-services/composables";
3
- import { type UnitDefinition, unitRegistry } from "@dative-gpi/foundation-shared-domain/models";
4
-
5
- export function useUnitFormatter() {
6
- const { languageCode } = useAppLanguageCode();
7
-
8
- const SI_PREFIXES = [
9
- { prefix: UnitPrefix.Nano, factor: 1e-9 },
10
- { prefix: UnitPrefix.Micro, factor: 1e-6 },
11
- { prefix: UnitPrefix.Milli, factor: 1e-3 },
12
- { prefix: UnitPrefix.None, factor: 1 },
13
- { prefix: UnitPrefix.Kilo, factor: 1e3 },
14
- { prefix: UnitPrefix.Mega, factor: 1e6 },
15
- { prefix: UnitPrefix.Giga, factor: 1e9 },
16
- { prefix: UnitPrefix.Tera, factor: 1e12 },
17
- { prefix: UnitPrefix.Peta, factor: 1e15 },
18
- ];
19
-
20
- function scaleUpByParent(value: number, unit: string, fixedUnit?: string): { value: number; unit: string } {
21
- let currentValue = value;
22
- let currentUnit = unit;
23
- const SCALE_FACTOR = 1000;
24
-
25
- while (true) {
26
- const unitDefinition = unitRegistry[currentUnit];
27
-
28
- if (!unitDefinition?.parent) {
29
- break;
30
- }
31
-
32
- if (fixedUnit && currentUnit === fixedUnit) {
33
- break;
34
- }
35
-
36
- const scaledValue = currentValue / SCALE_FACTOR;
37
-
38
- if (Math.abs(scaledValue) >= 1) {
39
- currentValue = scaledValue;
40
- currentUnit = unitDefinition.parent;
41
- } else {
42
- break;
43
- }
44
- }
45
-
46
- return { value: currentValue, unit: currentUnit };
47
- }
48
-
49
- function applySIScale(value: number, unit: string) {
50
- const absoluteValue = Math.abs(value);
51
-
52
- let selectedPrefix = SI_PREFIXES[3];
53
-
54
- for (let i = SI_PREFIXES.length - 1; i >= 0; i--) {
55
- const prefix = SI_PREFIXES[i];
56
-
57
- if (absoluteValue >= prefix.factor) {
58
- selectedPrefix = prefix;
59
- break;
60
- }
61
- }
62
-
63
- return {
64
- value: value / selectedPrefix.factor,
65
- unit: `${selectedPrefix.prefix}${unit}`,
66
- };
67
- }
68
-
69
- function applyConversions(value: number, unit: string, unitDefinition: UnitDefinition) {
70
- if (!unitDefinition.conversions?.length) {
71
- return { value, unit };
72
- }
73
-
74
- for (const conversion of unitDefinition.conversions) {
75
- if (conversion.minThreshold != null && Math.abs(value) < conversion.minThreshold) {
76
- continue;
77
- }
78
-
79
- return {
80
- value: value * conversion.conversionRate,
81
- unit: conversion.targetUnit
82
- };
83
- }
84
-
85
- return { value, unit };
86
- }
87
-
88
- function formatNumber(value: number, decimalPrecision: number, locale?: string) {
89
- return new Intl.NumberFormat(locale, {
90
- minimumFractionDigits: 0,
91
- maximumFractionDigits: decimalPrecision,
92
- }).format(value);
93
- }
94
-
95
- const formatQuantity = (value: number, unit: string, options?: { decimalPrecision?: number; fixedUnit?: string;} ): string => {
96
- if (!isFinite(value)) {
97
- return "—";
98
- }
99
-
100
- const unitDefinition = unitRegistry[unit];
101
- let result: { value: number; unit: string };
102
- let decimalPrecision: number;
103
-
104
- if (unitDefinition) {
105
- result = applyConversions(value, unit, unitDefinition);
106
-
107
- result = scaleUpByParent(result.value, result.unit, options?.fixedUnit);
108
-
109
- decimalPrecision = options?.decimalPrecision ?? (unitRegistry[result.unit]?.precision ?? 2);
110
- } else {
111
- result = applySIScale(value, unit);
112
- decimalPrecision = options?.decimalPrecision ?? 2;
113
- }
114
-
115
- const unitSymbol = unitRegistry[result.unit]?.symbol ?? result.unit;
116
- return `${formatNumber(result.value, decimalPrecision, languageCode.value)} ${unitSymbol}`;
117
- };
118
-
119
- const formatQuantityParts = (value: number, unit: string, options?: { decimalPrecision?: number;fixedUnit?: string;} ): { value: string; unit: string } => {
120
-
121
- const formattedQuantity = formatQuantity(value, unit, {
122
- decimalPrecision: options?.decimalPrecision,
123
- fixedUnit: options?.fixedUnit
124
- });
125
-
126
- if (formattedQuantity === "—") {
127
- return {
128
- value: "—",
129
- unit: ""
130
- };
131
- }
132
-
133
- const lastSpaceIndex = formattedQuantity.lastIndexOf(" ");
134
-
135
- if (lastSpaceIndex === -1) {
136
- return {
137
- value: formattedQuantity,
138
- unit: ""
139
- };
140
- }
141
-
142
- return {
143
- value: formattedQuantity.slice(0, lastSpaceIndex),
144
- unit: formattedQuantity.slice(lastSpaceIndex + 1)
145
- };
146
- };
147
-
148
- return {
149
- formatQuantity,
150
- formatQuantityParts,
151
- };
152
- }