@xwordly/xword-parser
Version:
Fast, type-safe TypeScript library for parsing crossword puzzles (PUZ, iPUZ, JPZ, XD)
1 lines • 5.86 kB
Source Map (JSON)
{"version":3,"sources":["../src/lazy.ts"],"names":[],"mappings":";;;;AA2BA,eAAsB,SAAA,CACpB,MACA,OAAA,EACiB;AACjB,EAAA,IAAI,OAAA;AACJ,EAAA,IAAI,gBAAgB,WAAA,EAAa;AAC/B,IAAA,OAAA,GAAU,MAAA,CAAO,KAAK,IAAI,CAAA;AAAA,EAC5B,CAAA,MAAO;AACL,IAAA,OAAA,GAAU,IAAA;AAAA,EACZ;AAEA,EAAA,MAAM,YAAA,GAAe,sBAAA,CAAuB,OAAA,EAAS,OAAA,EAAS,QAAQ,CAAA;AACtE,EAAA,IAAI,SAAA;AAEJ,EAAA,KAAA,MAAW,UAAU,YAAA,EAAc;AACjC,IAAA,IAAI;AACF,MAAA,QAAQ,MAAA;AAAQ,QACd,KAAK,MAAA,EAAQ;AACX,UAAA,MAAM,WAAA,GACJ,OAAO,OAAA,KAAY,QAAA,GAAW,UAAU,OAAA,CAAQ,QAAA,CAAS,OAAA,EAAS,QAAA,IAAY,OAAO,CAAA;AACvF,UAAA,MAAM,EAAE,SAAA,EAAW,oBAAA,EAAqB,GAAI,MAAM,OAAO,qBAAQ,CAAA;AACjE,UAAA,MAAM,MAAA,GAAS,SAAA,CAAU,WAAA,EAAa,OAAO,CAAA;AAC7C,UAAA,OAAO,qBAAqB,MAAM,CAAA;AAAA,QACpC;AAAA,QACA,KAAK,KAAA,EAAO;AACV,UAAA,IAAI,OAAO,YAAY,QAAA,EAAU;AAC/B,YAAA,MAAM,EAAE,QAAA,EAAU,mBAAA,EAAoB,GAAI,MAAM,OAAO,oBAAO,CAAA;AAC9D,YAAA,MAAM,MAAA,GAAS,QAAA,CAAS,OAAA,EAAS,OAAO,CAAA;AACxC,YAAA,OAAO,oBAAoB,MAAM,CAAA;AAAA,UACnC;AACA,UAAA;AAAA,QACF;AAAA,QACA,KAAK,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import { FormatDetectionError, ParseError } from './errors';\nimport { getOrderedFormatsToTry } from './detect';\nimport type { Puzzle, ParseOptions } from './types';\n\n/**\n * Parse a crossword puzzle asynchronously with dynamic imports for smaller bundle size.\n * Parsers are loaded only when needed, reducing initial bundle size.\n *\n * @param data - The puzzle data as string, Buffer, or ArrayBuffer\n * @param options - Optional parsing options\n * @param options.filename - Filename hint to improve format detection\n * @param options.encoding - Character encoding for text formats (default: 'utf-8')\n * @param options.maxGridSize - Maximum allowed grid dimensions\n * @returns A Promise that resolves to a unified Puzzle object\n * @throws {FormatDetectionError} When the format cannot be detected\n * @throws {ParseError} When parsing fails for the detected format\n *\n * @example\n * ```typescript\n * import { parseLazy } from 'xword-parser/lazy';\n * import { readFileSync } from 'fs';\n *\n * const content = readFileSync('puzzle.ipuz');\n * const puzzle = await parseLazy(content, { filename: 'puzzle.ipuz' });\n * console.log(puzzle.title);\n * ```\n */\nexport async function parseLazy(\n data: string | Buffer | ArrayBuffer,\n options?: ParseOptions,\n): Promise<Puzzle> {\n let content: string | Buffer;\n if (data instanceof ArrayBuffer) {\n content = Buffer.from(data);\n } else {\n content = data;\n }\n\n const formatsToTry = getOrderedFormatsToTry(content, options?.filename);\n let lastError: unknown;\n\n for (const format of formatsToTry) {\n try {\n switch (format) {\n case 'ipuz': {\n const textContent =\n typeof content === 'string' ? content : content.toString(options?.encoding || 'utf-8');\n const { parseIpuz, convertIpuzToUnified } = await import('./ipuz');\n const puzzle = parseIpuz(textContent, options);\n return convertIpuzToUnified(puzzle);\n }\n case 'puz': {\n if (typeof content !== 'string') {\n const { parsePuz, convertPuzToUnified } = await import('./puz');\n const puzzle = parsePuz(content, options);\n return convertPuzToUnified(puzzle);\n }\n break;\n }\n case 'jpz': {\n const textContent =\n typeof content === 'string' ? content : content.toString(options?.encoding || 'utf-8');\n const { parseJpz, convertJpzToUnified } = await import('./jpz');\n const puzzle = parseJpz(textContent, options);\n return convertJpzToUnified(puzzle);\n }\n case 'xd': {\n const textContent =\n typeof content === 'string' ? content : content.toString(options?.encoding || 'utf-8');\n const { parseXd, convertXdToUnified } = await import('./xd');\n const puzzle = parseXd(textContent, options);\n return convertXdToUnified(puzzle);\n }\n }\n } catch (e) {\n lastError = e;\n // Only continue trying other formats if this is a format mismatch\n if (e instanceof ParseError && !e.isFormatMismatch()) {\n throw e;\n }\n }\n }\n\n // If we get here, no format worked\n // Only throw lastError if it was NOT a format mismatch (i.e., a real parse error)\n if (lastError && !(lastError instanceof ParseError && lastError.isFormatMismatch())) {\n if (lastError instanceof Error) {\n throw lastError;\n }\n throw new FormatDetectionError(\n 'Unable to detect puzzle format. Supported formats: iPUZ, PUZ, JPZ, XD',\n undefined,\n lastError,\n );\n }\n\n // Otherwise, we couldn't detect the format\n throw new FormatDetectionError(\n 'Unable to detect puzzle format. Supported formats: iPUZ, PUZ, JPZ, XD',\n );\n}\n"]}