feat: add "strictNullChecks"

This commit is contained in:
Adhiraj Singh
2022-07-08 10:38:25 +05:30
parent 7426b7aa2f
commit 40a1e268aa
42 changed files with 350 additions and 339 deletions

View File

@@ -11,18 +11,23 @@ export type JidWithDevice = {
device?: number
}
export type FullJid = JidWithDevice & {
server: JidServer | string
agent?: number
}
export const jidEncode = (user: string | number | null, server: JidServer, device?: number, agent?: number) => {
return `${user || ''}${!!agent ? `_${agent}` : ''}${!!device ? `:${device}` : ''}@${server}`
}
export const jidDecode = (jid: string) => {
export const jidDecode = (jid: string | undefined): FullJid | undefined => {
const sepIdx = typeof jid === 'string' ? jid.indexOf('@') : -1
if(sepIdx < 0) {
return undefined
}
const server = jid.slice(sepIdx + 1)
const userCombined = jid.slice(0, sepIdx)
const server = jid!.slice(sepIdx + 1)
const userCombined = jid!.slice(0, sepIdx)
const [userAgent, device] = userCombined.split(':')
const [user, agent] = userAgent.split('_')
@@ -36,7 +41,7 @@ export const jidDecode = (jid: string) => {
}
/** is the jid a user */
export const areJidsSameUser = (jid1: string, jid2: string) => (
export const areJidsSameUser = (jid1: string | undefined, jid2: string | undefined) => (
jidDecode(jid1)?.user === jidDecode(jid2)?.user
)
/** is the jid a user */
@@ -49,6 +54,6 @@ export const isJidGroup = (jid: string) => (jid?.endsWith('@g.us'))
export const isJidStatusBroadcast = (jid: string) => jid === 'status@broadcast'
export const jidNormalizedUser = (jid: string) => {
const { user, server } = jidDecode(jid)
const { user, server } = jidDecode(jid)!
return jidEncode(user, server === 'c.us' ? 's.whatsapp.net' : server as JidServer)
}