From 3d5079a4e6a9b80ef78993c57476aa751c2d0dfd Mon Sep 17 00:00:00 2001 From: catloversg <152669316+catloversg@users.noreply.github.com> Date: Fri, 27 Mar 2026 11:49:20 +0700 Subject: [PATCH] REFACTOR: Remove duplicate random alphanumeric string functions (#2601) --- src/CodingContract/ContractGenerator.ts | 10 +--------- src/Server/Server.ts | 4 ++-- src/utils/StringHelperFunctions.ts | 2 +- src/utils/helpers/createRandomString.ts | 12 ------------ 4 files changed, 4 insertions(+), 24 deletions(-) delete mode 100644 src/utils/helpers/createRandomString.ts diff --git a/src/CodingContract/ContractGenerator.ts b/src/CodingContract/ContractGenerator.ts index 472bf7021..9cb302c83 100644 --- a/src/CodingContract/ContractGenerator.ts +++ b/src/CodingContract/ContractGenerator.ts @@ -11,6 +11,7 @@ import { BaseServer } from "../Server/BaseServer"; import { getRandomIntInclusive } from "../utils/helpers/getRandomIntInclusive"; import { ContractFilePath, resolveContractFilePath } from "../Paths/ContractFilePath"; import { clampNumber } from "../utils/helpers/clampNumber"; +import { getRandomAlphanumericString } from "../utils/StringHelperFunctions"; export function tryGeneratingRandomContract(numberOfTries: number): void { /** @@ -213,15 +214,6 @@ function getRandomServer(): BaseServer | null { return randServer; } -function getRandomAlphanumericString(length: number) { - const alphanumericChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; - let result = ""; - for (let i = 0; i < length; ++i) { - result += alphanumericChars.charAt(Math.random() * alphanumericChars.length); - } - return result; -} - /** * This function will return null if the randomized name collides with another contract's name on the specified server. * Callers of this function must return early and not generate a contract when it happens. It likely happens when there diff --git a/src/Server/Server.ts b/src/Server/Server.ts index dd9980f49..44335de25 100644 --- a/src/Server/Server.ts +++ b/src/Server/Server.ts @@ -3,7 +3,7 @@ import { BaseServer } from "./BaseServer"; import { currentNodeMults } from "../BitNode/BitNodeMultipliers"; -import { createRandomString } from "../utils/helpers/createRandomString"; +import { getRandomAlphanumericString } from "../utils/StringHelperFunctions"; import { createRandomIp } from "../utils/IPAddress"; import { IReviverValue, constructorsForReviver } from "../utils/JSONReviver"; import { IPAddress } from "../Types/strings"; @@ -61,7 +61,7 @@ export class Server extends BaseServer { // "hacknet-node-X" hostnames are reserved for Hacknet Servers if (this.hostname.startsWith("hacknet-node-") || this.hostname.startsWith("hacknet-server-")) { - this.hostname = createRandomString(10); + this.hostname = getRandomAlphanumericString(10); } this.purchasedByPlayer = params.purchasedByPlayer != null ? params.purchasedByPlayer : false; diff --git a/src/utils/StringHelperFunctions.ts b/src/utils/StringHelperFunctions.ts index b89631d5c..5991972e6 100644 --- a/src/utils/StringHelperFunctions.ts +++ b/src/utils/StringHelperFunctions.ts @@ -78,7 +78,7 @@ export function containsAllStrings(arr: string[]): boolean { } // Generates a random alphanumeric string with N characters -export function generateRandomString(n: number): string { +export function getRandomAlphanumericString(n: number): string { let str = ""; const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; diff --git a/src/utils/helpers/createRandomString.ts b/src/utils/helpers/createRandomString.ts deleted file mode 100644 index 22ab439bc..000000000 --- a/src/utils/helpers/createRandomString.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Function that generates a random gibberish string of length n -const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; - -export function createRandomString(n: number): string { - let str = ""; - - for (let i = 0; i < n; ++i) { - str += chars.charAt(Math.floor(Math.random() * chars.length)); - } - - return str; -}