2017-08-30 19:44:29 +02:00
|
|
|
import {dialogBoxCreate} from "./DialogBox.js";
|
|
|
|
|
2016-12-19 19:20:19 +01:00
|
|
|
//Netburner String helper functions
|
2016-12-15 23:22:42 +01:00
|
|
|
|
|
|
|
//Searches for every occurence of searchStr within str and returns an array of the indices of
|
|
|
|
//all these occurences
|
2018-06-22 23:30:24 +02:00
|
|
|
function getIndicesOf(searchStr: string, str: string, caseSensitive: boolean): number[] {
|
|
|
|
let searchStrLen: number = searchStr.length;
|
2016-12-15 23:22:42 +01:00
|
|
|
if (searchStrLen == 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2018-06-22 23:30:24 +02:00
|
|
|
let startIndex: number = 0;
|
|
|
|
let index: number = 0;
|
|
|
|
let indices: number[] = [];
|
2016-12-15 23:22:42 +01:00
|
|
|
if (!caseSensitive) {
|
|
|
|
str = str.toLowerCase();
|
|
|
|
searchStr = searchStr.toLowerCase();
|
|
|
|
}
|
|
|
|
while ((index = str.indexOf(searchStr, startIndex)) > -1) {
|
|
|
|
indices.push(index);
|
|
|
|
startIndex = index + searchStrLen;
|
|
|
|
}
|
|
|
|
return indices;
|
2016-12-21 17:33:00 +01: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 {
|
|
|
|
return base.substr(0, index) + character + base.substr(index+character.length);
|
2017-02-06 06:01:01 +01: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 {
|
2017-02-06 06:01:01 +01:00
|
|
|
//Convert ms to seconds, since we only have second-level precision
|
|
|
|
time = Math.floor(time / 1000);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-06-22 23:30:24 +02:00
|
|
|
let days = Math.floor(time / 86400);
|
2017-05-15 16:15:59 +02:00
|
|
|
time %= 86400;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-06-22 23:30:24 +02:00
|
|
|
let hours = Math.floor(time / 3600);
|
2017-02-06 06:01:01 +01:00
|
|
|
time %= 3600;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-06-22 23:30:24 +02:00
|
|
|
let minutes = Math.floor(time / 60);
|
2017-02-06 06:01:01 +01:00
|
|
|
time %= 60;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-06-22 23:30:24 +02:00
|
|
|
let seconds: number = time;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-06-22 23:30:24 +02:00
|
|
|
let res = "";
|
2017-05-17 05:50:32 +02:00
|
|
|
if (days) {res += days + " days ";}
|
2017-05-15 16:15:59 +02:00
|
|
|
if (hours) {res += hours + " hours ";}
|
|
|
|
if (minutes) {res += minutes + " minutes ";}
|
2017-05-21 07:39:18 +02:00
|
|
|
res += seconds + " seconds ";
|
2017-05-15 16:15:59 +02:00
|
|
|
return res;
|
2017-05-01 23:38:49 +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 {
|
|
|
|
if (!containsAllStrings(strings)) {return "";}
|
|
|
|
if (strings.length == 0) {return "";}
|
|
|
|
|
|
|
|
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;
|
2017-09-12 01:14:51 +02:00
|
|
|
while(i<L && a1.charAt(i).toLowerCase() === a2.charAt(i).toLowerCase()) i++;
|
2017-05-01 23:38:49 +02:00
|
|
|
return a1.substring(0, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Returns whether a variable is a string
|
2018-06-22 23:30:24 +02:00
|
|
|
function isString(str: any): boolean {
|
2017-05-01 23:38:49 +02:00
|
|
|
return (typeof str === 'string' || str instanceof String);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Returns whether an array contains entirely of string objects
|
2018-06-22 23:30:24 +02:00
|
|
|
function containsAllStrings(arr: string[]): boolean {
|
2017-05-01 23:38:49 +02:00
|
|
|
return arr.every(isString);
|
2017-05-04 22:50:17 +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 {
|
2017-05-04 22:50:17 +02:00
|
|
|
return num.toLocaleString(undefined, {
|
|
|
|
minimumFractionDigits: numFractionDigits,
|
|
|
|
maximumFractionDigits: numFractionDigits
|
|
|
|
});
|
2017-05-10 23:10:06 +02:00
|
|
|
}
|
|
|
|
|
2017-05-15 17:38:17 +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 += "";
|
2017-05-10 23:10:06 +02:00
|
|
|
subString += "";
|
2018-06-22 23:30:24 +02:00
|
|
|
if (subString.length <= 0) return (text.length + 1);
|
2017-05-10 23:10:06 +02:00
|
|
|
|
2018-06-22 23:30:24 +02:00
|
|
|
let n: number = 0;
|
|
|
|
let pos: number = 0;
|
|
|
|
let step: number = subString.length;
|
2017-05-10 23:10:06 +02:00
|
|
|
|
|
|
|
while (true) {
|
2018-06-22 23:30:24 +02:00
|
|
|
pos = text.indexOf(subString, pos);
|
2017-05-10 23:10:06 +02:00
|
|
|
if (pos >= 0) {
|
|
|
|
++n;
|
|
|
|
pos += step;
|
|
|
|
} else break;
|
|
|
|
}
|
|
|
|
return n;
|
2017-05-15 17:38:17 +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-22 23:30:24 +02:00
|
|
|
dialogBoxCreate("ERROR in counting number of operators in script. This is a bug, please report to game developer", false);
|
|
|
|
return 0;
|
2017-05-15 17:38:17 +02:00
|
|
|
}
|
|
|
|
return total;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-09-15 16:06:59 +02:00
|
|
|
//Checks if a string contains HTML elements
|
2018-06-22 23:30:24 +02:00
|
|
|
function isHTML(str: string): boolean {
|
2017-09-15 16:06:59 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-24 23:55:06 +01:00
|
|
|
//Generates a random alphanumeric string with N characters
|
2018-06-22 23:30:24 +02:00
|
|
|
function generateRandomString(n: number): string {
|
2018-02-24 23:55:06 +01:00
|
|
|
var str = "",
|
|
|
|
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
|
|
|
|
|
for (var i = 0; i < n; i++)
|
|
|
|
str += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-08-30 19:44:29 +02:00
|
|
|
export {getIndicesOf, 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};
|