@dative-gpi/foundation-shared-services 1.1.24-unit-formatter → 1.1.24-unit-formatter-2
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 +2 -2
- package/composables/units/index.ts +2 -0
- package/composables/units/units.ts +14 -0
- package/composables/units/useUnitFormat.ts +66 -0
- package/package.json +4 -4
- package/tools/collectionTools.ts +18 -0
- package/tools/index.ts +1 -0
- package/composables/useUnitFormat.ts +0 -66
package/composables/index.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
export * from "./services";
|
|
2
2
|
export * from "./app";
|
|
3
3
|
|
|
4
|
+
export * from "./units";
|
|
4
5
|
export * from "./useDateFormat";
|
|
5
6
|
export * from "./useFiles";
|
|
6
7
|
export * from "./useFoundationShared";
|
|
7
8
|
export * from "./useRouting";
|
|
8
9
|
export * from "./useDateExpression";
|
|
9
|
-
export * from "./useTimeDuration";
|
|
10
|
-
export * from "./useUnitFormat";
|
|
10
|
+
export * from "./useTimeDuration";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { SI_PREFIXES } from "@/shared/foundation-shared-services/config";
|
|
2
|
+
import type { ComputedRef } from "vue";
|
|
3
|
+
|
|
4
|
+
export interface FormattedQuantity {
|
|
5
|
+
displayValue: ComputedRef<string>;
|
|
6
|
+
unit: ComputedRef<string>;
|
|
7
|
+
displayText: ComputedRef<string>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const NO_VALUE = "-";
|
|
11
|
+
|
|
12
|
+
export const DECADES_PER_PREFIX = 3;
|
|
13
|
+
export const SMALLEST_PREFIX_EXPONENT = Math.round(Math.log10(SI_PREFIXES[0].factor));
|
|
14
|
+
export const BASE_PREFIX_INDEX = -SMALLEST_PREFIX_EXPONENT / DECADES_PER_PREFIX;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { computed, toValue, type MaybeRefOrGetter } from "vue";
|
|
2
|
+
import { SI_PREFIXES } from "@dative-gpi/foundation-shared-services/config";
|
|
3
|
+
import { BASE_PREFIX_INDEX, DECADES_PER_PREFIX, NO_VALUE, SMALLEST_PREFIX_EXPONENT, type FormattedQuantity } from "./units";
|
|
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 prefix = (() => {
|
|
28
|
+
if (numericValue === 0) {
|
|
29
|
+
return SI_PREFIXES[BASE_PREFIX_INDEX];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const valueExponent = Math.floor(Math.log10(Math.abs(numericValue)));
|
|
33
|
+
|
|
34
|
+
const prefixExponent = Math.floor(valueExponent / DECADES_PER_PREFIX) * DECADES_PER_PREFIX;
|
|
35
|
+
|
|
36
|
+
const prefixIndex = (prefixExponent - SMALLEST_PREFIX_EXPONENT) / DECADES_PER_PREFIX;
|
|
37
|
+
|
|
38
|
+
const clampedPrefixIndex = Math.max(0, Math.min(prefixIndex, SI_PREFIXES.length - 1 ));
|
|
39
|
+
|
|
40
|
+
return SI_PREFIXES[clampedPrefixIndex];
|
|
41
|
+
})();
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
scaledValue: numericValue / prefix.factor,
|
|
45
|
+
unitSymbol: `${prefix.prefix}${toValue(baseUnit)}`,
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const displayValue = computed(() => {
|
|
50
|
+
if (quantityWithPrefix.value === null) {
|
|
51
|
+
return NO_VALUE;
|
|
52
|
+
}
|
|
53
|
+
return numberFormatter.value.format(quantityWithPrefix.value.scaledValue);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const unit = computed(() => quantityWithPrefix.value?.unitSymbol ?? "");
|
|
57
|
+
|
|
58
|
+
const displayText = computed(() =>
|
|
59
|
+
quantityWithPrefix.value === null ? NO_VALUE : `${displayValue.value} ${unit.value}`
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
return { displayValue, unit, displayText };
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return { format };
|
|
66
|
+
};
|
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-unit-formatter",
|
|
7
|
+
"version": "1.1.24-unit-formatter-2",
|
|
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.24-unit-formatter",
|
|
16
|
+
"@dative-gpi/foundation-shared-domain": "1.1.24-unit-formatter-2",
|
|
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": "
|
|
22
|
+
"vue": "3.5.26",
|
|
23
23
|
"vue-router": "^4.3.0"
|
|
24
24
|
},
|
|
25
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "8e4e425fd5a9d635fd66121d110beb5745e337f3"
|
|
26
26
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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
|
+
};
|
package/tools/index.ts
CHANGED
|
@@ -1,66 +0,0 @@
|
|
|
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
|
-
};
|