Skip to content

Commit

Permalink
Sort baselines (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored Oct 29, 2024
2 parents ee18d4a + 85e97ab commit 7427c43
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
48 changes: 47 additions & 1 deletion src/baseline.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type {
Baseline,
BaselineOperations,
BaselineOperationsIgnoreCoordinatesByRule,
CheckDocumentOutput,
CheckOperationsResult,
SourceResultsBySourceName,
Expand Down Expand Up @@ -41,7 +43,51 @@ export function generateBaseline(
}
}

return baseline;
return sortBaseline(baseline);
}

function sortIgnoreCoordinatesByRule(
icbr: BaselineOperationsIgnoreCoordinatesByRule,
): BaselineOperationsIgnoreCoordinatesByRule {
return Object.fromEntries(
Object.entries(icbr)
.sort((a, z) => {
return a[0].localeCompare(z[0], "en-US");
})
.map(([key, arr]) => [key, arr != null ? [...arr].sort() : arr]),
);
}

function sortOperations(ops: BaselineOperations): BaselineOperations {
return Object.fromEntries(
Object.entries(ops)
.sort((a, z) => a[0].localeCompare(z[0], "en-US"))
.map(([opName, val]) => {
if (val != null) {
const { ignoreCoordinatesByRule, ...rest } = val;
return [
opName,
{
ignoreCoordinatesByRule: sortIgnoreCoordinatesByRule(
ignoreCoordinatesByRule,
),
...rest,
},
];
} else {
return [opName, val];
}
}),
);
}

export function sortBaseline(baseline: Baseline): Baseline {
const { version, operations, ...rest } = baseline;
return {
version,
operations: sortOperations(operations),
...rest,
};
}

function filterOutput(
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
filterBaseline,
filterOnlyErrors,
generateBaseline,
sortBaseline,
} from "./baseline.js";
export {
Baseline,
Expand Down
22 changes: 13 additions & 9 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,21 @@ export interface CheckOperationsResult {
filtered: number;
}

export interface BaselineOperationsIgnoreCoordinatesByRule {
[infraction: string]: string[] | undefined;
}

export interface BaselineOperations {
[operationName: string]:
| {
ignoreCoordinatesByRule: BaselineOperationsIgnoreCoordinatesByRule;
}
| undefined;
}

export interface Baseline {
version: 1;
operations: {
[operationName: string]:
| {
ignoreCoordinatesByRule: {
[infraction: string]: string[] | undefined;
};
}
| undefined;
};
operations: BaselineOperations;
}

export interface CheckDocumentEvent {
Expand Down

0 comments on commit 7427c43

Please sign in to comment.