refactor: extract alphabetToNumber helper, optimize SSN blacklist lookup#157
refactor: extract alphabetToNumber helper, optimize SSN blacklist lookup#157Wolfvin wants to merge 1 commit into
Conversation
Changes: 1. src/util/checksum.ts: - Extract alphabetToNumber() helper from mod97base10Validate() - Uses early-return pattern instead of mutable fail flag - Cleaner iteration with for-of instead of split+map+join 2. src/us/ssn.ts: - Convert invalidSSN array to invalidSSNSet (Set) for O(1) lookup - Remove duplicate '999999999' entry from the blacklist - Change invalidSSN.includes() to invalidSSNSet.has() Both changes are behavior-preserving — verified by: - Full test suite: 1605 tests passing - Regrets regression: 7 clusters all GREEN - Raw output comparison: identical to pre-refactor truth - Fingerprint cross-match: all fingerprints match Note: luhnChecksumValidate was NOT refactored to delegate to luhnChecksum because they compute the Luhn algorithm differently (one reverses, one doesn't). Attempting this change broke 6 test suites, demonstrating the value of output-based regression testing.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces two performance and structural improvements. The changes focus on optimizing lookup operations for SSN validation and centralizing character conversion logic to reduce code duplication, all while maintaining existing behavior verified through regression testing. Highlights
New Features🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request optimizes SSN validation by converting the invalidSSN array into a Set for faster lookups and refactors the checksum utility by extracting a reusable alphabetToNumber helper function. Feedback suggests exporting the alphabetToNumber helper so it can be reused across other modules, and removing the remaining '999999999' entry from the SSN blacklist since it is already covered by an existing regex check.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| '888888888', | ||
| '999999999', | ||
| '123123123', | ||
| '999999999', |
There was a problem hiding this comment.
While removing the duplicate '999999999' is a good cleanup, the remaining '999999999' entry (now on line 28) is also completely redundant. Any SSN starting with 9 (including 999999999) is already rejected by the regex check /^(000|666|9)\d+/ on line 99. Therefore, both entries can be safely removed from the blacklist.
| function alphabetToNumber( | ||
| value: string, | ||
| alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', | ||
| ): string | null { |
There was a problem hiding this comment.
Since the PR description mentions that this helper is intended to be reused by other functions/modules that need alphanumeric-to-numeric conversion, it should be exported so that other files can import it.
| function alphabetToNumber( | |
| value: string, | |
| alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', | |
| ): string | null { | |
| export function alphabetToNumber( | |
| value: string, | |
| alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', | |
| ): string | null { |
| import { Validator, ValidateReturn } from '../types'; | ||
|
|
||
| const invalidSSN = [ | ||
| const invalidSSNSet = new Set([ |
There was a problem hiding this comment.
I will note that if this is a performance optimization, that object keys are the fastest with a if (value in invalidSSN) { as the check will out perform Set().
| * Convert a string using alphanumeric alphabet to its numeric representation. | ||
| * Returns null if any character is not in the alphabet. | ||
| */ | ||
| function alphabetToNumber( |
Summary
Two small, behavior-preserving refactors verified with output-based regression testing (using the Regrets skill).
Changes
1.
src/util/checksum.ts— ExtractalphabetToNumber()helpermod97base10Validate()into a dedicatedalphabetToNumber()functionfailflagfor-ofinstead ofsplit().map().join()2.
src/us/ssn.ts— Optimize blacklist lookupinvalidSSNarray toinvalidSSNSet(aSet) for O(1) lookup instead of O(n)999999999entry from the blacklistinvalidSSN.includes(value)toinvalidSSNSet.has(value)Verification
All changes verified with 3 independent methods:
Regrets Clusters Tested
us-ssn-validate— US SSN validationid-npwp-validate— Indonesian NPWP validationbr-cpf-validate— Brazilian CPF validationgb-nino-validate— UK NINO validationchecksum-luhn— Luhn checksumchecksum-mod97— ISO 7064 Mod 97,10checksum-mod11mod10— ISO 7064 Mod 11,10Key Finding
I initially attempted to refactor
luhnChecksumValidate()to delegate toluhnChecksum()(DRY principle). However, these two functions compute the Luhn algorithm differently —luhnChecksumreverses the string before processing whileluhnChecksumValidateprocesses left-to-right. The refactoring broke 6 test suites immediately. This demonstrates the value of output-based regression testing for catching subtle behavioral differences.I did NOT include this change since it alters behavior.