From 9d3235bcd8af81539241fff873c38e3bad0209af Mon Sep 17 00:00:00 2001 From: Scrum4Me Agent <30029041+madhura68@users.noreply.github.com> Date: Thu, 14 May 2026 15:47:51 +0200 Subject: [PATCH] feat(code): add parseCodeNumber helper to lib/code.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pure helper that extracts the trailing numeric sequence from a code string (ST-007 → 7, T-42 → 42). Non-conforming codes fall back to Number.MAX_SAFE_INTEGER so they sort to the end. Includes 5 unit tests. Co-Authored-By: Claude Sonnet 4.6 --- __tests__/lib/code.test.ts | 25 +++++++++++++++++++++++++ lib/code.ts | 10 ++++++++++ 2 files changed, 35 insertions(+) create mode 100644 __tests__/lib/code.test.ts diff --git a/__tests__/lib/code.test.ts b/__tests__/lib/code.test.ts new file mode 100644 index 0000000..7b83640 --- /dev/null +++ b/__tests__/lib/code.test.ts @@ -0,0 +1,25 @@ +import { describe, it, expect } from 'vitest' + +import { parseCodeNumber } from '@/lib/code' + +describe('parseCodeNumber', () => { + it('parses a standard story code', () => { + expect(parseCodeNumber('ST-001')).toBe(1) + }) + + it('parses a task code', () => { + expect(parseCodeNumber('T-42')).toBe(42) + }) + + it('parses a large number', () => { + expect(parseCodeNumber('ST-1000')).toBe(1000) + }) + + it('returns MAX_SAFE_INTEGER for a code with no trailing digits', () => { + expect(parseCodeNumber('FOO')).toBe(Number.MAX_SAFE_INTEGER) + }) + + it('returns MAX_SAFE_INTEGER for an empty string', () => { + expect(parseCodeNumber('')).toBe(Number.MAX_SAFE_INTEGER) + }) +}) diff --git a/lib/code.ts b/lib/code.ts index 9a247de..5de2b23 100644 --- a/lib/code.ts +++ b/lib/code.ts @@ -14,3 +14,13 @@ export function normalizeCode(input: string | null | undefined): string | null { const trimmed = input.trim() return trimmed === '' ? null : trimmed } + +/** + * Extract the trailing numeric sequence from a code (e.g. "ST-007" → 7, "T-42" → 42). + * Non-conforming codes (no trailing digits, empty string) return Number.MAX_SAFE_INTEGER + * so they sort to the end. + */ +export function parseCodeNumber(code: string): number { + const m = code.match(/(\d+)$/) + return m ? Number.parseInt(m[1], 10) : Number.MAX_SAFE_INTEGER +}