Guest User

Untitled

a guest
Nov 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. function getLowestIpRanges(ips) {
  2. let ipRanges = [];
  3.  
  4. ips.forEach(x => {
  5. let x3 = x.replace(/\d+$/, '');
  6. let x2 = x3.replace(/\d+\.?$/, '');
  7. let x1 = x2.replace(/\d+\.?$/, '');
  8.  
  9. let ex = ipRanges.filter(y => y == x3 + '0' || y == x2 + '0.0' || y == x1 + '0.0.0');
  10. if (ex.length) return;
  11.  
  12. ex = ipRanges.filter(y => y.startsWith(x2));
  13. if (ex.length) {
  14. ipRanges.splice(ipRanges.indexOf(x[0]), 1)
  15. ipRanges.push(x2 + '0.0')
  16. } else {
  17. ex = ipRanges.filter(y => y.startsWith(x1));
  18. if (ex.length) {
  19. ipRanges.splice(ipRanges.indexOf(x[0]), 1)
  20. ipRanges.push(x1 + '0.0.0')
  21. } else {
  22. ipRanges.push(x3 + '0')
  23. }
  24. }
  25. });
  26.  
  27. ipRanges = ipRanges.map(x => x.endsWith('.0.0.0') ? (x + '/8') : (x.endsWith('.0.0') ? (x + '/16') : (x + '/24')));
  28.  
  29. return ipRanges;
  30. }
Add Comment
Please, Sign In to add comment