mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
fix: say out loud what every sort was already doing
Sonar's S2871 flags a bare .sort(), which orders by UTF-16 code unit rather than by value. All 30 remaining sites turned out to sort ASCII strings — ISO dates, currency and country codes, setting keys, route names, OAuth scopes, zip entry names — where that order is already the intended one. Each says so explicitly now, with a comparator that reproduces it exactly rather than a localeCompare that would quietly reorder things. That mattered more than it sounds. Several of these feed something other than a display: useAtlas joins the sorted codes into the signature that decides whether to rebuild the region layer, daysSlice re-pins sorted dates positionally onto reordered days, and three timeline views pick a day's colour by indexOf into the sorted array. Switching any of them to a locale collator would have been a silent regression. Two smaller ones came with it. The .map(placeBtn) calls in FileManagerAssignModal pass the index as a second argument, which placeBtn declares and uses for its React key — correct already, now written out as .map((p, i) => placeBtn(p, i)). And budgetSlice's optimistic paid-toggle stored the raw boolean behind `paid as unknown as number`, while the schema declares z.number() and both the REST response and the WebSocket event carry 0/1. So the optimistic state and the confirmed state disagreed on type until a refresh. It normalises now, and the two tests that asserted `true` assert the contract. Four `| 0` truncations are listed in sonar-project.properties instead of changed. Two are a djb2 string hash where the 32-bit wrap is the algorithm. The other two look like plain truncation of a row id, but `| 0` is also coercing undefined to 0 there — the settlement flows the API returns carry no user_id, so that is the normal path, and Math.trunc returns NaN and crashes the lookup.
This commit is contained in:
@@ -254,7 +254,7 @@ export class AtlasService {
|
||||
const dates = countryTrips
|
||||
.map((t) => t.start_date)
|
||||
.filter(Boolean)
|
||||
.sort();
|
||||
.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
||||
return {
|
||||
code: c.code,
|
||||
placeCount: c.places.length,
|
||||
|
||||
@@ -166,5 +166,5 @@ export function splitManagedKeys<T extends Record<string, unknown>>(
|
||||
if (LOCKED.has(key)) blocked.push(key);
|
||||
else (allowed as Record<string, unknown>)[key] = value;
|
||||
}
|
||||
return { allowed, blocked: blocked.sort() };
|
||||
return { allowed, blocked: blocked.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)) };
|
||||
}
|
||||
|
||||
@@ -86,14 +86,14 @@ export function validateBodyContracts(
|
||||
problems.push(
|
||||
`${violations.length} mutation handler(s) read @Body() without a createZodDto class ` +
|
||||
`(add a <domain>.dto.ts wrapper over the @trek/shared schema — do NOT extend the allow-list): ` +
|
||||
violations.sort().join(', '),
|
||||
violations.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)).join(', '),
|
||||
);
|
||||
}
|
||||
if (stale.length) {
|
||||
problems.push(
|
||||
`stale body-contract allow-list entr${stale.length === 1 ? 'y' : 'ies'} ` +
|
||||
`(route removed or now validated — delete from body-contract-allow-list.ts): ` +
|
||||
stale.sort().join(', '),
|
||||
stale.sort((a, b) => (a < b ? -1 : a > b ? 1 : 0)).join(', '),
|
||||
);
|
||||
}
|
||||
if (problems.length) {
|
||||
|
||||
@@ -398,7 +398,7 @@ export class DaysService {
|
||||
|
||||
const oldDateById = new Map(rows.map(r => [r.id, r.date]));
|
||||
// Dates stay pinned to slots: position i keeps the i-th date (ascending).
|
||||
const sortedDates = rows.map(r => r.date).filter((d): d is string => !!d).sort();
|
||||
const sortedDates = rows.map(r => r.date).filter((d): d is string => !!d).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
||||
const isDated = sortedDates.length > 0;
|
||||
|
||||
const setDayNumber = this.db.prepare('UPDATE days SET day_number = ? WHERE id = ?');
|
||||
@@ -452,7 +452,7 @@ export class DaysService {
|
||||
}
|
||||
|
||||
// Dated trip: rebuild N+1 contiguous dates from the earliest date.
|
||||
const start = datedRows.map(r => r.date).sort()[0];
|
||||
const start = datedRows.map(r => r.date).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))[0];
|
||||
const dates = Array.from({ length: n + 1 }, (_, i) => addDays(start, i));
|
||||
const oldDateById = new Map(rows.map(r => [r.id, r.date]));
|
||||
const setDayNumberAndDate = this.db.prepare('UPDATE days SET day_number = ?, date = ? WHERE id = ?');
|
||||
|
||||
@@ -179,10 +179,10 @@ export interface StatsInput {
|
||||
*/
|
||||
export function computeJourneyStats(input: StatsInput): JourneyStats {
|
||||
const points = thinRoute(input.points);
|
||||
const dated = points.map(p => isoDay(p.date)).filter((d): d is string => !!d).sort();
|
||||
const dated = points.map(p => isoDay(p.date)).filter((d): d is string => !!d).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
||||
|
||||
const tripStarts = input.tripDates.map(t => isoDay(t.start)).filter((d): d is string => !!d).sort();
|
||||
const tripEnds = input.tripDates.map(t => isoDay(t.end)).filter((d): d is string => !!d).sort();
|
||||
const tripStarts = input.tripDates.map(t => isoDay(t.start)).filter((d): d is string => !!d).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
||||
const tripEnds = input.tripDates.map(t => isoDay(t.end)).filter((d): d is string => !!d).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
|
||||
|
||||
const start = dated[0] ?? tripStarts[0] ?? null;
|
||||
const end = dated[dated.length - 1] ?? tripEnds[tripEnds.length - 1] ?? null;
|
||||
|
||||
@@ -82,8 +82,8 @@ export function jsonRpcError(message: string, code = -32000): object {
|
||||
export function sameScopes(a: string[] | null, b: string[] | null): boolean {
|
||||
if (a === null || b === null) return a === b;
|
||||
if (a.length !== b.length) return false;
|
||||
const left = [...a].sort();
|
||||
const right = [...b].sort();
|
||||
const left = [...a].sort((x, y) => (x < y ? -1 : x > y ? 1 : 0));
|
||||
const right = [...b].sort((x, y) => (x < y ? -1 : x > y ? 1 : 0));
|
||||
return left.every((scope, i) => scope === right[i]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user