fix: use locale-independent comparison for deterministic serialization (#177)#181
fix: use locale-independent comparison for deterministic serialization (#177)#181guoyangzhen wants to merge 2 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughReplaced locale-dependent Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/serialize.ts`:
- Line 42: Parenthesize the nested ternary expressions to satisfy
unicorn/no-nested-ternary: replace instances like "return a < b ? -1 : a > b ? 1
: 0;" with a parenthesized nested branch, e.g. "return a < b ? -1 : (a > b ? 1 :
0);". Apply the same pattern to the other occurrences with the same nested
ternary structure so each outer ternary's alternate branch is wrapped in
parentheses.
🔗 Linked issue
Closes #177
📝 Description
serialize()usesString.localeCompare()for sorting object keys, which produces different results depending on the system locale.In Slovak (sk-SK), the digraph
chis treated as a distinct letter afterh. So{checkIn: 'foo', destination: 'bar'}serializes as{destination:'bar',checkIn:'foo'}in Slovak, but{checkIn:'foo',destination:'bar'}in English. This causeshash()to produce different values for the same input on different machines.The same issue affects
Setsorting and thecompare()fallback path.Fix
Replace all
localeComparecalls with code-point comparison (a < b ? -1 : a > b ? 1 : 0) which is deterministic across all locales:compare()for strings —a.localeCompare(b)→a < b ? -1 : a > b ? 1 : 0compare()fallback —String.prototype.localeCompare.call(...)→ code-point compare on serialized valuesObject.keys()sort —a.localeCompare(b)→ code-point compareCode-point comparison uses UTF-16 code unit values, which is stable and locale-independent.
Impact
Without this fix,
hash()produces different values for the same input depending on the system'sLANG/LC_ALLsetting. This breaks caching, content deduplication, and any system that relies on hash stability across environments.Summary by CodeRabbit