export type TickerSource = 'eod' | 'qm' | 'ib' | 'ceo'; export const MAX_CLASS_SUFFIX_LENGTH = 12; export interface ParsedTicker { tickerRoot: string; classSuffix: string; } export type ParseTickerFailureReason = 'invalid_ticker' | 'class_suffix_too_long'; export type ParseTickerDetailedResult = | { ok: true; tickerRoot: string; classSuffix: string } | { ok: false; reason: 'invalid_ticker' } | { ok: false; reason: 'class_suffix_too_long'; tickerRoot: string; classSuffix: string }; const CODE_TAB = 9; const CODE_SPACE = 32; const CODE_DASH = 45; const CODE_DOT = 46; const CODE_SLASH = 47; function isWhitespace(code: number): boolean { return code === CODE_SPACE || code === CODE_TAB; } function isSeparator(code: number): boolean { return ( code === CODE_DOT || code === CODE_DASH || code === CODE_SPACE || code === CODE_TAB || code === CODE_SLASH ); } function findNextSeparator(str: string, start: number): number { for (let i = start; i < str.length; i++) { if (isSeparator(str.charCodeAt(i))) { return i; } } return -1; } /** * Parses a raw provider ticker into a canonical uppercase `(tickerRoot, classSuffix)` pair, * returning a structured failure reason (`invalid_ticker` or `class_suffix_too_long`) on failure. * Strips embedded `.TO` / `.V` venue suffixes only when `source === 'ceo'`. */ export function parseTickerDetailed( raw: string | null | undefined, source: TickerSource ): ParseTickerDetailedResult { if (typeof raw !== 'string') { return { ok: false, reason: 'invalid_ticker' }; } let s = raw.trim().toUpperCase(); if (s.length === 0) { return { ok: false, reason: 'invalid_ticker' }; } if (source === 'ceo') { if (s.length > 3 && s.endsWith('.TO')) { s = s.slice(0, -3).trimEnd(); } else if (s.length > 2 && s.endsWith('.V')) { s = s.slice(0, -2).trimEnd(); } if (s.length === 0) { return { ok: false, reason: 'invalid_ticker' }; } } const firstSep = findNextSeparator(s, 0); if (firstSep === -1) { return { ok: true, tickerRoot: s, classSuffix: '' }; } if (firstSep === 0) { return { ok: false, reason: 'invalid_ticker' }; } const tickerRoot = s.slice(0, firstSep); let pos = firstSep + 1; while (pos < s.length && isWhitespace(s.charCodeAt(pos))) { pos++; } if (pos >= s.length || isSeparator(s.charCodeAt(pos))) { return { ok: false, reason: 'invalid_ticker' }; } const secondSep = findNextSeparator(s, pos); if (secondSep === -1) { const classSuffix = s.slice(pos); if (classSuffix.length > MAX_CLASS_SUFFIX_LENGTH) { return { ok: false, reason: 'class_suffix_too_long', tickerRoot, classSuffix }; } return { ok: true, tickerRoot, classSuffix }; } let classSuffix = s.slice(pos, secondSep); pos = secondSep; while (pos < s.length) { pos++; while (pos < s.length && isWhitespace(s.charCodeAt(pos))) { pos++; } if (pos >= s.length || isSeparator(s.charCodeAt(pos))) { return { ok: false, reason: 'invalid_ticker' }; } const nextSep = findNextSeparator(s, pos); if (nextSep === -1) { classSuffix += '.' + s.slice(pos); break; } classSuffix += '.' + s.slice(pos, nextSep); pos = nextSep; } if (classSuffix.length > MAX_CLASS_SUFFIX_LENGTH) { return { ok: false, reason: 'class_suffix_too_long', tickerRoot, classSuffix }; } return { ok: true, tickerRoot, classSuffix }; } /** * Parses a raw provider ticker into a canonical uppercase `(tickerRoot, classSuffix)` pair. * Strips embedded `.TO` / `.V` venue suffixes only when `source === 'ceo'`. * Plain tickers return `classSuffix: ''`; empty or malformed inputs return `null`. */ export function parseTicker( raw: string | null | undefined, source: TickerSource ): ParsedTicker | null { const result = parseTickerDetailed(raw, source); return result.ok ? { tickerRoot: result.tickerRoot, classSuffix: result.classSuffix } : null; }