mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-19 06:03:50 +01:00
Merge pull request #324 from hydroflame/hydroflame-convert-ts
first conversion
This commit is contained in:
commit
89ecb15b6f
@ -10,7 +10,7 @@ import {printArray, createElement,
|
|||||||
removeChildrenFromElement, exceptionAlert} from "../utils/HelperFunctions.js";
|
removeChildrenFromElement, exceptionAlert} from "../utils/HelperFunctions.js";
|
||||||
import {logBoxCreate} from "../utils/LogBox.js";
|
import {logBoxCreate} from "../utils/LogBox.js";
|
||||||
import numeral from "numeral/min/numeral.min";
|
import numeral from "numeral/min/numeral.min";
|
||||||
import {formatNumber} from "../utils/StringHelperFunctions.js";
|
import {formatNumber} from "../utils/StringHelperFunctions";
|
||||||
|
|
||||||
/* {
|
/* {
|
||||||
* serverName: {
|
* serverName: {
|
||||||
|
@ -16,7 +16,7 @@ import {createElement, createAccordionElement,
|
|||||||
removeChildrenFromElement, clearObject} from "../utils/HelperFunctions.js";
|
removeChildrenFromElement, clearObject} from "../utils/HelperFunctions.js";
|
||||||
import {Reviver, Generic_toJSON,
|
import {Reviver, Generic_toJSON,
|
||||||
Generic_fromJSON} from "../utils/JSONReviver.js";
|
Generic_fromJSON} from "../utils/JSONReviver.js";
|
||||||
import {isString} from "../utils/StringHelperFunctions.js";
|
import {isString} from "../utils/StringHelperFunctions";
|
||||||
|
|
||||||
//Augmentations
|
//Augmentations
|
||||||
function Augmentation(params) {
|
function Augmentation(params) {
|
||||||
|
@ -18,7 +18,7 @@ import {getRandomInt, addOffset, clearObject,
|
|||||||
import {Reviver, Generic_toJSON,
|
import {Reviver, Generic_toJSON,
|
||||||
Generic_fromJSON} from "../utils/JSONReviver.js";
|
Generic_fromJSON} from "../utils/JSONReviver.js";
|
||||||
import numeral from "numeral/min/numeral.min";
|
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"];
|
var CityNames = ["Aevum", "Chongqing", "Sector-12", "New Tokyo", "Ishima", "Volhaven"];
|
||||||
|
@ -2,7 +2,7 @@ import {Engine} from "./engine.js";
|
|||||||
|
|
||||||
import {removeChildrenFromElement,
|
import {removeChildrenFromElement,
|
||||||
createElement, exceptionAlert} from "../utils/HelperFunctions.js";
|
createElement, exceptionAlert} from "../utils/HelperFunctions.js";
|
||||||
import {isString} from "../utils/StringHelperFunctions.js";
|
import {isString} from "../utils/StringHelperFunctions";
|
||||||
|
|
||||||
var cinematicTextFlag = false;
|
var cinematicTextFlag = false;
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ import {getRandomInt, removeElementById,
|
|||||||
import {Reviver, Generic_toJSON,
|
import {Reviver, Generic_toJSON,
|
||||||
Generic_fromJSON} from "../utils/JSONReviver.js";
|
Generic_fromJSON} from "../utils/JSONReviver.js";
|
||||||
import numeral from "numeral/min/numeral.min";
|
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,
|
import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
|
||||||
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
|
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
|
||||||
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,
|
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,
|
||||||
|
@ -4,7 +4,7 @@ import {SpecialServerIps} from "./SpecialServerIps.js";
|
|||||||
import {post} from "./Terminal.js";
|
import {post} from "./Terminal.js";
|
||||||
|
|
||||||
import {isValidIPAddress} from "../utils/IPAddress.js";
|
import {isValidIPAddress} from "../utils/IPAddress.js";
|
||||||
import {formatNumber} from "../utils/StringHelperFunctions.js";
|
import {formatNumber} from "../utils/StringHelperFunctions";
|
||||||
|
|
||||||
|
|
||||||
/* DarkWeb.js */
|
/* DarkWeb.js */
|
||||||
|
@ -16,7 +16,7 @@ import {clearEventListeners, createElement,
|
|||||||
import {Reviver, Generic_toJSON,
|
import {Reviver, Generic_toJSON,
|
||||||
Generic_fromJSON} from "../utils/JSONReviver.js";
|
Generic_fromJSON} from "../utils/JSONReviver.js";
|
||||||
import numeral from "numeral/min/numeral.min";
|
import numeral from "numeral/min/numeral.min";
|
||||||
import {formatNumber} from "../utils/StringHelperFunctions.js";
|
import {formatNumber} from "../utils/StringHelperFunctions";
|
||||||
import {yesNoBoxCreate, yesNoBoxGetYesButton,
|
import {yesNoBoxCreate, yesNoBoxGetYesButton,
|
||||||
yesNoBoxGetNoButton, yesNoBoxClose} from "../utils/YesNoBox.js";
|
yesNoBoxGetNoButton, yesNoBoxClose} from "../utils/YesNoBox.js";
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ import {getRandomInt, createElement,
|
|||||||
createAccordionElement, createPopup,
|
createAccordionElement, createPopup,
|
||||||
removeElementById, removeElement} from "../utils/HelperFunctions.js";
|
removeElementById, removeElement} from "../utils/HelperFunctions.js";
|
||||||
import numeral from "numeral/min/numeral.min";
|
import numeral from "numeral/min/numeral.min";
|
||||||
import {formatNumber} from "../utils/StringHelperFunctions.js";
|
import {formatNumber} from "../utils/StringHelperFunctions";
|
||||||
import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
|
import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
|
||||||
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
|
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
|
||||||
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,
|
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,
|
||||||
|
@ -9,7 +9,7 @@ import {clearEventListeners, createElement,
|
|||||||
getElementById} from "../utils/HelperFunctions.js";
|
getElementById} from "../utils/HelperFunctions.js";
|
||||||
import {Reviver, Generic_toJSON,
|
import {Reviver, Generic_toJSON,
|
||||||
Generic_fromJSON} from "../utils/JSONReviver.js";
|
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.
|
* Overwrites the inner text of the specified HTML element if it is different from what currently exists.
|
||||||
|
@ -5,7 +5,7 @@ import {Player} from "./Player.js";
|
|||||||
import {dialogBoxCreate} from "../utils/DialogBox.js";
|
import {dialogBoxCreate} from "../utils/DialogBox.js";
|
||||||
import {clearEventListeners, getRandomInt} from "../utils/HelperFunctions.js";
|
import {clearEventListeners, getRandomInt} from "../utils/HelperFunctions.js";
|
||||||
import {infiltrationBoxCreate} from "../utils/InfiltrationBox.js";
|
import {infiltrationBoxCreate} from "../utils/InfiltrationBox.js";
|
||||||
import {formatNumber} from "../utils/StringHelperFunctions.js";
|
import {formatNumber} from "../utils/StringHelperFunctions";
|
||||||
|
|
||||||
/* Infiltration.js
|
/* Infiltration.js
|
||||||
*
|
*
|
||||||
|
@ -18,7 +18,7 @@ import {dialogBoxCreate} from "../utils/DialogBox.js";
|
|||||||
import {clearEventListeners, createElement} from "../utils/HelperFunctions.js";
|
import {clearEventListeners, createElement} from "../utils/HelperFunctions.js";
|
||||||
import {createRandomIp} from "../utils/IPAddress.js";
|
import {createRandomIp} from "../utils/IPAddress.js";
|
||||||
import numeral from "numeral/min/numeral.min";
|
import numeral from "numeral/min/numeral.min";
|
||||||
import {formatNumber} from "../utils/StringHelperFunctions.js";
|
import {formatNumber} from "../utils/StringHelperFunctions";
|
||||||
import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
|
import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
|
||||||
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
|
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
|
||||||
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,
|
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,
|
||||||
|
@ -6,7 +6,7 @@ import {dialogBoxCreate} from "../utils/DialogBox.js"
|
|||||||
import {addOffset, getRandomInt,
|
import {addOffset, getRandomInt,
|
||||||
clearEventListenersEl,
|
clearEventListenersEl,
|
||||||
clearEventListeners} from "../utils/HelperFunctions.js";
|
clearEventListeners} from "../utils/HelperFunctions.js";
|
||||||
import {formatNumber, isString} from "../utils/StringHelperFunctions.js";
|
import {formatNumber, isString} from "../utils/StringHelperFunctions";
|
||||||
import jsplumb from 'jsplumb'
|
import jsplumb from 'jsplumb'
|
||||||
|
|
||||||
let inMission = false; //Flag to denote whether a mission is running
|
let inMission = false; //Flag to denote whether a mission is running
|
||||||
|
@ -11,7 +11,7 @@ import {Script, findRunningScript,
|
|||||||
import {parse, Node} from "../utils/acorn.js";
|
import {parse, Node} from "../utils/acorn.js";
|
||||||
import {printArray} from "../utils/HelperFunctions.js";
|
import {printArray} from "../utils/HelperFunctions.js";
|
||||||
import {isValidIPAddress} from "../utils/IPAddress.js";
|
import {isValidIPAddress} from "../utils/IPAddress.js";
|
||||||
import {isString} from "../utils/StringHelperFunctions.js";
|
import {isString} from "../utils/StringHelperFunctions";
|
||||||
|
|
||||||
var Promise = require("bluebird");
|
var Promise = require("bluebird");
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ import Decimal from "decimal.js";
|
|||||||
import {dialogBoxCreate} from "../utils/DialogBox.js";
|
import {dialogBoxCreate} from "../utils/DialogBox.js";
|
||||||
import {printArray, powerOfTwo} from "../utils/HelperFunctions.js";
|
import {printArray, powerOfTwo} from "../utils/HelperFunctions.js";
|
||||||
import {createRandomIp} from "../utils/IPAddress.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,
|
import {yesNoBoxClose, yesNoBoxGetYesButton,
|
||||||
yesNoBoxGetNoButton, yesNoBoxCreate,
|
yesNoBoxGetNoButton, yesNoBoxCreate,
|
||||||
yesNoBoxOpen} from "../utils/YesNoBox.js";
|
yesNoBoxOpen} from "../utils/YesNoBox.js";
|
||||||
|
@ -27,7 +27,7 @@ import {Reviver, Generic_toJSON,
|
|||||||
Generic_fromJSON} from "../utils/JSONReviver.js";
|
Generic_fromJSON} from "../utils/JSONReviver.js";
|
||||||
import numeral from "numeral/min/numeral.min";
|
import numeral from "numeral/min/numeral.min";
|
||||||
import {formatNumber,
|
import {formatNumber,
|
||||||
convertTimeMsToTimeElapsedString} from "../utils/StringHelperFunctions.js";
|
convertTimeMsToTimeElapsedString} from "../utils/StringHelperFunctions";
|
||||||
|
|
||||||
function PlayerObject() {
|
function PlayerObject() {
|
||||||
//Skills and stats
|
//Skills and stats
|
||||||
|
@ -23,7 +23,7 @@ import {clearEventListeners, createElement,
|
|||||||
createPopup, removeElementById} from "../utils/HelperFunctions.js";
|
createPopup, removeElementById} from "../utils/HelperFunctions.js";
|
||||||
import {Reviver, Generic_toJSON,
|
import {Reviver, Generic_toJSON,
|
||||||
Generic_fromJSON} from "../utils/JSONReviver.js";
|
Generic_fromJSON} from "../utils/JSONReviver.js";
|
||||||
import {formatNumber} from "../utils/StringHelperFunctions.js";
|
import {formatNumber} from "../utils/StringHelperFunctions";
|
||||||
|
|
||||||
import Decimal from "decimal.js";
|
import Decimal from "decimal.js";
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ import {Reviver, Generic_toJSON,
|
|||||||
Generic_fromJSON} from "../utils/JSONReviver.js";
|
Generic_fromJSON} from "../utils/JSONReviver.js";
|
||||||
import {compareArrays, createElement} from "../utils/HelperFunctions.js";
|
import {compareArrays, createElement} from "../utils/HelperFunctions.js";
|
||||||
import {formatNumber, numOccurrences,
|
import {formatNumber, numOccurrences,
|
||||||
numNetscriptOperators} from "../utils/StringHelperFunctions.js";
|
numNetscriptOperators} from "../utils/StringHelperFunctions";
|
||||||
|
|
||||||
var keybindings = {
|
var keybindings = {
|
||||||
ace: null,
|
ace: null,
|
||||||
|
@ -12,7 +12,7 @@ import {clearEventListeners, getRandomInt,
|
|||||||
import {Reviver, Generic_toJSON,
|
import {Reviver, Generic_toJSON,
|
||||||
Generic_fromJSON} from "../utils/JSONReviver.js";
|
Generic_fromJSON} from "../utils/JSONReviver.js";
|
||||||
import numeral from "numeral/min/numeral.min";
|
import numeral from "numeral/min/numeral.min";
|
||||||
import {formatNumber} from "../utils/StringHelperFunctions.js";
|
import {formatNumber} from "../utils/StringHelperFunctions";
|
||||||
import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
|
import {yesNoBoxCreate, yesNoTxtInpBoxCreate,
|
||||||
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
|
yesNoBoxGetYesButton, yesNoBoxGetNoButton,
|
||||||
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,
|
yesNoTxtInpBoxGetYesButton, yesNoTxtInpBoxGetNoButton,
|
||||||
|
@ -34,7 +34,7 @@ import {SpecialServerIps,
|
|||||||
import {TextFile, getTextFile} from "./TextFile";
|
import {TextFile, getTextFile} from "./TextFile";
|
||||||
|
|
||||||
import {containsAllStrings, longestCommonStart,
|
import {containsAllStrings, longestCommonStart,
|
||||||
formatNumber, isString} from "../utils/StringHelperFunctions.js";
|
formatNumber, isString} from "../utils/StringHelperFunctions";
|
||||||
import {addOffset, printArray} from "../utils/HelperFunctions.js";
|
import {addOffset, printArray} from "../utils/HelperFunctions.js";
|
||||||
import {logBoxCreate} from "../utils/LogBox.js";
|
import {logBoxCreate} from "../utils/LogBox.js";
|
||||||
import {yesNoBoxCreate,
|
import {yesNoBoxCreate,
|
||||||
|
@ -5,7 +5,8 @@ import {clearEventListeners, createElement,
|
|||||||
exceptionAlert} from "../utils/HelperFunctions.js";
|
exceptionAlert} from "../utils/HelperFunctions.js";
|
||||||
import numeral from "numeral/min/numeral.min";
|
import numeral from "numeral/min/numeral.min";
|
||||||
import {formatNumber,
|
import {formatNumber,
|
||||||
convertTimeMsToTimeElapsedString} from "../utils/StringHelperFunctions.js";
|
convertTimeMsToTimeElapsedString,
|
||||||
|
replaceAt} from "../utils/StringHelperFunctions";
|
||||||
import {loxBoxCreate, logBoxUpdateText,
|
import {loxBoxCreate, logBoxUpdateText,
|
||||||
logBoxOpened} from "../utils/LogBox.js";
|
logBoxOpened} from "../utils/LogBox.js";
|
||||||
|
|
||||||
@ -1170,7 +1171,7 @@ let Engine = {
|
|||||||
|
|
||||||
//Update progress bar
|
//Update progress bar
|
||||||
while (Engine._actionProgressBarCount * 2 <= percent) {
|
while (Engine._actionProgressBarCount * 2 <= percent) {
|
||||||
Engine._actionProgressStr = Engine._actionProgressStr.replaceAt(Engine._actionProgressBarCount, "|");
|
Engine._actionProgressStr = replaceAt(Engine._actionProgressStr, Engine._actionProgressBarCount, "|");
|
||||||
Engine._actionProgressBarCount += 1;
|
Engine._actionProgressBarCount += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
//General helper functions
|
//General helper functions
|
||||||
import {isString} from "./StringHelperFunctions.js";
|
import {isString} from "./StringHelperFunctions";
|
||||||
import {dialogBoxCreate} from "./DialogBox.js";
|
import {dialogBoxCreate} from "./DialogBox.js";
|
||||||
|
|
||||||
//Returns the size (number of keys) of an object
|
//Returns the size (number of keys) of an object
|
||||||
|
@ -4,7 +4,7 @@ import {Factions, Faction} from "../src/Faction.js";
|
|||||||
import {Player} from "../src/Player.js";
|
import {Player} from "../src/Player.js";
|
||||||
import {dialogBoxCreate} from "./DialogBox.js";
|
import {dialogBoxCreate} from "./DialogBox.js";
|
||||||
import {clearEventListeners} from "./HelperFunctions.js";
|
import {clearEventListeners} from "./HelperFunctions.js";
|
||||||
import {formatNumber} from "./StringHelperFunctions.js";
|
import {formatNumber} from "./StringHelperFunctions";
|
||||||
|
|
||||||
/* InfiltrationBox.js */
|
/* InfiltrationBox.js */
|
||||||
function infiltrationBoxClose() {
|
function infiltrationBoxClose() {
|
||||||
|
@ -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<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) {
|
|
||||||
return (typeof str === 'string' || str instanceof String);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Returns whether an array contains entirely of string objects
|
|
||||||
function containsAllStrings(arr) {
|
|
||||||
return arr.every(isString);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Formats a number with commas and a specific number of decimal digits
|
|
||||||
function formatNumber(num, numFractionDigits) {
|
|
||||||
return num.toLocaleString(undefined, {
|
|
||||||
minimumFractionDigits: numFractionDigits,
|
|
||||||
maximumFractionDigits: numFractionDigits
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//Count the number of times a substring occurs in a string
|
|
||||||
function numOccurrences(string, subString) {
|
|
||||||
string += "";
|
|
||||||
subString += "";
|
|
||||||
if (subString.length <= 0) return (string.length + 1);
|
|
||||||
|
|
||||||
var n = 0, pos = 0, step = subString.length;
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
pos = string.indexOf(subString, pos);
|
|
||||||
if (pos >= 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};
|
|
151
utils/StringHelperFunctions.ts
Normal file
151
utils/StringHelperFunctions.ts
Normal file
@ -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};
|
Loading…
Reference in New Issue
Block a user