Skip to content
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

Cleans up code #3449

Merged
merged 1 commit into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,14 @@ export const getSentence = createSelector(
sentence = humdef ? globalHuman : globalInitial;
} else if (!deforest) sentence = noDeforest;

const rateFormat = rate < 1 ? '.3r' : '.3s';
const humanFormat = humdef < 1 ? '.3r' : '.3s';

const params = {
location: currentLabel,
year: period && period.label,
rate:
rate < 1
? `${format('.3r')(rate)}ha/yr`
: `${format('.3s')(rate)}ha/yr`,
human:
humdef < 1
? `${format('.3r')(humdef)}ha/yr`
: `${format('.3s')(humdef)}ha/yr`
rate: `${format(rateFormat)(rate)}ha/yr`,
human: `${format(humanFormat)(humdef)}ha/yr`
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ export const getSentence = createSelector(
if (currentLabel !== 'global') {
sentence = countryData && countryData.value > 0 ? initial : noReforest;
}
const formatType = rate < 1 ? '.3r' : '.3s';

const params = {
location: currentLabel,
year: period && period.label,
rate:
rate < 1 ? `${format('.3r')(rate)}ha/yr` : `${format('.3s')(rate)}ha/yr`
rate: `${format(formatType)(rate)}ha/yr`
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,11 @@ export const getSentence = createSelector(
}
const topCount = percentileCount / totalCount * 100;
const countArea = sumBy(data, 'area');
const formatType = countArea < 1 ? '.3r' : '.3s';
const params = {
timeframe: options.weeks.find(w => w.value === settings.weeks).label,
count: format(',')(sumBy(data, 'count')),
area:
countArea < 1
? `${format('.3r')(countArea)}ha`
: `${format('.3s')(countArea)}ha`,
area: `${format(formatType)(countArea)}ha`,
topPercent: `${format('.2r')(topCount)}%`,
topRegions: percentileLength,
location: currentLabel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,36 +92,30 @@ export const getSentence = createSelector(
percentileGain += data[percentileLength].gain;
percentileLength += 1;
}
const value =
topRegion.percentage > 0 && settings.unit === '%'
? `${format('.2r')(topRegion.percentage)}%`
: `${format('.3s')(topRegion.gain)}ha`;

const average =
topRegion.percentage > 0 && settings.unit === '%'
? `${format('.2r')(avgGainPercentage)}%`
: `${format('.3s')(avgGain)}ha`;

const topGain = percentileGain / totalGain * 100;
let sentence = !indicator ? initialPercent : withIndicatorPercent;
if (settings.unit !== '%') {
sentence = !indicator ? initial : withIndicator;
}

const valueFormat = topRegion.gain < 1 ? '.3r' : '.3s';
const aveFormat = avgGain < 1 ? '.3r' : '.3s';

const params = {
indicator: indicator && indicator.value.toLowerCase(),
location: currentLabel,
topGain: `${format('.2r')(topGain)}%`,
percentileLength,
region: percentileLength > 1 ? topRegion.label : 'This region',
value:
topRegion.gain < 1 && settings.unit === 'ha'
? `${format('.3r')(topRegion.gain)}ha`
: value,
topRegion.percentage > 0 && settings.unit === '%'
? `${format('.2r')(topRegion.percentage)}%`
: `${format(valueFormat)(topRegion.gain)}ha`,
average:
avgGain < 1 && settings.unit === 'ha'
? `${format('.3r')(avgGain)}ha`
: average
topRegion.percentage > 0 && settings.unit === '%'
? `${format('.2r')(avgGainPercentage)}%`
: `${format(aveFormat)(avgGain)}ha`
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,6 @@ export const getSentence = createSelector(
}
const topLoss = percentileLoss / totalLoss * 100 || 0;
let sentence = !indicator ? initialPercent : withIndicatorPercent;
const value =
topRegion.percentage > 0 && settings.unit === '%'
? `${format('.2r')(topRegion.percentage)}%`
: `${format('.3s')(topRegion.loss)}ha`;

const average =
topRegion.percentage > 0 && settings.unit === '%'
? `${format('.2r')(avgLossPercentage)}%`
: `${format('.3s')(avgLoss)}ha`;

if (settings.unit !== '%') {
sentence = !indicator ? initial : withIndicator;
Expand All @@ -124,6 +115,9 @@ export const getSentence = createSelector(
sentence = noLoss;
}

const valueFormat = topRegion.loss < 1 ? '.3r' : '.3s';
const aveFormat = avgLoss < 1 ? '.3r' : '.3s';

const params = {
indicator: indicator && indicator.label.toLowerCase(),
location: currentLabel,
Expand All @@ -133,13 +127,13 @@ export const getSentence = createSelector(
percentileLength,
region: percentileLength > 1 ? topRegion.label : 'This region',
value:
topRegion.loss < 1 && settings.unit === 'ha'
? `${format('.3r')(topRegion.loss)}ha`
: value,
topRegion.percentage > 0 && settings.unit === '%'
? `${format('.2r')(topRegion.percentage)}%`
: `${format(valueFormat)(topRegion.loss)}ha`,
average:
avgLoss < 1 && settings.unit === 'ha'
? `${format('.3r')(avgLoss)}ha`
: average
topRegion.percentage > 0 && settings.unit === '%'
? `${format('.2r')(avgLossPercentage)}%`
: `${format(aveFormat)(avgLoss)}ha`
};

return {
Expand Down