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
- Zero external dependencies
To install ip-navigator, run the following command in your terminal:
npm install ip-navigatorpnpm add ip-navigatoryarn add ip-navigatorip-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 falseChecks if the given string is a valid subnet mask.
isValidSubnetMask("255.255.255.0"); // returns true
isValidSubnetMask("255.255.256.0"); // returns falseChecks if the given string is a valid CIDR notation.
isValidCIDR("192.168.1.0/24"); // returns true
isValidCIDR("192.168.1.0/33"); // returns falseConverts 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 3232235777Converts an integer representation of an IP address to its decimal format.
integerToIP(3232235777); // returns '192.168.1.1'Calculates the subnet mask from the given CIDR notation.
cidrToSubnetMask(24); // returns '255.255.255.0'
cidrToSubnetMask(16); // returns '255.255.0.0'Calculates the CIDR notation from the given subnet mask.
subnetMaskToCIDR("255.255.255.0"); // returns 24
subnetMaskToCIDR("255.255.0.0"); // returns 16Calculates 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'Calculates the available IP addresses in a subnet.
calculateAvailableIPs("192.168.1.0", "255.255.255.252");
// returns ['192.168.1.1', '192.168.1.2']Note: For large subnets, be aware of potential memory usage when generating the full list.
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 trueChecks if an IP address is a public IP address.
isPublicIP("8.8.8.8"); // returns true
isPublicIP("192.168.1.1"); // returns falseChecks if an IP address is a private IP address.
isPrivateIP("192.168.1.1"); // returns true
isPrivateIP("8.8.8.8"); // returns falseGenerates 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']Compares two IP addresses numerically.
compareIPAddresses("192.168.1.1", "192.168.1.2"); // returns -1
compareIPAddresses("192.168.1.1", "192.168.1.1"); // returns 0
compareIPAddresses("192.168.1.2", "192.168.1.1"); // returns 1ip-navigator uses Jest for unit testing. To run the tests, use the following command:
pnpm test # or npm testOur 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.
Marc Tyson CLEBERT contact@marctysonclebert.com
- Website: marctysonclebert.com
- GitHub: @clebertmarctyson
- Twitter/X: @ClebertTyson
- Buy me a coffee: Support my work
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!
If ip-navigator has been helpful to you, consider:
- ⭐ Starring the repo
- ☕ Buying me a coffee
