pokerogue/src/utils.ts

99 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-03-28 11:54:52 -07:00
export function toPokemonUpperCase(input: string): string {
return input.replace(/([a-z]+)/g, s => s.toUpperCase());
}
export function clampInt(value: integer, min: integer, max: integer): integer {
return Math.min(Math.max(value, min), max);
}
export function randGauss(value: number): number {
let rand = 0;
for(var i = value; i > 0; i--)
rand += Math.random();
return rand / value;
}
export function padInt(value: integer, length: integer, padWith?: string): string {
if (!padWith)
padWith = '0';
let valueStr = value.toString();
while (valueStr.length < length)
valueStr = `${padWith}${valueStr}`;
return valueStr;
}
export function randInt(range: integer, min?: integer): integer {
if (!min)
min = 0;
if (range === 1)
return min;
2023-03-28 11:54:52 -07:00
return Math.floor(Math.random() * range) + min;
}
2023-06-04 18:47:43 -07:00
export function getFrameMs(frameCount: integer): integer {
return Math.floor((1 / 60) * 1000 * frameCount);
}
2023-03-28 11:54:52 -07:00
export function binToDec(input: string): integer {
let place:integer[] = [];
let binary:string[] = [];
let decimalNum = 0;
for (let i = 0; i < input.length; i++) {
binary.push(input[i]);
place.push(Math.pow(2, i));
decimalNum += place[i] * parseInt(binary[i]);
}
return decimalNum;
}
export function decToBin(input: integer): string {
let bin = '';
let intNum = input;
while (intNum > 0) {
bin = intNum % 2 ? `1${bin}` : `0${bin}`;
intNum = Math.floor(intNum * 0.5);
}
return bin;
}
2023-04-11 10:00:04 -07:00
export function getEnumKeys(enumType): string[] {
return Object.values(enumType).filter(v => isNaN(parseInt(v.toString()))).map(v => v.toString());
}
export function getEnumValues(enumType): integer[] {
2023-03-28 11:54:52 -07:00
return Object.values(enumType).filter(v => !isNaN(parseInt(v.toString()))).map(v => parseInt(v.toString()));
}
2023-04-12 16:09:15 -07:00
export class BooleanHolder {
public value: boolean;
constructor(value: boolean) {
this.value = value;
}
}
2023-03-30 20:02:35 -07:00
export class NumberHolder {
public value: number;
constructor(value: number) {
this.value = value;
}
}
2023-04-12 08:30:47 -07:00
2023-03-28 11:54:52 -07:00
export class IntegerHolder {
public value: integer;
constructor(value: integer) {
this.value = value;
}
2023-04-12 08:30:47 -07:00
}
export class FixedInt extends IntegerHolder {
constructor(value: integer) {
super(value);
}
2023-03-28 11:54:52 -07:00
}