Skip to content

Fix user-facing correctness bugs (UPC-E check digit, silent validation passes, CJS types) - #1

Merged
x1ee7 merged 2 commits into
mainfrom
claude/nice-gates-fr3m0b
Sep 27, 2026
Merged

x1ee7 merged 2 commits into
mainfrom
claude/nice-gates-fr3m0b

Conversation

@x1ee7

@x1ee7 x1ee7 commented Sep 27, 2026

Copy link
Copy Markdown
Owner

Fixes bugs found in a full audit of the library. Each fix has a regression test (13 → 27 tests).

Correctness

  • UPC-E check digit was wrong. It was computed by weighting the 7 digits directly. GS1 computes it on the expanded UPC-A form. As a result, computeCheckDigit("upce"), validateBarcodeText("upce") and buildSequential("upce") returned wrong answers, and about 42% of generateRandom("upce") codes failed the library's own validation. Checked against the reference code 01234565.
  • Unknown formats passed validation silently. validateBarcodeText("UPCA", "garbage") returned { ok: true }, and generateRandom("UPCA") crashed with a TypeError. Every function now throws a descriptive error.
  • Stray letters were accepted. validateBarcodeText("upca", "hello 036000291452") was ok. computeCheckDigit turned "ABC" into 000000000000 and silently cut off payloads that were too long. Now only digits, spaces and hyphens are accepted. Short payloads are still left-padded with zeros.
  • ISBN-13 now requires the 978/979 prefix. buildSequential("isbn", "") used to produce 0000000000017. Random ISBNs now also use 979.
  • UPC-E number system must be 0 or 1. upcEExpand validates its inputs; a short body used to return "0120000undefined".
  • buildSequential rejects a negative, fractional or NaN start/count (a negative start produced garbage codes) and checks for overflow before generating anything.
  • Variable-length formats now reject empty text, and Code 128 / FNSKU reject non-ASCII text.

Packaging

  • CommonJS TypeScript users got ESM typings, which arethetypeswrong reports as "Masquerading as ESM". exports.require now points at index.d.cts. arethetypeswrong is green for node10, node16 (CJS and ESM) and bundler.

Docs / CI

  • README: the buildSequential example output was wrong (…012 → …019), the sponsor badge URL had a stray space, and the behaviour description is updated.
  • CI now uses npm ci and adds a typecheck step, which also runs before publishing.

Behaviour changes to note

Inputs that used to be silently accepted now throw or fail validation, so this is worth a minor version bump (0.2.0) before publishing. Code 128 is still upper-cased by normalizeText, as before. That was left alone because it looks like a deliberate product choice.

🤖 Generated with Claude Code

https://claude.ai/code/session_015Mx4zmKAWVwBdSCtc7W97k


Generated by Claude Code

- UPC-E check digit was computed with direct 7-digit weights instead of on
  the UPC-A expansion (per GS1), so computeCheckDigit/validate/buildSequential
  gave wrong answers and ~42% of generateRandom("upce") codes failed the
  library's own validation.
- Unknown format strings (e.g. "UPCA") made validateBarcodeText return
  { ok: true } for anything; all functions now throw a clear error.
- validateBarcodeText accepted text with stray letters ("hello 0360...");
  computeCheckDigit silently truncated over-long payloads and turned letters
  into zeros. Only digits, spaces and hyphens are accepted now.
- ISBN-13 now requires the 978/979 prefix; random ISBNs also use 979.
- UPC-E number system must be 0/1; upcEExpand validates its inputs.
- buildSequential rejects negative/fractional/NaN start and count and
  detects overflow up front.
- Variable-length validation rejects empty text and non-ASCII Code 128/FNSKU.
- package exports: CJS consumers got ESM typings (index.d.ts); now resolve
  index.d.cts.
- README: wrong buildSequential example output, broken sponsor badge URL.
- CI: npm ci + typecheck step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015Mx4zmKAWVwBdSCtc7W97k
@x1ee7
x1ee7 merged commit c836b19 into main Sep 27, 2026
1 check passed
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