Skip to content

Commit b2717e4

Browse files
author
Michael Smith
committed
deps: ip-address@10.2.0
1 parent 1c4a796 commit b2717e4

6 files changed

Lines changed: 590 additions & 221 deletions

File tree

node_modules/ip-address/dist/common.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
exports.isInSubnet = isInSubnet;
44
exports.isCorrect = isCorrect;
5+
exports.prefixLengthFromMask = prefixLengthFromMask;
56
exports.numberToPaddedHex = numberToPaddedHex;
67
exports.stringToPaddedHex = stringToPaddedHex;
78
exports.testBit = testBit;
9+
const address_error_1 = require("./address-error");
810
function isInSubnet(address) {
911
if (this.subnetMask < address.subnetMask) {
1012
return false;
@@ -25,6 +27,25 @@ function isCorrect(defaultBits) {
2527
return this.parsedSubnet === String(this.subnetMask);
2628
};
2729
}
30+
/**
31+
* Returns the prefix length (number of leading 1 bits) of a contiguous
32+
* subnet mask. Throws `AddressError` if the mask is non-contiguous (e.g.
33+
* `255.0.255.0`).
34+
*/
35+
function prefixLengthFromMask(value, totalBits) {
36+
const binary = value.toString(2).padStart(totalBits, '0');
37+
if (binary.length > totalBits) {
38+
throw new address_error_1.AddressError('Invalid subnet mask.');
39+
}
40+
const firstZero = binary.indexOf('0');
41+
if (firstZero === -1) {
42+
return totalBits;
43+
}
44+
if (binary.slice(firstZero).includes('1')) {
45+
throw new address_error_1.AddressError('Invalid subnet mask.');
46+
}
47+
return firstZero;
48+
}
2849
function numberToPaddedHex(number) {
2950
return number.toString(16).padStart(2, '0');
3051
}

0 commit comments

Comments
 (0)