Create New Item
Item Type
File
Folder
Item Name
Search file in folder and subfolders...
Are you sure want to rename?
optometrists
/
cache
/
cache
/
cache
/
cache
/
cache
/
.wp-cli
/
wp-content
/
plugins
/
woocommerce
/
packages
/
woocommerce-blocks
/
assets
/
js
/
blocks
/
price-filter
:
use-price-constraints.js
Advanced Search
Upload
New Item
Settings
Back
Back Up
Advanced Editor
Save
/** * External dependencies */ import { usePrevious } from '@woocommerce/base-hooks'; /** * Internal dependencies */ import { ROUND_UP, ROUND_DOWN } from './constants'; /** * Return the price constraint. * * @param {number} price Price in minor unit, e.g. cents. * @param {number} minorUnit Price minor unit (number of digits after the decimal separator). * @param {ROUND_UP|ROUND_DOWN} direction Rounding flag whether we round up or down. */ export const usePriceConstraint = ( price, minorUnit, direction ) => { const step = 10 * 10 ** minorUnit; let currentConstraint; if ( direction === ROUND_UP ) { currentConstraint = isNaN( price ) ? null : Math.ceil( parseFloat( price, 10 ) / step ) * step; } else if ( direction === ROUND_DOWN ) { currentConstraint = isNaN( price ) ? null : Math.floor( parseFloat( price, 10 ) / step ) * step; } const previousConstraint = usePrevious( currentConstraint, Number.isFinite ); return Number.isFinite( currentConstraint ) ? currentConstraint : previousConstraint; }; export default ( { minPrice, maxPrice, minorUnit } ) => { return { minConstraint: usePriceConstraint( minPrice, minorUnit, ROUND_DOWN ), maxConstraint: usePriceConstraint( maxPrice, minorUnit, ROUND_UP ), }; };