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

ip-navigator

v1.0.14

Published

A tool for IP address manipulation and calculation

Downloads

750

Readme

ip-navigator

A comprehensive TypeScript package for IP address manipulation and subnet calculations.

Table of Contents

Description

ip-navigator is a powerful and user-friendly TypeScript package designed to simplify IP address management and subnet calculations. Whether you're a network administrator, developer, or student learning about networking, this tool provides an intuitive set of functions for handling various IP address operations.

Features

  • Validate IPv4 addresses and subnet masks
  • Convert between decimal, binary, and integer IP formats
  • Perform subnet calculations (network address, broadcast address, available hosts)
  • Manipulate and compare IP addresses
  • Work with CIDR notation
  • Classify IP addresses (public, private)
  • Generate IP address ranges
  • Check if IP addresses belong to specific subnets
  • Full TypeScript support with type definitions
  • Comprehensive test suite using Jest

Installation

To install ip-navigator, run the following command in your terminal:

npm install ip-navigator
pnpm add ip-navigator

Usage

ip-navigator is designed for use in TypeScript projects. Here's a basic example of how to use it:

import { isValidIPAddress, ipToBinary } from "ip-navigator";

// Example usage
console.log(isValidIPAddress("192.168.1.1")); // true
console.log(ipToBinary("192.168.1.1")); // '11000000.10101000.00000001.00000001'

API Reference

Validation Functions

isValidIPAddress(ipAddress: string): boolean

Validates whether the given string is a valid IPv4 address.

isValidIPAddress("192.168.1.1"); // returns true
isValidIPAddress("256.1.2.3"); // returns false

isValidSubnetMask(subnetMask: string): boolean

Checks if the given string is a valid subnet mask.

isValidSubnetMask("255.255.255.0"); // returns true
isValidSubnetMask("255.255.256.0"); // returns false

isValidCIDR(cidr: string): boolean

Checks if the given string is a valid CIDR notation.

isValidCIDR("192.168.1.0/24"); // returns true
isValidCIDR("192.168.1.0/33"); // returns false

Conversion Functions

ipToBinary(ipAddress: string): string

Converts an IPv4 address to its binary representation.

ipToBinary("192.168.1.1"); // returns '11000000.10101000.00000001.00000001'

binaryToIP(binaryIP: string): string

Converts a binary representation of an IP address to its decimal format.

binaryToIP("11000000.10101000.00000001.00000001"); // returns '192.168.1.1'

ipToInteger(ipAddress: string): number

Converts an IPv4 address to its integer representation.

ipToInteger("192.168.1.1"); // returns 3232235777

integerToIP(integer: number): string

Converts an integer representation of an IP address to its decimal format.

integerToIP(3232235777); // returns '192.168.1.1'

Subnet Operations

calculateNetworkAddress(ipAddress: string, subnetMask: string): string

Calculates the network address based on an IP address and subnet mask.

calculateNetworkAddress("192.168.1.100", "255.255.255.0"); // returns '192.168.1.0'

calculateBroadcastAddress(ipAddress: string, subnetMask: string): string

Calculates the broadcast address based on an IP address and subnet mask.

calculateBroadcastAddress("192.168.1.100", "255.255.255.0"); // returns '192.168.1.255'

getSubnetInfo(ipAddress: string, subnetMask: string): SubnetInfo

Retrieves comprehensive information about a subnet.

getSubnetInfo("192.168.1.100", "255.255.255.0");
// returns {
//   networkAddress: '192.168.1.0',
//   broadcastAddress: '192.168.1.255',
//   totalHosts: 256,
//   usableHosts: 254,
//   firstUsableHost: '192.168.1.1',
//   lastUsableHost: '192.168.1.254'
// }

IP Address Operations

getNextIPAddress(ipAddress: string): string

Returns the next IP address in sequential order.

getNextIPAddress("192.168.1.1"); // returns '192.168.1.2'

getPreviousIPAddress(ipAddress: string): string

Returns the previous IP address in sequential order.

getPreviousIPAddress("192.168.1.2"); // returns '192.168.1.1'

isIPAddressInSubnet(ipAddress: string, networkAddress: string, subnetMask: string): boolean

Checks if an IP address belongs to a given subnet.

isIPAddressInSubnet("192.168.1.100", "192.168.1.0", "255.255.255.0"); // returns true

isPublicIP(ipAddress: string): boolean

Checks if an IP address is a public IP address.

isPublicIP("8.8.8.8"); // returns true
isPublicIP("192.168.1.1"); // returns false

isPrivateIP(ipAddress: string): boolean

Checks if an IP address is a private IP address.

isPrivateIP("192.168.1.1"); // returns true
isPrivateIP("8.8.8.8"); // returns false

getIPRange(startIP: string, endIP: string): string[]

Generates an array of IP addresses within the specified range.

getIPRange("192.168.1.1", "192.168.1.3");
// returns ['192.168.1.1', '192.168.1.2', '192.168.1.3']

Testing

ip-navigator uses Jest for unit testing. To run the tests, use the following command:

pnpm test # or npm test

Our test suite covers all functions in the package, ensuring reliability and correctness. We strive for high test coverage and encourage contributors to write tests for any new features or bug fixes.

Contributing

We welcome contributions from the community! If you'd like to contribute to ip-navigator, please follow these steps:

  1. Fork the repository
  2. Create a new branch for your feature or bug fix
  3. Make your changes and write tests if applicable
  4. Ensure all tests pass by running pnpm test # or npm test
  5. Commit your changes and push to your fork
  6. Create a pull request with a clear description of your changes

Please ensure that your code adheres to the existing style and that all tests pass before submitting a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Bugs and Issues

If you encounter any bugs or have suggestions for improvements, please report them on our GitHub Issues page. When reporting an issue, please include:

  • A clear and descriptive title
  • A detailed description of the issue
  • Steps to reproduce the problem
  • Expected behavior
  • Actual behavior
  • Any relevant code snippets or error messages

Your feedback helps us improve ip-navigator for everyone!