Created a sticker seeder.

This commit is contained in:
Lucid Kobold
2022-01-03 13:30:57 -06:00
parent 3d847fc6aa
commit 36aea09aff

51
data/stickerSeeder.ts Normal file
View File

@@ -0,0 +1,51 @@
import { getDaysInMonth, getYear, getMonth, isBefore } from "date-fns";
/**
* This seeder is to simulate the date and sticker info from the database.
* Filling up an array for the current month with sticker from ths first to
* the day before the current date, leaving the rest of the month empty.
*/
interface Sticker {
date: Date;
sticker: -2 | -1 | 0 | 1 | 2 | null;
}
const generateSticker = (): -2 | -1 | 0 | 1 | 2 => {
const stickerPossibility = [-2, -1, 0, 1, 2];
const sticker = Math.floor(Math.random() * stickerPossibility.length);
if (
sticker === -2 ||
sticker === -1 ||
sticker === 0 ||
sticker === 1 ||
sticker === 2
) {
return sticker;
}
};
const stickersSeeder = (): Sticker[] => {
const stickers = [] as Sticker[];
const now = new Date();
const daysOfThisMonth = getDaysInMonth(now);
for (let i = 0; i <= daysOfThisMonth; i++) {
const currDate = new Date(getYear(now), getMonth(now) - 1, i);
const newSticker: Sticker = {
date: currDate,
sticker: isBefore(currDate, now) ? generateSticker() : null
};
stickers.push(newSticker);
}
if (stickers.length === daysOfThisMonth) {
return stickers;
}
};
export default stickersSeeder;