diff --git a/src/index.js b/src/index.js index 91a1153..88a483a 100644 --- a/src/index.js +++ b/src/index.js @@ -453,7 +453,7 @@ if (ips.length == 0) { } // Server IP address -var host = ips[(ips.length) - 1]; +var host = ips[ips.length - 1]; if (!host) host = "[offline]"; // TODO: Public IP address-related diff --git a/src/utils/ipSubnetUtils.js b/src/utils/ipSubnetUtils.js index 8c11eeb..e2aeeeb 100644 --- a/src/utils/ipSubnetUtils.js +++ b/src/utils/ipSubnetUtils.js @@ -1,7 +1,9 @@ function calculateBroadcastIPv4FromCidr(ipWithCidr) { // Check if CIDR notation is valid, if it's not, return null if (!ipWithCidr) return null; - const ipCA = ipWithCidr.match(/^((?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)\.?\b){4})\/([0-2][0-9]|3[0-2]|[0-9])$/); + const ipCA = ipWithCidr.match( + /^((?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)\.?\b){4})\/([0-2][0-9]|3[0-2]|[0-9])$/, + ); if (!ipCA) return null; // Extract IP and mask (numeric format) @@ -24,7 +26,9 @@ function calculateBroadcastIPv4FromCidr(ipWithCidr) { function calculateNetworkIPv4FromCidr(ipWithCidr) { // Check if CIDR notation is valid, if it's not, return null if (!ipWithCidr) return null; - const ipCA = ipWithCidr.match(/^((?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)\.?\b){4})\/([0-2][0-9]|3[0-2]|[0-9])$/); + const ipCA = ipWithCidr.match( + /^((?:(?:25[0-5]|(?:2[0-4]|1\d|[1-9]|)\d)\.?\b){4})\/([0-2][0-9]|3[0-2]|[0-9])$/, + ); if (!ipCA) return null; // Extract IP and mask (numeric format) diff --git a/tests/utils/ipSubnetUtils.test.js b/tests/utils/ipSubnetUtils.test.js index a92cc29..340c8c9 100644 --- a/tests/utils/ipSubnetUtils.test.js +++ b/tests/utils/ipSubnetUtils.test.js @@ -1,49 +1,84 @@ const { - calculateBroadcastIPv4FromCidr, - calculateNetworkIPv4FromCidr, - } = require('../../src/utils/ipSubnetUtils'); - - describe('IPv4 subnet utilties', () => { - describe('calculateBroadcastIPv4FromCidr', () => { - test('should return the broadcast address for a given CIDR', () => { - expect(calculateBroadcastIPv4FromCidr('192.168.1.0/24')).toBe('192.168.1.255'); - expect(calculateBroadcastIPv4FromCidr('192.168.1.0/25')).toBe('192.168.1.127'); - expect(calculateBroadcastIPv4FromCidr('192.168.1.0/26')).toBe('192.168.1.63'); - expect(calculateBroadcastIPv4FromCidr('192.168.1.0/27')).toBe('192.168.1.31'); - expect(calculateBroadcastIPv4FromCidr('192.168.1.0/28')).toBe('192.168.1.15'); - expect(calculateBroadcastIPv4FromCidr('192.168.1.0/29')).toBe('192.168.1.7'); - expect(calculateBroadcastIPv4FromCidr('192.168.1.0/30')).toBe('192.168.1.3'); - expect(calculateBroadcastIPv4FromCidr('192.168.1.0/31')).toBe('192.168.1.1'); - expect(calculateBroadcastIPv4FromCidr('192.168.1.0/32')).toBe('192.168.1.0'); - }); - - test('should return null for invalid CIDR notation', () => { - expect(calculateBroadcastIPv4FromCidr(null)).toBe(null); - expect(calculateBroadcastIPv4FromCidr('192.168.1.0')).toBe(null); - expect(calculateBroadcastIPv4FromCidr('192.168.1.0/')).toBe(null); - expect(calculateBroadcastIPv4FromCidr('192.168.1.0/abc')).toBe(null); - }); + calculateBroadcastIPv4FromCidr, + calculateNetworkIPv4FromCidr, +} = require("../../src/utils/ipSubnetUtils"); + +describe("IPv4 subnet utilties", () => { + describe("calculateBroadcastIPv4FromCidr", () => { + test("should return the broadcast address for a given CIDR", () => { + expect(calculateBroadcastIPv4FromCidr("192.168.1.0/24")).toBe( + "192.168.1.255", + ); + expect(calculateBroadcastIPv4FromCidr("192.168.1.0/25")).toBe( + "192.168.1.127", + ); + expect(calculateBroadcastIPv4FromCidr("192.168.1.0/26")).toBe( + "192.168.1.63", + ); + expect(calculateBroadcastIPv4FromCidr("192.168.1.0/27")).toBe( + "192.168.1.31", + ); + expect(calculateBroadcastIPv4FromCidr("192.168.1.0/28")).toBe( + "192.168.1.15", + ); + expect(calculateBroadcastIPv4FromCidr("192.168.1.0/29")).toBe( + "192.168.1.7", + ); + expect(calculateBroadcastIPv4FromCidr("192.168.1.0/30")).toBe( + "192.168.1.3", + ); + expect(calculateBroadcastIPv4FromCidr("192.168.1.0/31")).toBe( + "192.168.1.1", + ); + expect(calculateBroadcastIPv4FromCidr("192.168.1.0/32")).toBe( + "192.168.1.0", + ); }); - - describe('calculateNetworkIPv4FromCidr', () => { - test('should return the network address for a given CIDR', () => { - expect(calculateNetworkIPv4FromCidr('192.168.1.0/24')).toBe('192.168.1.0'); - expect(calculateNetworkIPv4FromCidr('192.168.1.0/25')).toBe('192.168.1.0'); - expect(calculateNetworkIPv4FromCidr('192.168.1.0/26')).toBe('192.168.1.0'); - expect(calculateNetworkIPv4FromCidr('192.168.1.0/27')).toBe('192.168.1.0'); - expect(calculateNetworkIPv4FromCidr('192.168.1.0/28')).toBe('192.168.1.0'); - expect(calculateNetworkIPv4FromCidr('192.168.1.0/29')).toBe('192.168.1.0'); - expect(calculateNetworkIPv4FromCidr('192.168.1.0/30')).toBe('192.168.1.0'); - expect(calculateNetworkIPv4FromCidr('192.168.1.0/31')).toBe('192.168.1.0'); - expect(calculateNetworkIPv4FromCidr('192.168.1.0/32')).toBe('192.168.1.0'); - }); - - test('should return null for invalid CIDR notation', () => { - expect(calculateNetworkIPv4FromCidr(null)).toBe(null); - expect(calculateNetworkIPv4FromCidr('192.168.1.0')).toBe(null); - expect(calculateNetworkIPv4FromCidr('192.168.1.0/')).toBe(null); - expect(calculateNetworkIPv4FromCidr('192.168.1.0/abc')).toBe(null); - }); + + test("should return null for invalid CIDR notation", () => { + expect(calculateBroadcastIPv4FromCidr(null)).toBe(null); + expect(calculateBroadcastIPv4FromCidr("192.168.1.0")).toBe(null); + expect(calculateBroadcastIPv4FromCidr("192.168.1.0/")).toBe(null); + expect(calculateBroadcastIPv4FromCidr("192.168.1.0/abc")).toBe(null); }); }); - \ No newline at end of file + + describe("calculateNetworkIPv4FromCidr", () => { + test("should return the network address for a given CIDR", () => { + expect(calculateNetworkIPv4FromCidr("192.168.1.0/24")).toBe( + "192.168.1.0", + ); + expect(calculateNetworkIPv4FromCidr("192.168.1.0/25")).toBe( + "192.168.1.0", + ); + expect(calculateNetworkIPv4FromCidr("192.168.1.0/26")).toBe( + "192.168.1.0", + ); + expect(calculateNetworkIPv4FromCidr("192.168.1.0/27")).toBe( + "192.168.1.0", + ); + expect(calculateNetworkIPv4FromCidr("192.168.1.0/28")).toBe( + "192.168.1.0", + ); + expect(calculateNetworkIPv4FromCidr("192.168.1.0/29")).toBe( + "192.168.1.0", + ); + expect(calculateNetworkIPv4FromCidr("192.168.1.0/30")).toBe( + "192.168.1.0", + ); + expect(calculateNetworkIPv4FromCidr("192.168.1.0/31")).toBe( + "192.168.1.0", + ); + expect(calculateNetworkIPv4FromCidr("192.168.1.0/32")).toBe( + "192.168.1.0", + ); + }); + + test("should return null for invalid CIDR notation", () => { + expect(calculateNetworkIPv4FromCidr(null)).toBe(null); + expect(calculateNetworkIPv4FromCidr("192.168.1.0")).toBe(null); + expect(calculateNetworkIPv4FromCidr("192.168.1.0/")).toBe(null); + expect(calculateNetworkIPv4FromCidr("192.168.1.0/abc")).toBe(null); + }); + }); +});