fakejson match subscription id.

This commit is contained in:
fiatjaf 2023-02-08 12:14:17 -03:00
parent 4485c8ed5e
commit b5c8255b2f
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1
2 changed files with 29 additions and 0 deletions

View File

@ -33,3 +33,17 @@ test('match kind', () => {
) )
).toBeTruthy() ).toBeTruthy()
}) })
test('match subscription id', () => {
expect(fj.getSubscriptionId('["EVENT","",{}]')).toEqual('')
expect(fj.getSubscriptionId('["EVENT","_",{}]')).toEqual('_')
expect(fj.getSubscriptionId('["EVENT","subname",{}]')).toEqual('subname')
expect(fj.getSubscriptionId('["EVENT", "kasjbdjkav", {}]')).toEqual(
'kasjbdjkav'
)
expect(
fj.getSubscriptionId(
' [ \n\n "EVENT" , \n\n "y4d5ow45gfwoiudfÇA VSADLKAN KLDASB[12312535]SFMZSNJKLH" , {}]'
)
).toEqual('y4d5ow45gfwoiudfÇA VSADLKAN KLDASB[12312535]SFMZSNJKLH')
})

View File

@ -13,6 +13,21 @@ export function getInt(json: string, field: string): number {
return parseInt(sliced.slice(0, end), 10) return parseInt(sliced.slice(0, end), 10)
} }
export function getSubscriptionId(json: string): string | null {
let idx = json.slice(0, 22).indexOf(`"EVENT"`)
if (idx === -1) return null
let pstart = json.slice(idx + 7 + 1).indexOf(`"`)
if (pstart === -1) return null
let start = idx + 7 + 1 + pstart
let pend = json.slice(start + 1, 80).indexOf(`"`)
if (pend === -1) return null
let end = start + 1 + pend
return json.slice(start + 1, end)
}
export function matchEventId(json: string, id: string): boolean { export function matchEventId(json: string, id: string): boolean {
return id === getHex64(json, 'id') return id === getHex64(json, 'id')
} }