From 6b1bbfb432c45a262ff929fc101e7ae02c5ca3e9 Mon Sep 17 00:00:00 2001 From: Christian Fehmer Date: Wed, 9 Jul 2025 12:05:18 +0200 Subject: [PATCH] fix: pin implementation (@fehmer) (#6699) --- frontend/src/ts/test/words-generator.ts | 31 ++++++++++++++----------- frontend/src/ts/utils/json-data.ts | 3 +++ packages/util/src/numbers.ts | 11 ++++++--- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/frontend/src/ts/test/words-generator.ts b/frontend/src/ts/test/words-generator.ts index fae481822..a2af3e80d 100644 --- a/frontend/src/ts/test/words-generator.ts +++ b/frontend/src/ts/test/words-generator.ts @@ -25,6 +25,9 @@ import { import { WordGenError } from "../utils/word-gen-error"; import * as Loader from "../elements/loader"; +//pin implementation +const random = Math.random; + function shouldCapitalize(lastChar: string): boolean { return /[?!.؟]/.test(lastChar); } @@ -60,7 +63,7 @@ export async function punctuateWord( } if (currentLanguage === "spanish") { - const rand = Math.random(); + const rand = random(); if (rand > 0.9) { word = "¿" + word; spanishSentenceTracker = "?"; @@ -70,7 +73,7 @@ export async function punctuateWord( } } } else if ( - (Math.random() < 0.1 && + (random() < 0.1 && lastChar !== "." && lastChar !== "," && index !== maxindex - 2) || @@ -82,7 +85,7 @@ export async function punctuateWord( spanishSentenceTracker = ""; } } else { - const rand = Math.random(); + const rand = random(); if (rand <= 0.8) { if (currentLanguage === "kurdish") { word += "."; @@ -134,14 +137,14 @@ export async function punctuateWord( } } } else if ( - Math.random() < 0.01 && + random() < 0.01 && lastChar !== "," && lastChar !== "." && currentLanguage !== "russian" ) { word = `"${word}"`; } else if ( - Math.random() < 0.011 && + random() < 0.011 && lastChar !== "," && lastChar !== "." && currentLanguage !== "russian" && @@ -149,9 +152,9 @@ export async function punctuateWord( currentLanguage !== "slovak" ) { word = `'${word}'`; - } else if (Math.random() < 0.012 && lastChar !== "," && lastChar !== ".") { + } else if (random() < 0.012 && lastChar !== "," && lastChar !== ".") { if (currentLanguage === "code") { - const r = Math.random(); + const r = random(); const brackets = ["()", "{}", "[]", "<>"]; // add `word` in javascript @@ -172,7 +175,7 @@ export async function punctuateWord( word = `(${word})`; } } else if ( - Math.random() < 0.013 && + random() < 0.013 && lastChar !== "," && lastChar !== "." && lastChar !== ";" && @@ -189,14 +192,14 @@ export async function punctuateWord( word += ":"; } } else if ( - Math.random() < 0.014 && + random() < 0.014 && lastChar !== "," && lastChar !== "." && previousWord !== "-" ) { word = "-"; } else if ( - Math.random() < 0.015 && + random() < 0.015 && lastChar !== "," && lastChar !== "." && lastChar !== ";" && @@ -218,7 +221,7 @@ export async function punctuateWord( } else { word += ";"; } - } else if (Math.random() < 0.2 && lastChar !== ",") { + } else if (random() < 0.2 && lastChar !== ",") { if ( currentLanguage === "arabic" || currentLanguage === "urdu" || @@ -233,7 +236,7 @@ export async function punctuateWord( } else { word += ","; } - } else if (Math.random() < 0.25 && currentLanguage === "code") { + } else if (random() < 0.25 && currentLanguage === "code") { const specials = ["{", "}", "[", "]", "(", ")", ";", "=", "+", "%", "/"]; const specialsC = [ "{", @@ -284,7 +287,7 @@ export async function punctuateWord( } } } else if ( - Math.random() < 0.5 && + random() < 0.5 && currentLanguage === "english" && (await EnglishPunctuation.check(word)) ) { @@ -907,7 +910,7 @@ export async function getNextWord( ); } if (Config.numbers) { - if (Math.random() < 0.1) { + if (random() < 0.1) { randomWord = GetText.getNumbers(4); if (Config.language.startsWith("kurdish")) { diff --git a/frontend/src/ts/utils/json-data.ts b/frontend/src/ts/utils/json-data.ts index c1c76364f..38a55f41b 100644 --- a/frontend/src/ts/utils/json-data.ts +++ b/frontend/src/ts/utils/json-data.ts @@ -2,6 +2,9 @@ import { FunboxName } from "@monkeytype/contracts/schemas/configs"; import { Language } from "@monkeytype/contracts/schemas/languages"; import { Accents } from "../test/lazy-mode"; +//pin implementation +const fetch = window.fetch; + /** * Fetches JSON data from the specified URL using the fetch API. * @param url - The URL to fetch the JSON data from. diff --git a/packages/util/src/numbers.ts b/packages/util/src/numbers.ts index 240558732..a18370d38 100644 --- a/packages/util/src/numbers.ts +++ b/packages/util/src/numbers.ts @@ -1,3 +1,8 @@ +//pin implementations +const random = Math.random; +const ceil = Math.ceil; +const floor = Math.floor; + /** * Rounds a number to one decimal places. * @param num The number to round. @@ -85,9 +90,9 @@ export function kogasa(cov: number): number { * @returns Random integer betwen min and max. */ export function randomIntFromRange(min: number, max: number): number { - const minNorm = Math.ceil(min); - const maxNorm = Math.floor(max); - return Math.floor(Math.random() * (maxNorm - minNorm + 1) + minNorm); + const minNorm = ceil(min); + const maxNorm = floor(max); + return floor(random() * (maxNorm - minNorm + 1) + minNorm); } /**