Skip to content

Commit

Permalink
support arrays in .contains
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Feb 15, 2022
1 parent 3cd3f5b commit b07f09c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ Returns a boolean that indicates if `networksA` overlap (intersect) with `networ

### cidrTools.contains(networkA, networkB)

- `networkA` *String*: A CIDR or IP address.
- `networkB` *String*: A CIDR or IP address.
- `networksA` *String* or *Array*: One or more CIDR or IP address.
- `networksB` *String* or *Array*: One or more CIDR or IP address.

Returns a boolean that indicates whether `networkA` fully contains `networkB`.
Returns a boolean that indicates whether `networksA` fully contain all `networksB`.

### cidrTools.normalize(network)

Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface CIDRTools {
expand(networks: Networks): Network[];
overlap(networksA: Networks, networksB: Networks): boolean;
normalize(cidr: Network): Network;
contains(networkA: Network, networkB: Network): boolean;
contains(networksA: Networks, networksB: Networks): boolean;
}

declare const cidrTools : CIDRTools;
Expand Down
26 changes: 20 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,27 @@ module.exports.overlap = (a, b) => {
};

module.exports.contains = (a, b) => {
const aParsed = parse(a);
const bParsed = parse(b);
const aNets = uniq(Array.isArray(a) ? a : [a]);
const bNets = uniq(Array.isArray(b) ? b : [b]);

const numExpected = bNets.length;
let numFound = 0;
for (const a of aNets) {
const aParsed = parse(a);
for (const b of bNets) {
const bParsed = parse(b);

// version mismatch
if (aParsed.address.v4 !== bParsed.address.v4) {
return false;
// version mismatch
if (aParsed.address.v4 !== bParsed.address.v4) {
continue;
}

if (contains(aParsed, bParsed)) {
numFound++;
continue;
}
}
}

return contains(aParsed, bParsed);
return numFound === numExpected;
};
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ test("contains", () => {
expect(contains("::/128", "::1")).toEqual(false);
expect(contains("::/128", "0.0.0.0")).toEqual(false);
expect(contains("::/128", "0.0.0.1")).toEqual(false);
expect(contains(["1.0.0.0/24"], ["1.0.0.1"])).toEqual(true);
expect(contains("1.0.0.0/24", ["1.0.0.1"])).toEqual(true);
expect(contains(["1.0.0.0/24"], "1.0.0.1")).toEqual(true);
expect(contains(["1.0.0.0/24", "2.0.0.0"], "1.0.0.1")).toEqual(true);
expect(contains(["1.0.0.0/24", "2.0.0.0"], "3.0.0.1")).toEqual(false);
expect(contains(["1.0.0.0/24", "::/0"], "3.0.0.1")).toEqual(false);
expect(contains(["1.0.0.0/24", "::/0", "3.0.0.0/24"], "3.0.0.1")).toEqual(true);
expect(contains(["1.0.0.0/24", "::/0", "3.0.0.0/24"], "::1")).toEqual(true);
expect(contains(["1.0.0.0/24", "::/0", "3.0.0.0/24"], ["::1"])).toEqual(true);
expect(contains(["1.0.0.0/24", "::/0", "3.0.0.0/24"], ["::1", "::2"])).toEqual(true);
expect(contains(["1.0.0.0/24", "::/128", "3.0.0.0/24"], "::1")).toEqual(false);
expect(contains(["1.0.0.0/24", "::/128", "3.0.0.0/24"], ["::1", "::2"])).toEqual(false);
});

0 comments on commit b07f09c

Please sign in to comment.