{"version":3,"file":"index.cjs.development.cjs","sources":["../src/index.ts"],"sourcesContent":["import BluebirdPromise from 'bluebird';\nimport { parseLine as parseLineSegment } from '@novel-segment/loaders/segment/index';\nimport { IDict, load } from '@novel-segment/loader-line';\nimport { array_unique } from 'array-hyper-unique';\n\n/**\n * CJK (中日韓) 模式常數\n * CJK (Chinese, Japanese, Korean) mode constant\n *\n * 用於指定 CJK 字元處理模式，數值 2 代表使用進階模式。\n * Used to specify the CJK character processing mode, value 2 represents advanced mode.\n */\nexport const USE_CJK_MODE = 2 as const;\n\n/**\n * 行類型列舉\n * Line Type Enumeration\n *\n * 用於識別字典檔案中每一行的類型，以便進行對應的處理。\n * Used to identify the type of each line in dictionary files for appropriate processing.\n */\nexport const enum EnumLineType\n{\n\t// 基礎行類型，代表正常的字典資料行 / Base line type, representing normal dictionary data line\n\tBASE = 0,\n\t// 註解行類型，以 // 開頭的行 / Comment line type, lines starting with //\n\tCOMMENT = 1,\n\t// 標籤註解行類型，包含特殊標籤如 @todo 或格式說明 / Tagged comment line type, containing special tags like @todo or format descriptions\n\tCOMMENT_TAG = 2,\n}\n\n/**\n * 擴展的字典檔案行資料介面\n * Extended Dictionary File Row Data Interface\n *\n * 繼承自 ILoadDictFileRow，額外包含檔案來源、CJK ID 及行類型資訊。\n * Extends ILoadDictFileRow with additional file source, CJK ID, and line type information.\n */\nexport type ILoadDictFileRow2<D extends any = [string, number, number, ...any[]]> = ILoadDictFileRow<D> & {\n\t// 來源檔案路徑 / Source file path\n\tfile: string,\n\t// CJK 字元的標準化識別 ID / Normalized identification ID for CJK characters\n\tcjk_id: string,\n\n\t// 行類型，用於分類處理 / Line type, used for classification processing\n\tline_type: EnumLineType,\n}\n\n/**\n * 字典檔案行資料介面\n * Dictionary File Row Data Interface\n *\n * 定義字典檔案中每一行的標準資料結構，包含解析後的資料、原始行內容及索引位置。\n * Defines the standard data structure for each line in a dictionary file, containing parsed data, original line content, and index position.\n */\nexport interface ILoadDictFileRow<D = [string, number, number, ...any[]]>\n{\n\t// 解析後的資料陣列 / Parsed data array\n\tdata: D,\n\t// 原始行字串 / Original line string\n\tline: string,\n\t// 在檔案中的行索引 / Line index in the file\n\tindex: number,\n}\n\n/**\n * 解包行資料類型\n * Unpack Row Data Type\n *\n * 從 ILoadDictFileRow 類型中提取 data 屬性的類型。\n * Extracts the type of the data property from ILoadDictFileRow type.\n */\nexport type IUnpackRowData<T extends ILoadDictFileRow<any>> = T extends {\n\tdata: infer D\n} ? D : never;\n\n/**\n * 解析函式類型\n * Parse Function Type\n *\n * 定義將原始行字串解析為結構化資料的函式簽名。\n * Defines the function signature for parsing raw line strings into structured data.\n */\nexport type IParseFn<D = any> = (line: string) => D;\n\n/**\n * 處理字典行的選項介面\n * Options Interface for Handling Dictionary Lines\n *\n * 包含解析函式等處理字典行所需的配置選項。\n * Contains configuration options needed for handling dictionary lines, including the parse function.\n */\nexport interface IOptionsHandleDictLines<D = any>\n{\n\t// 用於解析每一行的函式 / Function used to parse each line\n\tparseFn: IParseFn<D>;\n}\n\n/**\n * 部分處理字典行的選項介面\n * Partial Options Interface for Handling Dictionary Lines\n *\n * 所有屬性皆為選填的處理選項介面。\n * Options interface where all properties are optional.\n */\nexport interface IOptionsHandleDictLinesPartial<D = any> extends Partial<IOptionsHandleDictLines<D>>\n{\n\n}\n\n/**\n * 處理字典行的回呼函式類型\n * Callback Function Type for Handling Dictionary Lines\n *\n * 定義用於過濾或處理每一行資料的回呼函式簽名。\n * Defines the callback function signature for filtering or processing each line of data.\n */\nexport type IFnHandleDictLines<T = ILoadDictFileRow> = (list: T[], cur: T) => boolean;\n\n/**\n * 將字典行列表轉換為字串陣列\n * Convert Dictionary Line List to String Array\n *\n * 從行資料中提取原始行字串，並可選擇是否進行唯一化處理。\n * Extracts original line strings from row data, with optional uniqueness processing.\n *\n * @template T - 行資料類型，繼承自 ILoadDictFileRow / Row data type, extends ILoadDictFileRow\n * @param {T[]} list - 要處理的行資料陣列 / Array of row data to process\n * @param {object} [options] - 選項物件 / Options object\n * @param {boolean} [options.disableUnique] - 是否停用唯一化處理 / Whether to disable uniqueness processing\n * @returns {string[]} 行字串陣列 / Array of line strings\n */\nexport function stringifyHandleDictLinesList<T extends ILoadDictFileRow<any> = ILoadDictFileRow>(list: T[], options?: {\n\tdisableUnique?: boolean\n})\n{\n\t// 從每個行資料中提取原始行字串 / Extract original line strings from each row data\n\tlet lines = list.map(v => v.line);\n\n\t// 如果停用唯一化，直接返回原始陣列 / If uniqueness is disabled, return the original array\n\tif (options?.disableUnique)\n\t{\n\t\treturn lines\n\t}\n\n\t// 對行字串進行唯一化處理，移除重複項 / Perform uniqueness processing on line strings, removing duplicates\n\treturn array_unique(lines)\n}\n\n/**\n * 處理字典行資料\n * Handle Dictionary Line Data\n *\n * 將原始字典行資料解析為結構化格式，並可透過回呼函式進行過濾處理。\n * Parses raw dictionary line data into structured format, with optional filtering through callback function.\n *\n * @template T - 行資料類型，繼承自 ILoadDictFileRow / Row data type, extends ILoadDictFileRow\n * @param {IDict} lines - 字典行資料物件 / Dictionary line data object\n * @param {IFnHandleDictLines<T>} fn - 處理每一行的回呼函式，返回 true 保留該行 / Callback function for each line, return true to keep the line\n * @param {IOptionsHandleDictLines<IUnpackRowData<T>>} options - 處理選項，包含解析函式 / Processing options, containing parse function\n * @returns {T[]} 處理後的行資料陣列 / Processed array of row data\n */\nexport function handleDictLines<T extends ILoadDictFileRow<any> = ILoadDictFileRow>(lines: IDict,\n\tfn: IFnHandleDictLines<T>,\n\toptions: IOptionsHandleDictLines<IUnpackRowData<T>>,\n): T[]\n{\n\t// 如果沒有傳入行資料，返回空陣列 / If no line data is provided, return empty array\n\tif (!lines)\n\t{\n\t\treturn [] as T[]\n\t}\n\n\tconst { parseFn } = options;\n\n\t// 使用 reduce 迭代處理每一行 / Use reduce to iteratively process each line\n\treturn lines.reduce(function (a, line, index)\n\t{\n\t\tlet bool: boolean;\n\n\t\t// 使用解析函式將原始行轉換為結構化資料 / Use parse function to convert raw line to structured data\n\t\tlet data = parseFn(line);\n\n\t\t// 建構當前行的資料物件 / Construct the data object for current line\n\t\tlet cur = {\n\t\t\tdata,\n\t\t\tline,\n\t\t\tindex,\n\t\t};\n\n\t\t// 如果有傳入回呼函式，呼叫以決定是否保留該行 / If callback function is provided, call it to decide whether to keep the line\n\t\tif (fn)\n\t\t{\n\t\t\t// @ts-ignore\n\t\t\tbool = fn(a, cur)\n\t\t}\n\t\telse\n\t\t{\n\t\t\t// 沒有回呼函式時，預設保留所有行 / When no callback function, keep all lines by default\n\t\t\tbool = true;\n\t\t}\n\n\t\t// 如果回呼函式返回 true，將當前行加入結果陣列 / If callback returns true, add current line to result array\n\t\tif (bool)\n\t\t{\n\t\t\ta.push(cur as any);\n\t\t}\n\n\t\treturn a;\n\t}, [] as T[]);\n}\n\n/**\n * 載入字典檔案\n * Load Dictionary File\n *\n * 非同步載入字典檔案並解析為結構化的行資料陣列。\n * Asynchronously loads a dictionary file and parses it into a structured array of row data.\n *\n * @template T - 行資料類型，繼承自 ILoadDictFileRow / Row data type, extends ILoadDictFileRow\n * @param {string} file - 字典檔案路徑 / Dictionary file path\n * @param {IFnHandleDictLines<T>} [fn] - 處理每一行的回呼函式 / Callback function for processing each line\n * @param {IOptionsHandleDictLinesPartial<IUnpackRowData<T>>} [options] - 處理選項 / Processing options\n * @returns {BluebirdPromise<T[]>} 解析後的行資料陣列 Promise / Promise of parsed row data array\n */\nexport function loadDictFile<T extends ILoadDictFileRow<any> = ILoadDictFileRow>(file: string,\n\tfn?: IFnHandleDictLines<T>,\n\toptions?: IOptionsHandleDictLinesPartial<IUnpackRowData<T>>,\n): BluebirdPromise<T[]>\n{\n\toptions = options || {};\n\t// 如果未提供解析函式，使用預設的分段解析函式 / If no parse function provided, use default segment parse function\n\t// @ts-ignore\n\tconst parseFn: IParseFn<IUnpackRowData<T>> = options.parseFn = options.parseFn || parseLineSegment;\n\n\t// 載入檔案並處理每一行 / Load file and process each line\n\treturn load(file)\n\t\t.then(function (b)\n\t\t{\n\t\t\treturn handleDictLines(b, fn, {\n\t\t\t\tparseFn,\n\t\t\t})\n\t\t})\n\t\t;\n}\n\n/**\n * 檢查行類型\n * Check Line Type\n *\n * 分析行字串以判斷其類型（基礎行、註解行或標籤註解行）。\n * Analyzes a line string to determine its type (base, comment, or tagged comment).\n *\n * @param {string} line - 要檢查的行字串 / Line string to check\n * @returns {EnumLineType} 行類型列舉值 / Line type enumeration value\n */\nexport function chkLineType(line: string): EnumLineType\n{\n\t// 預設為基礎行類型 / Default to base line type\n\tlet ret = EnumLineType.BASE;\n\n\t// 檢查是否以 // 開頭，判斷為註解行 / Check if starts with //, identify as comment line\n\tif (line.indexOf('//') == 0)\n\t{\n\t\tret = EnumLineType.COMMENT;\n\n\t\t// 檢查是否包含特殊標籤如 @todo 或格式說明 / Check if contains special tags like @todo or format descriptions\n\t\tif (/^\\/\\/ +(?:\\@todo|格式\\:)/i.test(line))\n\t\t{\n\t\t\tret = EnumLineType.COMMENT_TAG;\n\t\t}\n\t}\n\n\treturn ret;\n}\n"],"names":["USE_CJK_MODE","EnumLineType","stringifyHandleDictLinesList","list","options","lines","map","v","line","disableUnique","array_unique","handleDictLines","fn","parseFn","reduce","a","index","bool","data","cur","push","loadDictFile","file","parseLineSegment","load","then","b","chkLineType","ret","BASE","indexOf","COMMENT","test","COMMENT_TAG"],"mappings":";;;;;;;;AAYO,MAAMA,YAAY,GAAG,EAAU;AASpBC,IAAAA,YAAY,0BAAZA,YAAY,EAAA;AAAZA,EAAAA,YAAY,CAAZA,YAAY,CAAA,MAAA,CAAA,GAAA,CAAA,CAAA,GAAA,MAAA,CAAA;AAAZA,EAAAA,YAAY,CAAZA,YAAY,CAAA,SAAA,CAAA,GAAA,CAAA,CAAA,GAAA,SAAA,CAAA;AAAZA,EAAAA,YAAY,CAAZA,YAAY,CAAA,aAAA,CAAA,GAAA,CAAA,CAAA,GAAA,aAAA,CAAA;AAAA,EAAA,OAAZA,YAAY,CAAA;AAAA,CAAA,CAAA,EAAA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