sokko-g/website/src/features/toolboxes/storage/modules/shared.js
Shinuwa 992665f4ea
All checks were successful
Deploy Sokko G / deploy (push) Successful in 14s
Add calendar toolbox with weekly and monthly scheduling
2026-08-20 15:48:24 +02:00

57 lines
1.5 KiB
JavaScript

// Rôle : fournit les primitives communes de normalisation et d'identifiants du stockage toolbox.
export const ID_PREFIXES = {
tbx: "t",
mod: "m",
item: "i",
section: "g",
image: "s",
link: "l",
counter: "c",
combo: "o",
calc: "r",
calendar: "h",
marker: "k",
timer: "z",
task: "a",
relation: "e",
stroke: "d",
equipmentType: "y",
equipment: "q",
trait: "v",
material: "n",
socketItem: "j",
socketLink: "u",
socketBonus: "b"
};
const ID_ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
function randomToken(length = 6) {
const bytes = new Uint8Array(length);
if (globalThis.crypto?.getRandomValues) {
globalThis.crypto.getRandomValues(bytes);
return [...bytes].map((byte) => ID_ALPHABET[byte % ID_ALPHABET.length]).join("");
}
return Math.random().toString(36).slice(2, 2 + length).padEnd(length, "0");
}
export function uid(prefix) {
return `${ID_PREFIXES[prefix] || "x"}${randomToken()}`;
}
export function parsePositiveInt(value, fallback = 1) {
const parsed = Number.parseInt(value, 10);
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
}
export function normalizeIsoDate(value) {
const text = String(value || "").trim();
if (!text) return "";
const timestamp = Date.parse(text);
return Number.isFinite(timestamp) ? new Date(timestamp).toISOString() : "";
}
export function normalizeTimestamp(value) {
const parsed = Number(value);
return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
}