From 396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b Mon Sep 17 00:00:00 2001 From: Fuwn <50817549+Fuwn@users.noreply.github.com> Date: Sat, 24 Jan 2026 13:09:50 +0000 Subject: Initial commit Created from https://vercel.com/new --- scripts/seed/utils.ts | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 scripts/seed/utils.ts (limited to 'scripts/seed/utils.ts') diff --git a/scripts/seed/utils.ts b/scripts/seed/utils.ts new file mode 100644 index 0000000..7b44261 --- /dev/null +++ b/scripts/seed/utils.ts @@ -0,0 +1,85 @@ +import { v4 as uuidv4 } from 'uuid'; + +export interface WeightedOption { + value: T; + weight: number; +} + +export function weightedRandom(options: WeightedOption[]): T { + const totalWeight = options.reduce((sum, opt) => sum + opt.weight, 0); + let random = Math.random() * totalWeight; + + for (const option of options) { + random -= option.weight; + if (random <= 0) { + return option.value; + } + } + + return options[options.length - 1].value; +} + +export function randomInt(min: number, max: number): number { + return Math.floor(Math.random() * (max - min + 1)) + min; +} + +export function randomFloat(min: number, max: number): number { + return Math.random() * (max - min) + min; +} + +export function pickRandom(array: T[]): T { + return array[Math.floor(Math.random() * array.length)]; +} + +export function shuffleArray(array: T[]): T[] { + const result = [...array]; + for (let i = result.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [result[i], result[j]] = [result[j], result[i]]; + } + return result; +} + +export function uuid(): string { + return uuidv4(); +} + +export function generateDatesBetween(startDate: Date, endDate: Date): Date[] { + const dates: Date[] = []; + const current = new Date(startDate); + current.setHours(0, 0, 0, 0); + + while (current <= endDate) { + dates.push(new Date(current)); + current.setDate(current.getDate() + 1); + } + + return dates; +} + +export function addHours(date: Date, hours: number): Date { + return new Date(date.getTime() + hours * 60 * 60 * 1000); +} + +export function addMinutes(date: Date, minutes: number): Date { + return new Date(date.getTime() + minutes * 60 * 1000); +} + +export function addSeconds(date: Date, seconds: number): Date { + return new Date(date.getTime() + seconds * 1000); +} + +export function subDays(date: Date, days: number): Date { + return new Date(date.getTime() - days * 24 * 60 * 60 * 1000); +} + +export function formatNumber(num: number): string { + return num.toLocaleString(); +} + +export function progressBar(current: number, total: number, width = 30): string { + const percent = current / total; + const filled = Math.round(width * percent); + const empty = width - filled; + return `[${'█'.repeat(filled)}${'░'.repeat(empty)}] ${Math.round(percent * 100)}%`; +} -- cgit v1.2.3