Skip to content

Commit

Permalink
Add isCallsignSSID.
Browse files Browse the repository at this point in the history
  • Loading branch information
brannondorsey committed Sep 20, 2018
1 parent 2a62a23 commit c871206
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
21 changes: 13 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ export function stationToCallsignSSID(station: Station): string {
`${station.callsign}-${station.ssid}` : station.callsign
}

export function callsignSSIDToStation(callsign: string): Station {
if (callsign.includes('-')) {
const parts = callsign.split('-')
if (parts.length !== 2) throw TypeError(`Invalid callsign + SSID combo: ${callsign}`)
const ssid = parseInt(parts[1])
if (ssid < 0 || ssid > 15) throw TypeError(`Invalid SSID in ${callsign}`)
return { callsign: parts[0], ssid }
export function callsignSSIDToStation(callsignSSID: string): Station {
if (isCallsignSSID(callsignSSID)) {
const [callsign, ssid] = callsignSSID.split('-')
return { callsign: callsign, ssid: parseInt(ssid) }
} else if(isCallsign(callsignSSID)) {
return { callsign: callsignSSID, ssid: 0 }
} else {
return { callsign, ssid: 0 }
throw TypeError(`Invalid callsign: ${callsignSSID}`)
}
}

Expand All @@ -34,4 +33,10 @@ export function isCallsign(callsign: string): boolean {
return false
}
return true
}

export function isCallsignSSID(callsignSSID: string): boolean {
if ((callsignSSID.match(/-/g) || []).length !== 1) return false
const [callsign, ssid] = callsignSSID.split('-')
return parseInt(ssid) >= 0 && parseInt(ssid) <= 15 && isCallsign(callsign)
}
6 changes: 3 additions & 3 deletions test/Packet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ describe('Packet', () => {

it('should construct an AX25 Packet with a short message and a signature', async () => {
const signature = ks.sign(shortMessage, private)
shortSignedAX25Packet = await Packet.ToAX25Packet('N0CALL', 'CQ', shortMessage, signature)
shortSignedAX25Packet = await Packet.ToAX25Packet('N0CALL-1', 'KC3LZO', shortMessage, signature)
})

it('should construct an AX25 Packet with a long message and no signature', async () => {
longUnsignedAX25Packet = await Packet.ToAX25Packet('N0CALL', 'CQ', longMessage)
longUnsignedAX25Packet = await Packet.ToAX25Packet('N0CALL', 'KC3LZO-2', longMessage)
})

it('should construct an AX25 Packet with a long message and a signature', async () => {
const signature = ks.sign(longMessage, private)
longSignedAX25Packet = await Packet.ToAX25Packet('N0CALL', 'CQ', longMessage, signature)
longSignedAX25Packet = await Packet.ToAX25Packet('N0CALL-3', 'KC3LZO-3', longMessage, signature)
})
})

Expand Down

0 comments on commit c871206

Please sign in to comment.