mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-18 21:16:47 +00:00
fix(dayplan): sort the trip's dates with an explicit compare
ISO dates sort the same as plain strings, but a sort without a compare function reads as an accident to anyone checking it, and SonarCloud flags it as one. The three places that order the trip's dates now say how.
This commit is contained in:
@@ -28,7 +28,7 @@ type DayState = Pick<TripStoreState, 'days' | 'assignments' | 'dayNotes' | 'sele
|
|||||||
*/
|
*/
|
||||||
export function withoutDay(state: DayState, dayId: number): DayState {
|
export function withoutDay(state: DayState, dayId: number): DayState {
|
||||||
const ordered = [...state.days].sort((a, b) => (a.day_number ?? 0) - (b.day_number ?? 0))
|
const ordered = [...state.days].sort((a, b) => (a.day_number ?? 0) - (b.day_number ?? 0))
|
||||||
const dates = ordered.map(d => d.date).filter((d): d is string => !!d).sort()
|
const dates = ordered.map(d => d.date).filter((d): d is string => !!d).sort((a, b) => a.localeCompare(b))
|
||||||
const days = ordered
|
const days = ordered
|
||||||
.filter(d => d.id !== dayId)
|
.filter(d => d.id !== dayId)
|
||||||
.map((d, i) => ({ ...d, day_number: i + 1, date: dates.length ? (dates[i] ?? null) : d.date }))
|
.map((d, i) => ({ ...d, day_number: i + 1, date: dates.length ? (dates[i] ?? null) : d.date }))
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ export function dayDeleteImpact(
|
|||||||
const position = new Map(ordered.map((d, i) => [d.id, i]))
|
const position = new Map(ordered.map((d, i) => [d.id, i]))
|
||||||
const at = position.get(day.id) ?? -1
|
const at = position.get(day.id) ?? -1
|
||||||
const remaining = ordered.filter(d => d.id !== day.id)
|
const remaining = ordered.filter(d => d.id !== day.id)
|
||||||
const dates = ordered.map(d => d.date).filter((d): d is string => !!d).sort()
|
const dates = ordered.map(d => d.date).filter((d): d is string => !!d).sort((a, b) => a.localeCompare(b))
|
||||||
|
|
||||||
const moved = new Set<number>()
|
const moved = new Set<number>()
|
||||||
let datedSpare: DatedSpare | null = null
|
let datedSpare: DatedSpare | null = null
|
||||||
|
|||||||
@@ -209,7 +209,7 @@ export class DayRemovalService {
|
|||||||
*/
|
*/
|
||||||
private renumber(tripId: number, rows: DayRow[], remaining: DayRow[]): string | null {
|
private renumber(tripId: number, rows: DayRow[], remaining: DayRow[]): string | null {
|
||||||
// ISO dates sort as plain strings.
|
// ISO dates sort as plain strings.
|
||||||
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.localeCompare(b));
|
||||||
const setNumber = this.db.prepare('UPDATE days SET day_number = ? WHERE id = ?');
|
const setNumber = this.db.prepare('UPDATE days SET day_number = ? WHERE id = ?');
|
||||||
const setNumberAndDate = this.db.prepare('UPDATE days SET day_number = ?, date = ? WHERE id = ?');
|
const setNumberAndDate = this.db.prepare('UPDATE days SET day_number = ?, date = ? WHERE id = ?');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user