Welcome to mirror list, hosted at ThFree Co, Russian Federation.

getFormattedEvolution.ts « src « vue « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 84b8e57decae6b0f7de7bdddc8db0fbcb3c5db18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*!
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

import Matomo from './Matomo/Matomo';

function calculateEvolution(currentValue: string|number, pastValue: string|number) {
  const pastValueParsed = parseInt(pastValue as string, 10);
  const currentValueParsed = parseInt(currentValue as string, 10) - pastValueParsed;

  let evolution: number;

  if (currentValueParsed === 0 || Number.isNaN(currentValueParsed)) {
    evolution = 0;
  } else if (pastValueParsed === 0 || Number.isNaN(pastValueParsed)) {
    evolution = 100;
  } else {
    evolution = (currentValueParsed / pastValueParsed) * 100;
  }

  return evolution;
}

function formatEvolution(evolution: number) {
  return `${evolution > 0 ? Matomo.numbers.symbolPlus : ''}${Math.round(evolution)}}%`;
}

export default function getFormattedEvolution(
  currentValue: string|number,
  pastValue: string|number,
): string {
  const evolution = calculateEvolution(currentValue, pastValue);
  return formatEvolution(evolution);
}