Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/netmask.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/netmask.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
});
});
});