Skip to content

Add SI-prefix-aware sorting column for parts tableFeature/si value sort#1344

Merged
jbtronics merged 3 commits intoPart-DB:masterfrom
wschopohl:feature/si-value-sort
Apr 15, 2026
Merged

Add SI-prefix-aware sorting column for parts tableFeature/si value sort#1344
jbtronics merged 3 commits intoPart-DB:masterfrom
wschopohl:feature/si-value-sort

Conversation

@wschopohl
Copy link
Copy Markdown
Contributor

Summary

  • Adds an optional "SI Value" column to the parts table that parses numeric values with SI prefixes (p, n, u/µ, m, k/K, M, G, T) from part names and sorts by the resulting physical value
  • Useful for electronic components where alphabetical sorting produces wrong results — e.g. 100nF, 10pF, 1uF sort as 10pF < 100nF < 1uF
  • The column displays the parsed float value, or an empty cell for non-matching names
  • Only matches numbers at the start of the name — part numbers like "MCP2515" or "Crystal 20MHz" are not affected
  • Supports both dot and comma decimal separators (European locale)
  • Falls back to NATSORT for parts without SI values, so they sort alphabetically rather than arbitrarily
  • Opt-in: not in default columns, users enable it via the column visibility button

Implementation

  • New SiValueSort DQL function with platform-specific SQL for PostgreSQL (POSIX regex), MySQL/MariaDB (REGEXP_SUBSTR), and SQLite (PHP callback via the existing middleware)
  • 33 unit tests covering all platforms, SI prefixes, edge cases, and sort order verification

Test plan

  • Unit tests pass (33 tests, all platforms)
  • PHPStan level 2 passes
  • Tested against real parts database with SQLite

Adds an optional "Name (SI)" column that parses numeric values with SI
prefixes (p, n, u/µ, m, k/K, M, G, T) from part names and sorts by the
resulting physical value. This is useful for electronic components where
alphabetical sorting produces wrong results — e.g. 100nF, 10pF, 1uF
should sort as 10pF < 100nF < 1uF.

Implementation:
- New SiValueSort DQL function with platform-specific SQL generation
  for PostgreSQL (POSIX regex), MySQL/MariaDB (REGEXP_SUBSTR), and
  SQLite (PHP callback registered via the existing middleware).
- The regex is start-anchored: only names beginning with a number are
  matched. Part numbers like "MCP2515" or "Crystal 20MHz" are ignored.
- When SI sort is active, NATSORT is appended as a secondary sort so
  that non-matching parts fall back to natural string ordering instead
  of appearing in arbitrary order.
- The column is opt-in (not in default columns) and displays the parsed
  float value, or an empty cell for non-matching names.
The column now shows the parsed numeric value rather than the part name,
so the label should reflect that.
Part names using European decimal notation (e.g. "4,7 kΩ", "2,2uF")
were parsed incorrectly because the regex only recognized dots. Now
commas are normalized to dots before parsing, matching the existing
pattern used elsewhere in the codebase (PartNormalizer, price providers).
@wschopohl
Copy link
Copy Markdown
Contributor Author

SI Value Demo

For completeness, here is a screenshot showing how SI Value improves table ordering. It uses the value from 'Name' under the hood, extracting leading numbers with SI prefixes.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 15, 2026

Codecov Report

❌ Patch coverage is 85.91549% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.14%. Comparing base (5b86d6f) to head (93b9b29).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
src/Doctrine/Functions/SiValueSort.php 88.67% 6 Missing ⚠️
src/DataTables/PartsDataTable.php 76.47% 4 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1344      +/-   ##
============================================
+ Coverage     57.08%   57.14%   +0.05%     
- Complexity     8381     8398      +17     
============================================
  Files           615      616       +1     
  Lines         26994    27065      +71     
============================================
+ Hits          15410    15465      +55     
- Misses        11584    11600      +16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jbtronics
Copy link
Copy Markdown
Member

Looks good. Thanks.
That should be quite useful.

@jbtronics jbtronics merged commit 29db029 into Part-DB:master Apr 15, 2026
15 of 16 checks passed
@wschopohl
Copy link
Copy Markdown
Contributor Author

Awesome and thanks for fixing the PHPStan issue - must have slipped my attention

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.

2 participants