Skip to content

Add networkForm() for CIDR network-address strings - #199

Merged
beaugunderson merged 1 commit into
mainfrom
bg-network-form
May 1, 2026
Merged

beaugunderson merged 1 commit into
mainfrom
bg-network-form

Conversation

@beaugunderson

Copy link
Copy Markdown
Owner

Adds networkForm() to both Address4 and Address6, returning the network address with the CIDR suffix preserved as a string.

new Address4('205.65.224.110/29').networkForm(); // '205.65.224.104/29'
new Address4('192.168.1.5/24').networkForm();    // '192.168.1.0/24'
new Address4('192.168.1.5').networkForm();       // '192.168.1.5/32'

new Address6('2001:db8::1/32').networkForm();    // '2001:db8::/32'
new Address6('2001:db8::1').networkForm();       // '2001:db8::1/128'

Closes the gap raised in #39 (specifically the 2025 follow-up from @nfriedly): previously callers had to assemble the result by hand with `${addr.startAddress().correctForm()}/${addr.subnetMask}`, which is brittle string templating around a value the library already knows.

The Form suffix matches the existing correctForm() / canonicalForm() / reverseForm() family — these are the methods that return strings rather than Address4/Address6 instances.

The result round-trips through the Address4/Address6 constructor, so new Address4(addr.networkForm()) reproduces the network address with its prefix intact.

Test plan

  • 6 new tests for Address4.networkForm() covering /0, /8, the issue What is the point of the mask method? #39 example, /24, the no-subnet (default /32) case, and a constructor round-trip
  • 5 new tests for Address6.networkForm() covering /0, /32, /64, the no-subnet (default /128) case, and a constructor round-trip
  • Full suite green (3500 passing)
  • README regenerated via npm run docs

Returns the network address with the CIDR suffix preserved as a string,
e.g. `205.65.224.110/29` → `205.65.224.104/29`. Closes the gap raised
in #39: previously callers had to assemble the result by hand
(`${addr.startAddress().correctForm()}/${addr.subnetMask}`). Available
on both Address4 and Address6.
@codecov

codecov Bot commented May 1, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a9443a7) to head (a79c083).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #199   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         6           
  Lines          330       332    +2     
  Branches       186       186           
=========================================
+ Hits           330       332    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant