2021-04-29 02:07:26 +02:00
|
|
|
import numeral from 'numeral';
|
2018-09-12 17:53:08 +02:00
|
|
|
import 'numeral/locales/bg';
|
|
|
|
import 'numeral/locales/cs';
|
|
|
|
import 'numeral/locales/da-dk';
|
|
|
|
import 'numeral/locales/de';
|
|
|
|
import 'numeral/locales/en-au';
|
|
|
|
import 'numeral/locales/en-gb';
|
|
|
|
import 'numeral/locales/es';
|
|
|
|
import 'numeral/locales/fr';
|
|
|
|
import 'numeral/locales/hu';
|
|
|
|
import 'numeral/locales/it';
|
|
|
|
import 'numeral/locales/lv';
|
|
|
|
import 'numeral/locales/no';
|
|
|
|
import 'numeral/locales/pl';
|
|
|
|
import 'numeral/locales/ru';
|
|
|
|
|
2018-09-12 18:19:59 +02:00
|
|
|
/* eslint-disable class-methods-use-this */
|
|
|
|
|
2021-06-02 22:07:52 +02:00
|
|
|
const extraFormats = [1e15, 1e18, 1e21, 1e24, 1e27, 1e30];
|
|
|
|
const extraNotations = ['q', 'Q', 's', 'S', 'o', 'n'];
|
|
|
|
|
2021-06-03 03:33:27 +02:00
|
|
|
|
2018-09-12 17:53:08 +02:00
|
|
|
class NumeralFormatter {
|
2018-12-09 14:36:18 +01:00
|
|
|
// Default Locale
|
2021-04-29 02:07:26 +02:00
|
|
|
defaultLocale = "en";
|
2018-12-09 14:36:18 +01:00
|
|
|
|
2018-09-12 18:19:59 +02:00
|
|
|
constructor() {
|
|
|
|
this.defaultLocale = 'en';
|
|
|
|
}
|
|
|
|
|
2018-12-09 14:36:18 +01:00
|
|
|
updateLocale(l: string): boolean {
|
2018-09-12 17:53:08 +02:00
|
|
|
if (numeral.locale(l) == null) {
|
|
|
|
console.warn(`Invalid locale for numeral: ${l}`);
|
|
|
|
|
2018-09-12 18:19:59 +02:00
|
|
|
numeral.locale(this.defaultLocale);
|
2018-09-12 17:53:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-12-09 14:36:18 +01:00
|
|
|
format(n: number, format: string): string {
|
2018-11-21 06:43:15 +01:00
|
|
|
// numeraljs doesnt properly format numbers that are too big or too small
|
|
|
|
if (Math.abs(n) < 1e-6) { n = 0; }
|
2021-03-20 04:08:41 +01:00
|
|
|
const answer = numeral(n).format(format);
|
|
|
|
if (answer === 'NaN') {
|
|
|
|
return `${n}`;
|
|
|
|
}
|
|
|
|
return answer;
|
2018-09-12 17:53:08 +02:00
|
|
|
}
|
2019-01-09 01:41:42 +01:00
|
|
|
|
2019-01-15 04:34:04 +01:00
|
|
|
formatBigNumber(n: number): string {
|
|
|
|
return this.format(n, "0.000a");
|
|
|
|
}
|
|
|
|
|
2021-06-06 21:06:39 +02:00
|
|
|
// TODO: leverage numeral.js to do it. This function also implies you can
|
|
|
|
// use this format in some text field but you can't. ( "1t" will parse but
|
|
|
|
// "1s" will not)
|
|
|
|
formatReallyBigNumber(n: number, decimalPlaces = 3): string {
|
|
|
|
if(n === Infinity) return "∞";
|
2021-06-03 03:33:27 +02:00
|
|
|
for(let i = 0; i < extraFormats.length; i++) {
|
|
|
|
if(extraFormats[i] < n && n <= extraFormats[i]*1000) {
|
|
|
|
return this.format(n/extraFormats[i], '0.'+'0'.repeat(decimalPlaces))+extraNotations[i];
|
2021-05-27 20:03:53 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-03 22:02:26 +02:00
|
|
|
if(Math.abs(n) < 1000) {
|
2021-06-03 03:33:27 +02:00
|
|
|
return this.format(n, '0.'+'0'.repeat(decimalPlaces));
|
2021-05-03 07:44:02 +02:00
|
|
|
}
|
2021-06-03 03:33:27 +02:00
|
|
|
const str = this.format(n, '0.'+'0'.repeat(decimalPlaces) + 'a');
|
|
|
|
if(str === "NaNt") return this.format(n, '0.' + ' '.repeat(decimalPlaces) + 'e+0');
|
2021-05-26 20:14:20 +02:00
|
|
|
return str;
|
2019-01-09 01:41:42 +01:00
|
|
|
}
|
2019-01-09 11:06:49 +01:00
|
|
|
|
2021-06-03 03:33:27 +02:00
|
|
|
formatHp(n: number): string {
|
2021-06-03 03:57:31 +02:00
|
|
|
if(n < 1e6){
|
|
|
|
return this.format(n, "0,0");
|
|
|
|
}
|
2021-06-06 21:06:39 +02:00
|
|
|
return this.formatReallyBigNumber(n);
|
2021-06-03 03:33:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
formatMoney(n: number): string {
|
2021-06-06 21:06:39 +02:00
|
|
|
return "$" + this.formatReallyBigNumber(n);
|
2021-06-03 03:33:27 +02:00
|
|
|
}
|
|
|
|
|
2021-03-31 06:45:21 +02:00
|
|
|
formatSkill(n: number): string {
|
2021-06-03 03:54:11 +02:00
|
|
|
if(n < 1e15){
|
|
|
|
return this.format(n, "0,0");
|
|
|
|
}
|
2021-06-06 21:06:39 +02:00
|
|
|
return this.formatReallyBigNumber(n);
|
2021-03-31 06:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
formatExp(n: number): string {
|
2021-06-06 21:06:39 +02:00
|
|
|
return this.formatReallyBigNumber(n);
|
2021-03-31 06:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
formatHashes(n: number): string {
|
2021-06-06 21:06:39 +02:00
|
|
|
return this.formatReallyBigNumber(n);
|
2021-03-31 06:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
formatReputation(n: number): string {
|
2021-06-06 21:06:39 +02:00
|
|
|
return this.formatReallyBigNumber(n);
|
2021-03-31 06:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
formatFavor(n: number): string {
|
|
|
|
return this.format(n, "0,0");
|
|
|
|
}
|
|
|
|
|
|
|
|
formatRAM(n: number): string {
|
|
|
|
return this.format(n, "0.00")+"GB";
|
|
|
|
}
|
|
|
|
|
2021-04-29 02:07:26 +02:00
|
|
|
formatPercentage(n: number, decimalPlaces = 2): string {
|
2019-01-15 04:34:04 +01:00
|
|
|
const formatter: string = "0." + "0".repeat(decimalPlaces) + "%";
|
|
|
|
return this.format(n, formatter);
|
2019-01-09 11:06:49 +01:00
|
|
|
}
|
2019-01-15 04:34:04 +01:00
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
formatServerSecurity(n: number): string {
|
2021-03-31 06:45:21 +02:00
|
|
|
return this.format(n, "0,0.000");
|
|
|
|
}
|
|
|
|
|
|
|
|
formatRespect(n: number): string {
|
2021-06-06 21:06:39 +02:00
|
|
|
return this.formatReallyBigNumber(n, 5);
|
2021-03-31 06:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
formatWanted(n: number): string {
|
2021-06-06 21:06:39 +02:00
|
|
|
return this.formatReallyBigNumber(n, 5);
|
2021-03-31 06:45:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
formatMultiplier(n: number): string {
|
|
|
|
return this.format(n, "0,0.00");
|
|
|
|
}
|
2019-01-15 04:34:04 +01:00
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
formatSleeveShock(n: number): string {
|
2021-03-31 06:45:21 +02:00
|
|
|
return this.format(n, "0,0.000");
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
formatSleeveSynchro(n: number): string {
|
2021-03-31 06:45:21 +02:00
|
|
|
return this.format(n, "0,0.000");
|
|
|
|
}
|
|
|
|
|
2021-05-01 09:17:31 +02:00
|
|
|
formatSleeveMemory(n: number): string {
|
2021-03-31 06:45:21 +02:00
|
|
|
return this.format(n, "0");
|
|
|
|
}
|
|
|
|
|
|
|
|
formatPopulation(n: number): string {
|
|
|
|
return this.format(n, "0.000a");
|
|
|
|
}
|
|
|
|
|
|
|
|
formatStamina(n: number): string {
|
|
|
|
return this.format(n, "0.0");
|
|
|
|
}
|
|
|
|
|
|
|
|
formatShares(n: number): string {
|
2021-06-03 03:41:58 +02:00
|
|
|
if (n < 1000) {
|
|
|
|
return this.format(n, "0");
|
|
|
|
}
|
2021-06-06 21:06:39 +02:00
|
|
|
return this.formatReallyBigNumber(n);
|
2021-06-03 03:33:27 +02:00
|
|
|
}
|
2021-04-29 02:07:26 +02:00
|
|
|
|
|
|
|
formatInfiltrationSecurity(n: number): string {
|
2021-06-06 21:06:39 +02:00
|
|
|
return this.formatReallyBigNumber(n);
|
2021-04-29 02:07:26 +02:00
|
|
|
}
|
2021-05-03 06:45:21 +02:00
|
|
|
|
|
|
|
formatThreads(n: number): string {
|
|
|
|
return this.format(n, "0,0");
|
|
|
|
}
|
2021-05-04 19:00:38 +02:00
|
|
|
|
2021-05-08 03:24:16 +02:00
|
|
|
parseMoney(s: string): number {
|
2021-05-04 19:00:38 +02:00
|
|
|
// numeral library does not handle formats like 1e10 well (returns 110),
|
|
|
|
// so if both return a valid number, return the biggest one
|
|
|
|
const numeralValue = numeral(s).value();
|
|
|
|
const parsed = parseFloat(s);
|
|
|
|
if (isNaN(parsed) && numeralValue === null) {
|
|
|
|
return NaN;
|
|
|
|
} else if (isNaN(parsed)) {
|
|
|
|
return numeralValue;
|
|
|
|
} else if (numeralValue === null) {
|
|
|
|
return parsed;
|
|
|
|
} else {
|
|
|
|
return Math.max(numeralValue, parsed);
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 17:53:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export const numeralWrapper = new NumeralFormatter();
|