From 59e7188543e335583bdb2219cd28312a25ced663 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Wed, 24 Nov 2021 20:39:20 +0100 Subject: [PATCH] Changed most checks using Optional chaining and Nullish coalescing operator --- src/streamHandler/PacketStreamFactory.js | 29 ++++++++---------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/src/streamHandler/PacketStreamFactory.js b/src/streamHandler/PacketStreamFactory.js index 48e910f..32193b0 100644 --- a/src/streamHandler/PacketStreamFactory.js +++ b/src/streamHandler/PacketStreamFactory.js @@ -49,30 +49,19 @@ class PacketStreamFactory extends Transform{ // Convert time to epoch-micros Unfortunately luxon doesnt use micros, but millis as smallest time-unit requiring some "hacks" packet.timestampMicros = DateTime.fromISO(data.slice(0, 12)).toSeconds() + data.slice(12, 15)/1000000; - packet.dataRate = Number(data.match(/(^| )([0-9]+(\.[0-9]+)?) Mb\/s($| )/i)[2]); - packet.frequency = Number(data.match(/(^| )([0-9]{4}) MHz($| )/i)[2]); + packet.dataRate = Number(data.match(/(^| )([0-9]+(\.[0-9]+)?) Mb\/s($| )/i)?.[2]) || null; + packet.frequency = Number(data.match(/(^| )([0-9]{4}) MHz($| )/i)?.[2]) || null; - let signalStrMatch = data.match(/(^| )(-[0-9]{2})dBm Signal($| )/i); - if(signalStrMatch) packet.signal = Number(signalStrMatch[2]); - else packet.signal = -100; + packet.signal = Number(data.match(/(^| )(-[0-9]{2})dBm Signal($| )/i)?.[2]) || null; - let packetTypeStrMatch = data.match(new RegExp(`(^|.{80} )(${PACKET_TYPES_REGEX})($| )`, 'i')); - let packetTypeStr; - if(packetTypeStrMatch) { - packetTypeStr = packetTypeStrMatch[2]; - packet.packetType = PACKET_TYPE_MAP[packetTypeStr]; - } - else - packet.packetType = PacketType.Unknown; + let packetTypeStr = data.match(new RegExp(`(^|.{80} )(${PACKET_TYPES_REGEX})($| )`, 'i'))?.[2]; + packet.packetType = packetTypeStr? PACKET_TYPE_MAP[packetTypeStr]: PacketType.Unknown; - let srcMacMatch = data.match(/(^| )(SA|TA):(.{17})($| )/i); - if(srcMacMatch) packet.srcMac = srcMacMatch[3]; + packet.srcMac = data.match(/(^| )(SA|TA):(.{17})($| )/i)?.[3] ?? null; - let dstMacMatch = data.match(/(^| )(DA|RA):(.{17})($| )/i); - if(dstMacMatch) packet.dstMac = dstMacMatch[3]; + packet.dstMac = data.match(/(^| )(DA|RA):(.{17})($| )/i)?.[3] ?? null; - let bssidMatch = data.match(/(^| )BSSID:(.{17})($| )/i) - if(bssidMatch) packet.bssid = bssidMatch[2]; + packet.bssid = data.match(/(^| )BSSID:(.{17})($| )/i)?.[2] ?? null; // Cover special cases with more data switch(packet.packetType){ @@ -80,7 +69,7 @@ class PacketStreamFactory extends Transform{ case PacketType.ProbeRequest: case PacketType.ProbeResponse: packet = Object.assign(new PacketWithSSID(), packet); // Create new, more specific, packet and copy old data over - packet.ssid = data.match(new RegExp(`(^| )${packetTypeStr} `+'\\'+`((.{0,32})`+'\\'+`)($| )`, 'i'))[2]; + packet.ssid = data.match(new RegExp(`(^| )${packetTypeStr} `+'\\'+`((.{0,32})`+'\\'+`)($| )`, 'i'))?.[2] ?? null; break; } }