IPv4 natively handles conversions between String, Numeric and other IPv4 instances.

Classes

IPv4

Methods

(static) from(ip)

Constructor Alias
Parameters:
NameTypeDescription
ipIPv4 | String | NumberThe IPv4 value
Returns:
an IPv4 instance
Examples
const ip = IPv4.from('192.168.23.52');

ip.log();
const ip = IPv4.from(0xC0A81738);

ip.log();

(static) use(plugin)

Implement plugins as IPv4 static and prototype members
Parameters:
NameTypeDescription
pluginclassA class template with static & prototype members
Example
const { IPv4, Hardware } = require('ipv4-utils');

IPv4.use(Hardware);

(inner) copy() → {IPv4}

Copy an IPv4 instance
Returns:
A copy of the instance
Type: 
IPv4
Example
const host1 = IPv4.from('192.168.1.12');

const host2 = host1.copy().add(12);

host1.toString() // 192.168.1.12
host2.toString() // 192.168.1.24

(inner) op(operation) → {this}

Run an assignment operation on the uint32
Parameters:
NameTypeDescription
operationfunctionAssignement operation function, takes in the current value, returns the new value
Returns:
A pointer to itself
Type: 
this
Example
const ip   = IPv4.from('192.168.1.12');
const mask = IPv4.from('255.255.255.0');

const wildcard_ip = mask.op(x => ~x).and(ip); 
wildcard_ip.log();

(inner) toString() → {String}

Convert an IPv4 instance to a string
Returns:
ip as a string
Type: 
String
Example
const mask = IPv4.from(0xFFFF0000);

console.log(mask.toString()); // 255.255.0.0

(inner) u32()

Get the stored uint32 representing your ip instance
Returns:
The full IPv4 as an uint32
Example
const u32_ip = IPv4.from('192.168.1.1').u32();

console.log(u32_ip); // 0xC0A80101

(inner) u8(index) → {Number}

Get a specific byte from the stored uint32 representing your ip instance
Parameters:
NameTypeDescription
indexNumberThe byte index
Returns:
The selected IPv4 byte as a uint8
Type: 
Number
Example
const ip = IPv4.from('192.168.1.1');

const msb_byte = ip.u8(3); // 192
// ... //        ip.u8(2); // 168
// ... //        ip.u8(1); //   1
const lsb_byte = ip.u8(0); //   1

(inner) valueOf()

native prototype method override
Returns:
The full IPv4 as an uint32
Example
const ip = IPv4.from('192.168.1.1');

console.log(ip | 0xFF); // 0xC0A801FF