From 8e5143fba84c6cc4abc0311ac28b26b6ef6e1da4 Mon Sep 17 00:00:00 2001 From: Florian Schmid Date: Fri, 28 Apr 2023 21:08:10 +0200 Subject: [PATCH] use quartal instead of whole date --- src/newMode.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/newMode.ts b/src/newMode.ts index 065c4f4..54b508a 100644 --- a/src/newMode.ts +++ b/src/newMode.ts @@ -129,6 +129,7 @@ class ViewModel { const dateArray = cpuDate.split("-"); const year = Number(dateArray[0]); + let currentCpuQuartal = this.getCurrentQuartal(dateArray); let newDates: string[] = new Array(4); @@ -136,7 +137,12 @@ class ViewModel { for (let index = 0; index < 4; index++) { newDates[index] = this.getRandomDate(new Date((year - 1) + "-" + "01-01"), new Date((year + 1) + "-" + "01-01")); } - newDates[this.getRandomInt(0, 4)] = cpuDate; + // check if the cpus quartal was randomly choosen already + if (newDates.includes(currentCpuQuartal)) { + return newDates; + } + + newDates[this.getRandomInt(0, 4)] = currentCpuQuartal; return newDates; } @@ -144,7 +150,13 @@ class ViewModel { for (let index = 0; index < 4; index++) { newDates[index] = this.getRandomDate(new Date("2000-01-01"), new Date(Date.now())); } - newDates[this.getRandomInt(0, 4)] = cpuDate; + + // check if the cpus quartal was randomly choosen already + if (newDates.includes(currentCpuQuartal)) { + return newDates; + } + + newDates[this.getRandomInt(0, 4)] = currentCpuQuartal; return newDates; } @@ -153,12 +165,16 @@ class ViewModel { max = Math.floor(max); return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive } + + private getCurrentQuartal(dateArray) { + let quartal = Math.ceil(dateArray[1] % 4); + return "Q" + quartal + " " + dateArray[0]; + } private getRandomDate(startDate: Date, endDate: Date) { - const minValue = startDate.getTime(); - const maxValue = endDate.getTime(); - const timestamp = Math.floor(Math.random() * (maxValue - minValue + 1) + minValue); - return new Date(timestamp).toISOString().split("T")[0]; + let newYear = this.getRandomInt(startDate.getFullYear(), endDate.getFullYear() + 1); + let quartal = this.getRandomInt(1, 5); + return "Q" + quartal + " " + newYear; } processClick(text: string): boolean {