bitburner-src/utils/StringHelperFunctions.ts

152 lines
4.8 KiB
TypeScript
Raw Normal View History

2018-06-25 02:55:43 +02:00
import { dialogBoxCreate } from "./DialogBox.js";
2018-06-25 02:55:43 +02:00
// Netburner String helper functions
2018-06-25 02:55:43 +02:00
// Replaces the character at an index with a new character
2018-06-22 23:30:24 +02:00
function replaceAt(base: string, index: number, character: string): string {
2018-06-25 02:55:43 +02:00
return base.substr(0, index) + character + base.substr(index + character.length);
2017-02-06 06:01:01 +01:00
}
2018-06-25 02:55:43 +02:00
/*
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"
*/
2018-06-22 23:30:24 +02:00
function convertTimeMsToTimeElapsedString(time: number): string {
2018-06-25 02:55:43 +02:00
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);
2018-06-25 02:55:43 +02:00
const days: number = Math.floor(totalSeconds / secondPerDay);
const secTruncDays: number = totalSeconds % secondPerDay;
2018-06-25 02:55:43 +02:00
const hours: number = Math.floor(secTruncDays / secondPerHours);
const secTruncHours: number = secTruncDays % secondPerHours;
2018-06-25 02:55:43 +02:00
const minutes: number = Math.floor(secTruncHours / secondPerMinute);
const secTruncMinutes: number = secTruncHours % secondPerMinute;
2018-06-25 02:55:43 +02:00
const seconds: number = secTruncMinutes;
2018-06-22 23:30:24 +02:00
let res = "";
2018-06-25 02:55:43 +02:00
if (days) {res += `${days} days `; }
if (hours) {res += `${hours} hours `; }
if (minutes) {res += `${minutes} minutes `; }
res += `${seconds} seconds `;
return res;
}
2018-06-25 02:55:43 +02:00
// Finds the longest common starting substring in a set of strings
2018-06-22 23:30:24 +02:00
function longestCommonStart(strings: string[]): string {
2018-06-25 02:55:43 +02:00
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);
}
2018-06-25 02:55:43 +02:00
// Returns whether a variable is a string
2018-06-22 23:30:24 +02:00
function isString(str: any): boolean {
2018-06-25 02:55:43 +02:00
return (typeof str === "string" || str instanceof String);
}
2018-06-25 02:55:43 +02:00
// Returns whether an array contains entirely of string objects
2018-06-22 23:30:24 +02:00
function containsAllStrings(arr: string[]): boolean {
return arr.every(isString);
}
2018-06-25 02:55:43 +02:00
// Formats a number with commas and a specific number of decimal digits
2018-06-22 23:30:24 +02:00
function formatNumber(num: number, numFractionDigits: number): string {
return num.toLocaleString(undefined, {
2018-06-25 02:55:43 +02:00
maximumFractionDigits: numFractionDigits,
minimumFractionDigits: numFractionDigits,
});
}
2018-06-25 02:55:43 +02:00
// Count the number of times a substring occurs in a string
2018-06-22 23:30:24 +02:00
function numOccurrences(text: string, subString: string): number {
text += "";
subString += "";
2018-06-25 02:55:43 +02:00
if (subString.length <= 0) { return (text.length + 1); }
2018-06-25 02:55:43 +02:00
let n = 0;
let pos = 0;
const step: number = subString.length;
while (true) {
2018-06-22 23:30:24 +02:00
pos = text.indexOf(subString, pos);
if (pos >= 0) {
++n;
pos += step;
2018-06-25 02:55:43 +02:00
} else { break; }
}
2018-06-25 02:55:43 +02:00
return n;
2017-05-15 17:38:17 +02:00
}
2018-06-25 02:55:43 +02:00
// Counters the number of Netscript operators in a string
2018-06-22 23:30:24 +02:00
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, "!=");
2017-05-15 17:38:17 +02:00
if (isNaN(total)) {
2018-06-25 02:55:43 +02:00
const message = "ERROR in counting number of operators in script. This is a bug, please report to game developer";
dialogBoxCreate(message, false);
2018-06-22 23:30:24 +02:00
return 0;
2017-05-15 17:38:17 +02:00
}
2018-06-25 02:55:43 +02:00
2017-05-15 17:38:17 +02:00
return total;
}
2018-06-25 02:55:43 +02:00
// Checks if a string contains HTML elements
2018-06-22 23:30:24 +02:00
function isHTML(str: string): boolean {
2018-06-25 02:55:43 +02:00
const a = document.createElement("div");
2017-09-15 16:06:59 +02:00
a.innerHTML = str;
2018-06-25 02:55:43 +02:00
const c = a.childNodes;
for (let i = c.length; i--;) {
if (c[i].nodeType == 1) { return true; }
2017-09-15 16:06:59 +02:00
}
2018-06-25 02:55:43 +02:00
2017-09-15 16:06:59 +02:00
return false;
}
2018-06-25 02:55:43 +02:00
// Generates a random alphanumeric string with N characters
2018-06-22 23:30:24 +02:00
function generateRandomString(n: number): string {
2018-06-25 02:55:43 +02:00
let str = "";
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
2018-06-25 02:55:43 +02:00
for (let i = 0; i < n; i++) {
str += chars.charAt(Math.floor(Math.random() * chars.length));
2018-06-25 02:55:43 +02:00
}
return str;
}
2018-06-25 02:55:43 +02:00
export {convertTimeMsToTimeElapsedString, longestCommonStart,
2018-03-27 02:46:21 +02:00
isString, containsAllStrings, formatNumber,
2018-06-22 23:30:24 +02:00
numOccurrences, numNetscriptOperators, isHTML, generateRandomString, replaceAt};