mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-05-18 09:11:34 -05:00
* YouTube lite embed + CSS bundled import * Migration initial * New VoD page initial functioning * Table changes + add TODOs * New structure for add vod page * WIP add new VoD backend * Merge branch 'rewrite' of https://github.com/Sendouc/sendou.ink into vods * Fix when leaderboard appears * Function new vod form * Single vod page initial * Different YouTubeEmbed * Scroll to top when going to timestamp * Vod match weapon/mode icons * Vod page user * Add date to vod page * Adjust migration order * Vod page many weapons * Add title to vod page * New vods page cast many weapons * Add player index to order by * Vods new more validation * Vod listing page initial * Vods page with filters * Show message if no vods * Fix not being to unset filters * Fix seed sometimes throwing errors * User page VoDs * Vods nullable weapon combobox * Link directly to user custom url from vod page * Make video adder admin action * Can add video checks * i18n * New VoD form tests * VoD operates filters test * Vods behind flag * Remove from config
148 lines
3.2 KiB
TypeScript
148 lines
3.2 KiB
TypeScript
import { z } from "zod";
|
|
import {
|
|
abilities,
|
|
mainWeaponIds,
|
|
modesShort,
|
|
stageIds,
|
|
} from "~/modules/in-game-lists";
|
|
|
|
export const id = z.coerce.number().int().positive();
|
|
|
|
const abilityNameToType = (val: string) =>
|
|
abilities.find((ability) => ability.name === val)?.type;
|
|
export const headMainSlotAbility = z
|
|
.string()
|
|
.refine((val) =>
|
|
["STACKABLE", "HEAD_MAIN_ONLY"].includes(abilityNameToType(val) as any)
|
|
);
|
|
export const clothesMainSlotAbility = z
|
|
.string()
|
|
.refine((val) =>
|
|
["STACKABLE", "CLOTHES_MAIN_ONLY"].includes(abilityNameToType(val) as any)
|
|
);
|
|
export const shoesMainSlotAbility = z
|
|
.string()
|
|
.refine((val) =>
|
|
["STACKABLE", "SHOES_MAIN_ONLY"].includes(abilityNameToType(val) as any)
|
|
);
|
|
export const stackableAbility = z
|
|
.string()
|
|
.refine((val) => abilityNameToType(val) === "STACKABLE");
|
|
|
|
export const weaponSplId = z.preprocess(
|
|
actualNumber,
|
|
z
|
|
.number()
|
|
.refine((val) =>
|
|
mainWeaponIds.includes(val as (typeof mainWeaponIds)[number])
|
|
)
|
|
);
|
|
|
|
export const modeShort = z
|
|
.string()
|
|
.refine((val) => modesShort.includes(val as any));
|
|
|
|
export const stageId = z.preprocess(
|
|
actualNumber,
|
|
z
|
|
.number()
|
|
.refine((val) => stageIds.includes(val as (typeof stageIds)[number]))
|
|
);
|
|
|
|
export function processMany(
|
|
...processFuncs: Array<(value: unknown) => unknown>
|
|
) {
|
|
return (value: unknown) => {
|
|
let result = value;
|
|
|
|
for (const processFunc of processFuncs) {
|
|
result = processFunc(result);
|
|
}
|
|
|
|
return result;
|
|
};
|
|
}
|
|
|
|
export function safeJSONParse(value: unknown): unknown {
|
|
try {
|
|
if (typeof value !== "string") return value;
|
|
const parsedValue = z.string().parse(value);
|
|
return JSON.parse(parsedValue);
|
|
} catch (e) {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
export function falsyToNull(value: unknown): unknown {
|
|
if (value) return value;
|
|
|
|
return null;
|
|
}
|
|
|
|
export function jsonParseable(value: unknown) {
|
|
try {
|
|
JSON.parse(value as string);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function undefinedToNull(value: unknown): unknown {
|
|
if (value === undefined) return null;
|
|
|
|
return value;
|
|
}
|
|
|
|
export function actualNumber(value: unknown) {
|
|
if (value === "") return undefined;
|
|
|
|
const parsed = Number(value);
|
|
|
|
return Number.isNaN(parsed) ? undefined : parsed;
|
|
}
|
|
|
|
export function trimmedString(value: unknown) {
|
|
if (typeof value !== "string") {
|
|
throw new Error("Expected string value");
|
|
}
|
|
|
|
return value.trim();
|
|
}
|
|
|
|
export function date(value: unknown) {
|
|
if (typeof value === "string" || typeof value === "number") {
|
|
const valueAsNumber = Number(value);
|
|
|
|
return new Date(Number.isNaN(valueAsNumber) ? value : valueAsNumber);
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
export function noDuplicates(arr: (number | string)[]) {
|
|
return new Set(arr).size === arr.length;
|
|
}
|
|
|
|
export function removeDuplicates(value: unknown) {
|
|
if (!Array.isArray(value)) return value;
|
|
|
|
return Array.from(new Set(value));
|
|
}
|
|
|
|
export function toArray<T>(value: T | Array<T>) {
|
|
if (Array.isArray(value)) return value;
|
|
|
|
return [value];
|
|
}
|
|
|
|
export function checkboxValueToBoolean(value: unknown) {
|
|
if (!value) return false;
|
|
|
|
if (typeof value !== "string") {
|
|
throw new Error("Expected string checkbox value");
|
|
}
|
|
|
|
return value === "on";
|
|
}
|