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{U32 as t}from"../number/U32Static.js";import{sLen as r,sNum as s}from"../../safe/safe.js";import{parseDec as o}from"../number/xtUint.js";import{ByteWriter as e}from"../ByteWriter.js";const n=Symbol.for("nodejs.util.inspect.custom"),i="IpV4";export class IpV4{constructor(t){this.bytes=t}toString(){return this.bytes.join(".")}valueOf(){return(this.bytes[0]<<24|this.bytes[1]<<16|this.bytes[2]<<8|this.bytes[3])>>>0}equals(t){return t.valueOf()==this.valueOf()}get[Symbol.toStringTag](){return i}[n](){return`${i}(${this.toString()})`}static fromParts(t,r,o,e){return s("first part",t).unsigned().atMost(255).throwNot(),s("second part",r).unsigned().atMost(255).throwNot(),s("third part",o).unsigned().atMost(255).throwNot(),s("fourth part",e).unsigned().atMost(255).throwNot(),new IpV4(Uint8Array.of(t,r,o,e))}static fromString(t){const s=t.split(".");r("parts",s).exactly(4).throwNot();const e=o(s[0]),n=o(s[1]),i=o(s[2]),a=o(s[3]);return IpV4.fromParts(e,n,i,a)}static fromInt(r){const s=new Uint8Array(4),o=e.mount(s);return t.intoBytesBE(r,o),new IpV4(s)}static fromBytes(t){return r("bytes",t).exactly(4).throwNot(),new IpV4(t)}}