Total Complexity | 7 |
Complexity/F | 3.5 |
Lines of Code | 45 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | /** |
||
8 | export function calculateProgressValue( raised, target ) { |
||
9 | raised = parseFloat( raised ? raised : 0 ); |
||
10 | target = parseFloat( target ? target : 0 ); |
||
11 | |||
12 | if ( 0 == target && ( 0 == raised || raised > target ) ) { |
||
13 | target = 100; |
||
14 | } |
||
15 | |||
16 | return Math.floor( ( raised / target ) * 100 ); |
||
17 | }; |
||
18 | |||
19 | /** |
||
20 | * Format money. |
||
21 | * |
||
22 | * @param string value Amount to format. |
||
23 | * @return string |
||
24 | */ |
||
25 | export function formatMoney ( value, currencyCode, currencyDecimals, locale ) { |
||
26 | let lastPeriod = value.lastIndexOf( '.' ); |
||
27 | |||
28 | if ( lastPeriod > -1 ) { |
||
29 | let decimals = value.substring( lastPeriod + 1 ).replace( /[0]+$/g, '' ).padEnd( currencyDecimals, '0' ); |
||
30 | |||
31 | value = value.substring( 0, lastPeriod ).replace( /\./g, '' ) + '.' + decimals; |
||
32 | } |
||
33 | |||
34 | value = parseFloat( value ); |
||
35 | |||
36 | // Fraction digits. |
||
37 | let fractionDigits = currencyDecimals; |
||
38 | |||
39 | if ( 0 === ( value * 100 ) % 100 ) { |
||
40 | fractionDigits = 0; |
||
41 | } |
||
42 | |||
43 | return value.toLocaleString( |
||
44 | locale, |
||
45 | { |
||
46 | style: 'currency', |
||
47 | currency: currencyCode, |
||
48 | minimumFractionDigits: fractionDigits, |
||
49 | maximumFractionDigits: fractionDigits |
||
50 | } |
||
51 | ); |
||
52 | } |