diff --git a/lib/netmask.coffee b/lib/netmask.coffee index 1e77497..64370c8 100644 --- a/lib/netmask.coffee +++ b/lib/netmask.coffee @@ -157,6 +157,11 @@ class Netmask long++ return + slice: (start, end) -> + ips = [] + @forEach((ip) -> ips.push(ip)) + return ips.slice(start, end) + # Returns the complete netmask formatted as `base/bitmask` toString: -> return @base + "/" + @bitmask diff --git a/tests/netmask.js b/tests/netmask.js index 38b39bf..9978f6d 100644 --- a/tests/netmask.js +++ b/tests/netmask.js @@ -132,4 +132,20 @@ describe('Netmask', () => { }); }); }); + + describe('can return range of IPs from block', () => { + let block = new Netmask('10.1.2.0/24'); + + it('should be able to return the first N IPs', () => { + assert.deepEqual(block.slice(0, 3), ['10.1.2.1', '10.1.2.2', '10.1.2.3']); + }); + + it('should be able to return the last N IPs', () => { + assert.deepEqual(block.slice(block.size - 5, block.size - 1), ['10.1.2.252', '10.1.2.253', '10.1.2.254']); + }); + + it('should be able to return a range of IPs', () => { + assert.deepEqual(block.slice(2, 5), ['10.1.2.3', '10.1.2.4', '10.1.2.5']); + }); + }); }); \ No newline at end of file