Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Feb 22, 2024
1 parent 3f5c35c commit 7722dbc
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 206 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: ${{matrix.node}}
- run: make test
- run: make lint test
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ lint-fix: node_modules
npx eslint --color . --fix

.PHONY: test
test: node_modules lint
test: node_modules
npx vitest
npx tsd

Expand Down
49 changes: 29 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,48 +230,57 @@ function formatPart(part, version) {
}

function mapNets(nets) {
const maps = {4: new Map(), 6: new Map()};
for (const {start, end, version} of nets) {
const dataStart = maps[version].get(start) || {};
dataStart.start = dataStart?.start ? (dataStart.start + 1) : 1;
maps[version].set(start, dataStart);

const dataEnd = maps[version].get(end) || {};
dataEnd.end = dataEnd?.end ? (dataEnd.end + 1) : 1;
maps[version].set(end, dataEnd);
}

console.log("----------------");
console.log(Array.from(maps[4].keys()));
console.log(Object.keys(mapNetsOld(nets)[4]));

return maps;
}

function mapNetsOld(nets) {
const maps = {4: {}, 6: {}}; // TODO: use Map with BigInt key
for (const {start, end, version} of nets) {
if (!maps[version][start]) maps[version][start] = {};
if (!maps[version][end]) maps[version][end] = {};

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

if (maps[version][end].end) {
maps[version][end].end += 1;
} else {
maps[version][end].end = 1;
}
maps[version][start].start = maps[version][start].start ? maps[version][start].start + 1 : 1;
maps[version][end].end = maps[version][end].end ? maps[version][end].end + 1 : 1;
}
return maps;
}

function doMerge(maps, v) {
function doMerge(map, v) {
let start = null;
let end = null;
const numbers = Object.keys(maps);
const numbers = Array.from(map.keys());
let depth = 0;
const merged = [];

for (const [index, number] of numbers.entries()) {
const marker = maps[number];
const marker = map.get(number);

if (start === null && marker.start) {
start = BigInt(number);
start = number;
}
if (marker.end) {
end = BigInt(number);
end = number;
}

if (marker.start) depth += marker.start;
if (marker.end) depth -= marker.end;

const next = numbers[index + 1];
if (marker.end && depth === 0 && next && ((BigInt(next) - BigInt(number)) > 1)) {
if (marker.end && depth === 0 && next && ((next - number) > 1n)) {
// when there is a end and the next part is more than one number away, we cut a part
for (const sub of subparts({start, end})) {
merged.push(formatPart(sub, v));
Expand All @@ -291,9 +300,9 @@ function doMerge(maps, v) {
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(parseCidr));
const maps = mapNets(nets);

const merged = {4: [], 6: []};
const maps = mapNets(nets);
const merged = {};
for (const v of [4, 6]) {
merged[v] = doMerge(maps[v], v);
}
Expand Down
Loading

0 comments on commit 7722dbc

Please sign in to comment.