Reviewers: #reviewers Subscribers: jenkins-user Differential Revision: https://hub.sealcode.org/D1451
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import type { BaseContext } from "koa";
|
|
import qs from "qs";
|
|
|
|
export async function sleep(time: number) {
|
|
return new Promise((resolve) => setTimeout(resolve, time));
|
|
}
|
|
|
|
export type Awaited<T> = T extends Promise<infer U> ? U : T;
|
|
export type UnwrapArray<T> = T extends Array<infer U> ? U : T;
|
|
|
|
export function* naturalNumbers(min: number, max: number) {
|
|
for (let i = min; i <= max; i++) {
|
|
yield i;
|
|
}
|
|
}
|
|
|
|
export function UrlWithNewParams(
|
|
ctx: BaseContext,
|
|
query_params: Record<string, unknown>
|
|
): string {
|
|
return `${ctx.path}?${qs.stringify(query_params)}`;
|
|
}
|
|
|
|
export function shuffle<T>(array: T[]): T[] {
|
|
const array_copy = [...array];
|
|
let currentIndex = array_copy.length;
|
|
|
|
// While there remain elements to shuffle...
|
|
while (currentIndex != 0) {
|
|
// Pick a remaining element...
|
|
const randomIndex = Math.floor(Math.random() * currentIndex);
|
|
currentIndex--;
|
|
|
|
// And swap it with the current element.
|
|
[array_copy[currentIndex], array_copy[randomIndex]] = [
|
|
array_copy[randomIndex],
|
|
array_copy[currentIndex],
|
|
];
|
|
}
|
|
return array_copy;
|
|
}
|