Rewrite this. Now, the system return 19 prefix length everytime it can't find next…
- User
Rewrite this. Now, the system return 19 prefix length everytime it can't find next address in it. You should make it work as intended: As example, let's take a list of IPs: 85.140.3.0, 85.140.4.0, 85.140.5.0 and 213.87.1.0. At this moment it returns 85.140.0.0/19, 213.87.1.0/24, but it should return 85.140.0.0/21 and 213.87.1.0/24. The problem is that it decreases mask to find next address in it, but it never finds 213.87.1.0 in 85.140.0.0/19 and never returns it back to 85.140.0.0/21.
js// Function to merge addresses into a list of netmasks const mergeToNetmask = (addressList) => { const result = []; let current = addressList[0]; for (let i = 1; i < addressList.length; ++i) { const isLastAddress = i === addressList.length - 1; const next = addressList[i]; let netmask = 23; // Start netmask // Find the appropriate netmask based on the current and next address while (netmask > 19 && !isIPInNetmask(next, `${ current }/${ netmask }`)) --netmask; if (!isLastAddress && isIPInNetmask(next, `${ current }/${ netmask }`)) { addressList.splice(i, 1); --i; // Decrement index since we removed an item } else { result.push(toBaseCIDR(`${ current }/${ netmask }`)); current = next; // Move to the next address if (isLastAddress) result.push(`${ current }/24`); } } return result; }; - Assistant
Here's a revised version of your code, fixing the issues you mentioned regarding the merging of IP addresses. This implementation adjusts the algorithm so it correctly handles finding the appropriate prefix length: