1- import { ParsedCard } from './types' ;
1+ import { ParsedCard } from './types.js ' ;
22
33/**
44 * Configuration for the bulk card parser
@@ -25,12 +25,15 @@ export const CARD_DELIMITER = '\n---\n---\n';
2525
2626/**
2727 * Parses a single card string into a structured object
28- *
28+ *
2929 * @param cardString - Raw string containing card content
3030 * @param config - Optional parser configuration
3131 * @returns ParsedCard object or null if parsing fails
3232 */
33- export function parseCard ( cardString : string , config : CardParserConfig = DEFAULT_PARSER_CONFIG ) : ParsedCard | null {
33+ export function parseCard (
34+ cardString : string ,
35+ config : CardParserConfig = DEFAULT_PARSER_CONFIG
36+ ) : ParsedCard | null {
3437 const trimmedCardString = cardString . trim ( ) ;
3538 if ( ! trimmedCardString ) {
3639 return null ;
@@ -40,18 +43,18 @@ export function parseCard(cardString: string, config: CardParserConfig = DEFAULT
4043 let tags : string [ ] = [ ] ;
4144 let elo : number | undefined = undefined ;
4245 const markdownLines = [ ...lines ] ;
43-
46+
4447 // Process the lines from bottom to top to handle metadata
4548 let metadataLines = 0 ;
46-
49+
4750 // Get the configured identifiers
4851 const tagId = config . tagIdentifier || DEFAULT_PARSER_CONFIG . tagIdentifier ;
4952 const eloId = config . eloIdentifier || DEFAULT_PARSER_CONFIG . eloIdentifier ;
50-
53+
5154 // Check the last few lines for metadata (tags and elo)
5255 for ( let i = lines . length - 1 ; i >= 0 && i >= lines . length - 2 ; i -- ) {
5356 const line = lines [ i ] . trim ( ) ;
54-
57+
5558 // Check for tags
5659 if ( line . toLowerCase ( ) . startsWith ( tagId ! . toLowerCase ( ) ) ) {
5760 tags = line
@@ -71,7 +74,7 @@ export function parseCard(cardString: string, config: CardParserConfig = DEFAULT
7174 metadataLines ++ ;
7275 }
7376 }
74-
77+
7578 // Remove metadata lines from the end of the content
7679 if ( metadataLines > 0 ) {
7780 markdownLines . splice ( markdownLines . length - metadataLines ) ;
@@ -82,29 +85,53 @@ export function parseCard(cardString: string, config: CardParserConfig = DEFAULT
8285 // Card must have some markdown content
8386 return null ;
8487 }
85-
88+
8689 return { markdown, tags, elo } ;
8790}
8891
8992/**
9093 * Splits a bulk text input into individual card strings
91- *
94+ *
9295 * @param bulkText - Raw string containing multiple cards
9396 * @returns Array of card strings
9497 */
9598export function splitCardsText ( bulkText : string ) : string [ ] {
96- return bulkText . split ( CARD_DELIMITER )
97- . map ( card => card . trim ( ) )
98- . filter ( card => card ) ; // Filter out empty strings
99+ return bulkText
100+ . split ( CARD_DELIMITER )
101+ . map ( ( card ) => card . trim ( ) )
102+ . filter ( ( card ) => card ) ; // Filter out empty strings
103+ }
104+
105+ /**
106+ * Parses a bulk text input into an array of structured ParsedCard objects.
107+ *
108+ * @param bulkText - Raw string containing multiple cards.
109+ * @param config - Optional parser configuration.
110+ * @returns Array of ParsedCard objects. Filters out cards that fail to parse.
111+ */
112+ export function parseBulkTextToCards (
113+ bulkText : string ,
114+ config : CardParserConfig = DEFAULT_PARSER_CONFIG
115+ ) : ParsedCard [ ] {
116+ const cardStrings = splitCardsText ( bulkText ) ;
117+ const parsedCards : ParsedCard [ ] = [ ] ;
118+
119+ for ( const cardString of cardStrings ) {
120+ const parsedCard = parseCard ( cardString , config ) ;
121+ if ( parsedCard ) {
122+ parsedCards . push ( parsedCard ) ;
123+ }
124+ }
125+ return parsedCards ;
99126}
100127
101128/**
102129 * Validates if a bulk text input has valid format
103- *
130+ *
104131 * @param bulkText - Raw string containing multiple cards
105132 * @returns true if valid, false otherwise
106133 */
107134export function isValidBulkFormat ( bulkText : string ) : boolean {
108135 const cardStrings = splitCardsText ( bulkText ) ;
109- return cardStrings . length > 0 && cardStrings . some ( card => ! ! card . trim ( ) ) ;
110- }
136+ return cardStrings . length > 0 && cardStrings . some ( ( card ) => ! ! card . trim ( ) ) ;
137+ }
0 commit comments