UNPKG

@xwordly/xword-parser

Version:

Fast, type-safe TypeScript library for parsing crossword puzzles (PUZ, iPUZ, JPZ, XD)

1 lines 5.79 kB
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AA+BO,SAAS,KAAA,CAAM,MAAqC,OAAA,EAAgC;AACzF,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,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,MAAA,GAAS,QAAA,CAAS,OAAA,EAAS,OAAO,CAAA;AACxC,YAAA,OAAO,oBAAoB,MAAM,CAAA;AAAA,UACnC;AACA,UAAA;AAAA,QACF;AAAA,QACA,KAAK,KAAA,EAAO;AACV,UAAA,MAAM,WAAA,GACJ,OAAO,OAAA,KAAY,QAAA,GAAW,UAAU,OAAA,CAAQ,QAAA,CAAS,OAAA,EAAS,QAAA,IAAY,OAAO,CAAA;AACvF,UAAA,MAAM,MAAA,GAAS,QAAA,CAAS,WAAA,EAAa,OAAO,CAAA;AAC5C,UAAA,OAAO,oBAAoB,MAAM,CAAA;AAAA,QACnC;AAAA,QACA,KAAK,IAAA,EAAM;AACT,UAAA,MAAM,WAAA,GACJ,OAAO,OAAA,KAAY,QAAA,GAAW,UAAU,OAAA,CAAQ,QAAA,CAAS,OAAA,EAAS,QAAA,IAAY,OAAO,CAAA;AACvF,UAAA,MAAM,MAAA,GAAS,OAAA,CAAQ,WAAA,EAAa,OAAO,CAAA;AAC3C,UAAA,OAAO,mBAAmB,MAAM,CAAA;AAAA,QAClC;AAAA;AACF,IACF,SAAS,CAAA,EAAG;AACV,MAAA,SAAA,GAAY,CAAA;AAEZ,MAAA,IAAI,CAAA,YAAa,UAAA,IAAc,CAAC,CAAA,CAAE,kBAAiB,EAAG;AACpD,QAAA,MAAM,CAAA;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAIA,EAAA,IAAI,aAAa,EAAE,SAAA,YAAqB,UAAA,IAAc,SAAA,CAAU,kBAAiB,CAAA,EAAI;AACnF,IAAA,IAAI,qBAAqB,KAAA,EAAO;AAC9B,MAAA,MAAM,SAAA;AAAA,IACR;AACA,IAAA,MAAM,IAAI,oBAAA;AAAA,MACR,uEAAA;AAAA,MACA,MAAA;AAAA,MACA;AAAA,KACF;AAAA,EACF;AAGA,EAAA,MAAM,IAAI,oBAAA;AAAA,IACR;AAAA,GACF;AACF","file":"index.mjs","sourcesContent":["import { parseIpuz, convertIpuzToUnified } from './ipuz';\nimport { parseXd, convertXdToUnified } from './xd';\nimport { parsePuz, convertPuzToUnified } from './puz';\nimport { parseJpz, convertJpzToUnified } from './jpz';\nimport { FormatDetectionError, ParseError } from './errors';\nimport { getOrderedFormatsToTry } from './detect';\nimport type { Puzzle, ParseOptions } from './types';\n\n/**\n * Parse a crossword puzzle from various formats (iPUZ, PUZ, JPZ, XD).\n * Automatically detects the format and returns a unified puzzle structure.\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 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 { parse } from 'xword-parser';\n * import { readFileSync } from 'fs';\n *\n * const content = readFileSync('puzzle.puz');\n * const puzzle = parse(content, { filename: 'puzzle.puz' });\n * console.log(puzzle.title, puzzle.grid.width + 'x' + puzzle.grid.height);\n * ```\n */\nexport function parse(data: string | Buffer | ArrayBuffer, options?: ParseOptions): 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 puzzle = parseIpuz(textContent, options);\n return convertIpuzToUnified(puzzle);\n }\n case 'puz': {\n if (typeof content !== 'string') {\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 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 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\nexport {\n parseIpuz,\n convertIpuzToUnified,\n parseXd,\n convertXdToUnified,\n parsePuz,\n convertPuzToUnified,\n parseJpz,\n convertJpzToUnified,\n};\n\nexport * from './types';\nexport * from './errors';\nexport * from './ipuz';\nexport * from './xd';\nexport * from './puz';\nexport * from './jpz';\n"]}