thinstuff

This commit is contained in:
Kim Ravn Hansen
2025-09-10 22:37:54 +02:00
parent ba293d08b3
commit 8c196bb6a1
13 changed files with 350 additions and 213 deletions

View File

@@ -1,6 +1,14 @@
const UID_DIGITS = 12;
const MINI_UID_REGEX = /\.uid\.[a-z0-9]{6,}$/;
const ID_SANITY_REGEX = /^:([a-z0-9]+\.)*[a-z0-9_]+$/;
import * as regex from "./regex.js";
const MINI_UID_REGEX = regex.pretty(
"\.uid\.", // Mini-uids always begin with ".uid."
"[a-z0-9]{6,}$", // Terminated by 6 or more random numbers and lowercase letters.
);
const ID_SANITY_REGEX = regex.pretty(
"^:", // All ids start with a colon
"([a-z0-9]+\.)*?", // Middle -optional- part :myid.gogle.thing.thang.thong
"[a-z0-9_]+$", // The terminating part of the id is numbers, lowercase letters, and -notably- underscores.
);
/**
* Sanity check a string to see if it is a potential id.
@@ -17,17 +25,22 @@ export function isIdSane(id) {
return false;
}
return ID_SANITY_REGEX.test(id);
if (!ID_SANITY_REGEX.test(id)) {
return false;
}
return true;
}
/**
* @returns {string} crypto-unsafe pseudo random number.
* @returns {string} crypto-unsafe pseudo random numbe"r.
*
* Generate a random number, convert it to base36, and return it as a string with 7-8 characters.
*/
export function miniUid() {
// we use 12 digits, but we could go up to 16
return Number(Math.random().toFixed(UID_DIGITS).substring(2)).toString(36);
// we use 12 digits, but we could go all the way to 16
const digits = 12;
return Number(Math.random().toFixed(digits).substring(2)).toString(36);
}
/**

80
server/utils/random.js Executable file
View File

@@ -0,0 +1,80 @@
/**
* Pseudo random number generator
* using the xorshift32 method.
*/
export class Xorshift32 {
/* @type {number} */
state;
/** @param {number} seed */
constructor(seed) {
this.state = seed | 0;
}
/** @protected Shuffle the internal state. */
shuffle() {
//
// Run the actual xorshift32 algorithm
let x = this.state;
x ^= x << 13;
x ^= x >>> 17;
x ^= x << 5;
x = (x >>> 0) / 4294967296;
this.state = x;
}
/**
* Get a random number and shuffle the internal state.
* @returns {number} a pseudo-random positive integer.
*/
get() {
this.shuffle();
return this.state;
}
/** @param {number} x @returns {number} a positive integer lower than x */
lowerThan(x) {
return this.get() % x;
}
/** @param {number} x @reurns {number} a positive integer lower than or equal to x */
lowerThanOrEqual(x) {
return this.get() % (x + 1);
}
/**
* @param {<T>[]} arr
*
* @return {<T>}
*/
randomElement(arr) {
const idx = this.lowerThan(arr.length);
return arr[idx];
}
/**
* @param {...<T>} args
* @returns {<T>}
*/
oneOf(...args) {
const idx = this.lowerThan(args.length);
return args[idx];
}
/**
* @param {number} lowerThanOrEqual a positive integer
* @param {number} greaterThanOrEqual a positive integer greater than lowerThanOrEqual
* @returns {number} a pseudo-random integer
*/
within(greaterThanOrEqual, lowerThanOrEqual) {
const range = lowerThanOrEqual - greaterThanOrEqual;
const num = this.lowerThanOrEqual(range);
return num + greaterThanOrEqual;
}
}

10
server/utils/regex.js Normal file
View File

@@ -0,0 +1,10 @@
/**
* Makes it easier to document regexes because you can break them up
*
* @param {...string} args
* @returns {Regexp}
*/
export function pretty(...args) {
const regexprStr = args.join("");
return new RegExp(regexprStr);
}