woocommerce/plugins/woocommerce-blocks/packages/prices/utils/price.ts

149 lines
3.7 KiB
TypeScript
Raw Normal View History

Active filters block (https://github.com/woocommerce/woocommerce-blocks/pull/1168) * Basic block construction * Register on PHP side * wc-active-filters script * Price utils * Refactor price slider so state reflects the query Moves some logic from the component to the block so that min and max price can change (via query) and be reflected by the price sliders. This allows the active filters block to change the query and have those new values reflected by the slider. * Fix type checking of numbers * Styles for filter block * Improved attribute helper for getting attribute taxonomy data from ID/taxonomy * Refactor attribute filter to use updateAttributeFilter helper * Disable checkboxes when loading to avoid multiple queries * Add todos - this is blocked * Remove checked state from Attribute Filter so it gets updated from the store (https://github.com/woocommerce/woocommerce-blocks/pull/1170) * isLoading check * active price filtering rendering * Block heading * Implement block options; chip display with clear button * Clear all should remove all attributes * Enable previews * Introduce a component to look up terms from slugs using collections (which are cached) * Correct all docblocks * activePriceFilters null return * renderRemovableListItem * Remove useMemo for hasFilters * Switch classnames notation * Ensure slug is array in removeAttributeFilterBySlug * null -> undefined return types for attributes * Remove fragment * Check we have a termObject in ActiveAttributeFilters * Refactor formatPriceRange return statements * Ensure query array index will exist * Only sort when adding a query * Remove aria-label with dupe text * hasFilters is function * Update useQueryStateByKey usage * More doc block fixes * Update getAttributeFromTaxonomy return and docblock * getAttributeFromID return/docblock
2019-11-15 14:41:23 +00:00
/**
* External dependencies
*/
import { CURRENCY } from '@woocommerce/settings';
import type { CurrencyResponseInfo } from '@woocommerce/type-defs/cart-response';
import type { CartShippingPackageShippingRate } from '@woocommerce/type-defs/cart';
/**
* Internal dependencies
*/
import type { Currency } from '../types';
type SymbolPosition = 'left' | 'left_space' | 'right' | 'right_space';
Active filters block (https://github.com/woocommerce/woocommerce-blocks/pull/1168) * Basic block construction * Register on PHP side * wc-active-filters script * Price utils * Refactor price slider so state reflects the query Moves some logic from the component to the block so that min and max price can change (via query) and be reflected by the price sliders. This allows the active filters block to change the query and have those new values reflected by the slider. * Fix type checking of numbers * Styles for filter block * Improved attribute helper for getting attribute taxonomy data from ID/taxonomy * Refactor attribute filter to use updateAttributeFilter helper * Disable checkboxes when loading to avoid multiple queries * Add todos - this is blocked * Remove checked state from Attribute Filter so it gets updated from the store (https://github.com/woocommerce/woocommerce-blocks/pull/1170) * isLoading check * active price filtering rendering * Block heading * Implement block options; chip display with clear button * Clear all should remove all attributes * Enable previews * Introduce a component to look up terms from slugs using collections (which are cached) * Correct all docblocks * activePriceFilters null return * renderRemovableListItem * Remove useMemo for hasFilters * Switch classnames notation * Ensure slug is array in removeAttributeFilterBySlug * null -> undefined return types for attributes * Remove fragment * Check we have a termObject in ActiveAttributeFilters * Refactor formatPriceRange return statements * Ensure query array index will exist * Only sort when adding a query * Remove aria-label with dupe text * hasFilters is function * Update useQueryStateByKey usage * More doc block fixes * Update getAttributeFromTaxonomy return and docblock * getAttributeFromID return/docblock
2019-11-15 14:41:23 +00:00
/**
* Get currency prefix.
Active filters block (https://github.com/woocommerce/woocommerce-blocks/pull/1168) * Basic block construction * Register on PHP side * wc-active-filters script * Price utils * Refactor price slider so state reflects the query Moves some logic from the component to the block so that min and max price can change (via query) and be reflected by the price sliders. This allows the active filters block to change the query and have those new values reflected by the slider. * Fix type checking of numbers * Styles for filter block * Improved attribute helper for getting attribute taxonomy data from ID/taxonomy * Refactor attribute filter to use updateAttributeFilter helper * Disable checkboxes when loading to avoid multiple queries * Add todos - this is blocked * Remove checked state from Attribute Filter so it gets updated from the store (https://github.com/woocommerce/woocommerce-blocks/pull/1170) * isLoading check * active price filtering rendering * Block heading * Implement block options; chip display with clear button * Clear all should remove all attributes * Enable previews * Introduce a component to look up terms from slugs using collections (which are cached) * Correct all docblocks * activePriceFilters null return * renderRemovableListItem * Remove useMemo for hasFilters * Switch classnames notation * Ensure slug is array in removeAttributeFilterBySlug * null -> undefined return types for attributes * Remove fragment * Check we have a termObject in ActiveAttributeFilters * Refactor formatPriceRange return statements * Ensure query array index will exist * Only sort when adding a query * Remove aria-label with dupe text * hasFilters is function * Update useQueryStateByKey usage * More doc block fixes * Update getAttributeFromTaxonomy return and docblock * getAttributeFromID return/docblock
2019-11-15 14:41:23 +00:00
*/
const getPrefix = (
// Currency symbol.
symbol: string,
// Position of currency symbol from settings.
symbolPosition: SymbolPosition
): string => {
const prefixes = {
left: symbol,
left_space: ' ' + symbol,
right: '',
right_space: '',
};
return prefixes[ symbolPosition ] || '';
};
/**
* Get currency suffix.
*/
const getSuffix = (
// Currency symbol.
symbol: string,
// Position of currency symbol from settings.
symbolPosition: SymbolPosition
): string => {
const suffixes = {
left: '',
left_space: '',
right: symbol,
right_space: ' ' + symbol,
};
return suffixes[ symbolPosition ] || '';
};
/**
* Currency information in normalized format from server settings.
*/
const siteCurrencySettings: Currency = {
code: CURRENCY.code,
symbol: CURRENCY.symbol,
thousandSeparator: CURRENCY.thousandSeparator,
decimalSeparator: CURRENCY.decimalSeparator,
minorUnit: CURRENCY.precision,
prefix: getPrefix(
CURRENCY.symbol,
CURRENCY.symbolPosition as SymbolPosition
),
suffix: getSuffix(
CURRENCY.symbol,
CURRENCY.symbolPosition as SymbolPosition
),
};
/**
* Gets currency information in normalized format from an API response or the server.
*/
export const getCurrencyFromPriceResponse = (
// Currency data object, for example an API response containing currency formatting data.
currencyData:
| CurrencyResponseInfo
| Record< string, never >
| CartShippingPackageShippingRate
): Currency => {
if ( ! currencyData || typeof currencyData !== 'object' ) {
return siteCurrencySettings;
}
const {
currency_code: code,
currency_symbol: symbol,
currency_thousand_separator: thousandSeparator,
currency_decimal_separator: decimalSeparator,
currency_minor_unit: minorUnit,
currency_prefix: prefix,
currency_suffix: suffix,
} = currencyData;
return {
code: code || 'USD',
symbol: symbol || '$',
thousandSeparator:
typeof thousandSeparator === 'string' ? thousandSeparator : ',',
decimalSeparator:
typeof decimalSeparator === 'string' ? decimalSeparator : '.',
minorUnit: Number.isFinite( minorUnit ) ? minorUnit : 2,
prefix: typeof prefix === 'string' ? prefix : '$',
suffix: typeof suffix === 'string' ? suffix : '',
};
};
/**
* Gets currency information in normalized format, allowing overrides.
*/
export const getCurrency = (
currencyData: Partial< Currency > = {}
): Currency => {
return {
...siteCurrencySettings,
...currencyData,
};
};
/**
* Format a price, provided using the smallest unit of the currency, as a
* decimal complete with currency symbols using current store settings.
*/
export const formatPrice = (
// Price in minor unit, e.g. cents.
price: number | string,
currencyData: Currency
): string => {
if ( price === '' || price === undefined ) {
Active filters block (https://github.com/woocommerce/woocommerce-blocks/pull/1168) * Basic block construction * Register on PHP side * wc-active-filters script * Price utils * Refactor price slider so state reflects the query Moves some logic from the component to the block so that min and max price can change (via query) and be reflected by the price sliders. This allows the active filters block to change the query and have those new values reflected by the slider. * Fix type checking of numbers * Styles for filter block * Improved attribute helper for getting attribute taxonomy data from ID/taxonomy * Refactor attribute filter to use updateAttributeFilter helper * Disable checkboxes when loading to avoid multiple queries * Add todos - this is blocked * Remove checked state from Attribute Filter so it gets updated from the store (https://github.com/woocommerce/woocommerce-blocks/pull/1170) * isLoading check * active price filtering rendering * Block heading * Implement block options; chip display with clear button * Clear all should remove all attributes * Enable previews * Introduce a component to look up terms from slugs using collections (which are cached) * Correct all docblocks * activePriceFilters null return * renderRemovableListItem * Remove useMemo for hasFilters * Switch classnames notation * Ensure slug is array in removeAttributeFilterBySlug * null -> undefined return types for attributes * Remove fragment * Check we have a termObject in ActiveAttributeFilters * Refactor formatPriceRange return statements * Ensure query array index will exist * Only sort when adding a query * Remove aria-label with dupe text * hasFilters is function * Update useQueryStateByKey usage * More doc block fixes * Update getAttributeFromTaxonomy return and docblock * getAttributeFromID return/docblock
2019-11-15 14:41:23 +00:00
return '';
}
const priceInt: number =
typeof price === 'number' ? price : parseInt( price, 10 );
if ( ! Number.isFinite( priceInt ) ) {
return '';
}
const currency: Currency = getCurrency( currencyData );
const formattedPrice: number = priceInt / 10 ** currency.minorUnit;
const formattedValue: string =
currency.prefix + formattedPrice + currency.suffix;
Active filters block (https://github.com/woocommerce/woocommerce-blocks/pull/1168) * Basic block construction * Register on PHP side * wc-active-filters script * Price utils * Refactor price slider so state reflects the query Moves some logic from the component to the block so that min and max price can change (via query) and be reflected by the price sliders. This allows the active filters block to change the query and have those new values reflected by the slider. * Fix type checking of numbers * Styles for filter block * Improved attribute helper for getting attribute taxonomy data from ID/taxonomy * Refactor attribute filter to use updateAttributeFilter helper * Disable checkboxes when loading to avoid multiple queries * Add todos - this is blocked * Remove checked state from Attribute Filter so it gets updated from the store (https://github.com/woocommerce/woocommerce-blocks/pull/1170) * isLoading check * active price filtering rendering * Block heading * Implement block options; chip display with clear button * Clear all should remove all attributes * Enable previews * Introduce a component to look up terms from slugs using collections (which are cached) * Correct all docblocks * activePriceFilters null return * renderRemovableListItem * Remove useMemo for hasFilters * Switch classnames notation * Ensure slug is array in removeAttributeFilterBySlug * null -> undefined return types for attributes * Remove fragment * Check we have a termObject in ActiveAttributeFilters * Refactor formatPriceRange return statements * Ensure query array index will exist * Only sort when adding a query * Remove aria-label with dupe text * hasFilters is function * Update useQueryStateByKey usage * More doc block fixes * Update getAttributeFromTaxonomy return and docblock * getAttributeFromID return/docblock
2019-11-15 14:41:23 +00:00
// This uses a textarea to magically decode HTML currency symbols.
const txt = document.createElement( 'textarea' );
txt.innerHTML = formattedValue;
return txt.value;
};