bitburner-src/src/utils/StringHelperFunctions.ts

128 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-09-25 20:42:57 +02:00
import { EqualityFunc } from "../types";
import { isString } from "./helpers/isString";
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
2019-02-09 03:46:30 +01:00
e.g. 10000 -> "10 seconds"
120000 -> "2 minutes and 0 seconds"
2018-06-25 02:55:43 +02:00
*/
2021-09-09 05:47:34 +02:00
function convertTimeMsToTimeElapsedString(time: number, showMilli = false): string {
2021-09-05 01:09:30 +02:00
time = Math.floor(time);
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);
const days: number = Math.floor(totalSeconds / secondPerDay);
const secTruncDays: number = totalSeconds % secondPerDay;
const hours: number = Math.floor(secTruncDays / secondPerHours);
const secTruncHours: number = secTruncDays % secondPerHours;
const minutes: number = Math.floor(secTruncHours / secondPerMinute);
const secTruncMinutes: number = secTruncHours % secondPerMinute;
const milliTruncSec: string = (() => {
let str = `${time % millisecondsPerSecond}`;
while (str.length < 3) str = "0" + str;
return str;
})();
2021-09-09 05:47:34 +02:00
const seconds: string = showMilli ? `${secTruncMinutes}.${milliTruncSec}` : `${secTruncMinutes}`;
2021-09-05 01:09:30 +02:00
let res = "";
if (days > 0) {
res += `${days} days `;
}
if (hours > 0) {
res += `${hours} hours `;
}
if (minutes > 0) {
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 {
2021-09-05 01:09:30 +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;
2021-09-09 05:47:34 +02:00
const areEqualCaseInsensitive: EqualityFunc<string> = (a: string, b: string) => a.toUpperCase() === b.toUpperCase();
2021-09-05 01:09:30 +02:00
while (i < L && areEqualCaseInsensitive(a1.charAt(i), a2.charAt(i))) {
i++;
}
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 {
2021-09-05 01:09:30 +02:00
return arr.every(isString);
}
2018-06-25 02:55:43 +02:00
// Formats a number with commas and a specific number of decimal digits
2021-08-16 03:49:08 +02:00
function formatNumber(num: number, numFractionDigits = 0): string {
2021-09-05 01:09:30 +02:00
return num.toLocaleString(undefined, {
maximumFractionDigits: numFractionDigits,
minimumFractionDigits: numFractionDigits,
});
}
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 {
2021-09-05 01:09:30 +02:00
let str = "";
2021-09-09 05:47:34 +02:00
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
2021-09-05 01:09:30 +02:00
for (let i = 0; i < n; i++) {
str += chars.charAt(Math.floor(Math.random() * chars.length));
}
2021-09-05 01:09:30 +02:00
return str;
}
/**
* Hashes the input string. This is a fast hash, so NOT good for cryptography.
* This has been ripped off here: https://stackoverflow.com/a/52171480
* @param str The string that is to be hashed
* @param seed A seed to randomize the result
* @returns An hexadecimal string representation of the hashed input
*/
function cyrb53(str: string, seed = 0): string {
let h1 = 0xdeadbeef ^ seed;
let h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(16);
}
export {
convertTimeMsToTimeElapsedString,
longestCommonStart,
containsAllStrings,
formatNumber,
generateRandomString,
cyrb53,
};