fix(sort): make path comparator consistent for trailing-slash paths#229
Merged
Merged
Conversation
sortPathsByAlphabet split each path on "/" and used a truthy `!segment` test to detect a missing segment. That conflated an absent segment (`undefined`, the path has ended) with an empty segment (`''`, produced by a trailing slash). For two paths that differ only by a trailing slash (e.g. `/pets` and `/pets/`), the empty segment made both `compare(a, b)` and `compare(b, a)` return -1. The comparator was therefore inconsistent (non-antisymmetric), so Array.prototype.sort's result depended on the input order and the two paths swapped places on every run. Use a strict `=== undefined` check so only a truly absent segment sorts first; an empty trailing-slash segment is compared normally as `''`. This makes the comparator a total order and the output stable across runs, while leaving every other path ordering unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #228.
Problem
With
sortPathsBy: path, two paths that differ only by a trailing slash (e.g./petsand/pets/) swap order on every run, so formatting the same document never converges.sortPathsByAlphabetsplits each path on/and uses a truthy!segmenttest to detect a missing segment."/pets/".split('/')ends in''(empty segment) while"/pets".split('/')ends inundefined(no segment) — both falsy. ThepathAbranch then fires regardless of argument order, socompare(a, b)andcompare(b, a)both return-1. That comparator is not antisymmetric, so the sort result depends on the input order and flips between runs.Fix
Use a strict
=== undefinedcheck so only a truly absent segment sorts first; an empty trailing-slash segment is compared normally as''(which sorts before any non-empty segment). The comparator becomes a total order, output is stable across runs, and all other path orderings are unchanged.Tests
Added two unit tests in
test/sorting.test.js:/petsis ordered before/pets/regardless of input order (this is the regression: it fails onmainfor the swapped-input case)./pets,/pets/,/pets/{id}keep a stable, sensible order.Verified the new "regardless of input order" test fails on the current comparator and passes with the fix. Full suite green (
npm test: 15 suites, 487 passing),sorting.jsat 100% coverage, andprettier --checkpasses on the changed files.