Refactor (1)

This commit is contained in:
2025-09-19 18:54:12 +09:00
committed by cannorin
parent 6b15542c40
commit c3b1bf39a4
26 changed files with 1684 additions and 105 deletions

View File

@@ -1,45 +1,3 @@
interface String {
concat<S extends string>(string: S): `${this}${S}`;
concat<S1 extends string, S2 extends string>(
s1: S1,
s2: S2,
): `${this}${S1}${S2}`;
startsWith<S extends string>(searchString: S): this is `${S}${string}`;
endsWith<S extends string>(searchString: S): this is `${string}${S}`;
includes<S extends string>(
searchString: S,
position?: number,
): this is `${string}${S}${string}`;
}
type LiteralUnionLike<T> = T extends string
? T extends ""
? T
: T extends `${T}${T}`
? never
: T
: T extends number
? `${T}0` extends `${number}`
? T
: never
: T extends null | undefined
? T
: never;
interface Array<T> {
includes(
searchElement: T extends LiteralUnionLike<T> ? unknown : never,
fromIndex?: number,
): searchElement is T extends LiteralUnionLike<T> ? T : never;
}
interface ReadonlyArray<T> {
includes(
searchElement: T extends LiteralUnionLike<T> ? unknown : never,
fromIndex?: number,
): searchElement is T extends LiteralUnionLike<T> ? T : never;
}
declare module "*&enhanced" {
import type { Picture } from "vite-imagetools";

View File

@@ -1,17 +0,0 @@
export function tryOneOf<const T>(
value: T extends LiteralUnionLike<T> ? unknown : never,
consts: readonly T[],
) {
if (consts.includes(value)) return value;
return undefined;
}
export function sample<T>(arr: T[], n: number = arr.length): T[] {
if (n > arr.length) return sample(arr, arr.length);
const copy = [...arr];
for (let i = 0; i < n; i++) {
const j = i + Math.floor(Math.random() * (copy.length - i));
[copy[i], copy[j]] = [copy[j] as T, copy[i] as T];
}
return copy.slice(0, n);
}