Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to get array (toArray) or traverse (forEach) of ip addresses from Range? #6

Open
nodkz opened this issue Mar 19, 2020 · 4 comments

Comments

@nodkz
Copy link

nodkz commented Mar 19, 2020

For example:

const myRange = new Ip.Range('10.0.0.1', '10.0.0.5');
myRange.toArray(); // ['10.0.0.1', '10.0.0.2', '10.0.0.3', '10.0.0.4', '10.0.0.5']
myRange.forEach((ip) => { console.log(ip) }); // 5 times output ip adresses to console
@vasturiano
Copy link
Owner

@nodkz you could achieve that by iterating through the IPs as numbers, for instance:

const myRange = new Ip.Range('10.0.0.1', '10.0.0.5');
for (let ip = myRange.firstIp().toNum(), lastIp = myRange.lastIp().toNum(); ip <= lastIp; ip++) {
  console.log(new Ip.Addr(ip).toString());
}

@nodkz
Copy link
Author

nodkz commented Mar 19, 2020

toArray & forEach helper methods will be very appreciated 🙏

In such case, we may forget about such packages like

@vasturiano
Copy link
Owner

vasturiano commented Mar 19, 2020

You can have it easily by using the iteration mentioned above:

function toArray(range) {
  const result = [];
  for (let ip = range.firstIp().toNum(), lastIp = range.lastIp().toNum(); ip <= lastIp; ip++) {
    result.push(new Ip.Addr(ip));
  }
  return result;
}

Because this is a rather specific use case, for the sake of keeping API simplicity and because it's relatively easy to transform externally, I'd rather not include it as an internal method.

@nodkz
Copy link
Author

nodkz commented Mar 19, 2020

I disagree with you. These methods are greatly simplifying work with ranges. And writing them manually is quite tedious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants