38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
|
import { MaybeDynamic, MtCuteError } from '../types'
|
||
|
import { BigInteger } from 'big-integer'
|
||
|
import { randomBytes } from '@mtcute/core/dist/utils/buffer-utils'
|
||
|
import { bufferToBigInt } from '@mtcute/core/dist/utils/bigint-utils'
|
||
|
import { tl } from '@mtcute/tl'
|
||
|
|
||
|
export const EMPTY_BUFFER = Buffer.alloc(0)
|
||
|
|
||
|
export function normalizePhoneNumber(phone: string): string {
|
||
|
phone = phone.trim().replace(/[+()\s-]/g, '')
|
||
|
if (!phone.match(/^\d+$/)) throw new MtCuteError('Invalid phone number')
|
||
|
|
||
|
return phone
|
||
|
}
|
||
|
|
||
|
export async function resolveMaybeDynamic<T>(val: MaybeDynamic<T>): Promise<T> {
|
||
|
return val instanceof Function ? await val() : await val
|
||
|
}
|
||
|
|
||
|
export function randomUlong(): BigInteger {
|
||
|
return bufferToBigInt(randomBytes(8))
|
||
|
}
|
||
|
|
||
|
export function extractChannelIdFromUpdate(
|
||
|
upd: tl.TypeUpdate
|
||
|
): number | undefined {
|
||
|
// holy shit
|
||
|
return 'channelId' in upd
|
||
|
? upd.channelId
|
||
|
: 'message' in upd &&
|
||
|
typeof upd.message !== 'string' &&
|
||
|
'peerId' in upd.message &&
|
||
|
upd.message.peerId &&
|
||
|
'channelId' in upd.message.peerId
|
||
|
? upd.message.peerId.channelId
|
||
|
: undefined
|
||
|
}
|