UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 1.2 kB
/*! Copyright 2023-2025 the gnablib contributors MPL-1.1 */ import{lsbMask as t}from"../xtBit.js";import{IpV4 as r}from"./Ip.js";import{ContentError as s}from"../../error/ContentError.js";import{sInt as i,sLen as o}from"../../safe/safe.js";import{parseDec as n}from"../number/xtUint.js";const a=Symbol.for("nodejs.util.inspect.custom"),e="Cidr";export class Cidr{constructor(s,o){i("mask",o).unsigned().atMost(32).throwNot();const n=s.valueOf();this.bitMask=t(32-o);const a=(n&~this.bitMask)>>>0;this.dist=n-a,this.startIp=r.fromInt(a),this.mask=o}get count(){return 1+(4294967295*(1-(this.mask>>5&1))>>>this.mask)}get normalForm(){return 0===this.dist}get endIp(){return r.fromInt(this.startIp.valueOf()|this.bitMask)}containsIp(t){return(t.valueOf()&~this.bitMask)===this.startIp.valueOf()}equals(t){return this.mask===t.mask&&this.startIp.valueOf()===t.startIp.valueOf()}toString(){return this.startIp.toString()+"/"+this.mask}get[Symbol.toStringTag](){return e}[a](){return`${e}(${this.toString()})`}static fromString(t){const i=t.split("/");o("parts",i).exactly(2).throwNot();const a=r.fromString(i[0]),e=n(i[1]);if(isNaN(e))throw new s("Expecting integer 0-32","Mask",i[1]);return new this(a,e)}}