-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement @woocommerce/price-format dependency #29
base: trunk
Are you sure you want to change the base?
Changes from 1 commit
6fc3130
b6204e9
e0ab51f
8d3ba4c
e85f38c
fce48d6
aadbd61
f7518da
2ce76b9
9974eaf
63aac05
c48fcc2
bff7992
e1c27dc
d272664
fcefbeb
30f5a83
5c7f6ab
2cf0023
1d9e48d
706a248
96d7c07
0aa53b1
e6572fb
995f5a0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* Get the current totals. | ||
* | ||
* @param {Object} cart The cart object. | ||
* @returns {Number} The totals of the cart object, if available, else the totals of the cart progress bar preview. | ||
*/ | ||
export function getCurrentTotal( cart ) { | ||
if ( | ||
undefined !== cart && | ||
undefined !== cart.cartTotals.total_price && | ||
undefined !== cart.cartTotals.currency_minor_unit | ||
nielslange marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
const total_price = cart.cartTotals.total_price; | ||
const minor_unit = cart.cartTotals.currency_minor_unit; | ||
|
||
return total_price / Math.pow( 10, minor_unit ); | ||
mikejolley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return ''; | ||
} | ||
|
||
/** | ||
* Get the minor unit. | ||
* | ||
* @param {Object} cart The cart object. | ||
* @returns {Number} The totals of the cart object, if available, else the totals of the cart progress bar preview. | ||
*/ | ||
export function getMinorUnit( cart ) { | ||
if ( | ||
undefined !== cart && | ||
undefined !== cart.cartTotals.currency_minor_unit | ||
) { | ||
return cart.cartTotals.currency_minor_unit; | ||
nielslange marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return ''; | ||
nielslange marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
nielslange marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Get the remaining value. | ||
* | ||
* @param {Number} freeShippingFrom The free shipping from value. | ||
* @param {Number} currentTotal The current total value. | ||
* @param {Number} minorUnit The minor unit value. | ||
* @returns {Number} The difference between freeShippingFrom and currentTotal. | ||
*/ | ||
export function getRemaining( freeShippingFrom, currentTotal, minorUnit ) { | ||
const remaining = freeShippingFrom - currentTotal; | ||
const power = Math.pow( 10, minorUnit ); | ||
const result = remaining.toFixed( minorUnit ) * power; | ||
|
||
return result; | ||
mikejolley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Get the currency format. | ||
* | ||
* @param {Object} cart The cart object. | ||
* @returns {Object} The currency format object. | ||
*/ | ||
export function getCurrencyFormat( cart ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There seems to be a lot of redundant logic here, you can get currency by using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. I removed my function |
||
let format = {}; | ||
|
||
if ( undefined === cart ) { | ||
return ''; | ||
} | ||
|
||
if ( undefined !== cart.cartTotals.currency_code ) { | ||
format.code = cart.cartTotals.currency_code; | ||
} | ||
|
||
if ( undefined !== cart.cartTotals.currency_decimal_separator ) { | ||
format.decimalSeparator = cart.cartTotals.currency_decimal_separator; | ||
} | ||
|
||
if ( undefined !== cart.cartTotals.currency_minor_unit ) { | ||
format.minorUnit = cart.cartTotals.currency_minor_unit; | ||
} | ||
|
||
if ( undefined !== cart.cartTotals.currency_prefix ) { | ||
format.prefix = cart.cartTotals.currency_prefix; | ||
} | ||
|
||
if ( undefined !== cart.cartTotals.currency_suffix ) { | ||
format.suffix = cart.cartTotals.currency_suffix; | ||
} | ||
|
||
if ( undefined !== cart.cartTotals.currency_symbol ) { | ||
format.symbol = cart.cartTotals.currency_symbol; | ||
} | ||
|
||
if ( undefined !== cart.cartTotals.currency_thousand_separator ) { | ||
format.thousandSeparator = cart.cartTotals.currency_thousand_separator; | ||
} | ||
|
||
return format; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getCurrentTotal, getMinorUnit, getRemaining } from './util'; | ||
|
||
describe( 'The function getRemaining()', () => { | ||
test.each` | ||
freeShippingFrom | currentTotal | minorUnit | expected | ||
${ 50 } | ${ 48 } | ${ 2 } | ${ 200 } | ||
${ 50 } | ${ 48 } | ${ 3 } | ${ 2000 } | ||
${ 50 } | ${ 48 } | ${ 0 } | ${ 2 } | ||
${ 50 } | ${ 23.5 } | ${ 2 } | ${ 2650 } | ||
${ 50 } | ${ 23.05 } | ${ 2 } | ${ 2695 } | ||
${ 50 } | ${ 23.5 } | ${ 3 } | ${ 26500 } | ||
${ 50 } | ${ 23.05 } | ${ 3 } | ${ 26950 } | ||
${ 50 } | ${ 23.5 } | ${ 0 } | ${ 27 } | ||
${ 50 } | ${ 23.05 } | ${ 0 } | ${ 27 } | ||
`( | ||
'correctly returns the remaining price given $minorUnit minorUnit, $freeShippingFrom freeShippingFrom and $currentTotal currentTotal as $expected', | ||
( { minorUnit, freeShippingFrom, currentTotal, expected } ) => { | ||
const remaining = getRemaining( | ||
freeShippingFrom, | ||
currentTotal, | ||
minorUnit | ||
); | ||
expect( remaining ).toEqual( expected ); | ||
} | ||
); | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like an early abstraction that shouldn't happen yet until you actually need it, further more, some of functions are useless if you check for cart here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean with the early abstraction. Could you elaborate on that? And could you tell me which functions you think are useless here? I created them to extract the cart total, the minor units and the currency format from the cart object, if a cart object is available.