189 lines
5.0 KiB
TypeScript
Executable File
189 lines
5.0 KiB
TypeScript
Executable File
import { type ClassValue, clsx } from "clsx";
|
|
import { twMerge } from "tailwind-merge";
|
|
import { cubicOut } from "svelte/easing";
|
|
import type { TransitionConfig } from "svelte/transition";
|
|
import type { Draw, ServerError } from "./utils/data.types";
|
|
import { rng } from "./utils/rng";
|
|
import { ulid } from "ulid";
|
|
import { v4 } from "uuid";
|
|
import { parseISO } from "date-fns";
|
|
import { PROXIES } from "./utils/constants";
|
|
|
|
export function hasDrawBeenClosed(
|
|
date: string,
|
|
draw: Draw | undefined,
|
|
now: Date = new Date(),
|
|
) {
|
|
let hasTimePassed = false;
|
|
|
|
const chosenDate = new Date(parseISO(date));
|
|
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
|
|
|
|
let _ct = draw?.closeTime?.split(" ")[1] ?? "";
|
|
const closeTime = new Date(parseISO(`${date}T${_ct}`));
|
|
|
|
if (today > chosenDate || now > closeTime) {
|
|
hasTimePassed = true;
|
|
} else {
|
|
hasTimePassed = false;
|
|
}
|
|
|
|
console.log("===============");
|
|
console.log(now);
|
|
console.log(closeTime);
|
|
console.log(`Has time passed : ${hasTimePassed}`);
|
|
|
|
return hasTimePassed;
|
|
}
|
|
|
|
export const parseToErrorList = (errors: ServerError | Array<ServerError>) => {
|
|
if (Array.isArray(errors)) {
|
|
return errors;
|
|
}
|
|
return [errors];
|
|
};
|
|
|
|
export const parseToSelectList = (
|
|
arr: string[],
|
|
): { label: string; value: string; id: number }[] => {
|
|
return arr.map((item, index) => {
|
|
return { label: item, value: item, id: index };
|
|
});
|
|
};
|
|
|
|
export const randomString = (length: number) => {
|
|
const chars =
|
|
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
let result = "";
|
|
for (let i = length; i > 0; --i) result += chars[rng(0, chars.length - 1)];
|
|
return result;
|
|
};
|
|
|
|
export function pickRandomIP() {
|
|
return PROXIES[rng(0, PROXIES.length - 1)];
|
|
}
|
|
|
|
export const getUUID = () => {
|
|
return v4();
|
|
};
|
|
|
|
export const getULID = () => {
|
|
return ulid();
|
|
};
|
|
|
|
function getRandomVersion() {
|
|
const majorVersion = rng(1, 10);
|
|
const minorVersion = rng(0, 10);
|
|
const patchVersion = rng(0, 10);
|
|
return `${majorVersion}.${minorVersion}.${patchVersion}`;
|
|
}
|
|
|
|
export const getRandomUserAgent = () => {
|
|
const browsers = ["Chrome", "Firefox", "Safari", "Opera", "Edge"];
|
|
const operatingSystems = [
|
|
"Windows NT 10.0",
|
|
"Windows NT 6.3",
|
|
"Macintosh; Intel Mac OS X 10_15_6",
|
|
"Macintosh; Intel Mac OS X 10_14_6",
|
|
"X11; Ubuntu; Linux x86_64",
|
|
"X11; Fedora; Linux x86_64",
|
|
"X11; Linux x86_64",
|
|
"Android 10",
|
|
"Android 9",
|
|
"Android 8.1",
|
|
"Android 8.0",
|
|
"iPhone; CPU iPhone OS 14_0 like Mac OS X",
|
|
"iPhone; CPU iPhone OS 13_7 like Mac OS X",
|
|
"iPhone; CPU iPhone OS 12_4_8 like Mac OS X",
|
|
];
|
|
const randomBrowser = browsers[Math.floor(Math.random() * browsers.length)];
|
|
const randomOS =
|
|
operatingSystems[Math.floor(Math.random() * operatingSystems.length)];
|
|
const userAgent = `${randomBrowser}/${getRandomVersion()} (${randomOS})`;
|
|
return userAgent;
|
|
};
|
|
|
|
export const sleep = (ms: number) => {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
};
|
|
|
|
export const getParsedObject = (data: any) => {
|
|
const yeye: Record<string, any[]> = {};
|
|
for (const each of data.data.data) {
|
|
const un = each.user.userName;
|
|
if (!Object.keys(yeye).includes(un)) {
|
|
yeye[each.user.userName] = [];
|
|
}
|
|
yeye[each.user.userName].push(each.book);
|
|
}
|
|
return yeye;
|
|
};
|
|
|
|
export const getDefaultTotals = () => {
|
|
return {
|
|
commission: { first: 0, second: 0 },
|
|
netRate: { first: 0, second: 0 },
|
|
rate: { first: 0, second: 0 },
|
|
prize: { first: 0, second: 0 },
|
|
frequency: { first: 0, second: 0 },
|
|
};
|
|
};
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs));
|
|
}
|
|
|
|
type FlyAndScaleParams = {
|
|
y?: number;
|
|
x?: number;
|
|
start?: number;
|
|
duration?: number;
|
|
};
|
|
|
|
export const flyAndScale = (
|
|
node: Element,
|
|
params: FlyAndScaleParams = { y: -8, x: 0, start: 0.95, duration: 150 },
|
|
): TransitionConfig => {
|
|
const style = getComputedStyle(node);
|
|
const transform = style.transform === "none" ? "" : style.transform;
|
|
|
|
const scaleConversion = (
|
|
valueA: number,
|
|
scaleA: [number, number],
|
|
scaleB: [number, number],
|
|
) => {
|
|
const [minA, maxA] = scaleA;
|
|
const [minB, maxB] = scaleB;
|
|
|
|
const percentage = (valueA - minA) / (maxA - minA);
|
|
const valueB = percentage * (maxB - minB) + minB;
|
|
|
|
return valueB;
|
|
};
|
|
|
|
const styleToString = (
|
|
style: Record<string, number | string | undefined>,
|
|
): string => {
|
|
return Object.keys(style).reduce((str, key) => {
|
|
if (style[key] === undefined) return str;
|
|
return str + `${key}:${style[key]};`;
|
|
}, "");
|
|
};
|
|
|
|
return {
|
|
duration: params.duration ?? 200,
|
|
delay: 0,
|
|
css: (t) => {
|
|
const y = scaleConversion(t, [0, 1], [params.y ?? 5, 0]);
|
|
const x = scaleConversion(t, [0, 1], [params.x ?? 0, 0]);
|
|
const scale = scaleConversion(t, [0, 1], [params.start ?? 0.95, 1]);
|
|
|
|
return styleToString({
|
|
transform: `${transform} translate3d(${x}px, ${y}px, 0) scale(${scale})`,
|
|
opacity: t,
|
|
});
|
|
},
|
|
easing: cubicOut,
|
|
};
|
|
};
|