Skip to content

ipPrefix - Generate the CIDR range for a given subnet

The ipPrefix function is very useful for working out the subnet in which an IP address belongs.

Syntax

Like many functions in DataPrime, ipPrefix supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

ipPrefix(ip: string, subnetSize: number): string
ip: string.ipPrefix(subnetSize: number): string

Arguments

NameTypeRequiredDescription
ipstringtrueThe IP address to process
subnetSizenumbertrueThe number of mask bits in the subnet. For example, 10 will result in a CIDR range ending in /10

Example - Generating the CIDR range for a given IP in a subnet of known size

Consider the following document:

{
    "ip": "155.123.7.4"
}

To calculate the subnet for this IP, and assuming that we know the number of mask bits, we can use the ipPrefix function:

create ip_subnet from ipPrefix(ip, 10)
create ip_subnet from ip.ipPrefix(10)

This results in the following document:

{
    "ip": "155.123.7.4",
    "ip_subnet": "155.64.0.0/10"
}