REFACTOR: Remove duplicate random alphanumeric string functions (#2601)

This commit is contained in:
catloversg
2026-03-27 11:49:20 +07:00
committed by GitHub
parent fdd6d65c25
commit 3d5079a4e6
4 changed files with 4 additions and 24 deletions

View File

@@ -11,6 +11,7 @@ import { BaseServer } from "../Server/BaseServer";
import { getRandomIntInclusive } from "../utils/helpers/getRandomIntInclusive"; import { getRandomIntInclusive } from "../utils/helpers/getRandomIntInclusive";
import { ContractFilePath, resolveContractFilePath } from "../Paths/ContractFilePath"; import { ContractFilePath, resolveContractFilePath } from "../Paths/ContractFilePath";
import { clampNumber } from "../utils/helpers/clampNumber"; import { clampNumber } from "../utils/helpers/clampNumber";
import { getRandomAlphanumericString } from "../utils/StringHelperFunctions";
export function tryGeneratingRandomContract(numberOfTries: number): void { export function tryGeneratingRandomContract(numberOfTries: number): void {
/** /**
@@ -213,15 +214,6 @@ function getRandomServer(): BaseServer | null {
return randServer; 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. * 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 * Callers of this function must return early and not generate a contract when it happens. It likely happens when there

View File

@@ -3,7 +3,7 @@ import { BaseServer } from "./BaseServer";
import { currentNodeMults } from "../BitNode/BitNodeMultipliers"; import { currentNodeMults } from "../BitNode/BitNodeMultipliers";
import { createRandomString } from "../utils/helpers/createRandomString"; import { getRandomAlphanumericString } from "../utils/StringHelperFunctions";
import { createRandomIp } from "../utils/IPAddress"; import { createRandomIp } from "../utils/IPAddress";
import { IReviverValue, constructorsForReviver } from "../utils/JSONReviver"; import { IReviverValue, constructorsForReviver } from "../utils/JSONReviver";
import { IPAddress } from "../Types/strings"; import { IPAddress } from "../Types/strings";
@@ -61,7 +61,7 @@ export class Server extends BaseServer {
// "hacknet-node-X" hostnames are reserved for Hacknet Servers // "hacknet-node-X" hostnames are reserved for Hacknet Servers
if (this.hostname.startsWith("hacknet-node-") || this.hostname.startsWith("hacknet-server-")) { 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; this.purchasedByPlayer = params.purchasedByPlayer != null ? params.purchasedByPlayer : false;

View File

@@ -78,7 +78,7 @@ export function containsAllStrings(arr: string[]): boolean {
} }
// Generates a random alphanumeric string with N characters // Generates a random alphanumeric string with N characters
export function generateRandomString(n: number): string { export function getRandomAlphanumericString(n: number): string {
let str = ""; let str = "";
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

View File

@@ -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;
}