2018-06-27 03:40:26 +02:00
|
|
|
import { EqualityFunc } from "../src/types";
|
2018-07-04 06:33:55 +02:00
|
|
|
import { isString } from "./helpers/isString";
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-06-25 02:55:43 +02:00
|
|
|
// Netburner String helper functions
|
2016-12-15 23:22:42 +01:00
|
|
|
|
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-27 03:40:26 +02:00
|
|
|
const millisecondsPerSecond: number = 1000;
|
|
|
|
const secondPerMinute: number = 60;
|
|
|
|
const minutesPerHours: number = 60;
|
2018-06-25 02:55:43 +02:00
|
|
|
const secondPerHours: number = secondPerMinute * minutesPerHours;
|
2018-06-27 03:40:26 +02:00
|
|
|
const hoursPerDays: number = 24;
|
2018-06-25 02:55:43 +02:00
|
|
|
const secondPerDay: number = secondPerHours * hoursPerDays;
|
|
|
|
|
|
|
|
// Convert ms to seconds, since we only have second-level precision
|
|
|
|
const totalSeconds: number = Math.floor(time / millisecondsPerSecond);
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-06-25 02:55:43 +02:00
|
|
|
const days: number = Math.floor(totalSeconds / secondPerDay);
|
|
|
|
const secTruncDays: number = totalSeconds % secondPerDay;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-06-25 02:55:43 +02:00
|
|
|
const hours: number = Math.floor(secTruncDays / secondPerHours);
|
|
|
|
const secTruncHours: number = secTruncDays % secondPerHours;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-06-25 02:55:43 +02:00
|
|
|
const minutes: number = Math.floor(secTruncHours / secondPerMinute);
|
|
|
|
const secTruncMinutes: number = secTruncHours % secondPerMinute;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-06-25 02:55:43 +02:00
|
|
|
const seconds: number = secTruncMinutes;
|
2017-08-30 19:44:29 +02:00
|
|
|
|
2018-06-27 03:40:26 +02:00
|
|
|
let res: string = "";
|
|
|
|
if (days > 0) {res += `${days} days `; }
|
|
|
|
if (hours > 0) {res += `${hours} hours `; }
|
|
|
|
if (minutes > 0) {res += `${minutes} minutes `; }
|
2018-06-25 02:55:43 +02:00
|
|
|
res += `${seconds} seconds `;
|
|
|
|
|
2017-05-15 16:15:59 +02:00
|
|
|
return res;
|
2017-05-01 23:38:49 +02:00
|
|
|
}
|
|
|
|
|
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 ""; }
|
2018-06-27 03:40:26 +02:00
|
|
|
if (strings.length === 0) {return ""; }
|
2018-06-25 02:55:43 +02:00
|
|
|
|
2018-07-04 05:50:25 +02:00
|
|
|
const A: string[] = strings.concat()
|
|
|
|
.sort();
|
2018-06-25 02:55:43 +02:00
|
|
|
const a1: string = A[0];
|
|
|
|
const a2: string = A[A.length - 1];
|
|
|
|
const L: number = a1.length;
|
2018-06-27 03:40:26 +02:00
|
|
|
let i: number = 0;
|
|
|
|
const areEqualCaseInsensitive: EqualityFunc<string> = (a: string, b: string) => a.toUpperCase() === b.toUpperCase();
|
2018-06-29 07:39:05 +02:00
|
|
|
while (i < L && areEqualCaseInsensitive(a1.charAt(i), a2.charAt(i))) {
|
2018-06-27 03:40:26 +02:00
|
|
|
i++;
|
|
|
|
}
|
2018-06-25 02:55:43 +02:00
|
|
|
|
2017-05-01 23:38:49 +02:00
|
|
|
return a1.substring(0, i);
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-05-01 23:38:49 +02:00
|
|
|
return arr.every(isString);
|
2017-05-04 22:50:17 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2017-05-04 22:50:17 +02:00
|
|
|
return num.toLocaleString(undefined, {
|
2018-06-25 02:55:43 +02:00
|
|
|
maximumFractionDigits: numFractionDigits,
|
2017-05-04 22:50:17 +02:00
|
|
|
minimumFractionDigits: numFractionDigits,
|
|
|
|
});
|
2017-05-10 23:10:06 +02:00
|
|
|
}
|
|
|
|
|
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-27 03:40:26 +02:00
|
|
|
const element: HTMLDivElement = document.createElement("div");
|
|
|
|
element.innerHTML = str;
|
|
|
|
const c: NodeListOf<Node & ChildNode> = element.childNodes;
|
2018-07-04 05:50:25 +02:00
|
|
|
for (let i: number = c.length - 1; i >= 0; i--) {
|
2018-06-27 03:40:26 +02:00
|
|
|
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-27 03:40:26 +02:00
|
|
|
let str: string = "";
|
|
|
|
const chars: string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
2018-02-24 23:55:06 +01:00
|
|
|
|
2018-06-27 03:40:26 +02:00
|
|
|
for (let i: number = 0; i < n; i++) {
|
2018-02-24 23:55:06 +01:00
|
|
|
str += chars.charAt(Math.floor(Math.random() * chars.length));
|
2018-06-25 02:55:43 +02:00
|
|
|
}
|
2018-02-24 23:55:06 +01:00
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-06-25 02:55:43 +02:00
|
|
|
export {convertTimeMsToTimeElapsedString, longestCommonStart,
|
2018-07-04 06:33:55 +02:00
|
|
|
containsAllStrings, formatNumber,
|
2018-07-04 07:00:24 +02:00
|
|
|
isHTML, generateRandomString, replaceAt};
|