-
Notifications
You must be signed in to change notification settings - Fork 0
feat(types): add hashId() for deterministic short identifiers #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: p3-7/contracts/logger-child
Are you sure you want to change the base?
Conversation
8d043eb to
62826b8
Compare
f7beb26 to
501e6c9
Compare
62826b8 to
71b347d
Compare
501e6c9 to
538ae91
Compare
Greptile Summary
|
| Filename | Overview |
|---|---|
| packages/types/src/hash-id.ts | New module implementing hashId() with Bun.hash() and base62 encoding for deterministic short identifiers |
| packages/types/src/tests/hash-id.test.ts | Comprehensive test suite covering determinism, configurability, URL-safety, uniqueness, and edge cases |
Confidence score: 4/5
- This PR is safe to merge with only minor review needed for one implementation assumption
- Score reflects solid test coverage and clean implementation, but one test assumes prefix consistency behavior that may not hold for all hash/encoding combinations
- Pay close attention to the prefix consistency test in
hash-id.test.tsto verify the assumption aligns with actual implementation behavior
Sequence Diagram
sequenceDiagram
participant User
participant hashId
participant Bun
participant toBase62
User->>hashId: "hashId('user-123', 8)"
hashId->>Bun: "Bun.hash('user-123')"
Bun-->>hashId: "64-bit hash number"
hashId->>toBase62: "toBase62(BigInt(hash))"
toBase62-->>hashId: "base62 string"
hashId->>hashId: "padStart(8, '0')"
hashId->>hashId: "slice(0, 8)"
hashId-->>User: "URL-safe hash ID"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3 files reviewed, 1 comment
| it("same input with different lengths produces consistent prefixes", () => { | ||
| // Longer hashes should be extensions of shorter ones | ||
| // (this tests that the algorithm is consistent) | ||
| const input = "consistent-test"; | ||
| const short = hashId(input, 3); | ||
| const medium = hashId(input, 6); | ||
| const long = hashId(input, 10); | ||
|
|
||
| // The shorter hash should be a prefix of longer ones | ||
| expect(medium.startsWith(short)).toBe(true); | ||
| expect(long.startsWith(short)).toBe(true); | ||
| expect(long.startsWith(medium)).toBe(true); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: This test assumes that longer hashes are extensions of shorter ones, but this may not hold true depending on how the base62 encoding is implemented. If the encoding uses modular arithmetic or truncation after encoding, this assumption could fail. Does the hashId implementation guarantee that longer results will always be prefixes of shorter ones for the same input?
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/types/src/__tests__/hash-id.test.ts
Line: 135:147
Comment:
**logic:** This test assumes that longer hashes are extensions of shorter ones, but this may not hold true depending on how the base62 encoding is implemented. If the encoding uses modular arithmetic or truncation after encoding, this assumption could fail. Does the hashId implementation guarantee that longer results will always be prefixes of shorter ones for the same input?
How can I resolve this? If you propose a fix, please make it concise.Uses Bun.hash() with base62 encoding for URL-safe, collision-resistant short IDs. Configurable length (default 5 chars, ~916M combinations). Closes #46 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
538ae91 to
a0c8b3f
Compare
71b347d to
3820a22
Compare

Uses Bun.hash() with base62 encoding for URL-safe, collision-resistant
short IDs. Configurable length (default 5 chars, ~916M combinations).
Closes #46
Co-Authored-By: Claude Opus 4.5 noreply@anthropic.com