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

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/CoreHome/vue/src/getFormattedEvolution.ts')
-rw-r--r--plugins/CoreHome/vue/src/getFormattedEvolution.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/plugins/CoreHome/vue/src/getFormattedEvolution.ts b/plugins/CoreHome/vue/src/getFormattedEvolution.ts
new file mode 100644
index 0000000000..40aa032e9e
--- /dev/null
+++ b/plugins/CoreHome/vue/src/getFormattedEvolution.ts
@@ -0,0 +1,35 @@
+/*!
+ * Matomo - free/libre analytics platform
+ *
+ * @link https://matomo.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ */
+
+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 ? '+' : ''}${Math.round(evolution)}}%`;
+}
+
+export default function getFormattedEvolution(
+ currentValue: string|number,
+ pastValue: string|number,
+): string {
+ const evolution = calculateEvolution(currentValue, pastValue);
+ return formatEvolution(evolution);
+}