Skip to content

Refactor (packages/web/src/middleware.ts): Found 17 lines of identical code in 2 locations (mass = 129) - #56

Open
adeleeuw17 wants to merge 1 commit into
CMU-313:mainfrom
adeleeuw17:refactor-middleware-locale-dedup
Open

Refactor (packages/web/src/middleware.ts): Found 17 lines of identical code in 2 locations (mass = 129)#56
adeleeuw17 wants to merge 1 commit into
CMU-313:mainfrom
adeleeuw17:refactor-middleware-locale-dedup

Conversation

@adeleeuw17

@adeleeuw17 adeleeuw17 commented Sep 5, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #54

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Qlty flagged 17 duplicate lines between packages/web/src/middleware.ts and packages/console/app/src/lib/language.ts. Both files parsed Accept-Language headers into a sorted list of pairs separately.

I moved that parsing into a new parseAcceptLanguage function in packages/web/src/i18n/locales.ts, since middleware.ts already imports functions from there. I also moved localeFromAcceptLanguage into the same file, because middleware.ts imports astro:middleware and can't be loaded in a plain test runner.

How did you verify your code works?

I added packages/web/src/middleware.test.ts with 8 tests covering both functions: null input, q-value sorting and defaulting, empty segments, wildcard skipping, and no-match fallback. All pass. Coverage on locales.ts is 93.3% functions / 90.4% lines, and the uncovered lines are pre-existing code I didn't touch. Reran qlty smells on middleware.ts and it's clean. oxlint on the changed files shows 0 warnings.

Screenshots / recordings

Screenshot 2026-09-05 at 12 37 51 PM

qlty before:
Screenshot 2026-09-05 at 1 01 30 PM

qlty after:
Screenshot 2026-09-05 at 12 38 57 PM

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

If you do not follow this template your PR will be automatically rejected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

P1B: Refactor (packages/web/src/middleware.ts:53): Found 17 lines of identical code in 2 locations (mass = 129)

1 participant