diff --git a/src/ActiveScriptsUI.js b/src/ActiveScriptsUI.js index b48169546..12b3880bf 100644 --- a/src/ActiveScriptsUI.js +++ b/src/ActiveScriptsUI.js @@ -10,7 +10,7 @@ import {printArray, createElement, removeChildrenFromElement, exceptionAlert} from "../utils/HelperFunctions.js"; import {logBoxCreate} from "../utils/LogBox.js"; import numeral from "numeral/min/numeral.min"; -import {formatNumber} from "../utils/StringHelperFunctions.js"; +import {formatNumber} from "../utils/StringHelperFunctions"; /* { * serverName: { diff --git a/src/Augmentations.js b/src/Augmentations.js index fe3329ad4..a73a2430b 100644 --- a/src/Augmentations.js +++ b/src/Augmentations.js @@ -16,7 +16,7 @@ import {createElement, createAccordionElement, removeChildrenFromElement, clearObject} from "../utils/HelperFunctions.js"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver.js"; -import {isString} from "../utils/StringHelperFunctions.js"; +import {isString} from "../utils/StringHelperFunctions"; //Augmentations function Augmentation(params) { diff --git a/src/Bladeburner.js b/src/Bladeburner.js index 5451c3410..ae33a011f 100644 --- a/src/Bladeburner.js +++ b/src/Bladeburner.js @@ -18,7 +18,7 @@ import {getRandomInt, addOffset, clearObject, import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver.js"; import numeral from "numeral/min/numeral.min"; -import {formatNumber} from "../utils/StringHelperFunctions.js"; +import {formatNumber} from "../utils/StringHelperFunctions"; var CityNames = ["Aevum", "Chongqing", "Sector-12", "New Tokyo", "Ishima", "Volhaven"]; diff --git a/src/CinematicText.js b/src/CinematicText.js index 4203335ba..41424e7ad 100644 --- a/src/CinematicText.js +++ b/src/CinematicText.js @@ -2,7 +2,7 @@ import {Engine} from "./engine.js"; import {removeChildrenFromElement, createElement, exceptionAlert} from "../utils/HelperFunctions.js"; -import {isString} from "../utils/StringHelperFunctions.js"; +import {isString} from "../utils/StringHelperFunctions"; var cinematicTextFlag = false; diff --git a/src/CompanyManagement.js b/src/CompanyManagement.js index 3486a0f25..16c391c0c 100644 --- a/src/CompanyManagement.js +++ b/src/CompanyManagement.js @@ -14,7 +14,7 @@ import {getRandomInt, removeElementById, import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver.js"; import numeral from "numeral/min/numeral.min"; -import {formatNumber, isString, generateRandomString} from "../utils/StringHelperFunctions.js"; +import {formatNumber, isString, generateRandomString} from "../utils/StringHelperFunctions"; import {yesNoBoxCreate, yesNoTxtInpBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, diff --git a/src/DarkWeb.js b/src/DarkWeb.js index ed9992e20..4d10eb7cf 100644 --- a/src/DarkWeb.js +++ b/src/DarkWeb.js @@ -4,7 +4,7 @@ import {SpecialServerIps} from "./SpecialServerIps.js"; import {post} from "./Terminal.js"; import {isValidIPAddress} from "../utils/IPAddress.js"; -import {formatNumber} from "../utils/StringHelperFunctions.js"; +import {formatNumber} from "../utils/StringHelperFunctions"; /* DarkWeb.js */ diff --git a/src/Faction.js b/src/Faction.js index c0607aa5e..4af69703c 100644 --- a/src/Faction.js +++ b/src/Faction.js @@ -16,7 +16,7 @@ import {clearEventListeners, createElement, import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver.js"; import numeral from "numeral/min/numeral.min"; -import {formatNumber} from "../utils/StringHelperFunctions.js"; +import {formatNumber} from "../utils/StringHelperFunctions"; import {yesNoBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoBoxClose} from "../utils/YesNoBox.js"; diff --git a/src/Gang.js b/src/Gang.js index 484f61b57..b3d107c72 100644 --- a/src/Gang.js +++ b/src/Gang.js @@ -11,7 +11,7 @@ import {getRandomInt, createElement, createAccordionElement, createPopup, removeElementById, removeElement} from "../utils/HelperFunctions.js"; import numeral from "numeral/min/numeral.min"; -import {formatNumber} from "../utils/StringHelperFunctions.js"; +import {formatNumber} from "../utils/StringHelperFunctions"; import {yesNoBoxCreate, yesNoTxtInpBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, diff --git a/src/HacknetNode.js b/src/HacknetNode.js index b188a079a..938313a61 100644 --- a/src/HacknetNode.js +++ b/src/HacknetNode.js @@ -9,7 +9,7 @@ import {clearEventListeners, createElement, getElementById} from "../utils/HelperFunctions.js"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver.js"; -import {formatNumber} from "../utils/StringHelperFunctions.js"; +import {formatNumber} from "../utils/StringHelperFunctions"; /** * Overwrites the inner text of the specified HTML element if it is different from what currently exists. diff --git a/src/Infiltration.js b/src/Infiltration.js index 345adf9f0..454ab3faa 100644 --- a/src/Infiltration.js +++ b/src/Infiltration.js @@ -5,7 +5,7 @@ import {Player} from "./Player.js"; import {dialogBoxCreate} from "../utils/DialogBox.js"; import {clearEventListeners, getRandomInt} from "../utils/HelperFunctions.js"; import {infiltrationBoxCreate} from "../utils/InfiltrationBox.js"; -import {formatNumber} from "../utils/StringHelperFunctions.js"; +import {formatNumber} from "../utils/StringHelperFunctions"; /* Infiltration.js * diff --git a/src/Location.js b/src/Location.js index 2e1d59085..1d24e3271 100644 --- a/src/Location.js +++ b/src/Location.js @@ -18,7 +18,7 @@ import {dialogBoxCreate} from "../utils/DialogBox.js"; import {clearEventListeners, createElement} from "../utils/HelperFunctions.js"; import {createRandomIp} from "../utils/IPAddress.js"; import numeral from "numeral/min/numeral.min"; -import {formatNumber} from "../utils/StringHelperFunctions.js"; +import {formatNumber} from "../utils/StringHelperFunctions"; import {yesNoBoxCreate, yesNoTxtInpBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, diff --git a/src/Missions.js b/src/Missions.js index c5128bff1..d7a5e34a7 100644 --- a/src/Missions.js +++ b/src/Missions.js @@ -6,7 +6,7 @@ import {dialogBoxCreate} from "../utils/DialogBox.js" import {addOffset, getRandomInt, clearEventListenersEl, clearEventListeners} from "../utils/HelperFunctions.js"; -import {formatNumber, isString} from "../utils/StringHelperFunctions.js"; +import {formatNumber, isString} from "../utils/StringHelperFunctions"; import jsplumb from 'jsplumb' let inMission = false; //Flag to denote whether a mission is running diff --git a/src/NetscriptEvaluator.js b/src/NetscriptEvaluator.js index de611370e..6f2e33057 100644 --- a/src/NetscriptEvaluator.js +++ b/src/NetscriptEvaluator.js @@ -11,7 +11,7 @@ import {Script, findRunningScript, import {parse, Node} from "../utils/acorn.js"; import {printArray} from "../utils/HelperFunctions.js"; import {isValidIPAddress} from "../utils/IPAddress.js"; -import {isString} from "../utils/StringHelperFunctions.js"; +import {isString} from "../utils/StringHelperFunctions"; var Promise = require("bluebird"); diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index b3e8ce6c4..edf3d453a 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -51,7 +51,7 @@ import Decimal from "decimal.js"; import {dialogBoxCreate} from "../utils/DialogBox.js"; import {printArray, powerOfTwo} from "../utils/HelperFunctions.js"; import {createRandomIp} from "../utils/IPAddress.js"; -import {formatNumber, isString, isHTML} from "../utils/StringHelperFunctions.js"; +import {formatNumber, isString, isHTML} from "../utils/StringHelperFunctions"; import {yesNoBoxClose, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoBoxCreate, yesNoBoxOpen} from "../utils/YesNoBox.js"; diff --git a/src/Player.js b/src/Player.js index c7497e1ec..effd037cc 100644 --- a/src/Player.js +++ b/src/Player.js @@ -27,7 +27,7 @@ import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver.js"; import numeral from "numeral/min/numeral.min"; import {formatNumber, - convertTimeMsToTimeElapsedString} from "../utils/StringHelperFunctions.js"; + convertTimeMsToTimeElapsedString} from "../utils/StringHelperFunctions"; function PlayerObject() { //Skills and stats diff --git a/src/SaveObject.js b/src/SaveObject.js index aa6195f45..eb08fc7a9 100644 --- a/src/SaveObject.js +++ b/src/SaveObject.js @@ -23,7 +23,7 @@ import {clearEventListeners, createElement, createPopup, removeElementById} from "../utils/HelperFunctions.js"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver.js"; -import {formatNumber} from "../utils/StringHelperFunctions.js"; +import {formatNumber} from "../utils/StringHelperFunctions"; import Decimal from "decimal.js"; diff --git a/src/Script.js b/src/Script.js index a636b8506..88981d632 100644 --- a/src/Script.js +++ b/src/Script.js @@ -38,7 +38,7 @@ import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver.js"; import {compareArrays, createElement} from "../utils/HelperFunctions.js"; import {formatNumber, numOccurrences, - numNetscriptOperators} from "../utils/StringHelperFunctions.js"; + numNetscriptOperators} from "../utils/StringHelperFunctions"; var keybindings = { ace: null, diff --git a/src/StockMarket.js b/src/StockMarket.js index 6db4a46a7..7dc46c5bd 100644 --- a/src/StockMarket.js +++ b/src/StockMarket.js @@ -12,7 +12,7 @@ import {clearEventListeners, getRandomInt, import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver.js"; import numeral from "numeral/min/numeral.min"; -import {formatNumber} from "../utils/StringHelperFunctions.js"; +import {formatNumber} from "../utils/StringHelperFunctions"; import {yesNoBoxCreate, yesNoTxtInpBoxCreate, yesNoBoxGetYesButton, yesNoBoxGetNoButton, yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton, diff --git a/src/Terminal.js b/src/Terminal.js index b17ac39aa..3b4eae01d 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -34,7 +34,7 @@ import {SpecialServerIps, import {TextFile, getTextFile} from "./TextFile"; import {containsAllStrings, longestCommonStart, - formatNumber, isString} from "../utils/StringHelperFunctions.js"; + formatNumber, isString} from "../utils/StringHelperFunctions"; import {addOffset, printArray} from "../utils/HelperFunctions.js"; import {logBoxCreate} from "../utils/LogBox.js"; import {yesNoBoxCreate, diff --git a/src/engine.js b/src/engine.js index d57232da8..6db35356a 100644 --- a/src/engine.js +++ b/src/engine.js @@ -5,7 +5,8 @@ import {clearEventListeners, createElement, exceptionAlert} from "../utils/HelperFunctions.js"; import numeral from "numeral/min/numeral.min"; import {formatNumber, - convertTimeMsToTimeElapsedString} from "../utils/StringHelperFunctions.js"; + convertTimeMsToTimeElapsedString, + replaceAt} from "../utils/StringHelperFunctions"; import {loxBoxCreate, logBoxUpdateText, logBoxOpened} from "../utils/LogBox.js"; @@ -1170,7 +1171,7 @@ let Engine = { //Update progress bar while (Engine._actionProgressBarCount * 2 <= percent) { - Engine._actionProgressStr = Engine._actionProgressStr.replaceAt(Engine._actionProgressBarCount, "|"); + Engine._actionProgressStr = replaceAt(Engine._actionProgressStr, Engine._actionProgressBarCount, "|"); Engine._actionProgressBarCount += 1; } diff --git a/utils/HelperFunctions.js b/utils/HelperFunctions.js index 3e682fe5b..9f07ff54c 100644 --- a/utils/HelperFunctions.js +++ b/utils/HelperFunctions.js @@ -1,5 +1,5 @@ //General helper functions -import {isString} from "./StringHelperFunctions.js"; +import {isString} from "./StringHelperFunctions"; import {dialogBoxCreate} from "./DialogBox.js"; //Returns the size (number of keys) of an object diff --git a/utils/InfiltrationBox.js b/utils/InfiltrationBox.js index 367d2c253..77bc501ad 100644 --- a/utils/InfiltrationBox.js +++ b/utils/InfiltrationBox.js @@ -4,7 +4,7 @@ import {Factions, Faction} from "../src/Faction.js"; import {Player} from "../src/Player.js"; import {dialogBoxCreate} from "./DialogBox.js"; import {clearEventListeners} from "./HelperFunctions.js"; -import {formatNumber} from "./StringHelperFunctions.js"; +import {formatNumber} from "./StringHelperFunctions"; /* InfiltrationBox.js */ function infiltrationBoxClose() { diff --git a/utils/StringHelperFunctions.js b/utils/StringHelperFunctions.js deleted file mode 100644 index 83b6856bc..000000000 --- a/utils/StringHelperFunctions.js +++ /dev/null @@ -1,150 +0,0 @@ -import {dialogBoxCreate} from "./DialogBox.js"; - -//Netburner String helper functions - -//Searches for every occurence of searchStr within str and returns an array of the indices of -//all these occurences -function getIndicesOf(searchStr, str, caseSensitive) { - var searchStrLen = searchStr.length; - if (searchStrLen == 0) { - return []; - } - var startIndex = 0, index, indices = []; - if (!caseSensitive) { - str = str.toLowerCase(); - searchStr = searchStr.toLowerCase(); - } - while ((index = str.indexOf(searchStr, startIndex)) > -1) { - indices.push(index); - startIndex = index + searchStrLen; - } - return indices; -} - -//Replaces the character at an index with a new character -String.prototype.replaceAt=function(index, character) { - return this.substr(0, index) + character + this.substr(index+character.length); -} - -//Converts a date representing time in milliseconds to a string with the format -// H hours M minutes and S seconds -// e.g. 10000 -> "0 hours 0 minutes and 10 seconds" -// 120000 -> "0 0 hours 2 minutes and 0 seconds" -function convertTimeMsToTimeElapsedString(time) { - //Convert ms to seconds, since we only have second-level precision - time = Math.floor(time / 1000); - - var days = Math.floor(time / 86400); - time %= 86400; - - var hours = Math.floor(time / 3600); - time %= 3600; - - var minutes = Math.floor(time / 60); - time %= 60; - - var seconds = time; - - var res = ""; - if (days) {res += days + " days ";} - if (hours) {res += hours + " hours ";} - if (minutes) {res += minutes + " minutes ";} - res += seconds + " seconds "; - return res; -} - -//Finds the longest common starting substring in a set of strings -function longestCommonStart(strings) { - if (!containsAllStrings(strings)) {return;} - if (strings.length == 0) {return;} - - var A = strings.concat().sort(), - a1= A[0], a2= A[A.length-1], L= a1.length, i= 0; - while(i= 0) { - ++n; - pos += step; - } else break; - } - return n; -} - -//Counters the number of Netscript operators in a string -function numNetscriptOperators(string) { - var total = 0; - total += numOccurrences(string, "+"); - total += numOccurrences(string, "-"); - total += numOccurrences(string, "*"); - total += numOccurrences(string, "/"); - total += numOccurrences(string, "%"); - total += numOccurrences(string, "&&"); - total += numOccurrences(string, "||"); - total += numOccurrences(string, "<"); - total += numOccurrences(string, ">"); - total += numOccurrences(string, "<="); - total += numOccurrences(string, ">="); - total += numOccurrences(string, "=="); - total += numOccurrences(string, "!="); - if (isNaN(total)) { - dialogBoxCreate("ERROR in counting number of operators in script. This is a bug, please report to game developer"); - total = 0; - } - return total; -} - -//Checks if a string contains HTML elements -function isHTML(str) { - var a = document.createElement('div'); - a.innerHTML = str; - for (var c = a.childNodes, i = c.length; i--; ) { - if (c[i].nodeType == 1) return true; - } - return false; -} - -//Generates a random alphanumeric string with N characters -function generateRandomString(n) { - var str = "", - chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; - - for (var i = 0; i < n; i++) - str += chars.charAt(Math.floor(Math.random() * chars.length)); - - return str; -} - -export {getIndicesOf, convertTimeMsToTimeElapsedString, longestCommonStart, - isString, containsAllStrings, formatNumber, - numOccurrences, numNetscriptOperators, isHTML, generateRandomString}; diff --git a/utils/StringHelperFunctions.ts b/utils/StringHelperFunctions.ts new file mode 100644 index 000000000..4f1994bd9 --- /dev/null +++ b/utils/StringHelperFunctions.ts @@ -0,0 +1,151 @@ +import { dialogBoxCreate } from "./DialogBox.js"; + +// Netburner String helper functions + +// Replaces the character at an index with a new character +function replaceAt(base: string, index: number, character: string): string { + return base.substr(0, index) + character + base.substr(index + character.length); +} + +/* +Converts a date representing time in milliseconds to a string with the format H hours M minutes and S seconds +e.g. 10000 -> "0 hours 0 minutes and 10 seconds" + 120000 -> "0 0 hours 2 minutes and 0 seconds" +*/ +function convertTimeMsToTimeElapsedString(time: number): string { + const millisecondsPerSecond = 1000; + const secondPerMinute = 60; + const minutesPerHours = 60; + const secondPerHours: number = secondPerMinute * minutesPerHours; + const hoursPerDays = 24; + const secondPerDay: number = secondPerHours * hoursPerDays; + + // Convert ms to seconds, since we only have second-level precision + const totalSeconds: number = Math.floor(time / millisecondsPerSecond); + + const days: number = Math.floor(totalSeconds / secondPerDay); + const secTruncDays: number = totalSeconds % secondPerDay; + + const hours: number = Math.floor(secTruncDays / secondPerHours); + const secTruncHours: number = secTruncDays % secondPerHours; + + const minutes: number = Math.floor(secTruncHours / secondPerMinute); + const secTruncMinutes: number = secTruncHours % secondPerMinute; + + const seconds: number = secTruncMinutes; + + let res = ""; + if (days) {res += `${days} days `; } + if (hours) {res += `${hours} hours `; } + if (minutes) {res += `${minutes} minutes `; } + res += `${seconds} seconds `; + + return res; +} + +// Finds the longest common starting substring in a set of strings +function longestCommonStart(strings: string[]): string { + if (!containsAllStrings(strings)) {return ""; } + if (strings.length == 0) {return ""; } + + const A: string[] = strings.concat().sort(); + const a1: string = A[0]; + const a2: string = A[A.length - 1]; + const L: number = a1.length; + let i = 0; + while (i < L && a1.charAt(i).toLowerCase() === a2.charAt(i).toLowerCase()) { i++; } + + return a1.substring(0, i); +} + +// Returns whether a variable is a string +function isString(str: any): boolean { + return (typeof str === "string" || str instanceof String); +} + +// Returns whether an array contains entirely of string objects +function containsAllStrings(arr: string[]): boolean { + return arr.every(isString); +} + +// Formats a number with commas and a specific number of decimal digits +function formatNumber(num: number, numFractionDigits: number): string { + return num.toLocaleString(undefined, { + maximumFractionDigits: numFractionDigits, + minimumFractionDigits: numFractionDigits, + }); +} + +// Count the number of times a substring occurs in a string +function numOccurrences(text: string, subString: string): number { + text += ""; + subString += ""; + if (subString.length <= 0) { return (text.length + 1); } + + let n = 0; + let pos = 0; + const step: number = subString.length; + + while (true) { + pos = text.indexOf(subString, pos); + if (pos >= 0) { + ++n; + pos += step; + } else { break; } + } + + return n; +} + +// Counters the number of Netscript operators in a string +function numNetscriptOperators(text: string): number { + const total: number = numOccurrences(text, "+") + + numOccurrences(text, "-") + + numOccurrences(text, "*") + + numOccurrences(text, "/") + + numOccurrences(text, "%") + + numOccurrences(text, "&&") + + numOccurrences(text, "||") + + numOccurrences(text, "<") + + numOccurrences(text, ">") + + numOccurrences(text, "<=") + + numOccurrences(text, ">=") + + numOccurrences(text, "==") + + numOccurrences(text, "!="); + if (isNaN(total)) { + const message = "ERROR in counting number of operators in script. This is a bug, please report to game developer"; + dialogBoxCreate(message, false); + + return 0; + } + + return total; +} + +// Checks if a string contains HTML elements +function isHTML(str: string): boolean { + const a = document.createElement("div"); + a.innerHTML = str; + const c = a.childNodes; + for (let i = c.length; i--;) { + if (c[i].nodeType == 1) { return true; } + } + + return false; +} + +// Generates a random alphanumeric string with N characters +function generateRandomString(n: number): string { + let str = ""; + const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + + for (let i = 0; i < n; i++) { + str += chars.charAt(Math.floor(Math.random() * chars.length)); + } + + return str; +} + +export {convertTimeMsToTimeElapsedString, longestCommonStart, + isString, containsAllStrings, formatNumber, + numOccurrences, numNetscriptOperators, isHTML, generateRandomString, replaceAt};