linting stuff

This commit is contained in:
Olivier Gagnon 2018-06-24 20:55:43 -04:00
parent 53ddfbc9ec
commit 1c9e6f5c26

@ -1,114 +1,103 @@
import {dialogBoxCreate} from "./DialogBox.js"; import { dialogBoxCreate } from "./DialogBox.js";
//Netburner String helper functions // Netburner String helper functions
//Searches for every occurence of searchStr within str and returns an array of the indices of // Replaces the character at an index with a new character
//all these occurences
function getIndicesOf(searchStr: string, str: string, caseSensitive: boolean): number[] {
let searchStrLen: number = searchStr.length;
if (searchStrLen == 0) {
return [];
}
let startIndex: number = 0;
let index: number = 0;
let indices: number[] = [];
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
function replaceAt(base: string, index: number, character: string): string { function replaceAt(base: string, index: number, character: string): string {
return base.substr(0, index) + character + base.substr(index+character.length); 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 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" e.g. 10000 -> "0 hours 0 minutes and 10 seconds"
// 120000 -> "0 0 hours 2 minutes and 0 seconds" 120000 -> "0 0 hours 2 minutes and 0 seconds"
*/
function convertTimeMsToTimeElapsedString(time: number): string { function convertTimeMsToTimeElapsedString(time: number): string {
//Convert ms to seconds, since we only have second-level precision const millisecondsPerSecond = 1000;
time = Math.floor(time / 1000); const secondPerMinute = 60;
const minutesPerHours = 60;
const secondPerHours: number = secondPerMinute * minutesPerHours;
const hoursPerDays = 24;
const secondPerDay: number = secondPerHours * hoursPerDays;
let days = Math.floor(time / 86400); // Convert ms to seconds, since we only have second-level precision
time %= 86400; const totalSeconds: number = Math.floor(time / millisecondsPerSecond);
let hours = Math.floor(time / 3600); const days: number = Math.floor(totalSeconds / secondPerDay);
time %= 3600; const secTruncDays: number = totalSeconds % secondPerDay;
let minutes = Math.floor(time / 60); const hours: number = Math.floor(secTruncDays / secondPerHours);
time %= 60; const secTruncHours: number = secTruncDays % secondPerHours;
let seconds: number = time; const minutes: number = Math.floor(secTruncHours / secondPerMinute);
const secTruncMinutes: number = secTruncHours % secondPerMinute;
const seconds: number = secTruncMinutes;
let res = ""; let res = "";
if (days) {res += days + " days ";} if (days) {res += `${days} days `; }
if (hours) {res += hours + " hours ";} if (hours) {res += `${hours} hours `; }
if (minutes) {res += minutes + " minutes ";} if (minutes) {res += `${minutes} minutes `; }
res += seconds + " seconds "; res += `${seconds} seconds `;
return res; return res;
} }
//Finds the longest common starting substring in a set of strings // Finds the longest common starting substring in a set of strings
function longestCommonStart(strings: string[]): string { function longestCommonStart(strings: string[]): string {
if (!containsAllStrings(strings)) {return "";} if (!containsAllStrings(strings)) {return ""; }
if (strings.length == 0) {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++; }
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<L && a1.charAt(i).toLowerCase() === a2.charAt(i).toLowerCase()) i++;
return a1.substring(0, i); return a1.substring(0, i);
} }
// Returns whether a variable is a string
//Returns whether a variable is a string
function isString(str: any): boolean { function isString(str: any): boolean {
return (typeof str === 'string' || str instanceof String); return (typeof str === "string" || str instanceof String);
} }
//Returns whether an array contains entirely of string objects // Returns whether an array contains entirely of string objects
function containsAllStrings(arr: string[]): boolean { function containsAllStrings(arr: string[]): boolean {
return arr.every(isString); return arr.every(isString);
} }
//Formats a number with commas and a specific number of decimal digits // Formats a number with commas and a specific number of decimal digits
function formatNumber(num: number, numFractionDigits: number): string { function formatNumber(num: number, numFractionDigits: number): string {
return num.toLocaleString(undefined, { return num.toLocaleString(undefined, {
maximumFractionDigits: numFractionDigits,
minimumFractionDigits: numFractionDigits, minimumFractionDigits: numFractionDigits,
maximumFractionDigits: numFractionDigits
}); });
} }
//Count the number of times a substring occurs in a string // Count the number of times a substring occurs in a string
function numOccurrences(text: string, subString: string): number { function numOccurrences(text: string, subString: string): number {
text += ""; text += "";
subString += ""; subString += "";
if (subString.length <= 0) return (text.length + 1); if (subString.length <= 0) { return (text.length + 1); }
let n: number = 0; let n = 0;
let pos: number = 0; let pos = 0;
let step: number = subString.length; const step: number = subString.length;
while (true) { while (true) {
pos = text.indexOf(subString, pos); pos = text.indexOf(subString, pos);
if (pos >= 0) { if (pos >= 0) {
++n; ++n;
pos += step; pos += step;
} else break; } else { break; }
} }
return n; return n;
} }
//Counters the number of Netscript operators in a string // Counters the number of Netscript operators in a string
function numNetscriptOperators(text: string): number { function numNetscriptOperators(text: string): number {
const total: number = numOccurrences(text, "+") + const total: number = numOccurrences(text, "+") +
numOccurrences(text, "-") + numOccurrences(text, "-") +
@ -124,33 +113,39 @@ function numNetscriptOperators(text: string): number {
numOccurrences(text, "==") + numOccurrences(text, "==") +
numOccurrences(text, "!="); numOccurrences(text, "!=");
if (isNaN(total)) { if (isNaN(total)) {
dialogBoxCreate("ERROR in counting number of operators in script. This is a bug, please report to game developer", false); 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 0;
} }
return total; return total;
} }
//Checks if a string contains HTML elements // Checks if a string contains HTML elements
function isHTML(str: string): boolean { function isHTML(str: string): boolean {
var a = document.createElement('div'); const a = document.createElement("div");
a.innerHTML = str; a.innerHTML = str;
for (var c = a.childNodes, i = c.length; i--; ) { const c = a.childNodes;
if (c[i].nodeType == 1) return true; for (let i = c.length; i--;) {
if (c[i].nodeType == 1) { return true; }
} }
return false; return false;
} }
//Generates a random alphanumeric string with N characters // Generates a random alphanumeric string with N characters
function generateRandomString(n: number): string { function generateRandomString(n: number): string {
var str = "", let str = "";
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < n; i++) for (let i = 0; i < n; i++) {
str += chars.charAt(Math.floor(Math.random() * chars.length)); str += chars.charAt(Math.floor(Math.random() * chars.length));
}
return str; return str;
} }
export {getIndicesOf, convertTimeMsToTimeElapsedString, longestCommonStart, export {convertTimeMsToTimeElapsedString, longestCommonStart,
isString, containsAllStrings, formatNumber, isString, containsAllStrings, formatNumber,
numOccurrences, numNetscriptOperators, isHTML, generateRandomString, replaceAt}; numOccurrences, numNetscriptOperators, isHTML, generateRandomString, replaceAt};