Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Revert #5188 in benefit of #5125
Browse files Browse the repository at this point in the history
  • Loading branch information
nielslange committed Nov 23, 2021
1 parent 9737c5f commit 42ae4ed
Showing 1 changed file with 16 additions and 42 deletions.
58 changes: 16 additions & 42 deletions packages/prices/utils/price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ const siteCurrencySettings: Currency = {

/**
* Gets currency information in normalized format from an API response or the server.
*
* If no currency was provided, or currency_code is empty, the default store currency will be used.
*/
export const getCurrencyFromPriceResponse = (
// Currency data object, for example an API response containing currency formatting data.
Expand All @@ -78,7 +76,7 @@ export const getCurrencyFromPriceResponse = (
| Record< string, never >
| CartShippingPackageShippingRate
): Currency => {
if ( ! currencyData?.currency_code ) {
if ( ! currencyData || typeof currencyData !== 'object' ) {
return siteCurrencySettings;
}

Expand Down Expand Up @@ -117,28 +115,6 @@ export const getCurrency = (
};
};

const applyThousandSeparator = (
numberString: string,
thousandSeparator: string
): string => {
return numberString.replace( /\B(?=(\d{3})+(?!\d))/g, thousandSeparator );
};

const splitDecimal = (
numberString: string
): {
beforeDecimal: string;
afterDecimal: string;
} => {
const parts = numberString.split( '.' );
const beforeDecimal = parts[ 0 ];
const afterDecimal = parts[ 1 ] || '';
return {
beforeDecimal,
afterDecimal,
};
};

/**
* Get the integer value from the decimal price.
*
Expand Down Expand Up @@ -205,26 +181,24 @@ export const formatPrice = (

const currency: Currency = getCurrency( currencyData );

const {
minorUnit,
prefix,
suffix,
decimalSeparator,
thousandSeparator,
} = currency;

const formattedPrice: number = priceInt / 10 ** minorUnit;
const integerValue: string = getIntegerValue(
priceInt,
currency.thousandSeparator,
currency.minorUnit
);

const { beforeDecimal, afterDecimal } = splitDecimal(
formattedPrice.toString()
const decimalValue: string | undefined = getDecimalValue(
priceInt,
currency.minorUnit
);

const formattedValue = `${ prefix }${ applyThousandSeparator(
beforeDecimal,
thousandSeparator
) }${
afterDecimal ? `${ decimalSeparator }${ afterDecimal }` : ''
}${ suffix }`;
const formattedPrice: string =
currency.minorUnit > 0 && decimalValue !== 'undefined'
? integerValue + currency.decimalSeparator + decimalValue
: integerValue;

const formattedValue: string =
currency.prefix + formattedPrice + currency.suffix;

// This uses a textarea to magically decode HTML currency symbols.
const txt = document.createElement( 'textarea' );
Expand Down

0 comments on commit 42ae4ed

Please sign in to comment.