cidr-tools
Advanced tools
Comparing version 8.0.1 to 9.0.0
@@ -21,19 +21,19 @@ type IPv4Address = string; | ||
export function merge(networks: Networks): Network[]; | ||
export function exclude(baseNetworks: Networks, excludeNetworks: Networks): Network[]; | ||
export function expand(networks: Networks): Network[]; | ||
export function overlap(networksA: Networks, networksB: Networks): boolean; | ||
export function normalize(cidr: Networks, opts?: NormalizeOpts): Networks; | ||
export function contains(networksA: Networks, networksB: Networks): boolean; | ||
export function parse(network: Network): Parsed; | ||
export function mergeCidr(networks: Networks): Network[]; | ||
export function excludeCidr(baseNetworks: Networks, excludeNetworks: Networks): Network[]; | ||
export function expandCidr(networks: Networks): Network[]; | ||
export function overlapCidr(networksA: Networks, networksB: Networks): boolean; | ||
export function normalizeCidr(cidr: Networks, opts?: NormalizeOpts): Networks; | ||
export function containsCidr(networksA: Networks, networksB: Networks): boolean; | ||
export function parseCidr(network: Network): Parsed; | ||
declare const _default: { | ||
merge: typeof merge; | ||
exclude: typeof exclude; | ||
expand: typeof expand; | ||
overlap: typeof overlap; | ||
normalize: typeof normalize; | ||
contains: typeof contains; | ||
parse: typeof parse; | ||
mergeCidr: typeof mergeCidr; | ||
excludeCidr: typeof excludeCidr; | ||
expandCidr: typeof expandCidr; | ||
overlapCidr: typeof overlapCidr; | ||
normalizeCidr: typeof normalizeCidr; | ||
containsCidr: typeof containsCidr; | ||
parseCidr: typeof parseCidr; | ||
}; | ||
export default _default; |
60
index.js
@@ -19,3 +19,3 @@ import {parseIp, stringifyIp, normalizeIp, ipVersion} from "ip-bigint"; | ||
function doNormalize(cidr, {compress = true, hexify = false} = {}) { | ||
const {start, end, prefix, version} = parse(cidr); | ||
const {start, end, prefix, version} = parseCidr(cidr); | ||
if (start !== end) { // cidr | ||
@@ -30,5 +30,5 @@ // set network address to first address | ||
export function normalize(cidr, {compress = true, hexify = false} = {}) { | ||
export function normalizeCidr(cidr, {compress = true, hexify = false} = {}) { | ||
if (Array.isArray(cidr)) { | ||
return cidr.map(entry => normalize(entry, {compress, hexify})); | ||
return cidr.map(entry => normalizeCidr(entry, {compress, hexify})); | ||
} else { | ||
@@ -39,3 +39,3 @@ return doNormalize(cidr, {compress, hexify}); | ||
export function parse(str) { | ||
export function parseCidr(str) { | ||
const cidrVer = cidrVersion(str); | ||
@@ -158,3 +158,3 @@ const parsed = Object.create(null); | ||
return merge(remaining); | ||
return mergeCidr(remaining); | ||
} | ||
@@ -230,3 +230,3 @@ | ||
function formatPart(part, version) { | ||
const ip = normalize(stringifyIp({number: BigInt(part.start.toString()), version})); | ||
const ip = normalizeCidr(stringifyIp({number: BigInt(part.start.toString()), version})); | ||
const zeroes = diff(part.end, part.start).toString(2); | ||
@@ -296,5 +296,5 @@ const prefix = bits[version] - (zeroes.match(/0/g) || []).length; | ||
export function merge(nets) { | ||
export function mergeCidr(nets) { | ||
// sort to workaround https://github.com/silverwind/cidr-tools/issues/17 | ||
nets = uniq((Array.isArray(nets) ? nets : [nets]).sort(compare).map(parse)); | ||
nets = uniq((Array.isArray(nets) ? nets : [nets]).sort(compare).map(parseCidr)); | ||
const maps = mapNets(nets); | ||
@@ -310,8 +310,8 @@ | ||
export function exclude(basenets, exclnets) { | ||
export function excludeCidr(basenets, exclnets) { | ||
basenets = uniq(Array.isArray(basenets) ? basenets : [basenets]); | ||
exclnets = uniq(Array.isArray(exclnets) ? exclnets : [exclnets]); | ||
basenets = merge(basenets); | ||
exclnets = merge(exclnets); | ||
basenets = mergeCidr(basenets); | ||
exclnets = mergeCidr(exclnets); | ||
@@ -332,4 +332,4 @@ const bases = {4: [], 6: []}; | ||
for (const [index, basecidr] of bases[v].entries()) { | ||
const base = parse(basecidr); | ||
const excl = parse(exclcidr); | ||
const base = parseCidr(basecidr); | ||
const excl = parseCidr(exclcidr); | ||
const remainders = excludeNets(base, excl, v); | ||
@@ -347,8 +347,8 @@ if (base.cidr !== remainders.toString()) { | ||
export function expand(nets) { | ||
export function expandCidr(nets) { | ||
nets = uniq(Array.isArray(nets) ? nets : [nets]); | ||
const ips = []; | ||
for (const net of merge(nets)) { | ||
const {start, end, version} = parse(net); | ||
for (const net of mergeCidr(nets)) { | ||
const {start, end, version} = parseCidr(net); | ||
for (let number = start; number <= end; number++) { | ||
@@ -358,6 +358,6 @@ ips.push(stringifyIp({number, version})); | ||
} | ||
return ips.map(normalize); | ||
return ips.map(normalizeCidr); | ||
} | ||
export function overlap(a, b) { | ||
export function overlapCidr(a, b) { | ||
const aNets = uniq(Array.isArray(a) ? a : [a]); | ||
@@ -367,5 +367,5 @@ const bNets = uniq(Array.isArray(b) ? b : [b]); | ||
for (const a of aNets) { | ||
const aParsed = parse(a); | ||
const aParsed = parseCidr(a); | ||
for (const b of bNets) { | ||
const bParsed = parse(b); | ||
const bParsed = parseCidr(b); | ||
@@ -386,3 +386,3 @@ // version mismatch | ||
export function contains(a, b) { | ||
export function containsCidr(a, b) { | ||
const aNets = uniq(Array.isArray(a) ? a : [a]); | ||
@@ -394,5 +394,5 @@ const bNets = uniq(Array.isArray(b) ? b : [b]); | ||
for (const a of aNets) { | ||
const aParsed = parse(a); | ||
const aParsed = parseCidr(a); | ||
for (const b of bNets) { | ||
const bParsed = parse(b); | ||
const bParsed = parseCidr(b); | ||
@@ -415,9 +415,9 @@ // version mismatch | ||
export default { | ||
merge, | ||
exclude, | ||
expand, | ||
overlap, | ||
contains, | ||
normalize, | ||
parse, | ||
mergeCidr, | ||
excludeCidr, | ||
expandCidr, | ||
overlapCidr, | ||
containsCidr, | ||
normalizeCidr, | ||
parseCidr, | ||
}; |
{ | ||
"name": "cidr-tools", | ||
"version": "8.0.1", | ||
"version": "9.0.0", | ||
"author": "silverwind <me@silverwind.io>", | ||
@@ -5,0 +5,0 @@ "description": "Tools to work with IPv4 and IPv6 CIDR", |
@@ -8,11 +8,11 @@ # cidr-tools | ||
```js | ||
import {merge, exclude, expand, overlap, contains, normalize, parse} from "cidr-tools"; | ||
import {mergeCidr, excludeCidr, expandCidr, overlapCidr, containsCidr, normalizeCidr, parseCidr} from "cidr-tools"; | ||
merge(["1.0.0.0/24", "1.0.1.0/24"]); //=> ["1.0.0.0/23"] | ||
exclude(["::1/127"], "::1/128") //=> ["::/128"] | ||
expand(["2001:db8::/126"]) //=> ["2001:db8::", "2001:db8::1", "2001:db8::2", "2001:db8::3"] | ||
overlap("1.0.0.0/24", "1.0.0.128/25") //=> true | ||
contains(["1.0.0.0/24", "2.0.0.0/24"], "1.0.0.1") //=> true | ||
normalize("::ffff/64") //=> "::/64" | ||
parse("::/64"); // => {cidr: "::/64", version: 6, prefix: "64", start: 0n, end: 18446744073709551615n} | ||
mergeCidr(["1.0.0.0/24", "1.0.1.0/24"]); //=> ["1.0.0.0/23"] | ||
excludeCidr(["::1/127"], "::1/128") //=> ["::/128"] | ||
expandCidr(["2001:db8::/126"]) //=> ["2001:db8::", "2001:db8::1", "2001:db8::2", "2001:db8::3"] | ||
overlapCidr("1.0.0.0/24", "1.0.0.128/25") //=> true | ||
containsCidr(["1.0.0.0/24", "2.0.0.0/24"], "1.0.0.1") //=> true | ||
normalizeCidr("::ffff/64") //=> "::/64" | ||
parseCidr("::/64"); // => {cidr: "::/64", version: 6, prefix: "64", start: 0n, end: 18446744073709551615n} | ||
``` | ||
@@ -28,3 +28,3 @@ | ||
### merge(networks) | ||
### mergeCidr(networks) | ||
@@ -35,3 +35,3 @@ - `networks` *String* or *Array*: One or more CIDR or IP addresses. | ||
### exclude(baseNetworks, excludeNetworks) | ||
### excludeCidr(baseNetworks, excludeNetworks) | ||
@@ -43,3 +43,3 @@ - `baseNetworks` *String* or *Array*: One or more CIDR or IP addresses. | ||
### expand(networks) | ||
### expandCidr(networks) | ||
@@ -50,3 +50,3 @@ - `networks` *String* or *Array*: One or more CIDR or IP addresses. | ||
### overlap(networksA, networksB) | ||
### overlapCidr(networksA, networksB) | ||
@@ -58,3 +58,3 @@ - `networksA` *String* or *Array*: One or more CIDR or IP address. | ||
### contains(networksA, networksB) | ||
### containsCidr(networksA, networksB) | ||
@@ -66,3 +66,3 @@ - `networksA` *String* or *Array*: One or more CIDR or IP address. | ||
### normalize(networks, [opts]) | ||
### normalizeCidr(networks, [opts]) | ||
@@ -77,3 +77,3 @@ - `networks` *String* or *Array*: One or more CIDR or IP address. | ||
### parse(network) | ||
### parseCidr(network) | ||
@@ -87,3 +87,3 @@ - `network` *String*: A CIDR or IP address. | ||
- `version` Number: IP protocol version. Either `4` or `6`. | ||
- `prefix` String: The network prefix, e.g. `/64`. | ||
- `prefix` String: The network prefix, e.g. `64`. | ||
- `start` BigInt: Start number of the network. | ||
@@ -90,0 +90,0 @@ - `end` BigInt: End number of the network. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16688