Skip to content

Commit

Permalink
refactor to avoid var renames
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Dec 22, 2023
1 parent e764959 commit 8d66cd0
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,32 +223,29 @@ function diff(a, b) {
return a - b;
}

function formatPart(part, v) {
const ip = normalize(stringifyIp({
number: BigInt(part.start.toString()),
version: v,
}));
function formatPart(part, version) {
const ip = normalize(stringifyIp({number: BigInt(part.start.toString()), version}));
const zeroes = diff(part.end, part.start).toString(2);
const prefix = bits[v] - (zeroes.match(/0/g) || []).length;
const prefix = bits[version] - (zeroes.match(/0/g) || []).length;
return `${ip}/${prefix}`;
}

function mapNets(nets) {
const maps = {4: {}, 6: {}};
for (const {start, end, version: v} of nets) {
if (!maps[v][start]) maps[v][start] = {};
if (!maps[v][end]) maps[v][end] = {};
for (const {start, end, version} of nets) {
if (!maps[version][start]) maps[version][start] = {};
if (!maps[version][end]) maps[version][end] = {};

if (maps[v][start].start) {
maps[v][start].start += 1;
if (maps[version][start].start) {
maps[version][start].start += 1;
} else {
maps[v][start].start = 1;
maps[version][start].start = 1;
}

if (maps[v][end].end) {
maps[v][end].end += 1;
if (maps[version][end].end) {
maps[version][end].end += 1;
} else {
maps[v][end].end = 1;
maps[version][end].end = 1;
}
}
return maps;
Expand Down

0 comments on commit 8d66cd0

Please sign in to comment.