From 85e97abfe17cbc721124dd6703b16db1b1d6d986 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Tue, 29 Oct 2024 11:39:43 +0000 Subject: [PATCH] Sort baselines --- src/baseline.ts | 48 ++++++++++++++++++++++++++++++++++++++++++++++- src/index.ts | 1 + src/interfaces.ts | 22 +++++++++++++--------- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/baseline.ts b/src/baseline.ts index aecbd10..768f2c0 100644 --- a/src/baseline.ts +++ b/src/baseline.ts @@ -1,5 +1,7 @@ import type { Baseline, + BaselineOperations, + BaselineOperationsIgnoreCoordinatesByRule, CheckDocumentOutput, CheckOperationsResult, SourceResultsBySourceName, @@ -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( diff --git a/src/index.ts b/src/index.ts index 5332144..723bc00 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,6 +13,7 @@ export { filterBaseline, filterOnlyErrors, generateBaseline, + sortBaseline, } from "./baseline.js"; export { Baseline, diff --git a/src/interfaces.ts b/src/interfaces.ts index 14d1073..9a8f46e 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -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 {