Created
March 16, 2026 12:55
-
-
Save MiranDaniel/c13359c7e3545d21416514f1f2b222e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const MODIFIER_PREFIXES = [ | |
| 'Alolan', 'Galarian', 'Hisuian', 'Paldean', | |
| 'Dark', 'Light', 'Shining', 'Shadow', 'Radiant', | |
| ]; | |
| // Longer multi-word suffixes MUST come before shorter ones | |
| const SUFFIXES = [ | |
| 'Prism Star', 'VMAX', 'VSTAR', 'BREAK', 'LV.X', | |
| 'VSTAR', 'GX', 'EX', 'ex', 'V', 'δ', '★', '◇', | |
| ]; | |
| interface ParsedCardName { | |
| prefix?: string; | |
| main: string; | |
| suffix?: string; | |
| } | |
| export function parsePokemonCardName(cardName: string): ParsedCardName { | |
| let prefix: string | undefined; | |
| let main = cardName.trim(); | |
| let suffix: string | undefined; | |
| // Step 1: Possessive — find LAST "'s " to handle edge cases | |
| const possessiveIdx = main.lastIndexOf("'s "); | |
| if (possessiveIdx !== -1) { | |
| prefix = main.slice(0, possessiveIdx + 2); // includes "'s" | |
| main = main.slice(possessiveIdx + 3); // everything after "'s " | |
| } | |
| // Step 2: Known modifier prefix (run on remainder after possessive strip) | |
| for (const mod of MODIFIER_PREFIXES) { | |
| if (main.startsWith(mod + ' ')) { | |
| prefix = prefix ? `${prefix} ${mod}` : mod; | |
| main = main.slice(mod.length + 1); | |
| break; | |
| } | |
| } | |
| // Step 3: Known suffix (longest first to avoid partial matches) | |
| for (const sfx of SUFFIXES) { | |
| if (main.endsWith(' ' + sfx)) { | |
| suffix = sfx; | |
| main = main.slice(0, main.length - sfx.length - 1); | |
| break; | |
| } | |
| } | |
| return { prefix, main, suffix }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment