Failed Conditions
Push — develop ( 78df23...8d22aa )
by Reüel
09:06
created

js/src/utils.js   A

Complexity

Total Complexity 7
Complexity/F 3.5

Size

Lines of Code 45
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 7
mnd 5
bc 5
fnc 2
bpm 2.5
cpm 3.5
noi 4

1 Function

Rating   Name   Duplication   Size   Complexity  
A utils.js ➔ formatMoney 0 28 3
1
/**
2
 * Calculate progress value.
3
 *
4
 * @param string raised Raised amount.
0 ignored issues
show
Documentation introduced by
The parameter string does not exist. Did you maybe forget to remove this comment?
Loading history...
5
 * @param string target Target amount.
0 ignored issues
show
Documentation introduced by
The parameter string has already been documented on line 4. The second definition is ignored.
Loading history...
6
 * @return float
7
 */
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 ) ) {
0 ignored issues
show
Best Practice introduced by
Comparing 0 to raised using the == operator is not safe. Consider using === instead.
Loading history...
Best Practice introduced by
Comparing 0 to target using the == operator is not safe. Consider using === instead.
Loading history...
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
}