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 3dfee17fb..73882a363 100644 --- a/src/Bladeburner.js +++ b/src/Bladeburner.js @@ -17,7 +17,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 c28388750..02cef2bed 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 1b5d08280..f6cc31dee 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 4b7a15e25..5c0e25d68 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 bbe707e84..b1a23ff12 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 39b30b817..98a641225 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 462c86a99..779b9ffcb 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 5acccfae1..763d489d2 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -33,7 +33,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 0363ab94d..9ad31d4ce 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.ts similarity index 57% rename from utils/StringHelperFunctions.js rename to utils/StringHelperFunctions.ts index 83b6856bc..c8ae4b032 100644 --- a/utils/StringHelperFunctions.js +++ b/utils/StringHelperFunctions.ts @@ -4,12 +4,14 @@ import {dialogBoxCreate} from "./DialogBox.js"; //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; +function getIndicesOf(searchStr: string, str: string, caseSensitive: boolean): number[] { + let searchStrLen: number = searchStr.length; if (searchStrLen == 0) { return []; } - var startIndex = 0, index, indices = []; + let startIndex: number = 0; + let index: number = 0; + let indices: number[] = []; if (!caseSensitive) { str = str.toLowerCase(); searchStr = searchStr.toLowerCase(); @@ -22,30 +24,30 @@ function getIndicesOf(searchStr, str, caseSensitive) { } //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); +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) { +function convertTimeMsToTimeElapsedString(time: number): string { //Convert ms to seconds, since we only have second-level precision time = Math.floor(time / 1000); - var days = Math.floor(time / 86400); + let days = Math.floor(time / 86400); time %= 86400; - var hours = Math.floor(time / 3600); + let hours = Math.floor(time / 3600); time %= 3600; - var minutes = Math.floor(time / 60); + let minutes = Math.floor(time / 60); time %= 60; - var seconds = time; + let seconds: number = time; - var res = ""; + let res = ""; if (days) {res += days + " days ";} if (hours) {res += hours + " hours ";} if (minutes) {res += minutes + " minutes ";} @@ -54,29 +56,32 @@ function convertTimeMsToTimeElapsedString(time) { } //Finds the longest common starting substring in a set of strings -function longestCommonStart(strings) { - if (!containsAllStrings(strings)) {return;} - if (strings.length == 0) {return;} +function longestCommonStart(strings: string[]): string { + 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; + let A: string[] = strings.concat().sort(); + let a1: string = A[0]; + let a2: string = A[A.length-1]; + let L: number = a1.length; + let i: number = 0; while(i= 0) { ++n; pos += step; @@ -102,30 +109,29 @@ function numOccurrences(string, subString) { } //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, "!="); +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)) { - dialogBoxCreate("ERROR in counting number of operators in script. This is a bug, please report to game developer"); - total = 0; + dialogBoxCreate("ERROR in counting number of operators in script. This is a bug, please report to game developer", false); + return 0; } return total; } //Checks if a string contains HTML elements -function isHTML(str) { +function isHTML(str: string): boolean { var a = document.createElement('div'); a.innerHTML = str; for (var c = a.childNodes, i = c.length; i--; ) { @@ -135,7 +141,7 @@ function isHTML(str) { } //Generates a random alphanumeric string with N characters -function generateRandomString(n) { +function generateRandomString(n: number): string { var str = "", chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; @@ -147,4 +153,4 @@ function generateRandomString(n) { export {getIndicesOf, convertTimeMsToTimeElapsedString, longestCommonStart, isString, containsAllStrings, formatNumber, - numOccurrences, numNetscriptOperators, isHTML, generateRandomString}; + numOccurrences, numNetscriptOperators, isHTML, generateRandomString, replaceAt};