mirror of
https://github.com/TheNetsky/Microsoft-Rewards-Script.git
synced 2026-01-11 02:46:17 +00:00
82 lines
2.3 KiB
TypeScript
82 lines
2.3 KiB
TypeScript
import ms, { StringValue } from 'ms'
|
|
|
|
export default class Util {
|
|
async wait(time: number | string): Promise<void> {
|
|
if (typeof time === 'string') {
|
|
time = this.stringToNumber(time)
|
|
}
|
|
|
|
return new Promise<void>(resolve => {
|
|
setTimeout(resolve, time)
|
|
})
|
|
}
|
|
|
|
getFormattedDate(ms = Date.now()): string {
|
|
const today = new Date(ms)
|
|
const month = String(today.getMonth() + 1).padStart(2, '0') // January is 0
|
|
const day = String(today.getDate()).padStart(2, '0')
|
|
const year = today.getFullYear()
|
|
|
|
return `${month}/${day}/${year}`
|
|
}
|
|
|
|
shuffleArray<T>(array: T[]): T[] {
|
|
return array
|
|
.map(value => ({ value, sort: Math.random() }))
|
|
.sort((a, b) => a.sort - b.sort)
|
|
.map(({ value }) => value)
|
|
}
|
|
|
|
randomNumber(min: number, max: number): number {
|
|
return Math.floor(Math.random() * (max - min + 1)) + min
|
|
}
|
|
|
|
chunkArray<T>(arr: T[], numChunks: number): T[][] {
|
|
const chunkSize = Math.ceil(arr.length / numChunks)
|
|
const chunks: T[][] = []
|
|
|
|
for (let i = 0; i < arr.length; i += chunkSize) {
|
|
const chunk = arr.slice(i, i + chunkSize)
|
|
chunks.push(chunk)
|
|
}
|
|
|
|
return chunks
|
|
}
|
|
|
|
stringToNumber(input: string | number): number {
|
|
if (typeof input === 'number') {
|
|
return input
|
|
}
|
|
const value = input.trim()
|
|
|
|
const milisec = ms(value as StringValue)
|
|
|
|
if (milisec === undefined) {
|
|
throw new Error(
|
|
`The input provided (${input}) cannot be parsed to a valid time! Use a format like "1 min", "1m" or "1 minutes"`
|
|
)
|
|
}
|
|
|
|
return milisec
|
|
}
|
|
|
|
normalizeString(string: string): string {
|
|
return string
|
|
.normalize('NFD')
|
|
.trim()
|
|
.toLowerCase()
|
|
.replace(/[^\x20-\x7E]/g, '')
|
|
.replace(/[?!]/g, '')
|
|
}
|
|
|
|
getEmailUsername(email: string): string {
|
|
return email.split('@')[0] ?? 'Unknown'
|
|
}
|
|
|
|
randomDelay(min: string | number, max: string | number): number {
|
|
const minMs = typeof min === 'number' ? min : this.stringToNumber(min)
|
|
const maxMs = typeof max === 'number' ? max : this.stringToNumber(max)
|
|
return Math.floor(this.randomNumber(minMs, maxMs))
|
|
}
|
|
}
|