1
1
import { uid } from "uid" ;
2
2
3
- const config = useRuntimeConfig ( ) ;
4
-
5
3
/**
6
4
* Checks if the given data object has any variations by accessing the product's variations nodes array.
7
5
*
@@ -15,19 +13,28 @@ export function hasVariations(data) {
15
13
}
16
14
17
15
/**
18
- * Formats the given price to the currency specified in the config file using the Intl.NumberFormat object .
16
+ * Formats a given price into a string with a currency symbol and locale .
19
17
*
20
- * @param {number } price - The price to be formatted.
21
- * @return {string } The formatted price as a string with the currency symbol and no decimal places.
18
+ * @param {string } price - The price to format.
19
+ * @param {string } currency - The currency to use. Defaults to "NOK".
20
+ * @param {string } currencyLocale - The locale to use for the currency symbol. Defaults to "nb-NO".
21
+ * @return {string } The formatted price as a string with the currency symbol and locale.
22
22
*/
23
- export function formatPrice ( price ) {
24
- // Convert the price string to a numerical float value
23
+ export function formatPrice ( price , currency , currencyLocale ) {
24
+ // Set default values
25
+ const currencyPrice = currency || "NOK" ;
26
+ const currencySymbol = currencyLocale || "nb-NO" ;
25
27
28
+ if ( ! price ) {
29
+ return ;
30
+ }
31
+
32
+ // Convert the price string to a numerical float value
26
33
const numericPrice = parseFloat ( price . replace ( / [ ^ \d . ] / g, "" ) ) ;
27
34
28
- return new Intl . NumberFormat ( config . public . currencyLocale , {
35
+ return new Intl . NumberFormat ( currencySymbol , {
29
36
style : "currency" ,
30
- currency : config . public . currency ,
37
+ currency : currencyPrice ,
31
38
minimumFractionDigits : 0 ,
32
39
maximumFractionDigits : 0 ,
33
40
} ) . format ( numericPrice ) ;
0 commit comments