Skip to content

refactor: extract alphabetToNumber helper, optimize SSN blacklist lookup#157

Open
Wolfvin wants to merge 1 commit into
koblas:mainfrom
Wolfvin:refactor/checksum-and-ssn-improvements
Open

refactor: extract alphabetToNumber helper, optimize SSN blacklist lookup#157
Wolfvin wants to merge 1 commit into
koblas:mainfrom
Wolfvin:refactor/checksum-and-ssn-improvements

Conversation

@Wolfvin

@Wolfvin Wolfvin commented Jun 13, 2026

Copy link
Copy Markdown

Summary

Two small, behavior-preserving refactors verified with output-based regression testing (using the Regrets skill).

Changes

1. src/util/checksum.ts — Extract alphabetToNumber() helper

  • Extract the alphabet-to-number conversion logic from mod97base10Validate() into a dedicated alphabetToNumber() function
  • Uses early-return pattern instead of mutable fail flag
  • Cleaner iteration with for-of instead of split().map().join()
  • The helper can be reused by other functions that need alphanumeric-to-numeric conversion

2. src/us/ssn.ts — Optimize blacklist lookup

  • Convert invalidSSN array to invalidSSNSet (a Set) for O(1) lookup instead of O(n)
  • Remove duplicate 999999999 entry from the blacklist
  • Change invalidSSN.includes(value) to invalidSSNSet.has(value)

Verification

All changes verified with 3 independent methods:

Method Result
Full test suite (1605 tests) ✅ All passing
Regrets regression (7 clusters) ✅ All GREEN
Raw output vs pre-refactor truth ✅ Identical
Fingerprint cross-match ✅ All match

Regrets Clusters Tested

  1. us-ssn-validate — US SSN validation
  2. id-npwp-validate — Indonesian NPWP validation
  3. br-cpf-validate — Brazilian CPF validation
  4. gb-nino-validate — UK NINO validation
  5. checksum-luhn — Luhn checksum
  6. checksum-mod97 — ISO 7064 Mod 97,10
  7. checksum-mod11mod10 — ISO 7064 Mod 11,10

Key Finding

I initially attempted to refactor luhnChecksumValidate() to delegate to luhnChecksum() (DRY principle). However, these two functions compute the Luhn algorithm differently — luhnChecksum reverses the string before processing while luhnChecksumValidate processes 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.

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.
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • Refactored Checksum Logic: Extracted the alphanumeric-to-numeric conversion into a reusable alphabetToNumber helper function in src/util/checksum.ts, improving code readability and maintainability.
  • Optimized SSN Blacklist Lookup: Converted the invalidSSN array to a Set in src/us/ssn.ts to achieve O(1) lookup performance and removed a duplicate entry from the blacklist.
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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/us/ssn.ts
'888888888',
'999999999',
'123123123',
'999999999',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Comment thread src/util/checksum.ts
Comment on lines +219 to +222
function alphabetToNumber(
value: string,
alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
): string | null {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
function alphabetToNumber(
value: string,
alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
): string | null {
export function alphabetToNumber(
value: string,
alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ',
): string | null {

Comment thread src/us/ssn.ts
import { Validator, ValidateReturn } from '../types';

const invalidSSN = [
const invalidSSNSet = new Set([

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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().

Comment thread src/util/checksum.ts
* Convert a string using alphanumeric alphabet to its numeric representation.
* Returns null if any character is not in the alphabet.
*/
function alphabetToNumber(

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels needless.

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.

2 participants