-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
313 lines (300 loc) · 9.43 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
enum Unit {
"one" = 0,
"ten" = 1,
"hundred" = 2,
"thousand" = 3,
"million" = 6,
"billion" = 9,
"trillion" = 12,
"quadrillion" = 15,
"quintillion" = 18,
"sextillion" = 21,
"septillion" = 24,
"octillion" = 27,
"nonillion" = 30,
"decillion" = 33,
"undecillion" = 36,
"duodecillion" = 39,
"tredecillion" = 42,
"quattuordecillion" = 45,
"quindecillion" = 48,
"sexdecillion" = 51,
"septendecillion" = 54,
"octodecillion" = 57,
"novemdecillion" = 60,
"vigintillion" = 63,
"unvigintillion" = 66,
"duovigintillion" = 69,
"trevigintillion" = 72,
"quattuorvigintillion" = 75,
"quinvigintillion" = 78,
"sexvigintillion" = 81,
"septenvigintillion" = 84,
"octovigintillion" = 87,
"novemvigintillion" = 90,
"trigintillion" = 93,
"untrigintillion" = 96,
"duotrigintillion" = 99,
"tretrigintillion" = 102,
"quattuortrigintillion" = 105,
"quintrigintillion" = 108,
"sextrigintillion" = 111,
"septentrigintillion" = 114,
"octotrigintillion" = 117,
"novemtrigintillion" = 120,
"quadragintillion" = 123,
"unquadragintillion" = 126,
"duoquadragintillion" = 129,
"trequadragintillion" = 132,
"quattuorquadragintillion" = 135,
"quinquadragintillion" = 138,
"sexquadragintillion" = 141,
"septenquadragintillion" = 144,
"octoquadragintillion" = 147,
"novemquadragintillion" = 150,
"quinquagintillion" = 153,
"unquinquagintillion" = 156,
"duoquinquagintillion" = 159,
"trequinquagintillion" = 162,
"quattuorquinquagintillion" = 165,
"quinquinquagintillion" = 168,
"sexquinquagintillion" = 171,
"septenquinquagintillion" = 174,
"octoquinquagintillion" = 177,
"novemquinquagintillion" = 180,
"sexagintillion" = 183,
"unsexagintillion" = 186,
"duosexagintillion" = 189,
"tresexagintillion" = 192,
"quattuorsexagintillion" = 195,
"quinsexagintillion" = 198,
"sexsexagintillion" = 201,
"septsexagintillion" = 204,
"octosexagintillion" = 207,
"novemsexagintillion" = 210,
"septuagintillion" = 213,
"unseptuagintillion" = 216,
"duoseptuagintillion" = 219,
"treseptuagintillion" = 222,
"quattuorseptuagintillion" = 225,
"quinseptuagintillion" = 228,
"sexseptuagintillion" = 231,
"septseptuagintillion" = 234,
"octoseptuagintillion" = 237,
"novemseptuagintillion" = 240,
"octogintillion" = 243,
"unoctogintillion" = 246,
"duooctogintillion" = 249,
"treoctogintillion" = 252,
"quattuoroctogintillion" = 255,
"quinoctogintillion" = 258,
"sexoctogintillion" = 261,
"septoctogintillion" = 264,
"octooctogintillion" = 267,
"novemoctogintillion" = 270,
"nonagintillion" = 273,
"unnonagintillion" = 276,
"duononagintillion" = 279,
"trenonagintillion" = 282,
"quattuornonagintillion" = 285,
"quinnonagintillion" = 288,
"sexnonagintillion" = 291,
"septnonagintillion" = 294,
"octononagintillion" = 297,
"novemnonagintillion" = 300,
"centillion" = 303,
}
enum UnitAbbreviation {
"O" = Unit.one,
"T" = Unit.ten,
"H" = Unit.hundred,
"K" = Unit.thousand,
"M" = Unit.million,
"B" = Unit.billion,
"Tr" = Unit.trillion,
"Qd" = Unit.quadrillion,
"Qt" = Unit.quintillion,
"Sxt" = Unit.sextillion,
"Spt" = Unit.septillion,
"Ot" = Unit.octillion,
"Nn" = Unit.nonillion,
"Dc" = Unit.decillion,
"Ud" = Unit.undecillion,
"Dd" = Unit.duodecillion,
"Td" = Unit.tredecillion,
"Qtd" = Unit.quattuordecillion,
"Qnd" = Unit.quindecillion,
"Sxd" = Unit.sexdecillion,
"Std" = Unit.septendecillion,
"Od" = Unit.octodecillion,
"Nd" = Unit.novemdecillion,
"V" = Unit.vigintillion,
"Uv" = Unit.unvigintillion,
"Dv" = Unit.duovigintillion,
"Tv" = Unit.trevigintillion,
"Qtv" = Unit.quattuorvigintillion,
"Qnv" = Unit.quinvigintillion,
"Sxv" = Unit.sexvigintillion,
"Stv" = Unit.septenvigintillion,
"Ov" = Unit.octovigintillion,
"Nv" = Unit.novemvigintillion,
"Tg" = Unit.trigintillion,
"Utg" = Unit.untrigintillion,
"Dtg" = Unit.duotrigintillion,
"Ttg" = Unit.tretrigintillion,
"Qttg" = Unit.quattuortrigintillion,
"Qntg" = Unit.quintrigintillion,
"Sxtg" = Unit.sextrigintillion,
"Sttg" = Unit.septentrigintillion,
"Otg" = Unit.septentrigintillion,
"Ntg" = Unit.novemtrigintillion,
"Qdr" = Unit.quadragintillion,
"Uqdr" = Unit.unquadragintillion,
"Dqdr" = Unit.duoquadragintillion,
"Tqdr" = Unit.trequadragintillion,
"Qtqdr" = Unit.quattuorquadragintillion,
"Qnqdr" = Unit.quinquadragintillion,
"Sxqdr" = Unit.sexquadragintillion,
"Stqdr" = Unit.septenquadragintillion,
"Oqdr" = Unit.octoquadragintillion,
"Nqdr" = Unit.novemquadragintillion,
"Qng" = Unit.quinquagintillion,
"Uqng" = Unit.unquinquagintillion,
"Dqng" = Unit.duoquinquagintillion,
"Tqng" = Unit.trequinquagintillion,
"Qtqng" = Unit.quattuorquinquagintillion,
"Qnqng" = Unit.quinquinquagintillion,
"Sxqng" = Unit.sexquinquagintillion,
"Stqng" = Unit.septenquinquagintillion,
"Oqng" = Unit.octoquinquagintillion,
"Nqng" = Unit.novemquinquagintillion,
"Sxa" = Unit.sexagintillion,
"Usxa" = Unit.unsexagintillion,
"Dsxa" = Unit.duosexagintillion,
"Tsxa" = Unit.tresexagintillion,
"Qtsxa" = Unit.quattuorsexagintillion,
"Qnsxa" = Unit.quinsexagintillion,
"Sxsxa" = Unit.sexsexagintillion,
"Stsxa" = Unit.septsexagintillion,
"Osxa" = Unit.octosexagintillion,
"Nsxa" = Unit.novemsexagintillion,
"Spg" = Unit.septuagintillion,
"Uspg" = Unit.unseptuagintillion,
"Dspg" = Unit.duoseptuagintillion,
"Tspg" = Unit.treseptuagintillion,
"Qtspg" = Unit.quattuorseptuagintillion,
"Qnspg" = Unit.quinseptuagintillion,
"Sxspg" = Unit.sexseptuagintillion,
"Stspg" = Unit.septseptuagintillion,
"Ospg" = Unit.octoseptuagintillion,
"Nspg" = Unit.novemseptuagintillion,
"Og" = Unit.octogintillion,
"Uog" = Unit.unoctogintillion,
"Dog" = Unit.duooctogintillion,
"Tog" = Unit.treoctogintillion,
"Qtog" = Unit.quattuoroctogintillion,
"Qnog" = Unit.quinoctogintillion,
"Sxog" = Unit.sexoctogintillion,
"Stog" = Unit.septoctogintillion,
"Oog" = Unit.octooctogintillion,
"Nog" = Unit.novemoctogintillion,
"Ng" = Unit.nonagintillion,
"Ung" = Unit.unnonagintillion,
"Dng" = Unit.duononagintillion,
"Tng" = Unit.trenonagintillion,
"Qtng" = Unit.quattuornonagintillion,
"Qnng" = Unit.quinnonagintillion,
"Sxng" = Unit.sexnonagintillion,
"Stng" = Unit.septnonagintillion,
"Ong" = Unit.octononagintillion,
"Nng" = Unit.novemnonagintillion,
"C" = Unit.centillion
}
const safeNumber = (number: ShortScaleUnit) => typeof number === "bigint" ? number.toString() : number;
export type ShortScaleUnit = number | bigint | string;
/**
* Provides the English unit name for the given number.
* @param number The number you want to match a unit to.
* @returns The matched unit name.
*/
export function unitNameFromNumber(number: ShortScaleUnit):string {
const power: number = getUnitPower(number);
return Unit[power];
}
/**
* Provides the base unit number for the given unit name.
* @param name The unit name you want the base number of.
* @param raw Specify `true` if you want the **raw unit integer**. Otherwise large numbers will be in scientific notation `1e+X`.
* @returns The unit value as a `number`, or a `string` if `raw` was specified true.
*/
export function numberFromUnitName(
name:string,
raw?: boolean
): number | string {
const power: number = Unit[name.toLowerCase()];
const unitNumber = Math.pow(10, power);
const isScientific = new RegExp(/e\+\d+$/).test(unitNumber.toString());
if (raw && isScientific) {
let string = "1";
const split = unitNumber.toString().split("+");
const roundUp = new RegExp(/9\.9+e$/).test(split[0]);
const power = roundUp ? parseInt(split[1]) + 1 : parseInt(split[1]);
for(let i=0; i<power; i++) {
string += "0";
}
return string;
} else if (raw) {
return unitNumber.toString();
} else {
return unitNumber;
}
}
/**
* Provides an abbreviation of the short unit name;
* @param name The full unit name.
* @returns The abbreviated unit name.
*/
export function trimName(name:string): string {
const power = Unit[name];
return UnitAbbreviation[power]
}
/**
* Provides the short-hand abbreviation of a number, relative to its unit.
* @param number The number you want abbreviated to its unit value.
* @returns The shortend number.
*/
export function trimNumber(number: ShortScaleUnit): number {
const power = getPower(number);
const base = Math.round(Math.max(power, 1));
const value = power > 3
? Math.floor(power/3)*3
: Math.round(power/base)*base;
return Math.floor(safeNumber(number) as number / eval(`1e+${value}`));
}
/**
* Provides the power for the given number, relative to its unit.
* - If you want the real power see `getPower`.
* @param number The number for which you want the unit power.
* @returns The numbers unit power.
*/
export function getUnitPower(number: ShortScaleUnit): number {
const power: number = Math.fround(getPower(number));
const base = Math.floor(Math.max(power, 1));
const value = power > 3
? Math.floor(power/3)*3
: Math.floor(power/base)*base;
return value;
}
/**
* Provides the power for the given number.
* - If you want the power relative to its unit use `getUnitPower`.
* @param number The number for which you want the power.
* @param round A boolean specifing the power should be rounded.
* @returns The numbers power
*/
export function getPower(number: ShortScaleUnit, round?: boolean): number {
const target = Math.max(safeNumber(number) as number, 1);
return round
? Math.round(Math.log(target) / Math.log(10))
: Math.log(target) / Math.log(10);
}