npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@supercat1337/ip-tools

v1.0.0

Published

IP Tools

Downloads

76

Readme

IP Tools

A collection of JavaScript functions for working with IP addresses, IP ranges, and CIDR notations.

Installation

$ npm install @supercat1337/ip-tools

Functions

convertCidrToRange(cidr)

  • cidr: The CIDR notation, e.g. "192.168.1.0/24"
  • Returns: An array of two IP addresses representing the range of addresses that the CIDR notation covers, e.g. ["192.168.1.0", "192.168.1.255"]

Example:

import { convertCidrToRange } from "@supercat1337/ip-tools";
const cidr = '192.168.1.0/24';
const range = convertCidrToRange(cidr);
console.log(range); // Output: ["192.168.1.0", "192.168.1.255"]

getIpFromRangeByIndex(index)

  • index: The index of the IP address in the range
  • Returns: The IP address at the given index in the range

Example:

import { getIpFromRangeByIndex } from "@supercat1337/ip-tools";
const cidr = '192.168.1.0/24';
const range = convertCidrToRange(cidr);
const ip = getIpFromRangeByIndex(range, 0);
console.log(ip); // Output: "192.168.1.0"

getIpRangeByCidr(cidr)

  • cidr: The CIDR notation, e.g. "192.168.1.0/24"
  • Returns: An array of two IP addresses representing the range of addresses that the CIDR notation covers, e.g. ["192.168.1.0", "192.168.1.255"]

Example:

import { getIpRangeByCidr } from "@supercat1337/ip-tools";
const cidr = '192.168.1.0/24';
const range = getIpRangeByCidr(cidr);
console.log(range); // Output: ["192.168.1.0", "192.168.1.255"]

getSubRange(start_ip, end_ip, chunk_size=1000)

  • start_ip: The start IP address, e.g. "192.168.1.0"
  • end_ip: The end IP address, e.g. "192.168.1.255"
  • chunk_size: The number of IP addresses to include in each sub-range, defaults to 1000
  • Returns: An array of two IP addresses representing the sub-range of addresses, e.g. ["192.168.1.0", "192.168.1.3"]

Example:

import { getSubRange } from "@supercat1337/ip-tools";
const start_ip = '192.168.1.0';
const end_ip = '192.168.1.3';
const subRange = getSubRange(start_ip, end_ip);
console.log(subRange); // Output: ["192.168.1.0", "192.168.1.3"]

getTotalIPs(ip1, ip2)

  • ip1: The start IP address, e.g. "192.168.1.0"
  • ip2: The end IP address, e.g. "192.168.1.255"
  • Returns: The total number of IP addresses in the given range

Example:

import { getTotalIPs } from "@supercat1337/ip-tools";
const ip1 = '192.168.1.0';
const ip2 = '192.168.1.255';
const totalIPs = getTotalIPs(ip1, ip2);
console.log(totalIPs); // Output: 256

ipFromLong(ipInt)

  • ipInt: The IP address as an integer
  • Returns: The IP address in string format, e.g. "192.168.1.1"

Example:

import { ipFromLong } from "@supercat1337/ip-tools";
const ipInt = 16909060;
const ip = ipFromLong(ipInt);
console.log(ip); // Output: "192.168.1.1"

ipToLong(ip)

  • ip: The IP address, e.g. "192.168.1.1"
  • Returns: The IP address as an integer

Example:

import { ipToLong } from "@supercat1337/ip-tools";
const ip = '192.168.1.1';
const ipInt = ipToLong(ip);
console.log(ipInt); // Output: 16909060

isCidr(cidr)

  • cidr: The CIDR notation, e.g. "192.168.1.0/24"
  • Returns: true if the CIDR notation is valid, false otherwise

Example:

import { isCidr } from "@supercat1337/ip-tools";
const cidr = '192.168.1.0/24';
const isCidr = isCidr(cidr);
console.log(isCidr); // Output: true

isIPv4(ip)

  • ip: The IP address, e.g. "192.168.1.1"
  • Returns: true if the IP address is valid, false otherwise

Example:

import { isIPv4 } from "@supercat1337/ip-tools";
const ip = '192.168.1.1';
const isIPv4 = isIPv4(ip);
console.log(isIPv4); // Output: true

isIPv6(ip)

  • ip: The IP address, e.g. "2b5b:1e49:8d01:c2ac:fffd:833e:dfee:13a4"
  • Returns: true if the IP address is valid, false otherwise

Example:

import { isIPv6 } from "@supercat1337/ip-tools";
const ip = '2b5b:1e49:8d01:c2ac:fffd:833e:dfee:13a4';
const isIPv6 = isIPv6(ip);
console.log(isIPv6); // Output: true

isIpInRange(ip, range)

  • ip: The IP address, e.g. "192.168.1.0"
  • range: An array of two IP addresses representing the range of addresses that the CIDR notation covers, e.g. ["192.168.1.0", "192.168.1.255"]
  • Returns: true if the IP address is in the range, false otherwise

Example:

import { isIpInRange } from "@supercat1337/ip-tools";
const ip = '192.168.1.0';
const range = ['192.168.1.0', '192.168.1.255'];
const isIpInRange = isIpInRange(ip, range);
console.log(isIpInRange); // Output: true

isPrivateIp(ip)

  • ip: The IP address, e.g. "192.168.1.1"
  • Returns: true if the IP address is in the private IP range, false otherwise

Example:

import { isPrivateIp } from "@supercat1337/ip-tools";
const ip = '192.168.1.1';
const isPrivateIp = isPrivateIp(ip);
console.log(isPrivateIp); // Output: true

isPublicIp(ip)

  • ip: The IP address, e.g. "192.168.1.1"
  • Returns: true if the IP address is in the public IP range, false otherwise

Example:

import { isPublicIp } from "@supercat1337/ip-tools";
const ip = '192.168.1.1';
const isPublicIp = isPublicIp(ip);
console.log(isPublicIp); // Output: false

isRange(ipRange)

  • ipRange: A valid IP range is a string with two IP addresses separated by a hyphen, e.g. 192.168.1.1-192.168.1.100
  • Returns: true if the IP range is valid, false otherwise

Example:

import { isRange } from "@supercat1337/ip-tools";
const ipRange = '192.168.1.1-192.168.1.100';
const isRange = isRange(ipRange);
console.log(isRange); // Output: true

generateArrayOfIp(start_ip, end_ip, chunk_size=1000)

  • start_ip: The start IP address, e.g. "192.168.1.0"
  • end_ip: The end IP address, e.g. "192.168.1.255"
  • chunk_size: The number of IP addresses to include in each sub-range, defaults to 1000
  • Returns: An array of IP addresses in the range [start_ip, end_ip] with at most chunk_size elements

Example:

import { generateArrayOfIp } from "@supercat1337/ip-tools";
const start_ip = '192.168.1.0';
const end_ip = '192.168.1.255';
const chunk_size = 1000;
const array = generateArrayOfIp(start_ip, end_ip, chunk_size);
console.log(array); // Output: ["192.168.1.0", "192.168.1.1", "192.168.1.2", ...]

License

MIT License