mirror of
https://github.com/TheNetsky/Microsoft-Rewards-Script.git
synced 2026-01-20 15:03:58 +00:00
v3 init
Based of v3.0.0b10.
This commit is contained in:
81
src/util/Utils.ts
Normal file
81
src/util/Utils.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user