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
|
|
|
|
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;
|
2016-12-21 17:33:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//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);
|
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"
|
|
|
|
function convertTimeMsToTimeElapsedString(time) {
|
|
|
|
//Convert ms to seconds, since we only have second-level precision
|
|
|
|
time = Math.floor(time / 1000);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-05-15 16:15:59 +02:00
|
|
|
var days = Math.floor(time / 86400);
|
|
|
|
time %= 86400;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-02-06 06:01:01 +01:00
|
|
|
var hours = Math.floor(time / 3600);
|
|
|
|
time %= 3600;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-02-06 06:01:01 +01:00
|
|
|
var minutes = Math.floor(time / 60);
|
|
|
|
time %= 60;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-02-06 06:01:01 +01:00
|
|
|
var seconds = time;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2017-05-15 16:15:59 +02:00
|
|
|
var 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
|
|
|
|
function longestCommonStart(strings) {
|
|
|
|
if (!containsAllStrings(strings)) {return;}
|
2017-05-02 18:28:54 +02:00
|
|
|
if (strings.length == 0) {return;}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
|
|
|
var A = strings.concat().sort(),
|
2017-05-01 23:38:49 +02:00
|
|
|
a1= A[0], a2= A[A.length-1], L= a1.length, i= 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
|
|
|
|
function isString(str) {
|
|
|
|
return (typeof str === 'string' || str instanceof String);
|
|
|
|
}
|
|
|
|
|
2017-05-20 10:19:13 +02:00
|
|
|
//Returns true if string contains only digits (meaning it would be a positive number)
|
|
|
|
function isPositiveNumber(str) {
|
|
|
|
return /^\d+$/.test(str);
|
|
|
|
}
|
|
|
|
|
2017-05-01 23:38:49 +02:00
|
|
|
//Returns whether an array contains entirely of string objects
|
|
|
|
function containsAllStrings(arr) {
|
|
|
|
return arr.every(isString);
|
2017-05-04 22:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Formats a number with commas and a specific number of decimal digits
|
|
|
|
function formatNumber(num, numFractionDigits) {
|
|
|
|
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
|
2017-05-10 23:10:06 +02:00
|
|
|
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;
|
2017-05-15 17:38:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//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;
|
|
|
|
}
|
2017-08-30 19:44:29 +02:00
|
|
|
|
|
|
|
export {getIndicesOf, convertTimeMsToTimeElapsedString, longestCommonStart,
|
|
|
|
isString, isPositiveNumber, containsAllStrings, formatNumber,
|
|
|
|
numOccurrences, numNetscriptOperators};
|