Socket
Socket
Sign inDemoInstall

cidr-js

Package Overview
Dependencies
49
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cidr-js

Utility for expanding cidr blocks in a list of IPs


Version published
Weekly downloads
149
decreased by-27.67%
Maintainers
1
Install size
4.17 MB
Created
Weekly downloads
 

Readme

Source

cidr-js

Node module for for expanding cidr blocks in a list of IP (a-z) or just the range (start & end)

Install

npm install cidr-js

Example Usage

Get just the start and end IPs

var CIDR = require('cidr-js');
var cidr = new CIDR();

var block = '127.0.0.0/31';
var results = cidr.range(block);

// should return ['127.0.0.0', '127.0.0.1']

Get a list of ips in a given range

var CIDR = require('cidr-js');
var cidr = new CIDR();
var block = '127.0.0.0/30';
var results = cidr.list(block);

// should return [ '127.0.0.0', '127.0.0.1', '127.0.0.2', '127.0.0.3' ]

Filter a list of ips into contiguous blocks

var ips = [
    // contiguous block
    '127.0.0.0',
    '127.0.0.1',
    '127.0.0.2',
    '127.0.0.3',
    '127.0.0.4',
    '127.0.0.5',
    '127.0.0.6',

    // new contiguous block
    '127.0.1.1',
    '127.0.1.2',
    '127.0.1.3',

    // dangling block
    '127.0.1.5'
];

var CIDR = require('cidr-js');
var cidr = new CIDR();
var results = cidr.filter(ips);

// should return
{
  '12700': [
    '127.0.0.1',
    '127.0.0.2',
    '127.0.0.3',
    '127.0.0.4',
    '127.0.0.5',
    '127.0.0.6'
  ],
  '12701': [
    '127.0.1.1',
    '127.0.1.2',
    '127.0.1.3'
  ],
  '127015': [
    '127.0.1.5'
  ]
}

Get IP blocks from a list of IPs

var ips = [
    // contiguous block
    '127.0.0.0',
    '127.0.0.1',
    '127.0.0.2',
    '127.0.0.3',
    '127.0.0.4',
    '127.0.0.5',
    '127.0.0.6',

    // new contiguous block
    '127.0.1.1',
    '127.0.1.2',
    '127.0.1.3',

    // dangling block
    '127.0.1.5'
];
var CIDR = require('cidr-js');
var cidr = new CIDR();
var results = cidr.getBlocks(ips);

// should return
[
  '127.0.0.0/30',
  '127.0.0.4/31',
  '127.0.0.6/32',
  '127.0.1.1/32',
  '127.0.1.2/31',
  '127.0.1.5'
]

Testing

npm test

Keywords

FAQs

Last updated on 22 Nov 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc