11import { uid } from "uid" ;
22
3- const config = useRuntimeConfig ( ) ;
4-
53/**
64 * Checks if the given data object has any variations by accessing the product's variations nodes array.
75 *
@@ -15,19 +13,28 @@ export function hasVariations(data) {
1513}
1614
1715/**
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 .
1917 *
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.
2222 */
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" ;
2527
28+ if ( ! price ) {
29+ return ;
30+ }
31+
32+ // Convert the price string to a numerical float value
2633 const numericPrice = parseFloat ( price . replace ( / [ ^ \d . ] / g, "" ) ) ;
2734
28- return new Intl . NumberFormat ( config . public . currencyLocale , {
35+ return new Intl . NumberFormat ( currencySymbol , {
2936 style : "currency" ,
30- currency : config . public . currency ,
37+ currency : currencyPrice ,
3138 minimumFractionDigits : 0 ,
3239 maximumFractionDigits : 0 ,
3340 } ) . format ( numericPrice ) ;
0 commit comments