2019-11-15 14:41:23 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { sprintf } from '@wordpress/i18n';
|
|
|
|
import { CURRENCY } from '@woocommerce/settings';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a price with currency data.
|
|
|
|
*
|
|
|
|
* @param {number} value Number to format.
|
|
|
|
* @param {string} priceFormat Price format string.
|
2019-12-16 16:48:02 +00:00
|
|
|
* @param {string} currencySymbol Currency symbol.
|
2019-11-15 14:41:23 +00:00
|
|
|
*/
|
|
|
|
export const formatPrice = (
|
|
|
|
value,
|
2019-12-17 10:25:37 +00:00
|
|
|
priceFormat = CURRENCY.priceFormat,
|
2019-11-15 14:41:23 +00:00
|
|
|
currencySymbol = CURRENCY.symbol
|
|
|
|
) => {
|
2019-12-16 16:48:02 +00:00
|
|
|
const formattedNumber = parseInt( value, 10 );
|
|
|
|
if ( ! isFinite( formattedNumber ) ) {
|
2019-11-15 14:41:23 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
const formattedValue = sprintf(
|
|
|
|
priceFormat,
|
|
|
|
currencySymbol,
|
|
|
|
formattedNumber
|
|
|
|
);
|
|
|
|
|
|
|
|
// This uses a textarea to magically decode HTML currency symbols.
|
|
|
|
const txt = document.createElement( 'textarea' );
|
|
|
|
txt.innerHTML = formattedValue;
|
|
|
|
return txt.value;
|
|
|
|
};
|