Skip to content

Commit

Permalink
apply review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
sgiehl committed Nov 18, 2024
1 parent f723f40 commit 7fea1f7
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 157 deletions.
40 changes: 15 additions & 25 deletions plugins/CoreHome/vue/dist/CoreHome.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/CoreHome/vue/dist/CoreHome.umd.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions plugins/CoreHome/vue/src/NumberFormatter/NumberFormatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,32 @@ describe('CoreHome/NumberFormatter', () => {
})
});

const formattedNumberTestData: Array<Array<any>> = [
['en', '+100%', 100],
['en', '-75%', -75],
['en', '12,245.66', 12245.66],
['en', '-0.555', -0.555],
['ar', '؜+51٬239٫56٪؜', 51239.56],
['be', '+51 239,56 %', 51239.56],
['de', '+51.239,56 %', 51239.56 ],
['de', '-239,56 $%', -239.56 ],
['he', '‎+152,551,239.56%', 152551239.56],
['he', '‎-152,551,239.56', -152551239.56],
['hi', '+15,25,51,240%', 152551240],
['lt', '−152 551 240 %', -152551240],
];

formattedNumberTestData.forEach((testdata) => {
const [ lang, input, expected ] = testdata;

it(`should correctly parse formatted number with (${lang}, ${input})`, () => {

window.piwik.numbers = formats[lang];

const result = NumberFormatter.parseFormattedNumber(input as string);

expect(result).toEqual(expected);
})
});

});
43 changes: 18 additions & 25 deletions plugins/CoreHome/vue/src/NumberFormatter/NumberFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,11 @@ import Matomo from '../Matomo/Matomo';

const { $ } = window;

/**
* Number Formatter for formatting numbers, percent and currencies values
*
* @type {object}
*/
class NumberFormatter {
defaultMinFractionDigits = 0;

defaultMaxFractionDigits = 2;

/**
* Formats the given numeric value with the given pattern
*
* @param value
* @param pattern
* @returns {string}
*/
private format(
val: string|number,
formatPattern: string,
Expand Down Expand Up @@ -119,12 +107,6 @@ class NumberFormatter {
return this.replaceSymbols(result);
}

/**
* Replaces the placeholders with real symbols
*
* @param value
* @returns {string}
*/
private replaceSymbols(value: string): string {
const replacements = {
'.': Matomo.numbers.symbolDecimal,
Expand Down Expand Up @@ -155,14 +137,25 @@ class NumberFormatter {
return newValue;
}

private valOrDefault(def: number, val?: number): number {
private valOrDefault(val: number|undefined, def: number): number {
if (typeof val === 'undefined') {
return def;
}

return val;
}

public parseFormattedNumber(value: string): number {
const isNegative = value.indexOf(Matomo.numbers.symbolMinus) > -1 || value.startsWith('-');
const numberParts = value.split(Matomo.numbers.symbolDecimal);

numberParts.forEach((val, index) => {
numberParts[index] = val.replace(/[^0-9]/g, '');
});

return (isNegative ? -1 : 1) * parseFloat(numberParts.join('.'));
}

public formatNumber(
value: string|number,
maxFractionDigits?: number,
Expand All @@ -171,8 +164,8 @@ class NumberFormatter {
return this.format(
value,
Matomo.numbers.patternNumber,
this.valOrDefault(this.defaultMaxFractionDigits, maxFractionDigits),
this.valOrDefault(this.defaultMinFractionDigits, minFractionDigits),
this.valOrDefault(maxFractionDigits, this.defaultMaxFractionDigits),
this.valOrDefault(minFractionDigits, this.defaultMinFractionDigits),
);
}

Expand All @@ -184,8 +177,8 @@ class NumberFormatter {
return this.format(
value,
Matomo.numbers.patternPercent,
this.valOrDefault(this.defaultMaxFractionDigits, maxFractionDigits),
this.valOrDefault(this.defaultMinFractionDigits, minFractionDigits),
this.valOrDefault(maxFractionDigits, this.defaultMaxFractionDigits),
this.valOrDefault(minFractionDigits, this.defaultMinFractionDigits),
);
}

Expand All @@ -198,8 +191,8 @@ class NumberFormatter {
const formatted = this.format(
value,
Matomo.numbers.patternCurrency,
this.valOrDefault(this.defaultMaxFractionDigits, maxFractionDigits),
this.valOrDefault(this.defaultMinFractionDigits, minFractionDigits),
this.valOrDefault(maxFractionDigits, this.defaultMaxFractionDigits),
this.valOrDefault(minFractionDigits, this.defaultMinFractionDigits),
);
return formatted.replace('¤', currency);
}
Expand Down
26 changes: 1 addition & 25 deletions plugins/MultiSites/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ private function buildDataTable($idSites, $period, $date, $segment, $_restrictSi
$pastData->filter('ReplaceColumnNames', array($columnNameRewrites));
$this->populateLabel($pastData); // labels are needed to calculate evolution
$this->calculateEvolutionPercentages($dataTable, $pastData, $apiMetrics);
$this->setPastTotalVisitsMetadata($dataTable, $pastData);
$this->setPreviousMetricsTotalsMetadata($dataTable, $pastData, $totalMetrics);

if ($dataTable instanceof DataTable) {
Expand Down Expand Up @@ -554,7 +553,7 @@ private function setMetricsTotalsMetadata($dataTable, $apiMetrics)
private function setPreviousMetricsTotalsMetadata($dataTable, $pastData, $apiMetrics)
{
if ($dataTable instanceof DataTable\Map) {
$currentDataTables = $pastData->getDataTables();
$currentDataTables = $dataTable->getDataTables();
$pastDataTables = $pastData->getDataTables();
$currentLabels = array_keys($currentDataTables);
$pastLabels = array_keys($pastDataTables);
Expand Down Expand Up @@ -589,29 +588,6 @@ private function setPreviousMetricsTotalsMetadata($dataTable, $pastData, $apiMet
}
}

/**
* Sets the number of total visits in the pastTable on the dataTable as metadata.
*
* @param DataTable $dataTable
* @param DataTable $pastTable
*/
private function setPastTotalVisitsMetadata($dataTable, $pastTable)
{
if ($pastTable instanceof DataTable) {
$total = 0;
$metric = 'nb_visits';

$rows = $pastTable->getRows();
$rows = $this->filterRowsForTotalsCalculation($rows);

foreach ($rows as $row) {
$total += $row->getColumn($metric);
}

$dataTable->setMetadata(self::getTotalMetadataName($metric . '_lastdate'), $total);
}
}

/**
* @param Row[] $rows
* @return mixed
Expand Down
Loading

0 comments on commit 7fea1f7

Please sign in to comment.