Code Monkey home page Code Monkey logo

cidr-calc's Introduction

Overview

cidr-calc is a small javascript library for converting IP range to CIDRs or CIDR to IP range.

API

The API consists of three classes: IpAddress, Cidr and IpRange.

import { Cidr, IpAddress, IpRange } from 'cidr-calc';

IpAddress

An IpAddress class represents an IP address. Its instance functions are:

toByteArray: returns a copy of the byte array representing the IP address
next: return the next IP address
prev: return the previous IP address
compareTo: returns the result (-1, 0, or 1) that it compares to another IpAddress
           Only IpAddresses of the same version can be compared
toString: return the string representation of the IP address
version: returns a string '4' or '6'
regularNotation: returns a regular string representation of the IP address
                 e.g., 10.0.1.2, 2001:0:0:f:f00:0:0:1
shortNotation: returns a compact string representation of the IP address
               e.g., 10.0.1.2, 2001::f:f00:0:0:1
fullNotation: returns a full string representation of the IP address
              e.g., 010.000.001.002, 2001:0000:0000:000f:0f00:0000:0000:0001

It also has two static functions:

of: parses a string into an IpAddress
fromByteArray: constructs an IpAddress from a byte array

The implementation of the of function was inspired by the InetAddresses class in guava, Google core libraries for Java.

Cidr

A Cidr class can be constructed with an IP address and a prefix length.

let cidr = new Cidr(IpAddress.of('192.168.0.0'), 16);

Then we can convert the CIDR to an IP range with the toIpRange function.

let ipRange = cidr.toIpRange(); // 192.168.0.0 - 192.168.255.255

Note that the IP address passed in to construct the CIDR may not be the start IP of the range.

IpRange

An IpRange class can be constructed with a start IP address and an end IP address.

let ipRange = new IpRange(IpAddress.of('3::'), IpAddress.of('7:ffff:ffff:ffff:ffff:ffff:ffff:ffff'));

Then we can convert the IP range to a list of CIDRs (as a Cidr array) with the toCidrs function.

let cidrs = ipRange.toCidrs(); // [3::/16, 4::/14]

The implementation of the toCidrs function was inspired by the IPv4Range and IPv6Range classes in ineter, a fast Java library for working with IP addresses, ranges, and subnets.

cidr-calc's People

Contributors

dependabot[bot] avatar gdubin avatar leoedeng avatar liangbodeng avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.