Skip to content

Replace javascript-natural-sort package with inline ESM implementation#3652

Open
andreialecu wants to merge 1 commit intojosdejong:developfrom
andreialecu:replace-javascript-natural-sort-with-esm
Open

Replace javascript-natural-sort package with inline ESM implementation#3652
andreialecu wants to merge 1 commit intojosdejong:developfrom
andreialecu:replace-javascript-natural-sort-with-esm

Conversation

@andreialecu
Copy link
Copy Markdown

Summary

  • Replaces the CommonJS-only javascript-natural-sort dependency with an inline ESM module
  • Eliminates the "Module 'javascript-natural-sort' is not ESM" warning in Angular and other ESM-first bundlers
  • Enables proper tree-shaking of this code

Part of #3649

Details

The javascript-natural-sort package hasn't been updated in 9+ years and only ships CommonJS. Rather than waiting for an upstream ESM build, this inlines the implementation as a clean ESM module.

The implementation preserves the original algorithm while improving readability (descriptive variable names, extracted helper functions) and performance:

Dataset Original Inline Speedup
strings (1000) 13.7ms 11.0ms +19%
numbers (1000) 7.2ms 5.3ms +26%
mixed (1000) 9.1ms 7.3ms +20%
versions (1000) 12.6ms 10.1ms +20%

Key optimizations: charCodeAt instead of regex for leading-zero detection, .test() instead of .match() to avoid array allocations, hoisted inline regex literals to module-level constants, removed per-call closure allocation.

Output is verified identical to the original package across all test data.

Test plan

  • New unit tests for the naturalSort module (34 tests: all upstream cases + edge cases for null, boolean, Infinity, scientific notation, large numbers, global regex stability)
  • Existing compareNatural.test.js tests pass (20 tests)
  • All 6670 unit tests pass
  • Lint passes with zero warnings

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.

1 participant