A comprehensive TypeScript package for IP address manipulation and subnet calculations.
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.
- 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
To install ip-navigator, run the following command in your terminal:
npm install ip-navigator
pnpm add ip-navigator
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'
Validates whether the given string is a valid IPv4 address.
isValidIPAddress("192.168.1.1"); // returns true
isValidIPAddress("256.1.2.3"); // returns false
Checks if the given string is a valid subnet mask.
isValidSubnetMask("255.255.255.0"); // returns true
isValidSubnetMask("255.255.256.0"); // returns false
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
Converts an IPv4 address to its binary representation.
ipToBinary("192.168.1.1"); // returns '11000000.10101000.00000001.00000001'
Converts a binary representation of an IP address to its decimal format.
binaryToIP("11000000.10101000.00000001.00000001"); // returns '192.168.1.1'
Converts an IPv4 address to its integer representation.
ipToInteger("192.168.1.1"); // returns 3232235777
Converts an integer representation of an IP address to its decimal format.
integerToIP(3232235777); // returns '192.168.1.1'
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'
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'
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'
// }
Returns the next IP address in sequential order.
getNextIPAddress("192.168.1.1"); // returns '192.168.1.2'
Returns the previous IP address in sequential order.
getPreviousIPAddress("192.168.1.2"); // returns '192.168.1.1'
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
Checks if an IP address is a public IP address.
isPublicIP("8.8.8.8"); // returns true
isPublicIP("192.168.1.1"); // returns false
Checks if an IP address is a private IP address.
isPrivateIP("192.168.1.1"); // returns true
isPrivateIP("8.8.8.8"); // returns false
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']
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.
We welcome contributions from the community! If you'd like to contribute to ip-navigator, please follow these steps:
- Fork the repository
- Create a new branch for your feature or bug fix
- Make your changes and write tests if applicable
- Ensure all tests pass by running
pnpm test # or npm test
- Commit your changes and push to your fork
- 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.
This project is licensed under the MIT License. See the LICENSE file for details.
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!