Skip to content

Commit 6b1bbfb

Browse files
authored
fix: pin implementation (@fehmer) (monkeytypegame#6699)
1 parent 06ca8c2 commit 6b1bbfb

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

frontend/src/ts/test/words-generator.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import {
2525
import { WordGenError } from "../utils/word-gen-error";
2626
import * as Loader from "../elements/loader";
2727

28+
//pin implementation
29+
const random = Math.random;
30+
2831
function shouldCapitalize(lastChar: string): boolean {
2932
return /[?!.؟]/.test(lastChar);
3033
}
@@ -60,7 +63,7 @@ export async function punctuateWord(
6063
}
6164

6265
if (currentLanguage === "spanish") {
63-
const rand = Math.random();
66+
const rand = random();
6467
if (rand > 0.9) {
6568
word = "¿" + word;
6669
spanishSentenceTracker = "?";
@@ -70,7 +73,7 @@ export async function punctuateWord(
7073
}
7174
}
7275
} else if (
73-
(Math.random() < 0.1 &&
76+
(random() < 0.1 &&
7477
lastChar !== "." &&
7578
lastChar !== "," &&
7679
index !== maxindex - 2) ||
@@ -82,7 +85,7 @@ export async function punctuateWord(
8285
spanishSentenceTracker = "";
8386
}
8487
} else {
85-
const rand = Math.random();
88+
const rand = random();
8689
if (rand <= 0.8) {
8790
if (currentLanguage === "kurdish") {
8891
word += ".";
@@ -134,24 +137,24 @@ export async function punctuateWord(
134137
}
135138
}
136139
} else if (
137-
Math.random() < 0.01 &&
140+
random() < 0.01 &&
138141
lastChar !== "," &&
139142
lastChar !== "." &&
140143
currentLanguage !== "russian"
141144
) {
142145
word = `"${word}"`;
143146
} else if (
144-
Math.random() < 0.011 &&
147+
random() < 0.011 &&
145148
lastChar !== "," &&
146149
lastChar !== "." &&
147150
currentLanguage !== "russian" &&
148151
currentLanguage !== "ukrainian" &&
149152
currentLanguage !== "slovak"
150153
) {
151154
word = `'${word}'`;
152-
} else if (Math.random() < 0.012 && lastChar !== "," && lastChar !== ".") {
155+
} else if (random() < 0.012 && lastChar !== "," && lastChar !== ".") {
153156
if (currentLanguage === "code") {
154-
const r = Math.random();
157+
const r = random();
155158
const brackets = ["()", "{}", "[]", "<>"];
156159

157160
// add `word` in javascript
@@ -172,7 +175,7 @@ export async function punctuateWord(
172175
word = `(${word})`;
173176
}
174177
} else if (
175-
Math.random() < 0.013 &&
178+
random() < 0.013 &&
176179
lastChar !== "," &&
177180
lastChar !== "." &&
178181
lastChar !== ";" &&
@@ -189,14 +192,14 @@ export async function punctuateWord(
189192
word += ":";
190193
}
191194
} else if (
192-
Math.random() < 0.014 &&
195+
random() < 0.014 &&
193196
lastChar !== "," &&
194197
lastChar !== "." &&
195198
previousWord !== "-"
196199
) {
197200
word = "-";
198201
} else if (
199-
Math.random() < 0.015 &&
202+
random() < 0.015 &&
200203
lastChar !== "," &&
201204
lastChar !== "." &&
202205
lastChar !== ";" &&
@@ -218,7 +221,7 @@ export async function punctuateWord(
218221
} else {
219222
word += ";";
220223
}
221-
} else if (Math.random() < 0.2 && lastChar !== ",") {
224+
} else if (random() < 0.2 && lastChar !== ",") {
222225
if (
223226
currentLanguage === "arabic" ||
224227
currentLanguage === "urdu" ||
@@ -233,7 +236,7 @@ export async function punctuateWord(
233236
} else {
234237
word += ",";
235238
}
236-
} else if (Math.random() < 0.25 && currentLanguage === "code") {
239+
} else if (random() < 0.25 && currentLanguage === "code") {
237240
const specials = ["{", "}", "[", "]", "(", ")", ";", "=", "+", "%", "/"];
238241
const specialsC = [
239242
"{",
@@ -284,7 +287,7 @@ export async function punctuateWord(
284287
}
285288
}
286289
} else if (
287-
Math.random() < 0.5 &&
290+
random() < 0.5 &&
288291
currentLanguage === "english" &&
289292
(await EnglishPunctuation.check(word))
290293
) {
@@ -907,7 +910,7 @@ export async function getNextWord(
907910
);
908911
}
909912
if (Config.numbers) {
910-
if (Math.random() < 0.1) {
913+
if (random() < 0.1) {
911914
randomWord = GetText.getNumbers(4);
912915

913916
if (Config.language.startsWith("kurdish")) {

frontend/src/ts/utils/json-data.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { FunboxName } from "@monkeytype/contracts/schemas/configs";
22
import { Language } from "@monkeytype/contracts/schemas/languages";
33
import { Accents } from "../test/lazy-mode";
44

5+
//pin implementation
6+
const fetch = window.fetch;
7+
58
/**
69
* Fetches JSON data from the specified URL using the fetch API.
710
* @param url - The URL to fetch the JSON data from.

packages/util/src/numbers.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
//pin implementations
2+
const random = Math.random;
3+
const ceil = Math.ceil;
4+
const floor = Math.floor;
5+
16
/**
27
* Rounds a number to one decimal places.
38
* @param num The number to round.
@@ -85,9 +90,9 @@ export function kogasa(cov: number): number {
8590
* @returns Random integer betwen min and max.
8691
*/
8792
export function randomIntFromRange(min: number, max: number): number {
88-
const minNorm = Math.ceil(min);
89-
const maxNorm = Math.floor(max);
90-
return Math.floor(Math.random() * (maxNorm - minNorm + 1) + minNorm);
93+
const minNorm = ceil(min);
94+
const maxNorm = floor(max);
95+
return floor(random() * (maxNorm - minNorm + 1) + minNorm);
9196
}
9297

9398
/**

0 commit comments

Comments
 (0)