Skip to content

Add UTF-8 BOM support for package.json files#39

Merged
Boshen merged 4 commits into
mainfrom
feat/utf8-bom-support
Jan 20, 2026
Merged

Add UTF-8 BOM support for package.json files#39
Boshen merged 4 commits into
mainfrom
feat/utf8-bom-support

Conversation

@Boshen
Copy link
Copy Markdown
Member

@Boshen Boshen commented Jan 20, 2026

Summary

  • Add UTF-8 BOM (Byte Order Mark) detection and preservation when sorting package.json files
  • Optimized implementation to avoid performance penalties
  • Comprehensive test coverage with dedicated BOM fixture

Motivation

Some tools and editors (like those used in Vite playground) create package.json files with UTF-8 BOM markers. When sorting these files, the BOM should be preserved to maintain compatibility with tools that expect it.

Reference: https://github.com/vitejs/vite/blob/main/playground/resolve/utf8-bom-package/package.json

Implementation

  1. BOM Detection: Uses strip_prefix() to detect and remove BOM (\u{FEFF}) before JSON parsing
  2. Preservation: If BOM was present in input, prepends it to the sorted output
  3. Performance: Pre-allocates exact capacity and builds output string efficiently (avoids O(n) insert(0) operation)
  4. No Side Effects: Files without BOM remain unchanged

Test Coverage

  • BOM preservation when present
  • No BOM added when not present
  • Idempotency (sorting twice produces same result)
  • New fixture: tests/fixtures/package-bom.json with actual UTF-8 BOM

Test Plan

  • All existing tests pass
  • New BOM test passes
  • Benchmarks show no performance regression
  • Manually verified with Vite example

🤖 Generated with Claude Code

Boshen and others added 3 commits January 20, 2026 10:32
Detect and preserve UTF-8 BOM when sorting package.json files.
Files with BOM (like Vite playground examples) now maintain the BOM
after sorting, ensuring compatibility with tools that expect it.

- Add BOM detection and stripping before JSON parsing
- Preserve BOM in output if present in input
- Add dedicated BOM test fixture (tests/fixtures/package-bom.json)
- Add comprehensive test coverage for BOM handling

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Replace String::insert(0, BOM) with pre-allocated String to avoid
expensive byte shifting. Use idiomatic strip_prefix for BOM detection.

- Use strip_prefix() instead of manual slicing
- Pre-allocate exact capacity when BOM is needed
- Push BOM first then append result (faster than insert at 0)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Combine three BOM tests into one that covers:
- BOM preservation when present
- No BOM added when not present
- Idempotency of BOM sorting

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jan 20, 2026

Merging this PR will not alter performance

✅ 4 untouched benchmarks


Comparing feat/utf8-bom-support (81a848c) with main (89d649f)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (6f66639) during the generation of this report, so 89d649f was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@Boshen Boshen merged commit 4e77e05 into main Jan 20, 2026
4 checks passed
@Boshen Boshen deleted the feat/utf8-bom-support branch January 20, 2026 04:17
@Boshen Boshen mentioned this pull request Jan 20, 2026
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