Skip to content

Commit

Permalink
Merge pull request #3411 from Vizzuality/fix/universal-formatting
Browse files Browse the repository at this point in the history
Universal Sentence Formatting
  • Loading branch information
edbrett committed May 14, 2018
2 parents 57c97f0 + cde6e15 commit 4994076
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ export const getSentence = createSelector(
typeof accumulator !== 'object'
? accumulator
: accumulator.emissions
.map(a => a.value)
.reduce((iSum, value) => iSum + value);
.map(a => a.value)
.reduce((iSum, value) => iSum + value);
const itemCount = item.emissions
.map(a => a.value)
.reduce((iSum, value) => iSum + value);
Expand All @@ -137,7 +137,7 @@ export const getSentence = createSelector(
percentage:
Math.abs(emissionFraction) < 0.1
? '<0.1%'
: `${format('.1f')(Math.abs(emissionFraction))}%`,
: `${format('.2r')(Math.abs(emissionFraction))}%`,
value: `${format('.3s')(
Math.abs(emissionsCount / (endYear - startYear))
)}tCO₂e/yr`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ export const parseData = createSelector(
return {
id: k,
color: colors.main,
percentage: countsAreaPerc,
percentage:
countsAreaPerc >= 0.1 ? `${format('.2r')(countsAreaPerc)}%` : '<0.1%',
countsPerHa,
count: counts,
area: countsArea,
Expand Down Expand Up @@ -109,11 +110,11 @@ export const getSentence = createSelector(
const params = {
timeframe: options.weeks.find(w => w.value === settings.weeks).label,
count: format(',')(sumBy(data, 'count')),
area: `${format('.2s')(sumBy(data, 'area'))}ha`,
topPercent: `${format('.2s')(topCount)}%`,
area: `${format('.3s')(sumBy(data, 'area'))}ha`,
topPercent: `${format('.2r')(topCount)}%`,
topRegions: percentileLength,
location: currentLabel,
indicator: `${indicator ? `${indicator.label} in ` : ''}`
indicator: `${indicator ? `${indicator.label.toLowerCase()} in ` : ''}`
};
return { sentence: initial, params };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ export const getSentence = createSelector(
location: currentLabel === 'global' ? 'globally' : currentLabel,
gain: `${format('.3s')(gain)}ha`,
indicator: (indicator && indicator.label.toLowerCase()) || 'region-wide',
percent: areaPercent >= 0.1 ? `${format('.1f')(areaPercent)}%` : '<0.1%',
percent: areaPercent >= 0.1 ? `${format('.2r')(areaPercent)}%` : '<0.1%',
globalPercent:
globalPercent >= 0.1 ? `${format('.1f')(globalPercent)}%` : '<0.1%',
globalPercent >= 0.1 ? `${format('.2r')(globalPercent)}%` : '<0.1%',
gainPercent:
gainPercent >= 0.1 ? `${format('.1f')(gainPercent)}%` : '<0.1%',
gainPercent >= 0.1 ? `${format('.2r')(gainPercent)}%` : '<0.1%',
extentYear: settings.extentYear,
parent: parent && parent.label
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,18 @@ export const getSentence = createSelector(
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export const getSentence = createSelector(
startYear,
endYear,
loss: `${format('.3s')(totalLoss)}ha`,
percent: `${format('.1f')(percentageLoss)}%`,
percent: `${format('.2r')(percentageLoss)}%`,
emissions: `${format('.3s')(totalEmissions)}t`,
extentYear
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,20 @@ export const getSentence = createSelector(
}

const params = {
indicator: indicator && indicator.label,
indicator: indicator && indicator.label.toLowerCase(),
location: currentLabel,
startYear,
endYear,
topLoss: `${format('.0f')(topLoss)}%`,
topLoss: `${format('.2r')(topLoss)}%`,
percentileLength,
region: percentileLength > 1 ? topRegion.label : 'This region',
value:
topRegion.percentage > 1 && settings.unit === '%'
? `${format('.0f')(topRegion.percentage)}%`
? `${format('.2r')(topRegion.percentage)}%`
: `${format('.3s')(topRegion.loss)}ha`,
average:
topRegion.percentage > 1 && settings.unit === '%'
? `${format('.0f')(avgLossPercentage)}%`
? `${format('.2r')(avgLossPercentage)}%`
: `${format('.3s')(avgLoss)}ha`
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const getSentence = createSelector(
startYear,
endYear,
lossPhrase,
value: `${format('.2s')(totalOutsideLoss)}t of CO<sub>2</sub> emissions`
value: `${format('.3s')(totalOutsideLoss)}t of CO<sub>2</sub> emissions`
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ export const getSentence = createSelector(
startYear,
endYear,
loss: loss ? `${format('.3s')(loss)}ha` : '0ha',
percent: areaPercent >= 0.1 ? `${format('.1f')(areaPercent)}%` : '<0.1%',
percent: areaPercent >= 0.1 ? `${format('.2r')(areaPercent)}%` : '<0.1%',
globalPercent:
globalPercent >= 0.1 ? `${format('.1f')(globalPercent)}%` : '<0.1%',
globalPercent >= 0.1 ? `${format('.2r')(globalPercent)}%` : '<0.1%',
extentYear: settings.extentYear
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const parseConfig = createSelector([getColors], colors => ({
{
key: 'percentage',
unit: '%',
unitFormat: value => format('.1f')(value)
unitFormat: value => format('.2r')(value)
}
]
}));
Expand Down Expand Up @@ -100,7 +100,7 @@ export const getSentence = createSelector(
startYear,
endYear,
loss: `${format('.3s')(totalLoss)}ha`,
percent: `${format('.1f')(percentageLoss)}%`,
percent: `${format('.2r')(percentageLoss)}%`,
emissions: `${format('.3s')(totalEmissions)}t`,
extentYear
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,21 @@ export const getSentence = createSelector(
if (isEmpty(data)) return null;
const { initial, noPrimary, globalInitial, globalNoPrimary } = sentences;
const { area_ha, extent, forest_primary } = data;
const primaryPercent =
forest_primary > 0
? forest_primary / area_ha * 100
: extent / area_ha * 100;

const params = {
location: currentLabel || 'globally',
extent: `${format('.3s')(extent)}ha`,
primaryPercent:
forest_primary > 0
? `${format('.0f')(forest_primary / area_ha * 100)}%`
: `${format('.0f')(extent / area_ha * 100)}%`
primaryPercent >= 0.1 ? `${format('.2r')(primaryPercent)}%` : '<0.1%'
};
let sentence = forest_primary > 0 ? initial : noPrimary;
if (!currentLabel) { sentence = forest_primary > 0 ? globalInitial : globalNoPrimary; }
if (!currentLabel) {
sentence = forest_primary > 0 ? globalInitial : globalNoPrimary;
}
return {
sentence,
params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const getSentence = createSelector(
percentage:
intactPercentage < 0.1
? '<0.1%'
: `${format('.0f')(intactPercentage)}%`,
: `${format('.2r')(intactPercentage)}%`,
intact: 'intact forest'
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const getSentence = createSelector(
percentage:
primaryPercentage < 0.1
? '<0.1%'
: `${format('.0f')(primaryPercentage)}%`,
: `${format('.2r')(primaryPercentage)}%`,
primary: 'primary forest',
extentYear: settings.extentYear
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const getSentences = state => state.config.sentences || null;
const getPlanationKeys = createSelector(
[getPlantations],
plantations =>
plantations ? Object.keys(groupBy(plantations, 'label')) : null
(plantations ? Object.keys(groupBy(plantations, 'label')) : null)
);

export const parseData = createSelector(
Expand Down Expand Up @@ -116,14 +116,14 @@ export const getSentence = createSelector(
),
'value'
);
const plantationLabel = topPlantation.label;
const plantationLabel = topPlantation.label.toLowerCase();
const isPlural = endsWith(plantationLabel, 's');

const params = {
location: currentLabel,
region: topRegion.region,
topType: `${plantationLabel}${isPlural ? 's' : ''} plantations`,
percentage: `${format('.1f')(data[0].total)}%`
percentage: `${format('.2r')(data[0].total)}%`
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ export const getSentence = createSelector(
const params = {
location: currentLabel === 'global' ? 'Globally' : currentLabel,
region: topRegion.label,
indicator: indicator && indicator.label,
percentage: topExtent ? `${format('.0f')(topExtent)}%` : '0%',
indicator: indicator && indicator.label.toLowerCase(),
percentage: topExtent ? `${format('.2r')(topExtent)}%` : '0%',
value:
settings.unit === '%'
? `${format('.0f')(topRegion.percentage)}%`
? `${format('.2r')(topRegion.percentage)}%`
: `${format('.3s')(topRegion.extent)}ha`,
average:
settings.unit === '%'
? `${format('.0f')(avgExtentPercentage)}%`
? `${format('.2r')(avgExtentPercentage)}%`
: `${format('.3s')(avgExtent)}ha`,
count: percentileLength,
metric: settings.unit === '%' ? 'relative tree cover' : 'tree cover'
Expand All @@ -101,7 +101,6 @@ export const getSentence = createSelector(
} else if (indicator) {
sentence = currentLabel === 'global' ? globalWithIndicator : hasIndicator;
}

return {
sentence,
params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ export const getSentence = createSelector(

const params = {
location: currentLabel,
firstSpecies: top[0].label,
secondSpecies: top.length > 1 && top[1].label,
firstSpecies: top[0].label.toLowerCase(),
secondSpecies: top.length > 1 && top[1].label.toLowerCase(),
type: settings.type === 'bound2' ? 'species' : 'type',
extent: `${format('.2s')(sumBy(top, 'value'))}ha`,
other: `${format('.2s')(sumBy(data.slice(2), 'value'))}ha`,
extent: `${format('.3s')(sumBy(top, 'value'))}ha`,
other: `${format('.3s')(sumBy(data.slice(2), 'value'))}ha`,
count: data.length - top.length,
topType: `${top[0].label}${endsWith(top[0].label, 's') ? '' : 's'}`
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ export const getSentence = createSelector(
extentYear: settings.extentYear,
location: currentLocation.label,
extent: `${extent ? format('.3s')(extent) : '0'}ha`,
indicator: indicator && indicator.value,
indicator: indicator && indicator.value.toLowerCase(),
percentage:
areaPercent >= 0.1 ? `${format('.1f')(areaPercent)}%` : '<0.1%'
areaPercent >= 0.1 ? `${format('.2r')(areaPercent)}%` : '<0.1%'
};

const sentence = indicator ? withInd : initial;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ export const getSentence = createSelector(
const params = {
year: settings.extentYear,
location: currentLabel || 'global',
indicator: indicator && indicator.label,
indicator: indicator && indicator.label.toLowerCase(),
percentage:
percentCover >= 0.1 ? `${format('.1f')(percentCover)}%` : '<0.1%',
percentCover >= 0.1 ? `${format('.2r')(percentCover)}%` : '<0.1%',
value: `${format('.3s')(data.cover)}ha`
};
let sentence = indicator ? withIndicator : initial;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ export const getSentence = createSelector(
currentLocation &&
currentLocation.label}'s`,
value: `${formatUSD(selectedFAO[0].net_usd, false)} USD`,
percentage: `${format('.2f')(selectedFAO[0].net_perc)}%`,
percentage:
selectedFAO[0].net_perc >= 0.1
? `${format('2r')(selectedFAO[0].net_perc)}%`
: '<0.1%',
year: settings.year
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ export const getSentence = createSelector(
: selectedFAO.male;
females = parseInt(selectedFAO.female, 10);
}
const percentage = 100 * females / employees;

const params = {
location: `${currentLocation &&
currentLocation &&
currentLocation.label}'s`,
value: `${employees ? format('.3s')(employees) : 'no'}`,
percent: `${format('.2s')(100 * females / employees)}%`,
percent: percentage >= 0.1 ? `${format('.2r')(percentage)}%` : '<0.1%',
year
};

Expand Down

0 comments on commit 4994076

Please sign in to comment.