@dev-pi2pie/word-counter 0.1.5 → 0.1.6-canary.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +0,0 @@
1
- {"version":3,"file":"count-worker.mjs","names":["mergeAdjacentChunks","getNonWordTotal","collectNonWordsAggregate","normalizeText","buildPerKeyItems","wordCounter","buildSingleItem","sumTotals","wordCounter","wordCounter"],"sources":["../../../src/markdown/toml/arrays.ts","../../../src/markdown/toml/keys.ts","../../../src/markdown/toml/strings.ts","../../../src/markdown/toml/values.ts","../../../src/markdown/toml/parse-frontmatter.ts","../../../src/markdown/parse-markdown.ts","../../../src/wc/segmenter.ts","../../../src/utils/append-all.ts","../../../src/wc/non-words.ts","../../../src/wc/analyze.ts","../../../src/wc/mode.ts","../../../src/wc/latin-hints.ts","../../../src/wc/locale-detect.ts","../../../src/wc/segment.ts","../../../src/wc/wc.ts","../../../src/wc/index.ts","../../../src/markdown/section-count.ts","../../../src/detector/debug.ts","../../../src/detector/none.ts","../../../src/detector/result-builder.ts","../../../src/detector/sections.ts","../../../src/detector/policy.ts","../../../src/detector/whatlang-map.ts","../../../src/detector/wasm-presegment.ts","../../../src/detector/whatlang-wasm.ts","../../../src/detector/wasm-engine.ts","../../../src/detector/wasm-resolution.ts","../../../src/detector/wasm-windows.ts","../../../src/detector/wasm.ts","../../../src/detector/index.ts","../../../src/cli/batch/aggregate-compact.ts","../../../src/cli/path/load.ts","../../../src/cli/batch/jobs/worker/count-worker.ts"],"sourcesContent":["export function ensureArrayContainer(\n result: Record<string, unknown>,\n key: string,\n): Record<string, unknown>[] {\n const existing = result[key];\n if (Array.isArray(existing)) {\n return existing as Record<string, unknown>[];\n }\n const list: Record<string, unknown>[] = [];\n result[key] = list;\n return list;\n}\n\nexport function flattenArrayTables(result: Record<string, unknown>): void {\n for (const [key, value] of Object.entries(result)) {\n if (!Array.isArray(value)) {\n continue;\n }\n const flattened = value\n .map((entry) =>\n Object.entries(entry)\n .map(([entryKey, entryValue]) => `${entryKey}=${entryValue}`)\n .join(\", \"),\n )\n .join(\" | \");\n result[key] = flattened;\n }\n}\n","function stripKeyQuotes(key: string): string {\n const trimmed = key.trim();\n if (\n (trimmed.startsWith('\"') && trimmed.endsWith('\"')) ||\n (trimmed.startsWith(\"'\") && trimmed.endsWith(\"'\"))\n ) {\n return trimmed.slice(1, -1);\n }\n return trimmed;\n}\n\nexport function normalizeKeyPath(key: string): string | null {\n const trimmed = key.trim();\n if (!trimmed) {\n return null;\n }\n\n if (\n (trimmed.startsWith('\"') && trimmed.endsWith('\"')) ||\n (trimmed.startsWith(\"'\") && trimmed.endsWith(\"'\"))\n ) {\n const unquoted = stripKeyQuotes(trimmed);\n return unquoted ? unquoted : null;\n }\n\n const segments = trimmed.split(\".\").map((segment) => segment.trim());\n if (segments.some((segment) => !segment)) {\n return null;\n }\n return segments.join(\".\");\n}\n","export function stripInlineComment(line: string): string {\n let inString: \"single\" | \"double\" | null = null;\n let escaped = false;\n\n for (let i = 0; i < line.length; i += 1) {\n const char = line[i] ?? \"\";\n\n if (inString) {\n if (escaped) {\n escaped = false;\n continue;\n }\n\n if (char === \"\\\\\" && inString === \"double\") {\n escaped = true;\n continue;\n }\n\n if (inString === \"double\" && char === '\"') {\n inString = null;\n continue;\n }\n\n if (inString === \"single\" && char === \"'\") {\n inString = null;\n continue;\n }\n\n continue;\n }\n\n if (char === '\"') {\n inString = \"double\";\n continue;\n }\n\n if (char === \"'\") {\n inString = \"single\";\n continue;\n }\n\n if (char === \"#\") {\n return line.slice(0, i).trimEnd();\n }\n }\n\n return line;\n}\n\nfunction unescapeBasic(input: string): string {\n return input\n .replace(/\\\\\\\\/g, \"\\\\\")\n .replace(/\\\\\"/g, '\"')\n .replace(/\\\\n/g, \"\\n\")\n .replace(/\\\\t/g, \"\\t\")\n .replace(/\\\\r/g, \"\\r\");\n}\n\nexport function parseStringLiteral(value: string): string | null {\n if (value.startsWith('\"\"\"') && value.endsWith('\"\"\"')) {\n const inner = value.slice(3, -3);\n return unescapeBasic(inner);\n }\n\n if (value.startsWith(\"'''\") && value.endsWith(\"'''\")) {\n return value.slice(3, -3);\n }\n\n if (value.startsWith('\"') && value.endsWith('\"')) {\n return unescapeBasic(value.slice(1, -1));\n }\n\n if (value.startsWith(\"'\") && value.endsWith(\"'\")) {\n return value.slice(1, -1);\n }\n\n return null;\n}\n","import { normalizeKeyPath } from \"./keys\";\nimport { parseStringLiteral } from \"./strings\";\nimport type { TomlValue } from \"./types\";\n\nfunction parsePrimitive(raw: string): string | number | boolean | null {\n const value = raw.trim();\n if (!value) {\n return null;\n }\n\n const stringLiteral = parseStringLiteral(value);\n if (stringLiteral !== null) {\n return stringLiteral;\n }\n\n if (value === \"true\") {\n return true;\n }\n\n if (value === \"false\") {\n return false;\n }\n\n if (/^[+-]?\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?$/.test(value)) {\n return Number(value);\n }\n\n if (/^\\d{4}-\\d{2}-\\d{2}/.test(value)) {\n return value;\n }\n\n return value;\n}\n\nfunction parseArray(raw: string): Array<string | number | boolean> | null {\n const value = raw.trim();\n if (!value.startsWith(\"[\") || !value.endsWith(\"]\")) {\n return null;\n }\n\n const inner = value.slice(1, -1).trim();\n if (!inner) {\n return [];\n }\n\n const items: Array<string | number | boolean> = [];\n let current = \"\";\n let inString: \"single\" | \"double\" | null = null;\n let escaped = false;\n\n for (let i = 0; i < inner.length; i += 1) {\n const char = inner[i] ?? \"\";\n\n if (inString) {\n current += char;\n if (escaped) {\n escaped = false;\n continue;\n }\n\n if (char === \"\\\\\" && inString === \"double\") {\n escaped = true;\n continue;\n }\n\n if (inString === \"double\" && char === '\"') {\n inString = null;\n } else if (inString === \"single\" && char === \"'\") {\n inString = null;\n }\n continue;\n }\n\n if (char === '\"') {\n inString = \"double\";\n current += char;\n continue;\n }\n\n if (char === \"'\") {\n inString = \"single\";\n current += char;\n continue;\n }\n\n if (char === \",\") {\n const item = parsePrimitive(current);\n if (item === null) {\n return null;\n }\n items.push(item);\n current = \"\";\n continue;\n }\n\n current += char;\n }\n\n const finalItem = parsePrimitive(current);\n if (finalItem === null) {\n return null;\n }\n items.push(finalItem);\n\n return items;\n}\n\nfunction parseInlineTable(raw: string): Record<string, TomlValue> | null {\n const trimmed = raw.trim();\n if (!trimmed.startsWith(\"{\") || !trimmed.endsWith(\"}\")) {\n return null;\n }\n\n const inner = trimmed.slice(1, -1).trim();\n if (!inner) {\n return {};\n }\n\n const pairs: string[] = [];\n let current = \"\";\n let inString: \"single\" | \"double\" | null = null;\n let escaped = false;\n let bracketDepth = 0;\n let braceDepth = 0;\n\n for (let i = 0; i < inner.length; i += 1) {\n const char = inner[i] ?? \"\";\n\n if (inString) {\n current += char;\n if (escaped) {\n escaped = false;\n continue;\n }\n\n if (char === \"\\\\\" && inString === \"double\") {\n escaped = true;\n continue;\n }\n\n if (inString === \"double\" && char === '\"') {\n inString = null;\n } else if (inString === \"single\" && char === \"'\") {\n inString = null;\n }\n continue;\n }\n\n if (char === '\"') {\n inString = \"double\";\n current += char;\n continue;\n }\n\n if (char === \"'\") {\n inString = \"single\";\n current += char;\n continue;\n }\n\n if (char === \"[\") {\n bracketDepth += 1;\n current += char;\n continue;\n }\n\n if (char === \"]\") {\n if (bracketDepth > 0) {\n bracketDepth -= 1;\n }\n current += char;\n continue;\n }\n\n if (char === \"{\") {\n braceDepth += 1;\n current += char;\n continue;\n }\n\n if (char === \"}\") {\n if (braceDepth > 0) {\n braceDepth -= 1;\n }\n current += char;\n continue;\n }\n\n if (char === \",\" && bracketDepth === 0 && braceDepth === 0) {\n pairs.push(current);\n current = \"\";\n continue;\n }\n\n current += char;\n }\n\n if (current.trim()) {\n pairs.push(current);\n }\n\n const output: Record<string, TomlValue> = {};\n for (const pair of pairs) {\n const separatorIndex = pair.indexOf(\"=\");\n if (separatorIndex === -1) {\n return null;\n }\n const key = normalizeKeyPath(pair.slice(0, separatorIndex));\n if (!key) {\n return null;\n }\n const valueRaw = pair.slice(separatorIndex + 1).trim();\n if (!valueRaw) {\n return null;\n }\n if (valueRaw.startsWith(\"{\")) {\n return null;\n }\n const normalized = normalizeValue(valueRaw);\n if (normalized === null) {\n return null;\n }\n if (typeof normalized === \"object\" && !Array.isArray(normalized)) {\n return null;\n }\n output[key] = normalized;\n }\n\n return output;\n}\n\nexport function normalizeValue(value: string): TomlValue | null {\n if (!value) {\n return null;\n }\n\n const trimmed = value.trim();\n if (trimmed.startsWith(\"{\") && trimmed.endsWith(\"}\")) {\n return parseInlineTable(trimmed);\n }\n\n const array = parseArray(trimmed);\n if (array) {\n return array;\n }\n\n if (trimmed.startsWith(\"[\") && trimmed.endsWith(\"]\")) {\n return null;\n }\n\n return parsePrimitive(trimmed);\n}\n\nexport function toPlainText(value: unknown): string {\n if (value == null) {\n return \"\";\n }\n if (Array.isArray(value)) {\n return value.map((item) => String(item)).join(\", \");\n }\n return String(value);\n}\n","import { ensureArrayContainer, flattenArrayTables } from \"./arrays\";\nimport { normalizeKeyPath } from \"./keys\";\nimport { stripInlineComment } from \"./strings\";\nimport { normalizeValue, toPlainText } from \"./values\";\n\nexport function parseTomlFrontmatter(frontmatter: string): Record<string, unknown> | null {\n const result: Record<string, unknown> = {};\n const lines = frontmatter.split(\"\\n\");\n let tablePrefix = \"\";\n let tableTarget: Record<string, unknown> | null = null;\n let tablePrefixInList = false;\n\n for (let index = 0; index < lines.length; index += 1) {\n const rawLine = lines[index] ?? \"\";\n const trimmedLine = rawLine.trim();\n if (!trimmedLine || trimmedLine.startsWith(\"#\")) {\n continue;\n }\n\n if (trimmedLine.startsWith(\"[[\")) {\n const match = trimmedLine.match(/^\\[\\[([^\\]]+)]]$/);\n if (!match) {\n return null;\n }\n const normalizedTable = normalizeKeyPath(match[1] ?? \"\");\n if (!normalizedTable) {\n return null;\n }\n const list = ensureArrayContainer(result, normalizedTable);\n const newEntry: Record<string, unknown> = {};\n list.push(newEntry);\n tableTarget = newEntry;\n tablePrefix = normalizedTable;\n tablePrefixInList = true;\n continue;\n }\n\n const tableMatch = trimmedLine.match(/^\\[([^\\]]+)]$/);\n if (tableMatch) {\n const normalizedTable = normalizeKeyPath(tableMatch[1] ?? \"\");\n if (!normalizedTable) {\n return null;\n }\n tablePrefix = normalizedTable;\n tablePrefixInList = false;\n tableTarget = null;\n continue;\n }\n\n const lineForParsing = /(\"\"\"|''')/.test(rawLine) ? rawLine : stripInlineComment(rawLine);\n const separatorIndex = lineForParsing.indexOf(\"=\");\n if (separatorIndex === -1) {\n return null;\n }\n\n const keyRaw = lineForParsing.slice(0, separatorIndex);\n const key = normalizeKeyPath(keyRaw);\n let valueRaw = lineForParsing.slice(separatorIndex + 1).trim();\n if (!key) {\n return null;\n }\n\n const tripleDelimiter = valueRaw.startsWith('\"\"\"')\n ? '\"\"\"'\n : valueRaw.startsWith(\"'''\")\n ? \"'''\"\n : null;\n if (tripleDelimiter) {\n const closingIndex = valueRaw.indexOf(tripleDelimiter, tripleDelimiter.length);\n if (closingIndex !== -1) {\n const after = valueRaw.slice(closingIndex + tripleDelimiter.length);\n const strippedAfter = stripInlineComment(after);\n valueRaw = `${valueRaw.slice(0, closingIndex + tripleDelimiter.length)}${strippedAfter}`;\n } else {\n const delimiter = tripleDelimiter;\n let combined = valueRaw;\n let closed = false;\n while (index + 1 < lines.length) {\n index += 1;\n const nextLine = lines[index] ?? \"\";\n combined += `\\n${nextLine}`;\n if (new RegExp(`${delimiter}\\\\s*$`).test(nextLine)) {\n closed = true;\n break;\n }\n }\n if (!closed) {\n return null;\n }\n valueRaw = combined;\n }\n }\n\n const normalized = normalizeValue(valueRaw);\n if (normalized === null) {\n return null;\n }\n\n const fullKey = tablePrefix ? `${tablePrefix}.${key}` : key;\n if (typeof normalized === \"object\" && !Array.isArray(normalized)) {\n for (const [inlineKey, inlineValue] of Object.entries(normalized)) {\n const entryKey = tablePrefixInList ? `${key}.${inlineKey}` : `${fullKey}.${inlineKey}`;\n if (tablePrefixInList && tableTarget) {\n tableTarget[entryKey] = toPlainText(inlineValue);\n } else {\n result[entryKey] = toPlainText(inlineValue);\n }\n }\n continue;\n }\n\n if (tablePrefixInList && tableTarget) {\n tableTarget[key] = toPlainText(normalized);\n continue;\n }\n\n result[fullKey] = toPlainText(normalized);\n }\n\n flattenArrayTables(result);\n\n return result;\n}\n","import { parseDocument } from \"yaml\";\nimport { parseTomlFrontmatter } from \"./toml-simple\";\nimport type { FrontmatterType, ParsedMarkdown } from \"./types\";\n\nconst FENCE_TO_TYPE: Record<string, FrontmatterType> = {\n \"---\": \"yaml\",\n \"+++\": \"toml\",\n \";;;\": \"json\",\n};\n\nfunction normalizeNewlines(input: string): string {\n return input.replace(/\\r\\n/g, \"\\n\");\n}\n\nfunction stripBom(line: string): string {\n return line.startsWith(\"\\uFEFF\") ? line.slice(1) : line;\n}\n\nfunction getFenceType(line: string): FrontmatterType | null {\n const match = line.match(/^[\\t ]*(---|\\+\\+\\+|;;;)[\\t ]*$/);\n if (!match) {\n return null;\n }\n return FENCE_TO_TYPE[match[1] ?? \"\"] ?? null;\n}\n\nfunction parseFrontmatter(\n frontmatter: string,\n type: FrontmatterType | null,\n): Record<string, unknown> | null {\n if (!type) {\n return null;\n }\n\n if (type === \"json\") {\n try {\n return JSON.parse(frontmatter) as Record<string, unknown>;\n } catch {\n return null;\n }\n }\n\n if (type === \"yaml\") {\n const doc = parseDocument(frontmatter, { prettyErrors: false });\n if (doc.errors.length > 0) {\n return null;\n }\n const data = doc.toJSON();\n if (!data || typeof data !== \"object\" || Array.isArray(data)) {\n return null;\n }\n return data as Record<string, unknown>;\n }\n\n if (type === \"toml\") {\n return parseTomlFrontmatter(frontmatter);\n }\n\n return null;\n}\n\nfunction extractJsonBlock(\n text: string,\n startIndex: number,\n): { jsonText: string; endIndex: number } | null {\n let depth = 0;\n let inString = false;\n let escaped = false;\n\n for (let i = startIndex; i < text.length; i += 1) {\n const char = text[i] ?? \"\";\n\n if (inString) {\n if (escaped) {\n escaped = false;\n continue;\n }\n\n if (char === \"\\\\\") {\n escaped = true;\n continue;\n }\n\n if (char === '\"') {\n inString = false;\n }\n\n continue;\n }\n\n if (char === '\"') {\n inString = true;\n continue;\n }\n\n if (char === \"{\") {\n depth += 1;\n continue;\n }\n\n if (char === \"}\") {\n depth -= 1;\n if (depth === 0) {\n const jsonText = text.slice(startIndex, i + 1);\n return { jsonText, endIndex: i };\n }\n }\n }\n\n return null;\n}\n\nexport function parseMarkdown(input: string): ParsedMarkdown {\n const normalized = normalizeNewlines(input);\n const lines = normalized.split(\"\\n\");\n if (lines.length === 0) {\n return { frontmatter: null, content: normalized, data: null, frontmatterType: null };\n }\n\n lines[0] = stripBom(lines[0] ?? \"\");\n const normalizedWithoutBom = lines.join(\"\\n\");\n\n const openingType = getFenceType(lines[0] ?? \"\");\n if (!openingType) {\n const leadingWhitespace = normalizedWithoutBom.match(/^[\\t \\n]*/)?.[0] ?? \"\";\n const jsonStart = leadingWhitespace.length;\n if (normalizedWithoutBom[jsonStart] !== \"{\") {\n return {\n frontmatter: null,\n content: normalizedWithoutBom,\n data: null,\n frontmatterType: null,\n };\n }\n\n const jsonBlock = extractJsonBlock(normalizedWithoutBom, jsonStart);\n if (!jsonBlock) {\n return {\n frontmatter: null,\n content: normalizedWithoutBom,\n data: null,\n frontmatterType: null,\n };\n }\n\n const frontmatter = jsonBlock.jsonText;\n let content = normalizedWithoutBom.slice(jsonBlock.endIndex + 1);\n if (content.startsWith(\"\\n\")) {\n content = content.slice(1);\n }\n const data = parseFrontmatter(frontmatter, \"json\");\n if (!data) {\n return {\n frontmatter: null,\n content: normalizedWithoutBom,\n data: null,\n frontmatterType: null,\n };\n }\n\n return {\n frontmatter,\n content,\n data,\n frontmatterType: \"json\",\n };\n }\n\n let closingIndex = -1;\n for (let i = 1; i < lines.length; i += 1) {\n if (getFenceType(lines[i] ?? \"\") === openingType) {\n closingIndex = i;\n break;\n }\n }\n\n if (closingIndex === -1) {\n return { frontmatter: null, content: normalizedWithoutBom, data: null, frontmatterType: null };\n }\n\n const frontmatter = lines.slice(1, closingIndex).join(\"\\n\");\n const content = lines.slice(closingIndex + 1).join(\"\\n\");\n const data = parseFrontmatter(frontmatter, openingType);\n\n return {\n frontmatter,\n content,\n data,\n frontmatterType: openingType,\n };\n}\n","const segmenterCache = new Map<string, Intl.Segmenter>();\nconst graphemeSegmenterCache = new Map<string, Intl.Segmenter>();\n\nexport function getSegmenter(locale: string): Intl.Segmenter {\n const cached = segmenterCache.get(locale);\n if (cached) {\n return cached;\n }\n const segmenter = new Intl.Segmenter(locale, { granularity: \"word\" });\n segmenterCache.set(locale, segmenter);\n return segmenter;\n}\n\nexport function getGraphemeSegmenter(locale: string): Intl.Segmenter {\n const cached = graphemeSegmenterCache.get(locale);\n if (cached) {\n return cached;\n }\n const segmenter = new Intl.Segmenter(locale, { granularity: \"grapheme\" });\n graphemeSegmenterCache.set(locale, segmenter);\n return segmenter;\n}\n\nfunction supportsSegmenter(): boolean {\n return typeof Intl !== \"undefined\" && typeof Intl.Segmenter === \"function\";\n}\n\nexport function countWordsForLocale(text: string, locale: string): number {\n const segmenter = getSegmenter(locale);\n let count = 0;\n for (const segment of segmenter.segment(text)) {\n if (segment.isWordLike) {\n count++;\n }\n }\n return count;\n}\n\nexport function countCharsForLocale(text: string, locale: string): number {\n if (!supportsSegmenter()) {\n return Array.from(text).length;\n }\n const segmenter = getGraphemeSegmenter(locale);\n let count = 0;\n for (const _segment of segmenter.segment(text)) {\n count++;\n }\n return count;\n}\n","export function appendAll<T>(target: T[], source: readonly T[]): void {\n for (const item of source) {\n target.push(item);\n }\n}\n","import type { NonWordCollection, WhitespaceCounts } from \"./types\";\nimport { appendAll } from \"../utils/append-all\";\n\nconst emojiRegex = /(?:\\p{Extended_Pictographic}|\\p{Emoji_Presentation})/u;\nconst emojiPresentationRegex = /\\p{Emoji_Presentation}/u;\nconst keycapEmojiRegex = /[0-9#*]\\uFE0F?\\u20E3/u;\nconst symbolRegex = /\\p{S}/u;\nconst punctuationRegex = /\\p{P}/u;\nconst whitespaceRegex = /\\s/u;\nconst newlineChars = new Set([\"\\n\", \"\\r\", \"\\u2028\", \"\\u2029\"]);\n\nexport function createNonWordCollection(): NonWordCollection {\n return {\n emoji: [],\n symbols: [],\n punctuation: [],\n counts: {\n emoji: 0,\n symbols: 0,\n punctuation: 0,\n },\n };\n}\n\nexport function addNonWord(\n collection: NonWordCollection,\n category: \"emoji\" | \"symbol\" | \"punctuation\",\n segment: string,\n): void {\n if (category === \"emoji\") {\n collection.emoji.push(segment);\n collection.counts.emoji += 1;\n return;\n }\n if (category === \"symbol\") {\n collection.symbols.push(segment);\n collection.counts.symbols += 1;\n return;\n }\n collection.punctuation.push(segment);\n collection.counts.punctuation += 1;\n}\n\nexport function addWhitespace(collection: NonWordCollection, segment: string): number {\n let whitespace = collection.whitespace;\n let count = 0;\n for (const char of segment) {\n if (char === \" \") {\n whitespace = whitespace ?? createWhitespaceCounts();\n whitespace.spaces += 1;\n count += 1;\n continue;\n }\n if (char === \"\\t\") {\n whitespace = whitespace ?? createWhitespaceCounts();\n whitespace.tabs += 1;\n count += 1;\n continue;\n }\n if (newlineChars.has(char)) {\n whitespace = whitespace ?? createWhitespaceCounts();\n whitespace.newlines += 1;\n count += 1;\n continue;\n }\n if (whitespaceRegex.test(char)) {\n whitespace = whitespace ?? createWhitespaceCounts();\n whitespace.other += 1;\n count += 1;\n }\n }\n\n if (count > 0) {\n collection.whitespace = whitespace ?? createWhitespaceCounts();\n collection.counts.whitespace = (collection.counts.whitespace ?? 0) + count;\n }\n\n return count;\n}\n\nexport function classifyNonWordSegment(segment: string): \"emoji\" | \"symbol\" | \"punctuation\" | null {\n const hasEmojiVariationSelector = segment.includes(\"\\uFE0F\");\n if (\n keycapEmojiRegex.test(segment) ||\n emojiPresentationRegex.test(segment) ||\n (hasEmojiVariationSelector && emojiRegex.test(segment))\n ) {\n return \"emoji\";\n }\n if (symbolRegex.test(segment)) {\n return \"symbol\";\n }\n if (punctuationRegex.test(segment)) {\n return \"punctuation\";\n }\n return null;\n}\n\nexport function mergeNonWordCollections(\n target: NonWordCollection,\n source: NonWordCollection,\n): NonWordCollection {\n if (source.counts.emoji > 0) {\n appendAll(target.emoji, source.emoji);\n target.counts.emoji += source.counts.emoji;\n }\n if (source.counts.symbols > 0) {\n appendAll(target.symbols, source.symbols);\n target.counts.symbols += source.counts.symbols;\n }\n if (source.counts.punctuation > 0) {\n appendAll(target.punctuation, source.punctuation);\n target.counts.punctuation += source.counts.punctuation;\n }\n if (source.counts.whitespace && source.counts.whitespace > 0 && source.whitespace) {\n const whitespace = target.whitespace ?? createWhitespaceCounts();\n whitespace.spaces += source.whitespace.spaces;\n whitespace.tabs += source.whitespace.tabs;\n whitespace.newlines += source.whitespace.newlines;\n whitespace.other += source.whitespace.other;\n target.whitespace = whitespace;\n target.counts.whitespace = (target.counts.whitespace ?? 0) + source.counts.whitespace;\n }\n return target;\n}\n\nfunction createWhitespaceCounts(): WhitespaceCounts {\n return { spaces: 0, tabs: 0, newlines: 0, other: 0 };\n}\n","import { countCharsForLocale, getSegmenter } from \"./segmenter\";\nimport {\n addNonWord,\n addWhitespace,\n classifyNonWordSegment,\n createNonWordCollection,\n mergeNonWordCollections,\n} from \"./non-words\";\nimport { appendAll } from \"../utils/append-all\";\nimport type {\n CharCollectorBreakdown,\n ChunkAnalysis,\n CollectorBreakdown,\n LocaleChunk,\n NonWordCollection,\n} from \"./types\";\n\ntype CharAnalysis = LocaleChunk & { chars: number; nonWords?: NonWordCollection };\ntype CharChunkAnalysis = CharAnalysis & { wordChars: number; nonWordChars: number };\n\nexport function analyzeChunk(\n chunk: LocaleChunk,\n collectNonWords?: boolean,\n includeWhitespace?: boolean,\n): ChunkAnalysis {\n const segmenter = getSegmenter(chunk.locale);\n const segments: string[] = [];\n const nonWords: NonWordCollection | null = collectNonWords ? createNonWordCollection() : null;\n for (const part of segmenter.segment(chunk.text)) {\n if (part.isWordLike) {\n segments.push(part.segment);\n } else if (collectNonWords && nonWords) {\n if (includeWhitespace) {\n addWhitespace(nonWords, part.segment);\n }\n const category = classifyNonWordSegment(part.segment);\n if (category) {\n addNonWord(nonWords, category, part.segment);\n }\n }\n }\n return {\n locale: chunk.locale,\n text: chunk.text,\n segments,\n words: segments.length,\n nonWords: nonWords ?? undefined,\n };\n}\n\nexport function analyzeCharChunk(\n chunk: LocaleChunk,\n collectNonWords?: boolean,\n includeWhitespace?: boolean,\n): CharChunkAnalysis {\n const segmenter = getSegmenter(chunk.locale);\n const nonWords: NonWordCollection | null = collectNonWords ? createNonWordCollection() : null;\n let chars = 0;\n let wordChars = 0;\n let nonWordChars = 0;\n\n for (const part of segmenter.segment(chunk.text)) {\n if (part.isWordLike) {\n const count = countCharsForLocale(part.segment, chunk.locale);\n chars += count;\n wordChars += count;\n continue;\n }\n\n if (collectNonWords && nonWords) {\n let whitespaceCount = 0;\n if (includeWhitespace) {\n whitespaceCount = addWhitespace(nonWords, part.segment);\n }\n const category = classifyNonWordSegment(part.segment);\n if (category) {\n addNonWord(nonWords, category, part.segment);\n }\n if (category || whitespaceCount > 0) {\n const count = countCharsForLocale(part.segment, chunk.locale);\n chars += count;\n nonWordChars += count;\n }\n }\n }\n\n return {\n locale: chunk.locale,\n text: chunk.text,\n chars,\n wordChars,\n nonWordChars,\n nonWords: nonWords ?? undefined,\n };\n}\n\nexport function aggregateCharsByLocale(\n chunks: CharChunkAnalysis[],\n): Array<CharCollectorBreakdown & { wordChars: number; nonWordChars: number }> {\n const order: string[] = [];\n const map = new Map<\n string,\n CharCollectorBreakdown & { wordChars: number; nonWordChars: number }\n >();\n\n for (const chunk of chunks) {\n const existing = map.get(chunk.locale);\n if (existing) {\n existing.chars += chunk.chars;\n existing.wordChars += chunk.wordChars;\n existing.nonWordChars += chunk.nonWordChars;\n if (chunk.nonWords) {\n if (!existing.nonWords) {\n existing.nonWords = createNonWordCollection();\n }\n mergeNonWordCollections(existing.nonWords, chunk.nonWords);\n }\n continue;\n }\n\n order.push(chunk.locale);\n map.set(chunk.locale, {\n locale: chunk.locale,\n chars: chunk.chars,\n wordChars: chunk.wordChars,\n nonWordChars: chunk.nonWordChars,\n nonWords: chunk.nonWords\n ? mergeNonWordCollections(createNonWordCollection(), chunk.nonWords)\n : undefined,\n });\n }\n\n return order.map((locale) => map.get(locale)!);\n}\n\nexport function aggregateByLocale(chunks: ChunkAnalysis[]): CollectorBreakdown[] {\n const order: string[] = [];\n const map = new Map<string, CollectorBreakdown>();\n\n for (const chunk of chunks) {\n const existing = map.get(chunk.locale);\n if (existing) {\n existing.words += chunk.words;\n appendAll(existing.segments, chunk.segments);\n continue;\n }\n\n order.push(chunk.locale);\n map.set(chunk.locale, {\n locale: chunk.locale,\n words: chunk.words,\n segments: [...chunk.segments],\n });\n }\n\n return order.map((locale) => map.get(locale)!);\n}\n","import type { WordCounterMode } from \"./types\";\n\nconst MODE_ALIASES: Record<string, WordCounterMode> = {\n chunk: \"chunk\",\n chunks: \"chunk\",\n segments: \"segments\",\n segment: \"segments\",\n seg: \"segments\",\n collector: \"collector\",\n collect: \"collector\",\n colle: \"collector\",\n char: \"char\",\n chars: \"char\",\n character: \"char\",\n characters: \"char\",\n \"char-collector\": \"char-collector\",\n};\n\nconst CHAR_MODE_ALIASES = new Set([\"char\", \"chars\", \"character\", \"characters\"]);\nconst COLLECTOR_MODE_ALIASES = new Set([\"collector\", \"collect\", \"colle\", \"col\"]);\n\nfunction collapseSeparators(value: string): string {\n return value.replace(/[-_\\s]+/g, \"\");\n}\n\nfunction isComposedCharCollectorFromTokens(value: string): boolean {\n const tokens = value\n .split(/[-_\\s]+/)\n .map((token) => token.trim())\n .filter((token) => token.length > 0);\n if (tokens.length < 2) {\n return false;\n }\n\n let hasCharAlias = false;\n let hasCollectorAlias = false;\n for (const token of tokens) {\n if (CHAR_MODE_ALIASES.has(token)) {\n hasCharAlias = true;\n continue;\n }\n if (COLLECTOR_MODE_ALIASES.has(token)) {\n hasCollectorAlias = true;\n continue;\n }\n return false;\n }\n\n return hasCharAlias && hasCollectorAlias;\n}\n\nfunction isComposedCharCollectorCompact(value: string): boolean {\n for (const charAlias of CHAR_MODE_ALIASES) {\n for (const collectorAlias of COLLECTOR_MODE_ALIASES) {\n if (value === `${charAlias}${collectorAlias}` || value === `${collectorAlias}${charAlias}`) {\n return true;\n }\n }\n }\n return false;\n}\n\nexport function normalizeMode(input?: string | null): WordCounterMode | null {\n if (!input) {\n return null;\n }\n const normalized = input.trim().toLowerCase();\n const direct = MODE_ALIASES[normalized];\n if (direct) {\n return direct;\n }\n\n if (isComposedCharCollectorFromTokens(normalized)) {\n return \"char-collector\";\n }\n\n const compact = collapseSeparators(normalized);\n if (isComposedCharCollectorCompact(compact)) {\n return \"char-collector\";\n }\n\n return MODE_ALIASES[compact] ?? null;\n}\n\nexport function resolveMode(\n input?: string | null,\n fallback: WordCounterMode = \"chunk\",\n): WordCounterMode {\n return normalizeMode(input) ?? fallback;\n}\n","import type { LatinHintRule } from \"./types\";\n\nconst DEFAULT_LATIN_HINT_RULES_SOURCE = [\n { tag: \"de\", pattern: \"[äöüÄÖÜß]\" },\n { tag: \"es\", pattern: \"[ñÑ¿¡]\" },\n { tag: \"pt\", pattern: \"[ãõÃÕ]\" },\n { tag: \"fr\", pattern: \"[œŒæÆ]\" },\n { tag: \"pl\", pattern: \"[ąćęłńśźżĄĆĘŁŃŚŹŻ]\" },\n { tag: \"tr\", pattern: \"[ıİğĞşŞ]\" },\n { tag: \"ro\", pattern: \"[ăĂâÂîÎșȘțȚ]\" },\n { tag: \"hu\", pattern: \"[őŐűŰ]\" },\n { tag: \"is\", pattern: \"[ðÐþÞ]\" },\n] satisfies LatinHintRule[];\n\nexport const DEFAULT_LATIN_HINT_RULES: ReadonlyArray<Readonly<LatinHintRule>> = Object.freeze(\n DEFAULT_LATIN_HINT_RULES_SOURCE.map((rule) => Object.freeze({ ...rule })),\n);\n","import type { LatinHintRule } from \"./types\";\nimport { DEFAULT_LATIN_HINT_RULES } from \"./latin-hints\";\n\nexport const DEFAULT_LOCALE = \"und-Latn\";\nexport const DEFAULT_HAN_TAG = \"und-Hani\";\n\nconst MAX_LATIN_HINT_PATTERN_LENGTH = 256;\n\nexport interface LocaleDetectOptions {\n latinLanguageHint?: string;\n latinTagHint?: string;\n latinLocaleHint?: string;\n latinHintRules?: LatinHintRule[];\n useDefaultLatinHints?: boolean;\n hanLanguageHint?: string;\n hanTagHint?: string;\n}\n\ntype ResolvedLatinHintRule = {\n tag: string;\n pattern: RegExp;\n priority: number;\n order: number;\n};\n\nexport type LocaleDetectContext = {\n latinHint?: string;\n hanHint?: string;\n latinHintRules: ResolvedLatinHintRule[];\n latinLocales: Set<string>;\n};\n\nexport type LocaleDetectTraceSource = \"script\" | \"hint\" | \"fallback\";\nexport type LocaleDetectTrace = {\n locale: string | null;\n source?: LocaleDetectTraceSource;\n reason?: string;\n};\n\nconst regex = {\n hiragana: /\\p{Script=Hiragana}/u,\n katakana: /\\p{Script=Katakana}/u,\n hangul: /\\p{Script=Hangul}/u,\n han: /\\p{Script=Han}/u,\n latin: /\\p{Script=Latin}/u,\n arabic: /\\p{Script=Arabic}/u,\n cyrillic: /\\p{Script=Cyrillic}/u,\n devanagari: /\\p{Script=Devanagari}/u,\n thai: /\\p{Script=Thai}/u,\n};\n\nconst defaultLatinLocales = new Set<string>([\n DEFAULT_LOCALE,\n ...DEFAULT_LATIN_HINT_RULES.map((hint) => hint.tag),\n]);\n\nexport function isLatinLocale(locale: string, context?: LocaleDetectContext): boolean {\n if (context) {\n return context.latinLocales.has(locale);\n }\n return defaultLatinLocales.has(locale);\n}\n\nfunction resolveLatinHint(options: LocaleDetectOptions): string | undefined {\n const latinTagHint = options.latinTagHint?.trim();\n if (latinTagHint) {\n return latinTagHint;\n }\n\n const latinLanguageHint = options.latinLanguageHint?.trim();\n if (latinLanguageHint) {\n return latinLanguageHint;\n }\n\n const latinLocaleHint = options.latinLocaleHint?.trim();\n if (latinLocaleHint) {\n return latinLocaleHint;\n }\n\n return undefined;\n}\n\nfunction resolveHanHint(options: LocaleDetectOptions): string | undefined {\n const hanTagHint = options.hanTagHint?.trim();\n if (hanTagHint) {\n return hanTagHint;\n }\n\n const hanLanguageHint = options.hanLanguageHint?.trim();\n if (hanLanguageHint) {\n return hanLanguageHint;\n }\n\n return undefined;\n}\n\nfunction compileLatinHintPattern(pattern: string | RegExp, label: string): RegExp {\n const source = typeof pattern === \"string\" ? pattern : pattern.source;\n const hasUnicodeMode =\n typeof pattern !== \"string\" && (pattern.flags.includes(\"u\") || pattern.flags.includes(\"v\"));\n const flags =\n typeof pattern === \"string\" ? \"u\" : hasUnicodeMode ? pattern.flags : `${pattern.flags}u`;\n if (source.length === 0) {\n throw new Error(`${label}: pattern must not be empty.`);\n }\n if (source.length > MAX_LATIN_HINT_PATTERN_LENGTH) {\n throw new Error(\n `${label}: pattern must be at most ${MAX_LATIN_HINT_PATTERN_LENGTH} characters.`,\n );\n }\n try {\n return new RegExp(source, flags);\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n throw new Error(`${label}: invalid Unicode regex pattern (${message}).`);\n }\n}\n\nfunction normalizeLatinHintPriority(priority: unknown, label: string): number {\n if (priority === undefined) {\n return 0;\n }\n if (typeof priority !== \"number\" || !Number.isFinite(priority)) {\n throw new Error(`${label}: priority must be a finite number when provided.`);\n }\n return priority;\n}\n\nfunction compileLatinHintRule(\n rule: Readonly<LatinHintRule>,\n order: number,\n label: string,\n): ResolvedLatinHintRule {\n const tag = typeof rule.tag === \"string\" ? rule.tag.trim() : \"\";\n if (!tag) {\n throw new Error(`${label}: tag must be a non-empty string.`);\n }\n const pattern = compileLatinHintPattern(rule.pattern, label);\n const priority = normalizeLatinHintPriority(rule.priority, label);\n return {\n tag,\n pattern,\n priority,\n order,\n };\n}\n\nfunction resolveLatinHintRules(options: LocaleDetectOptions): ResolvedLatinHintRule[] {\n const useDefaultLatinHints = options.useDefaultLatinHints !== false;\n const customRules = options.latinHintRules ?? [];\n const combinedRules: Array<{ rule: Readonly<LatinHintRule>; label: string }> = [];\n\n for (let index = 0; index < customRules.length; index += 1) {\n const rule = customRules[index];\n if (!rule) {\n continue;\n }\n combinedRules.push({\n rule,\n label: `Invalid custom Latin hint rule at index ${index}`,\n });\n }\n\n if (useDefaultLatinHints) {\n for (let index = 0; index < DEFAULT_LATIN_HINT_RULES.length; index += 1) {\n const rule = DEFAULT_LATIN_HINT_RULES[index];\n if (!rule) {\n continue;\n }\n combinedRules.push({\n rule,\n label: `Invalid default Latin hint rule at index ${index}`,\n });\n }\n }\n\n const resolvedRules = combinedRules.map((entry, index) =>\n compileLatinHintRule(entry.rule, index, entry.label),\n );\n\n resolvedRules.sort((left, right) => {\n if (left.priority !== right.priority) {\n return right.priority - left.priority;\n }\n return left.order - right.order;\n });\n\n return resolvedRules;\n}\n\nexport function resolveLocaleDetectContext(options: LocaleDetectOptions = {}): LocaleDetectContext {\n const latinHint = resolveLatinHint(options);\n const latinHintRules = resolveLatinHintRules(options);\n const latinLocales = new Set<string>([DEFAULT_LOCALE]);\n for (const rule of latinHintRules) {\n latinLocales.add(rule.tag);\n }\n if (latinHint) {\n latinLocales.add(latinHint);\n }\n\n return {\n latinHint,\n hanHint: resolveHanHint(options),\n latinHintRules,\n latinLocales,\n };\n}\n\nfunction detectLatinLocale(char: string, context: LocaleDetectContext): string {\n for (const hint of context.latinHintRules) {\n hint.pattern.lastIndex = 0;\n if (hint.pattern.test(char)) {\n return hint.tag;\n }\n }\n return DEFAULT_LOCALE;\n}\n\nexport function detectLocaleForCharTrace(\n char: string,\n previousLocale?: string | null,\n options: LocaleDetectOptions = {},\n context: LocaleDetectContext = resolveLocaleDetectContext(options),\n allowLatinLocaleCarry = true,\n allowJapaneseHanCarry = true,\n): LocaleDetectTrace {\n if (regex.hiragana.test(char) || regex.katakana.test(char)) {\n return {\n locale: \"ja\",\n source: \"script\",\n reason: \"hiragana-katakana\",\n };\n }\n if (regex.hangul.test(char)) {\n return {\n locale: \"ko\",\n source: \"script\",\n reason: \"hangul\",\n };\n }\n if (regex.arabic.test(char)) {\n return {\n locale: \"ar\",\n source: \"script\",\n reason: \"arabic\",\n };\n }\n if (regex.cyrillic.test(char)) {\n return {\n locale: \"ru\",\n source: \"script\",\n reason: \"cyrillic\",\n };\n }\n if (regex.devanagari.test(char)) {\n return {\n locale: \"hi\",\n source: \"script\",\n reason: \"devanagari\",\n };\n }\n if (regex.thai.test(char)) {\n return {\n locale: \"th\",\n source: \"script\",\n reason: \"thai\",\n };\n }\n\n if (regex.han.test(char)) {\n if (allowJapaneseHanCarry && previousLocale && previousLocale.startsWith(\"ja\")) {\n return {\n locale: previousLocale,\n source: \"script\",\n reason: \"japanese-han-carry\",\n };\n }\n if (context.hanHint) {\n return {\n locale: context.hanHint,\n source: \"hint\",\n reason: \"explicit-han-hint\",\n };\n }\n return {\n locale: DEFAULT_HAN_TAG,\n source: \"fallback\",\n reason:\n previousLocale && previousLocale.startsWith(\"ja\") && !allowJapaneseHanCarry\n ? \"han-fallback-after-boundary\"\n : \"han-fallback\",\n };\n }\n\n if (regex.latin.test(char)) {\n const hintedLocale = detectLatinLocale(char, context);\n if (hintedLocale !== DEFAULT_LOCALE) {\n return {\n locale: hintedLocale,\n source: \"hint\",\n reason: \"latin-hint-rule\",\n };\n }\n if (\n allowLatinLocaleCarry &&\n previousLocale &&\n isLatinLocale(previousLocale, context) &&\n previousLocale !== DEFAULT_LOCALE\n ) {\n return {\n locale: previousLocale,\n source: \"hint\",\n reason: \"latin-locale-carry\",\n };\n }\n if (context.latinHint) {\n return {\n locale: context.latinHint,\n source: \"hint\",\n reason: \"explicit-latin-hint\",\n };\n }\n return {\n locale: DEFAULT_LOCALE,\n source: \"fallback\",\n reason: \"latin-fallback\",\n };\n }\n\n return {\n locale: null,\n };\n}\n\nexport function detectLocaleForChar(\n char: string,\n previousLocale?: string | null,\n options: LocaleDetectOptions = {},\n context: LocaleDetectContext = resolveLocaleDetectContext(options),\n allowLatinLocaleCarry = true,\n allowJapaneseHanCarry = true,\n): string | null {\n return detectLocaleForCharTrace(\n char,\n previousLocale,\n options,\n context,\n allowLatinLocaleCarry,\n allowJapaneseHanCarry,\n ).locale;\n}\n","import {\n DEFAULT_LOCALE,\n detectLocaleForChar,\n isLatinLocale,\n resolveLocaleDetectContext,\n type LocaleDetectOptions,\n} from \"./locale-detect\";\nimport type { LocaleChunk } from \"./types\";\n\nconst HARD_BOUNDARY_REGEX = /[\\r\\n,.!?;:,、。!?;:.。、]/u;\nconst LATIN_PROMOTION_BREAK_REGEX = /[\\s,.!?;:,、。!?;:.。、]/u;\n\nexport function segmentTextByLocale(\n text: string,\n options: LocaleDetectOptions = {},\n): LocaleChunk[] {\n const context = resolveLocaleDetectContext(options);\n const chunks: LocaleChunk[] = [];\n // Keep currentLocale as a non-null string to simplify type-narrowing.\n let currentLocale: string = DEFAULT_LOCALE;\n let buffer = \"\";\n let bufferHasScript = false;\n let sawCarryBoundary = false;\n\n const updateCarryBoundaryState = (detected: string | null, char: string): void => {\n if (detected !== null) {\n sawCarryBoundary = false;\n return;\n }\n if (HARD_BOUNDARY_REGEX.test(char)) {\n sawCarryBoundary = true;\n }\n };\n\n for (const char of text) {\n const detected = detectLocaleForChar(\n char,\n currentLocale,\n options,\n context,\n !sawCarryBoundary,\n !sawCarryBoundary,\n );\n const targetLocale = detected ?? currentLocale;\n\n // If buffer is empty, this is the first character for a new chunk.\n if (buffer === \"\") {\n currentLocale = targetLocale;\n buffer = char;\n bufferHasScript = detected !== null;\n updateCarryBoundaryState(detected, char);\n continue;\n }\n\n if (detected !== null && !bufferHasScript) {\n currentLocale = targetLocale;\n buffer += char;\n bufferHasScript = true;\n updateCarryBoundaryState(detected, char);\n continue;\n }\n\n if (targetLocale !== currentLocale && detected !== null) {\n if (currentLocale === DEFAULT_LOCALE && isLatinLocale(targetLocale, context)) {\n const promotionBreakIndex = findLastLatinPromotionBreakIndex(buffer);\n if (promotionBreakIndex === -1) {\n currentLocale = targetLocale;\n buffer += char;\n bufferHasScript = true;\n updateCarryBoundaryState(detected, char);\n continue;\n }\n\n const prefix = buffer.slice(0, promotionBreakIndex + 1);\n const suffix = buffer.slice(promotionBreakIndex + 1);\n if (prefix.length > 0) {\n chunks.push({ locale: currentLocale, text: prefix });\n }\n currentLocale = targetLocale;\n buffer = `${suffix}${char}`;\n bufferHasScript = true;\n updateCarryBoundaryState(detected, char);\n continue;\n }\n // currentLocale is guaranteed to be a string here.\n chunks.push({ locale: currentLocale, text: buffer });\n currentLocale = targetLocale;\n buffer = char;\n bufferHasScript = true;\n updateCarryBoundaryState(detected, char);\n continue;\n }\n\n buffer += char;\n if (detected !== null) {\n bufferHasScript = true;\n }\n updateCarryBoundaryState(detected, char);\n }\n\n if (buffer.length > 0) {\n chunks.push({ locale: currentLocale, text: buffer });\n }\n\n return mergeAdjacentChunks(chunks);\n}\n\nfunction findLastLatinPromotionBreakIndex(buffer: string): number {\n for (let index = buffer.length - 1; index >= 0; index -= 1) {\n const char = buffer[index];\n if (!char) {\n continue;\n }\n if (LATIN_PROMOTION_BREAK_REGEX.test(char)) {\n return index;\n }\n }\n return -1;\n}\n\nfunction mergeAdjacentChunks(chunks: LocaleChunk[]): LocaleChunk[] {\n if (chunks.length === 0) {\n return chunks;\n }\n\n const merged: LocaleChunk[] = [];\n // We already returned for empty arrays above, so the first element is present.\n let last = chunks[0]!;\n\n for (let i = 1; i < chunks.length; i++) {\n const chunk = chunks[i]!;\n if (chunk.locale === last.locale) {\n last = { locale: last.locale, text: last.text + chunk.text };\n } else {\n merged.push(last);\n last = chunk;\n }\n }\n\n merged.push(last);\n return merged;\n}\n","import {\n analyzeCharChunk,\n analyzeChunk,\n aggregateByLocale,\n aggregateCharsByLocale,\n} from \"./analyze\";\nimport { resolveMode } from \"./mode\";\nimport { segmentTextByLocale } from \"./segment\";\nimport { countCharsForLocale, countWordsForLocale } from \"./segmenter\";\nimport { createNonWordCollection, mergeNonWordCollections } from \"./non-words\";\nimport type {\n CharBreakdown,\n CharCollectorBreakdown,\n ChunkBreakdown,\n ChunkWithSegments,\n NonWordCollection,\n WordCounterMode,\n WordCounterOptions,\n WordCounterResult,\n} from \"./types\";\n\nexport type {\n LatinHintRule,\n NonWordCollection,\n WordCounterMode,\n WordCounterOptions,\n WordCounterResult,\n WordCounterBreakdown,\n} from \"./types\";\n\nexport { countCharsForLocale, countWordsForLocale, segmentTextByLocale };\nexport { DEFAULT_LATIN_HINT_RULES } from \"./latin-hints\";\n\nexport function wordCounter(text: string, options: WordCounterOptions = {}): WordCounterResult {\n const mode: WordCounterMode = resolveMode(options.mode, \"chunk\");\n const collectNonWords = Boolean(options.nonWords);\n const includeWhitespace = Boolean(options.includeWhitespace);\n const chunks = segmentTextByLocale(text, {\n latinLanguageHint: options.latinLanguageHint,\n latinTagHint: options.latinTagHint,\n latinLocaleHint: options.latinLocaleHint,\n latinHintRules: options.latinHintRules,\n useDefaultLatinHints: options.useDefaultLatinHints,\n hanLanguageHint: options.hanLanguageHint,\n hanTagHint: options.hanTagHint,\n });\n\n if (mode === \"char\" || mode === \"char-collector\") {\n const analyzed = chunks.map((chunk) =>\n analyzeCharChunk(chunk, collectNonWords, includeWhitespace),\n );\n const total = analyzed.reduce((sum, chunk) => sum + chunk.chars, 0);\n const counts = collectNonWords\n ? {\n words: analyzed.reduce((sum, chunk) => sum + chunk.wordChars, 0),\n nonWords: analyzed.reduce((sum, chunk) => sum + chunk.nonWordChars, 0),\n total,\n }\n : undefined;\n\n if (mode === \"char\") {\n const items: CharBreakdown[] = analyzed.map((chunk) => ({\n locale: chunk.locale,\n text: chunk.text,\n chars: chunk.chars,\n nonWords: chunk.nonWords,\n }));\n return {\n total,\n counts,\n breakdown: {\n mode,\n items,\n },\n };\n }\n\n const aggregated = aggregateCharsByLocale(analyzed);\n const items: CharCollectorBreakdown[] = aggregated.map((chunk) => ({\n locale: chunk.locale,\n chars: chunk.chars,\n nonWords: chunk.nonWords,\n }));\n return {\n total,\n counts,\n breakdown: {\n mode,\n items,\n },\n };\n }\n\n const analyzed = chunks.map((chunk) => analyzeChunk(chunk, collectNonWords, includeWhitespace));\n const wordsTotal = analyzed.reduce((sum, chunk) => sum + chunk.words, 0);\n const nonWordsTotal = collectNonWords\n ? analyzed.reduce((sum, chunk) => {\n if (!chunk.nonWords) {\n return sum;\n }\n return sum + getNonWordTotal(chunk.nonWords);\n }, 0)\n : 0;\n const total = analyzed.reduce((sum, chunk) => {\n let chunkTotal = chunk.words;\n if (collectNonWords && chunk.nonWords) {\n chunkTotal += getNonWordTotal(chunk.nonWords);\n }\n return sum + chunkTotal;\n }, 0);\n\n const counts = collectNonWords\n ? { words: wordsTotal, nonWords: nonWordsTotal, total }\n : undefined;\n\n if (mode === \"segments\") {\n const items: ChunkWithSegments[] = analyzed.map((chunk) => ({\n locale: chunk.locale,\n text: chunk.text,\n words: chunk.words,\n segments: chunk.segments,\n nonWords: chunk.nonWords,\n }));\n return {\n total,\n counts,\n breakdown: {\n mode,\n items,\n },\n };\n }\n\n if (mode === \"collector\") {\n const items = aggregateByLocale(analyzed);\n const nonWords = collectNonWordsAggregate(analyzed, collectNonWords);\n return {\n total,\n counts,\n breakdown: {\n mode,\n items,\n nonWords,\n },\n };\n }\n\n const items: ChunkBreakdown[] = analyzed.map((chunk) => ({\n locale: chunk.locale,\n text: chunk.text,\n words: chunk.words,\n nonWords: chunk.nonWords,\n }));\n\n return {\n total,\n counts,\n breakdown: {\n mode,\n items,\n },\n };\n}\n\nfunction getNonWordTotal(nonWords: NonWordCollection): number {\n return (\n nonWords.counts.emoji +\n nonWords.counts.symbols +\n nonWords.counts.punctuation +\n (nonWords.counts.whitespace ?? 0)\n );\n}\n\nfunction collectNonWordsAggregate(\n analyzed: Array<{ nonWords?: NonWordCollection }>,\n enabled: boolean,\n): NonWordCollection | undefined {\n if (!enabled) {\n return undefined;\n }\n const collection = createNonWordCollection();\n for (const chunk of analyzed) {\n if (!chunk.nonWords) {\n continue;\n }\n mergeNonWordCollections(collection, chunk.nonWords);\n }\n return collection;\n}\n","import { wordCounter } from \"./wc\";\n\nexport default wordCounter;\nexport { countCharsForLocale, countWordsForLocale, segmentTextByLocale } from \"./wc\";\nexport { DEFAULT_LATIN_HINT_RULES } from \"./wc\";\nexport type {\n LatinHintRule,\n NonWordCollection,\n WordCounterMode,\n WordCounterOptions,\n WordCounterResult,\n WordCounterBreakdown,\n} from \"./wc\";\n","import type { WordCounterMode, WordCounterOptions, WordCounterResult } from \"../wc/types\";\nimport wordCounter from \"../wc\";\nimport { parseMarkdown } from \"./parse-markdown\";\nimport type { SectionMode, SectionedResult } from \"./types\";\n\nfunction normalizeText(value: unknown): string {\n if (value == null) {\n return \"\";\n }\n if (typeof value === \"string\") {\n return value;\n }\n if (typeof value === \"number\" || typeof value === \"boolean\") {\n return String(value);\n }\n try {\n return JSON.stringify(value);\n } catch {\n return String(value);\n }\n}\n\nfunction buildPerKeyItems(\n data: Record<string, unknown> | null,\n mode: WordCounterMode,\n options: WordCounterOptions,\n): Array<{ name: string; source: \"frontmatter\"; result: WordCounterResult }> {\n if (!data || typeof data !== \"object\" || Array.isArray(data)) {\n return [];\n }\n\n return Object.entries(data).map(([key, value]) => {\n const valueText = normalizeText(value);\n const text = valueText ? `${key}: ${valueText}` : key;\n return {\n name: key,\n source: \"frontmatter\",\n result: wordCounter(text, options),\n };\n });\n}\n\nfunction buildSingleItem(\n name: string,\n text: string,\n mode: WordCounterMode,\n options: WordCounterOptions,\n source: \"frontmatter\" | \"content\",\n) {\n return [{ name, source, result: wordCounter(text, options) }];\n}\n\nfunction sumTotals(items: Array<{ result: WordCounterResult }>): number {\n return items.reduce((sum, item) => sum + item.result.total, 0);\n}\n\nexport function countSections(\n input: string,\n section: SectionMode,\n options: WordCounterOptions = {},\n): SectionedResult {\n const mode: WordCounterMode = options.mode ?? \"chunk\";\n if (section === \"all\") {\n const result = wordCounter(input, options);\n return {\n section,\n total: result.total,\n frontmatterType: null,\n items: [{ name: \"all\", source: \"content\", result }],\n };\n }\n\n const parsed = parseMarkdown(input);\n const frontmatterText = parsed.frontmatter ?? \"\";\n const contentText = parsed.content ?? \"\";\n\n let items: Array<{ name: string; source: \"frontmatter\" | \"content\"; result: WordCounterResult }> =\n [];\n\n if (section === \"frontmatter\") {\n items = buildSingleItem(\"frontmatter\", frontmatterText, mode, options, \"frontmatter\");\n } else if (section === \"content\") {\n items = buildSingleItem(\"content\", contentText, mode, options, \"content\");\n } else if (section === \"split\") {\n items = [\n ...buildSingleItem(\"frontmatter\", frontmatterText, mode, options, \"frontmatter\"),\n ...buildSingleItem(\"content\", contentText, mode, options, \"content\"),\n ];\n } else if (section === \"per-key\") {\n items = buildPerKeyItems(parsed.data, mode, options);\n } else if (section === \"split-per-key\") {\n items = [\n ...buildPerKeyItems(parsed.data, mode, options),\n ...buildSingleItem(\"content\", contentText, mode, options, \"content\"),\n ];\n }\n\n return {\n section,\n total: sumTotals(items),\n frontmatterType: parsed.frontmatterType,\n items,\n };\n}\n","import type { SectionMode } from \"../markdown\";\nimport { DEFAULT_HAN_TAG, DEFAULT_LOCALE } from \"../wc/locale-detect\";\nimport type { WordCounterMode } from \"../wc/types\";\nimport type { DetectorRouteTag } from \"./policy\";\nimport type { DetectorMode } from \"./types\";\n\nexport type DetectorDebugVerbosity = \"compact\" | \"verbose\";\nexport const DETECTOR_EVIDENCE_PREVIEW_LIMIT = 160;\n\nexport type DetectorEvidenceConfig = {\n verbosity: DetectorDebugVerbosity;\n mode: WordCounterMode;\n section: SectionMode;\n};\n\nexport type DetectorDebugSummary = {\n mode: DetectorMode;\n engine: \"none\" | \"whatlang-wasm\";\n windowsTotal: number;\n accepted: number;\n fallback: number;\n routes: {\n latin: number;\n han: number;\n };\n acceptancePaths: {\n reliable: number;\n corroborated: number;\n };\n fallbackReasons: {\n notEligible: number;\n noCandidate: number;\n belowThreshold: number;\n qualityGate: number;\n corroborationUnreliable: number;\n };\n};\n\nexport type DetectorDebugContext = {\n emit?: (\n event: string,\n details?: Record<string, unknown>,\n options?: { verbosity?: DetectorDebugVerbosity },\n ) => void;\n summary?: DetectorDebugSummary;\n evidence?: DetectorEvidenceConfig;\n};\n\nexport type DetectorFallbackReason =\n | \"notEligible\"\n | \"noCandidate\"\n | \"belowThreshold\"\n | \"qualityGate\"\n | \"corroborationUnreliable\";\n\nexport function createDetectorDebugSummary(\n mode: DetectorMode,\n engine: DetectorDebugSummary[\"engine\"] = mode === \"wasm\" ? \"whatlang-wasm\" : \"none\",\n): DetectorDebugSummary {\n return {\n mode,\n engine,\n windowsTotal: 0,\n accepted: 0,\n fallback: 0,\n routes: {\n latin: 0,\n han: 0,\n },\n acceptancePaths: {\n reliable: 0,\n corroborated: 0,\n },\n fallbackReasons: {\n notEligible: 0,\n noCandidate: 0,\n belowThreshold: 0,\n qualityGate: 0,\n corroborationUnreliable: 0,\n },\n };\n}\n\nexport function mergeDetectorDebugSummaries(\n summaries: Array<DetectorDebugSummary | undefined>,\n): DetectorDebugSummary | undefined {\n const present = summaries.filter(\n (summary): summary is DetectorDebugSummary => summary !== undefined,\n );\n if (present.length === 0) {\n return undefined;\n }\n\n const first = present[0]!;\n const merged = createDetectorDebugSummary(first.mode, first.engine);\n for (const summary of present) {\n merged.windowsTotal += summary.windowsTotal;\n merged.accepted += summary.accepted;\n merged.fallback += summary.fallback;\n merged.routes.latin += summary.routes.latin;\n merged.routes.han += summary.routes.han;\n merged.acceptancePaths.reliable += summary.acceptancePaths.reliable;\n merged.acceptancePaths.corroborated += summary.acceptancePaths.corroborated;\n merged.fallbackReasons.notEligible += summary.fallbackReasons.notEligible;\n merged.fallbackReasons.noCandidate += summary.fallbackReasons.noCandidate;\n merged.fallbackReasons.belowThreshold += summary.fallbackReasons.belowThreshold;\n merged.fallbackReasons.qualityGate += summary.fallbackReasons.qualityGate;\n merged.fallbackReasons.corroborationUnreliable +=\n summary.fallbackReasons.corroborationUnreliable;\n }\n\n return merged;\n}\n\nexport function recordDetectorWindow(\n summary: DetectorDebugSummary | undefined,\n routeTag: DetectorRouteTag,\n): void {\n if (!summary) {\n return;\n }\n\n summary.windowsTotal += 1;\n if (routeTag === DEFAULT_LOCALE) {\n summary.routes.latin += 1;\n return;\n }\n if (routeTag === DEFAULT_HAN_TAG) {\n summary.routes.han += 1;\n }\n}\n\nexport function recordDetectorAccepted(\n summary: DetectorDebugSummary | undefined,\n path: \"reliable\" | \"corroborated\",\n): void {\n if (!summary) {\n return;\n }\n\n summary.accepted += 1;\n if (path === \"reliable\") {\n summary.acceptancePaths.reliable += 1;\n return;\n }\n\n summary.acceptancePaths.corroborated += 1;\n}\n\nexport function recordDetectorFallback(\n summary: DetectorDebugSummary | undefined,\n reason: DetectorFallbackReason,\n): void {\n if (!summary) {\n return;\n }\n\n summary.fallback += 1;\n summary.fallbackReasons[reason] += 1;\n}\n\nexport function createDetectorEvidencePreview(text: string): {\n preview: string;\n truncated: boolean;\n} {\n const collapsed = text.replace(/\\s+/gu, \" \").trim();\n const codePoints = Array.from(collapsed);\n if (codePoints.length <= DETECTOR_EVIDENCE_PREVIEW_LIMIT) {\n return {\n preview: collapsed,\n truncated: false,\n };\n }\n\n return {\n preview: codePoints.slice(0, DETECTOR_EVIDENCE_PREVIEW_LIMIT).join(\"\"),\n truncated: true,\n };\n}\n","import { countSections } from \"../markdown\";\nimport wordCounter, { segmentTextByLocale } from \"../wc\";\nimport {\n createInspectChunk,\n createInspectInput,\n segmentTextByLocaleWithTrace,\n} from \"./inspect-helpers\";\nimport type { DetectorInspectOptions, DetectorInspectResult } from \"./inspect-types\";\nimport type {\n DetectorCountSectionsOptions,\n DetectorLocaleOptions,\n DetectorWordCounterOptions,\n} from \"./types\";\n\nexport async function segmentTextByLocaleWithRegexDetector(\n text: string,\n options: DetectorLocaleOptions = {},\n) {\n return segmentTextByLocale(text, options);\n}\n\nexport async function wordCounterWithRegexDetector(\n text: string,\n options: DetectorWordCounterOptions = {},\n) {\n return wordCounter(text, options);\n}\n\nexport async function countSectionsWithRegexDetector(\n input: string,\n section: Parameters<typeof countSections>[1],\n options: DetectorCountSectionsOptions = {},\n) {\n return countSections(input, section, options);\n}\n\nexport async function inspectTextWithRegexDetector(\n text: string,\n options: DetectorInspectOptions = {},\n): Promise<DetectorInspectResult> {\n if (text.trim().length === 0) {\n return {\n schemaVersion: 1,\n kind: \"detector-inspect\",\n view: \"pipeline\",\n detector: \"regex\",\n input: createInspectInput(text, options.input),\n chunks: [],\n decision: {\n kind: \"empty\",\n notes: [\"No detector-eligible content was present.\"],\n },\n resolvedChunks: [],\n };\n }\n\n const chunks = segmentTextByLocaleWithTrace(text, options);\n\n return {\n schemaVersion: 1,\n kind: \"detector-inspect\",\n view: \"pipeline\",\n detector: \"regex\",\n input: createInspectInput(text, options.input),\n chunks: chunks.map((chunk, index) =>\n createInspectChunk(index, chunk, {\n source: chunk.source,\n reason: chunk.reason,\n }),\n ),\n decision: {\n kind: \"deterministic\",\n notes: [\n \"Regex inspection does not use detector windows or engine confidence.\",\n \"Final locales come directly from script detection, hint rules, and fallback rules.\",\n ],\n },\n resolvedChunks: chunks.map((chunk, index) => createInspectChunk(index, chunk)),\n };\n}\n","import {\n analyzeCharChunk,\n analyzeChunk,\n aggregateByLocale,\n aggregateCharsByLocale,\n} from \"../wc/analyze\";\nimport { resolveMode } from \"../wc/mode\";\nimport { createNonWordCollection, mergeNonWordCollections } from \"../wc/non-words\";\nimport type {\n CharBreakdown,\n CharCollectorBreakdown,\n ChunkBreakdown,\n ChunkWithSegments,\n LocaleChunk,\n NonWordCollection,\n WordCounterMode,\n WordCounterOptions,\n WordCounterResult,\n} from \"../wc/types\";\n\nfunction getNonWordTotal(nonWords: NonWordCollection): number {\n return (\n nonWords.counts.emoji +\n nonWords.counts.symbols +\n nonWords.counts.punctuation +\n (nonWords.counts.whitespace ?? 0)\n );\n}\n\nfunction collectNonWordsAggregate(\n analyzed: Array<{ nonWords?: NonWordCollection }>,\n enabled: boolean,\n): NonWordCollection | undefined {\n if (!enabled) {\n return undefined;\n }\n const collection = createNonWordCollection();\n for (const chunk of analyzed) {\n if (!chunk.nonWords) {\n continue;\n }\n mergeNonWordCollections(collection, chunk.nonWords);\n }\n return collection;\n}\n\nexport function buildWordCounterResultFromChunks(\n chunks: LocaleChunk[],\n options: WordCounterOptions = {},\n): WordCounterResult {\n const mode: WordCounterMode = resolveMode(options.mode, \"chunk\");\n const collectNonWords = Boolean(options.nonWords);\n const includeWhitespace = Boolean(options.includeWhitespace);\n\n if (mode === \"char\" || mode === \"char-collector\") {\n const analyzed = chunks.map((chunk) =>\n analyzeCharChunk(chunk, collectNonWords, includeWhitespace),\n );\n const total = analyzed.reduce((sum, chunk) => sum + chunk.chars, 0);\n const counts = collectNonWords\n ? {\n words: analyzed.reduce((sum, chunk) => sum + chunk.wordChars, 0),\n nonWords: analyzed.reduce((sum, chunk) => sum + chunk.nonWordChars, 0),\n total,\n }\n : undefined;\n\n if (mode === \"char\") {\n const items: CharBreakdown[] = analyzed.map((chunk) => ({\n locale: chunk.locale,\n text: chunk.text,\n chars: chunk.chars,\n nonWords: chunk.nonWords,\n }));\n return {\n total,\n counts,\n breakdown: {\n mode,\n items,\n },\n };\n }\n\n const aggregated = aggregateCharsByLocale(analyzed);\n const items: CharCollectorBreakdown[] = aggregated.map((chunk) => ({\n locale: chunk.locale,\n chars: chunk.chars,\n nonWords: chunk.nonWords,\n }));\n return {\n total,\n counts,\n breakdown: {\n mode,\n items,\n },\n };\n }\n\n const analyzed = chunks.map((chunk) => analyzeChunk(chunk, collectNonWords, includeWhitespace));\n const wordsTotal = analyzed.reduce((sum, chunk) => sum + chunk.words, 0);\n const nonWordsTotal = collectNonWords\n ? analyzed.reduce((sum, chunk) => {\n if (!chunk.nonWords) {\n return sum;\n }\n return sum + getNonWordTotal(chunk.nonWords);\n }, 0)\n : 0;\n const total = analyzed.reduce((sum, chunk) => {\n let chunkTotal = chunk.words;\n if (collectNonWords && chunk.nonWords) {\n chunkTotal += getNonWordTotal(chunk.nonWords);\n }\n return sum + chunkTotal;\n }, 0);\n\n const counts = collectNonWords\n ? { words: wordsTotal, nonWords: nonWordsTotal, total }\n : undefined;\n\n if (mode === \"segments\") {\n const items: ChunkWithSegments[] = analyzed.map((chunk) => ({\n locale: chunk.locale,\n text: chunk.text,\n words: chunk.words,\n segments: chunk.segments,\n nonWords: chunk.nonWords,\n }));\n return {\n total,\n counts,\n breakdown: {\n mode,\n items,\n },\n };\n }\n\n if (mode === \"collector\") {\n const items = aggregateByLocale(analyzed);\n const nonWords = collectNonWordsAggregate(analyzed, collectNonWords);\n return {\n total,\n counts,\n breakdown: {\n mode,\n items,\n nonWords,\n },\n };\n }\n\n const items: ChunkBreakdown[] = analyzed.map((chunk) => ({\n locale: chunk.locale,\n text: chunk.text,\n words: chunk.words,\n nonWords: chunk.nonWords,\n }));\n\n return {\n total,\n counts,\n breakdown: {\n mode,\n items,\n },\n };\n}\n","import { parseMarkdown } from \"../markdown\";\nimport type { SectionMode, SectionedResult } from \"../markdown\";\nimport type { WordCounterResult } from \"../wc/types\";\nimport type { DetectorCountSectionsOptions } from \"./types\";\nimport { wordCounterWithDetector } from \"./index\";\n\nfunction normalizeText(value: unknown): string {\n if (value == null) {\n return \"\";\n }\n if (typeof value === \"string\") {\n return value;\n }\n if (typeof value === \"number\" || typeof value === \"boolean\") {\n return String(value);\n }\n try {\n return JSON.stringify(value);\n } catch {\n return String(value);\n }\n}\n\nasync function buildPerKeyItems(\n data: Record<string, unknown> | null,\n options: DetectorCountSectionsOptions,\n): Promise<Array<{ name: string; source: \"frontmatter\"; result: WordCounterResult }>> {\n if (!data || typeof data !== \"object\" || Array.isArray(data)) {\n return [];\n }\n\n return Promise.all(\n Object.entries(data).map(async ([key, value]) => {\n const valueText = normalizeText(value);\n const text = valueText ? `${key}: ${valueText}` : key;\n return {\n name: key,\n source: \"frontmatter\" as const,\n result: await wordCounterWithDetector(text, options),\n };\n }),\n );\n}\n\nasync function buildSingleItem(\n name: string,\n text: string,\n options: DetectorCountSectionsOptions,\n source: \"frontmatter\" | \"content\",\n): Promise<Array<{ name: string; source: \"frontmatter\" | \"content\"; result: WordCounterResult }>> {\n return [{ name, source, result: await wordCounterWithDetector(text, options) }];\n}\n\nfunction sumTotals(items: Array<{ result: WordCounterResult }>): number {\n return items.reduce((sum, item) => sum + item.result.total, 0);\n}\n\nexport async function countSectionsWithResolvedDetector(\n input: string,\n section: SectionMode,\n options: DetectorCountSectionsOptions = {},\n): Promise<SectionedResult> {\n if (section === \"all\") {\n const result = await wordCounterWithDetector(input, options);\n return {\n section,\n total: result.total,\n frontmatterType: null,\n items: [{ name: \"all\", source: \"content\", result }],\n };\n }\n\n const parsed = parseMarkdown(input);\n const frontmatterText = parsed.frontmatter ?? \"\";\n const contentText = parsed.content ?? \"\";\n\n let items: Array<{ name: string; source: \"frontmatter\" | \"content\"; result: WordCounterResult }> =\n [];\n\n if (section === \"frontmatter\") {\n items = await buildSingleItem(\"frontmatter\", frontmatterText, options, \"frontmatter\");\n } else if (section === \"content\") {\n items = await buildSingleItem(\"content\", contentText, options, \"content\");\n } else if (section === \"split\") {\n items = [\n ...(await buildSingleItem(\"frontmatter\", frontmatterText, options, \"frontmatter\")),\n ...(await buildSingleItem(\"content\", contentText, options, \"content\")),\n ];\n } else if (section === \"per-key\") {\n items = await buildPerKeyItems(parsed.data, options);\n } else if (section === \"split-per-key\") {\n items = [\n ...(await buildPerKeyItems(parsed.data, options)),\n ...(await buildSingleItem(\"content\", contentText, options, \"content\")),\n ];\n }\n\n return {\n section,\n total: sumTotals(items),\n frontmatterType: parsed.frontmatterType,\n items,\n };\n}\n","import { DEFAULT_HAN_TAG, DEFAULT_LOCALE } from \"../wc/locale-detect\";\nimport type { LocaleChunk } from \"../wc/types\";\nimport type { DetectorContentGateMode, DetectorResult } from \"./types\";\n\nexport const LATIN_WASM_MIN_SCRIPT_CHARS = 24;\nexport const HANI_WASM_MIN_SCRIPT_CHARS = 12;\nexport const LATIN_WASM_MIN_CONFIDENCE = 0.75;\nexport const HANI_WASM_MIN_CONFIDENCE = 0.9;\nexport const LATIN_WASM_CORROBORATED_MIN_CONFIDENCE = 0.7;\nconst LATIN_WASM_STRICT_MIN_SCRIPT_CHARS = 30;\nconst LATIN_WASM_LOOSE_MIN_SCRIPT_CHARS = 20;\nconst HANI_WASM_STRICT_MIN_SCRIPT_CHARS = 16;\nconst HANI_WASM_LOOSE_MIN_FOCUS_HAN_CHARS = 4;\n\nconst LATIN_SCRIPT_REGEX = /\\p{Script=Latin}/u;\nconst HAN_SCRIPT_REGEX = /\\p{Script=Han}/u;\nconst HANI_DIAGNOSTIC_SCRIPT_REGEX = /[\\p{Script=Han}\\p{Script=Hiragana}\\p{Script=Katakana}]/u;\nconst LATIN_WORD_REGEX = /\\p{Script=Latin}+/gu;\nconst WHITESPACE_REGEX = /\\s/u;\nconst JAPANESE_CONTEXT_LOCALE = \"ja\";\n\nexport type DetectorRouteTag = typeof DEFAULT_LOCALE | typeof DEFAULT_HAN_TAG;\nexport type DetectorContentGatePolicy = \"latinProse\" | \"none\";\nexport type DetectorDiagnosticTextSource = \"focus\" | \"borrowed-context\";\n\nexport type DetectorWindow = {\n routeTag: DetectorRouteTag;\n startIndex: number;\n endIndex: number;\n text: string;\n};\n\nexport type DetectorBorrowedContext = {\n leftChunkIndex?: number;\n rightChunkIndex?: number;\n};\n\nexport type DetectorDiagnosticSample = {\n focusText: string;\n text: string;\n normalizedText: string;\n normalizedApplied: boolean;\n textSource: DetectorDiagnosticTextSource;\n borrowedContext?: DetectorBorrowedContext;\n};\n\nexport type DetectorEligibilityResult = {\n scriptChars: number;\n minScriptChars: number;\n passed: boolean;\n};\n\nexport type DetectorContentGateResult = {\n applied: boolean;\n passed: boolean;\n policy: DetectorContentGatePolicy;\n mode: DetectorContentGateMode;\n};\n\nexport type DetectorCorroboratedAcceptance =\n | {\n accepted: true;\n confidence: number;\n hasReliableCorroboration: true;\n }\n | {\n accepted: false;\n confidence: number;\n hasReliableCorroboration: boolean;\n reason: \"mismatch\" | \"belowThreshold\" | \"unreliable\";\n };\n\nexport type DetectorRoutePolicy = {\n routeTag: DetectorRouteTag;\n eligibility: {\n evaluate: (\n sample: DetectorDiagnosticSample,\n mode?: DetectorContentGateMode,\n ) => DetectorEligibilityResult;\n };\n buildDiagnosticSample: (\n window: DetectorWindow,\n chunks: LocaleChunk[],\n ) => DetectorDiagnosticSample;\n evaluateContentGate: (\n sample: DetectorDiagnosticSample,\n mode?: DetectorContentGateMode,\n ) => DetectorContentGateResult;\n accept: (candidate: DetectorResult) => boolean;\n acceptCorroborated?: (\n raw: DetectorResult,\n normalized: DetectorResult,\n ) => DetectorCorroboratedAcceptance;\n fallbackTag: string;\n};\n\nfunction countMatchingChars(text: string, matcher: RegExp): number {\n let count = 0;\n for (const char of text) {\n if (matcher.test(char)) {\n count += 1;\n }\n }\n return count;\n}\n\nfunction getSampleScriptMatcher(routeTag: DetectorRouteTag): RegExp {\n return routeTag === DEFAULT_HAN_TAG ? HAN_SCRIPT_REGEX : LATIN_SCRIPT_REGEX;\n}\n\nfunction getEligibilityScriptMatcher(routeTag: DetectorRouteTag): RegExp {\n return routeTag === DEFAULT_HAN_TAG ? HANI_DIAGNOSTIC_SCRIPT_REGEX : LATIN_SCRIPT_REGEX;\n}\n\nfunction normalizeSampleText(text: string, routeTag: DetectorRouteTag): string {\n const matcher = getSampleScriptMatcher(routeTag);\n return [...text]\n .map((char) => {\n if (matcher.test(char) || WHITESPACE_REGEX.test(char)) {\n return char;\n }\n return \" \";\n })\n .join(\"\")\n .replace(/\\s+/g, \" \")\n .trim();\n}\n\nfunction buildFocusOnlyDiagnosticSample(window: DetectorWindow): DetectorDiagnosticSample {\n const normalizedText = normalizeSampleText(window.text, window.routeTag);\n return {\n focusText: window.text,\n text: window.text,\n normalizedText,\n normalizedApplied: normalizedText !== window.text,\n textSource: \"focus\",\n };\n}\n\nfunction buildHaniDiagnosticSample(\n window: DetectorWindow,\n chunks: LocaleChunk[],\n): DetectorDiagnosticSample {\n const borrowedContext: DetectorBorrowedContext = {};\n const sampleParts: string[] = [];\n\n const leftChunk = chunks[window.startIndex - 1];\n if (leftChunk?.locale === JAPANESE_CONTEXT_LOCALE) {\n borrowedContext.leftChunkIndex = window.startIndex - 1;\n sampleParts.push(leftChunk.text);\n }\n\n sampleParts.push(window.text);\n\n const rightChunk = chunks[window.endIndex + 1];\n if (rightChunk?.locale === JAPANESE_CONTEXT_LOCALE) {\n borrowedContext.rightChunkIndex = window.endIndex + 1;\n sampleParts.push(rightChunk.text);\n }\n\n const text = sampleParts.join(\"\");\n const normalizedText = normalizeSampleText(text, window.routeTag);\n const borrowed =\n borrowedContext.leftChunkIndex !== undefined || borrowedContext.rightChunkIndex !== undefined;\n\n return {\n focusText: window.text,\n text,\n normalizedText,\n normalizedApplied: normalizedText !== text,\n textSource: borrowed ? \"borrowed-context\" : \"focus\",\n ...(borrowed ? { borrowedContext } : {}),\n };\n}\n\nfunction countLatinWords(text: string): number {\n return text.match(LATIN_WORD_REGEX)?.length ?? 0;\n}\n\nfunction isTechnicalLikeLatinLine(line: string, latinWords: number): boolean {\n const trimmed = line.trim();\n if (!trimmed) {\n return false;\n }\n\n if (/^[>#$]/u.test(trimmed)) {\n return true;\n }\n\n if (/(^|\\s)--[a-z0-9][a-z0-9-]*/iu.test(trimmed)) {\n return true;\n }\n\n if (/`[^`]+`/u.test(trimmed)) {\n return true;\n }\n\n if (/(^|[\\s\"'`])(?:\\.{0,2}\\/|\\/)?[\\w./-]+\\.[a-z0-9]{1,6}(?=$|[\\s\"'`])/iu.test(trimmed)) {\n return true;\n }\n\n if (/^[-*\\d.)\\s]*[\\p{L}\\p{N}_.-]+:\\s+\\S/iu.test(trimmed) && latinWords <= 8) {\n return true;\n }\n\n return false;\n}\n\nfunction shouldTreatLatinProseBlockAsSentenceLike(\n latinWords: number,\n lineCount: number,\n hasSentencePunctuation: boolean,\n): boolean {\n if (latinWords < 4) {\n return false;\n }\n\n if (hasSentencePunctuation) {\n return true;\n }\n\n return lineCount <= 1 ? latinWords >= 5 : latinWords >= 8;\n}\n\nfunction evaluateLatinContentGate(\n text: string,\n normalizedSample: string,\n mode: Exclude<DetectorContentGateMode, \"off\">,\n): DetectorContentGateResult {\n const minNormalizedLatinWords = mode === \"strict\" ? 5 : 4;\n const minProseWords = mode === \"strict\" ? 5 : mode === \"loose\" ? 3 : 4;\n const technicalWordSlack = mode === \"loose\" ? 2 : 0;\n const normalizedLatinWords = countLatinWords(normalizedSample);\n if (normalizedLatinWords < minNormalizedLatinWords) {\n return {\n applied: true,\n passed: false,\n policy: \"latinProse\",\n mode,\n };\n }\n\n let proseWords = 0;\n let technicalWords = 0;\n let proseBlockWords = 0;\n let proseBlockLines = 0;\n let proseBlockHasSentencePunctuation = false;\n\n const flushProseBlock = () => {\n if (\n shouldTreatLatinProseBlockAsSentenceLike(\n proseBlockWords,\n proseBlockLines,\n proseBlockHasSentencePunctuation,\n )\n ) {\n proseWords += proseBlockWords;\n }\n\n proseBlockWords = 0;\n proseBlockLines = 0;\n proseBlockHasSentencePunctuation = false;\n };\n\n for (const rawLine of text.split(/\\r?\\n/u)) {\n const line = rawLine.trim();\n if (!line || line === \"---\" || line === \"```\") {\n flushProseBlock();\n continue;\n }\n\n const latinWords = countLatinWords(line);\n if (latinWords === 0) {\n continue;\n }\n\n const technicalLike = isTechnicalLikeLatinLine(line, latinWords);\n if (technicalLike) {\n flushProseBlock();\n technicalWords += latinWords;\n continue;\n }\n\n proseBlockWords += latinWords;\n proseBlockLines += 1;\n proseBlockHasSentencePunctuation ||= /[.!?]/u.test(line);\n }\n\n flushProseBlock();\n return {\n applied: true,\n passed: proseWords >= minProseWords && proseWords + technicalWordSlack >= technicalWords,\n policy: \"latinProse\",\n mode,\n };\n}\n\nfunction evaluateEligibility(\n sample: DetectorDiagnosticSample,\n routeTag: DetectorRouteTag,\n minScriptChars: number,\n): DetectorEligibilityResult {\n const scriptChars = countMatchingChars(sample.text, getEligibilityScriptMatcher(routeTag));\n return {\n scriptChars,\n minScriptChars,\n passed: scriptChars >= minScriptChars,\n };\n}\n\nfunction getLatinEligibilityMinScriptChars(mode: DetectorContentGateMode = \"default\"): number {\n if (mode === \"strict\") {\n return LATIN_WASM_STRICT_MIN_SCRIPT_CHARS;\n }\n\n if (mode === \"loose\") {\n return LATIN_WASM_LOOSE_MIN_SCRIPT_CHARS;\n }\n\n return LATIN_WASM_MIN_SCRIPT_CHARS;\n}\n\nfunction evaluateHaniEligibility(\n sample: DetectorDiagnosticSample,\n mode: DetectorContentGateMode = \"default\",\n): DetectorEligibilityResult {\n if (mode === \"loose\") {\n const scriptChars = countMatchingChars(sample.focusText, HAN_SCRIPT_REGEX);\n return {\n scriptChars,\n minScriptChars: HANI_WASM_LOOSE_MIN_FOCUS_HAN_CHARS,\n passed: scriptChars >= HANI_WASM_LOOSE_MIN_FOCUS_HAN_CHARS,\n };\n }\n\n const minScriptChars =\n mode === \"strict\" ? HANI_WASM_STRICT_MIN_SCRIPT_CHARS : HANI_WASM_MIN_SCRIPT_CHARS;\n return evaluateEligibility(sample, DEFAULT_HAN_TAG, minScriptChars);\n}\n\nfunction shouldAcceptCandidate(\n confidence: number | undefined,\n reliable: boolean | undefined,\n minConfidence: number,\n): boolean {\n if (reliable !== true) {\n return false;\n }\n\n if (confidence === undefined) {\n return false;\n }\n\n return confidence >= minConfidence;\n}\n\nfunction evaluateLatinCorroboratedAcceptance(\n raw: DetectorResult,\n normalized: DetectorResult,\n): DetectorCorroboratedAcceptance {\n if (raw.tag !== normalized.tag) {\n return {\n accepted: false,\n confidence: Math.max(raw.confidence ?? 0, normalized.confidence ?? 0),\n hasReliableCorroboration: raw.reliable === true || normalized.reliable === true,\n reason: \"mismatch\",\n };\n }\n\n const confidence = Math.max(raw.confidence ?? 0, normalized.confidence ?? 0);\n const hasReliableCorroboration = raw.reliable === true || normalized.reliable === true;\n\n if (!hasReliableCorroboration && confidence >= LATIN_WASM_CORROBORATED_MIN_CONFIDENCE) {\n return {\n accepted: false,\n confidence,\n hasReliableCorroboration,\n reason: \"unreliable\",\n };\n }\n\n if (confidence < LATIN_WASM_CORROBORATED_MIN_CONFIDENCE) {\n return {\n accepted: false,\n confidence,\n hasReliableCorroboration,\n reason: \"belowThreshold\",\n };\n }\n\n if (!hasReliableCorroboration) {\n return {\n accepted: false,\n confidence,\n hasReliableCorroboration,\n reason: \"unreliable\",\n };\n }\n\n return {\n accepted: true,\n confidence,\n hasReliableCorroboration: true,\n };\n}\n\nfunction createLatinRoutePolicy(): DetectorRoutePolicy {\n return {\n routeTag: DEFAULT_LOCALE,\n eligibility: {\n evaluate(sample, mode = \"default\") {\n return evaluateEligibility(sample, DEFAULT_LOCALE, getLatinEligibilityMinScriptChars(mode));\n },\n },\n buildDiagnosticSample(window) {\n return buildFocusOnlyDiagnosticSample(window);\n },\n evaluateContentGate(sample, mode = \"default\") {\n if (mode === \"off\") {\n return {\n applied: false,\n passed: true,\n policy: \"none\",\n mode,\n };\n }\n\n return evaluateLatinContentGate(sample.text, sample.normalizedText, mode);\n },\n accept(candidate) {\n return shouldAcceptCandidate(\n candidate.confidence,\n candidate.reliable,\n LATIN_WASM_MIN_CONFIDENCE,\n );\n },\n acceptCorroborated(raw, normalized) {\n return evaluateLatinCorroboratedAcceptance(raw, normalized);\n },\n fallbackTag: DEFAULT_LOCALE,\n };\n}\n\nfunction createHaniRoutePolicy(): DetectorRoutePolicy {\n return {\n routeTag: DEFAULT_HAN_TAG,\n eligibility: {\n evaluate(sample, mode = \"default\") {\n return evaluateHaniEligibility(sample, mode);\n },\n },\n buildDiagnosticSample(window, chunks) {\n return buildHaniDiagnosticSample(window, chunks);\n },\n evaluateContentGate(_sample, mode = \"default\") {\n return {\n applied: false,\n passed: true,\n policy: \"none\",\n mode,\n };\n },\n accept(candidate) {\n return shouldAcceptCandidate(\n candidate.confidence,\n candidate.reliable,\n HANI_WASM_MIN_CONFIDENCE,\n );\n },\n fallbackTag: DEFAULT_HAN_TAG,\n };\n}\n\nexport const DETECTOR_ROUTE_POLICIES: Record<DetectorRouteTag, DetectorRoutePolicy> = {\n [DEFAULT_LOCALE]: createLatinRoutePolicy(),\n [DEFAULT_HAN_TAG]: createHaniRoutePolicy(),\n};\n\nexport function isAmbiguousDetectorRoute(locale: string): locale is DetectorRouteTag {\n return locale === DEFAULT_LOCALE || locale === DEFAULT_HAN_TAG;\n}\n\nexport function countScriptBearingCharsForRoute(text: string, routeTag: DetectorRouteTag): number {\n return countMatchingChars(text, getEligibilityScriptMatcher(routeTag));\n}\n\nexport function normalizeDetectorSampleForRoute(text: string, routeTag: DetectorRouteTag): string {\n return normalizeSampleText(text, routeTag);\n}\n","import { DEFAULT_HAN_TAG, DEFAULT_LOCALE } from \"../wc/locale-detect\";\nimport type { DetectorRouteTag } from \"./policy\";\nimport type { DetectorResult } from \"./types\";\n\nexport interface WhatlangWasmResult {\n lang: string;\n script: string;\n confidence: number;\n reliable: boolean;\n}\n\nconst LATIN_LANGUAGE_TAGS: Record<string, string> = {\n cat: \"ca\",\n ces: \"cs\",\n dan: \"da\",\n deu: \"de\",\n eng: \"en\",\n fin: \"fi\",\n fra: \"fr\",\n hun: \"hu\",\n ita: \"it\",\n lat: \"la\",\n nld: \"nl\",\n pol: \"pl\",\n por: \"pt\",\n ron: \"ro\",\n spa: \"es\",\n swe: \"sv\",\n tur: \"tr\",\n};\n\nconst HANI_LANGUAGE_TAGS: Record<string, string> = {\n cmn: \"zh\",\n jpn: \"ja\",\n};\n\nfunction hasSupportedScript(result: WhatlangWasmResult, routeTag: DetectorRouteTag): boolean {\n if (routeTag === DEFAULT_LOCALE) {\n return result.script === \"Latin\";\n }\n\n if (result.lang === \"cmn\") {\n return result.script === \"Mandarin\";\n }\n\n if (result.lang === \"jpn\") {\n return (\n result.script === \"Mandarin\" || result.script === \"Hiragana\" || result.script === \"Katakana\"\n );\n }\n\n return false;\n}\n\nfunction remapLanguageTag(lang: string, routeTag: DetectorRouteTag): string | undefined {\n if (routeTag === DEFAULT_LOCALE) {\n return LATIN_LANGUAGE_TAGS[lang];\n }\n\n return HANI_LANGUAGE_TAGS[lang];\n}\n\nexport function remapWhatlangResult(\n result: WhatlangWasmResult,\n routeTag: DetectorRouteTag,\n): DetectorResult | null {\n if (!hasSupportedScript(result, routeTag)) {\n return null;\n }\n\n const tag = remapLanguageTag(result.lang, routeTag);\n if (!tag) {\n return null;\n }\n\n return {\n tag,\n confidence: result.confidence,\n reliable: result.reliable,\n source: \"wasm\",\n };\n}\n\nexport function getDetectorFallbackTag(routeTag: DetectorRouteTag): string {\n return routeTag === DEFAULT_HAN_TAG ? DEFAULT_HAN_TAG : DEFAULT_LOCALE;\n}\n","import { segmentTextByLocale } from \"../wc\";\nimport { DEFAULT_LOCALE } from \"../wc/locale-detect\";\nimport type { LocaleChunk } from \"../wc/types\";\nimport { getDetectorFallbackTag } from \"./whatlang-map\";\nimport type { DetectorLocaleOptions } from \"./types\";\nimport type { DetectorWindow } from \"./policy\";\n\nexport function createDeferredLatinPreSegmentOptions(\n options: DetectorLocaleOptions,\n): DetectorLocaleOptions {\n return {\n ...options,\n latinLanguageHint: undefined,\n latinTagHint: undefined,\n latinLocaleHint: undefined,\n latinHintRules: undefined,\n useDefaultLatinHints: false,\n };\n}\n\nfunction createRuleOnlyLatinOptions(options: DetectorLocaleOptions): DetectorLocaleOptions {\n return {\n ...options,\n latinLanguageHint: undefined,\n latinTagHint: undefined,\n latinLocaleHint: undefined,\n };\n}\n\nfunction mergeAdjacentChunks(chunks: LocaleChunk[]): LocaleChunk[] {\n if (chunks.length === 0) {\n return chunks;\n }\n\n const merged: LocaleChunk[] = [];\n let last = chunks[0]!;\n\n for (let index = 1; index < chunks.length; index += 1) {\n const chunk = chunks[index]!;\n if (chunk.locale === last.locale) {\n last = {\n locale: last.locale,\n text: last.text + chunk.text,\n };\n continue;\n }\n merged.push(last);\n last = chunk;\n }\n\n merged.push(last);\n return merged;\n}\n\nexport function reapplyDeferredLatinFallback(\n chunks: LocaleChunk[],\n options: DetectorLocaleOptions,\n): LocaleChunk[] {\n const relabeled: LocaleChunk[] = [];\n\n for (const chunk of chunks) {\n if (chunk.locale !== DEFAULT_LOCALE) {\n relabeled.push(chunk);\n continue;\n }\n\n relabeled.push(...segmentTextByLocale(chunk.text, options));\n }\n\n return mergeAdjacentChunks(relabeled);\n}\n\nexport function reapplyResolvedLatinHintRules(\n resolvedChunks: LocaleChunk[],\n originalChunks: LocaleChunk[],\n options: DetectorLocaleOptions,\n): LocaleChunk[] {\n const relabeled: LocaleChunk[] = [];\n const ruleOnlyOptions = createRuleOnlyLatinOptions(options);\n\n for (let index = 0; index < resolvedChunks.length; index += 1) {\n const chunk = resolvedChunks[index];\n const originalChunk = originalChunks[index];\n if (!chunk || !originalChunk) {\n continue;\n }\n\n if (originalChunk.locale !== DEFAULT_LOCALE || chunk.locale === DEFAULT_LOCALE) {\n relabeled.push(chunk);\n continue;\n }\n\n const hintedChunks = segmentTextByLocale(chunk.text, ruleOnlyOptions).map((hintedChunk) => ({\n locale: hintedChunk.locale === DEFAULT_LOCALE ? chunk.locale : hintedChunk.locale,\n text: hintedChunk.text,\n }));\n relabeled.push(...hintedChunks);\n }\n\n return mergeAdjacentChunks(relabeled);\n}\n\nexport function resolveFallbackDebugOutcome(\n window: DetectorWindow,\n options: DetectorLocaleOptions,\n): {\n finalTag: string;\n finalLocales?: string[];\n} {\n const fallbackTag = getDetectorFallbackTag(window.routeTag);\n if (window.routeTag !== DEFAULT_LOCALE) {\n return { finalTag: fallbackTag };\n }\n\n const relabeled = reapplyDeferredLatinFallback(\n [\n {\n locale: fallbackTag,\n text: window.text,\n },\n ],\n options,\n );\n const finalLocales = relabeled.map((chunk) => chunk.locale);\n if (finalLocales.length === 1) {\n return {\n finalTag: finalLocales[0]!,\n };\n }\n\n return finalLocales.length > 1\n ? {\n finalTag: fallbackTag,\n finalLocales,\n }\n : {\n finalTag: fallbackTag,\n };\n}\n","import { existsSync } from \"node:fs\";\nimport { dirname, join } from \"node:path\";\nimport { createRequire } from \"node:module\";\nimport { fileURLToPath } from \"node:url\";\nimport type { DetectorRouteTag } from \"./policy\";\nimport type { WhatlangWasmResult } from \"./whatlang-map\";\n\nconst GENERATED_FOLDER_NAME = \"wasm-language-detector\";\nconst GENERATED_MODULE_FILE = \"language_detector.js\";\nconst MAX_SEARCH_DEPTH = 8;\nconst requireFromHere = createRequire(import.meta.url);\n\nexport const WASM_DETECTOR_RUNTIME_UNAVAILABLE_MESSAGE =\n \"WASM detector runtime is unavailable. Run `bun run build:wasm` to generate it.\";\n\ntype WhatlangWasmModule = {\n detect_language: (text: string, routeTag: string) => WhatlangWasmResult | null;\n};\n\nlet modulePromise: Promise<WhatlangWasmModule> | null = null;\n\nfunction resolveCandidateModulePaths(): string[] {\n const moduleDir = dirname(fileURLToPath(import.meta.url));\n const candidates = new Set<string>();\n let currentDir = moduleDir;\n\n for (let depth = 0; depth < MAX_SEARCH_DEPTH; depth += 1) {\n candidates.add(join(currentDir, GENERATED_FOLDER_NAME, GENERATED_MODULE_FILE));\n candidates.add(join(currentDir, \"generated\", GENERATED_FOLDER_NAME, GENERATED_MODULE_FILE));\n\n const parentDir = dirname(currentDir);\n if (parentDir === currentDir) {\n break;\n }\n currentDir = parentDir;\n }\n\n return [...candidates];\n}\n\nfunction resolveWhatlangWasmModulePath(): string {\n for (const candidate of resolveCandidateModulePaths()) {\n if (existsSync(candidate)) {\n return candidate;\n }\n }\n\n throw new Error(WASM_DETECTOR_RUNTIME_UNAVAILABLE_MESSAGE);\n}\n\nasync function loadWhatlangWasmModule(): Promise<WhatlangWasmModule> {\n if (!modulePromise) {\n modulePromise = (async () => {\n const modulePath = resolveWhatlangWasmModulePath();\n return requireFromHere(modulePath) as WhatlangWasmModule;\n })();\n }\n\n return modulePromise;\n}\n\nexport async function detectWithWhatlangWasm(\n text: string,\n routeTag: DetectorRouteTag,\n): Promise<WhatlangWasmResult | null> {\n const wasmModule = await loadWhatlangWasmModule();\n return wasmModule.detect_language(text, routeTag);\n}\n","import type { DetectorDiagnosticSample, DetectorRouteTag } from \"./policy\";\nimport { remapWhatlangResult } from \"./whatlang-map\";\nimport { detectWithWhatlangWasm } from \"./whatlang-wasm\";\nimport type { DetectorResult } from \"./types\";\n\nexport type ExecutedEngineSample = {\n rawResult: Awaited<ReturnType<typeof detectWithWhatlangWasm>> | null;\n rawRemapped: DetectorResult | null;\n normalizedResult: Awaited<ReturnType<typeof detectWithWhatlangWasm>> | null;\n normalizedRemapped: DetectorResult | null;\n};\n\nexport function buildEvidenceSample(\n result: Awaited<ReturnType<typeof detectWithWhatlangWasm>> | null,\n remappedTag: string | null,\n) {\n return {\n lang: result?.lang ?? null,\n script: result?.script ?? null,\n confidence: result?.confidence ?? null,\n reliable: result?.reliable ?? null,\n remappedTag,\n };\n}\n\nexport async function executeEngineSample(\n sample: DetectorDiagnosticSample,\n routeTag: DetectorRouteTag,\n): Promise<ExecutedEngineSample> {\n const rawResult = await detectWithWhatlangWasm(sample.text, routeTag);\n const rawRemapped = rawResult ? remapWhatlangResult(rawResult, routeTag) : null;\n const normalizedResult =\n sample.normalizedApplied && sample.normalizedText.length > 0\n ? await detectWithWhatlangWasm(sample.normalizedText, routeTag)\n : null;\n const normalizedRemapped = normalizedResult\n ? remapWhatlangResult(normalizedResult, routeTag)\n : null;\n\n return {\n rawResult,\n rawRemapped,\n normalizedResult,\n normalizedRemapped,\n };\n}\n","import {\n createDetectorEvidencePreview,\n recordDetectorAccepted,\n recordDetectorFallback,\n recordDetectorWindow,\n type DetectorFallbackReason,\n} from \"./debug\";\nimport {\n DETECTOR_ROUTE_POLICIES,\n type DetectorContentGateResult,\n type DetectorDiagnosticSample,\n type DetectorEligibilityResult,\n type DetectorWindow,\n} from \"./policy\";\nimport type { LocaleChunk } from \"../wc/types\";\nimport { getDetectorFallbackTag } from \"./whatlang-map\";\nimport type { DetectorLocaleOptions } from \"./types\";\nimport { buildEvidenceSample, executeEngineSample } from \"./wasm-engine\";\nimport { resolveFallbackDebugOutcome } from \"./wasm-presegment\";\n\nexport type ResolvedDetectorWindow = {\n resolvedLocale: string;\n sample: DetectorDiagnosticSample;\n eligibility: DetectorEligibilityResult;\n contentGate: DetectorContentGateResult;\n engineExecuted: boolean;\n engineReason?: \"notEligible\";\n rawResult: Awaited<ReturnType<typeof executeEngineSample>>[\"rawResult\"];\n rawRemappedTag: string | null;\n normalizedResult: Awaited<ReturnType<typeof executeEngineSample>>[\"normalizedResult\"];\n normalizedRemappedTag: string | null;\n decision: {\n accepted: boolean;\n path: \"reliable\" | \"corroborated\" | null;\n finalTag: string;\n finalLocales?: string[];\n fallbackReason: DetectorFallbackReason | null;\n };\n};\n\nfunction emitDetectorWindowEvidence({\n window,\n windowIndex,\n sample,\n eligibility,\n contentGate,\n rawResult,\n rawRemappedTag,\n normalizedResult,\n normalizedRemappedTag,\n decision,\n debug,\n}: {\n window: DetectorWindow;\n windowIndex: number;\n sample: DetectorDiagnosticSample;\n eligibility: DetectorEligibilityResult;\n contentGate: DetectorContentGateResult;\n rawResult: ResolvedDetectorWindow[\"rawResult\"];\n rawRemappedTag: string | null;\n normalizedResult: ResolvedDetectorWindow[\"normalizedResult\"];\n normalizedRemappedTag: string | null;\n decision: ResolvedDetectorWindow[\"decision\"];\n debug?: DetectorLocaleOptions[\"detectorDebug\"];\n}): void {\n const evidence = debug?.evidence;\n if (!evidence || !debug.emit) {\n return;\n }\n\n const baseDetails = {\n engine: \"whatlang-wasm\",\n routeTag: window.routeTag,\n windowIndex,\n startIndex: window.startIndex,\n endIndex: window.endIndex,\n mode: evidence.mode,\n section: evidence.section,\n textLength: sample.text.length,\n focusTextLength: sample.focusText.length,\n normalizedLength: sample.normalizedText.length,\n normalizedApplied: sample.normalizedApplied,\n textSource: sample.textSource,\n scriptChars: eligibility.scriptChars,\n minScriptChars: eligibility.minScriptChars,\n eligible: eligibility.passed,\n contentGate,\n qualityGate: contentGate.passed,\n raw: buildEvidenceSample(rawResult, rawRemappedTag),\n normalized: buildEvidenceSample(normalizedResult, normalizedRemappedTag),\n decision,\n ...(sample.borrowedContext ? { borrowedContext: sample.borrowedContext } : {}),\n };\n\n if (evidence.verbosity === \"verbose\") {\n debug.emit(\n \"detector.window.evidence\",\n {\n ...baseDetails,\n text: sample.text,\n normalizedText: sample.normalizedText,\n },\n { verbosity: \"verbose\" },\n );\n return;\n }\n\n const textPreview = createDetectorEvidencePreview(sample.text);\n const normalizedPreview = createDetectorEvidencePreview(sample.normalizedText);\n debug.emit(\n \"detector.window.evidence\",\n {\n ...baseDetails,\n textPreview: textPreview.preview,\n textPreviewTruncated: textPreview.truncated,\n normalizedPreview: normalizedPreview.preview,\n normalizedPreviewTruncated: normalizedPreview.truncated,\n },\n { verbosity: \"compact\" },\n );\n}\n\nexport async function resolveWindowLocale(\n window: DetectorWindow,\n windowIndex: number,\n chunks: LocaleChunk[],\n options: DetectorLocaleOptions,\n debug?: DetectorLocaleOptions[\"detectorDebug\"],\n): Promise<ResolvedDetectorWindow> {\n const routePolicy = DETECTOR_ROUTE_POLICIES[window.routeTag];\n const sample = routePolicy.buildDiagnosticSample(window, chunks);\n const configuredMode = options.contentGate?.mode;\n const eligibility = routePolicy.eligibility.evaluate(sample, configuredMode);\n const contentGate = routePolicy.evaluateContentGate(sample, configuredMode);\n\n recordDetectorWindow(debug?.summary, window.routeTag);\n debug?.emit?.(\n \"detector.window.start\",\n {\n routeTag: window.routeTag,\n startIndex: window.startIndex,\n endIndex: window.endIndex,\n textLength: window.text.length,\n sampleTextLength: sample.text.length,\n textSource: sample.textSource,\n ...(sample.borrowedContext ? { borrowedContext: sample.borrowedContext } : {}),\n },\n { verbosity: \"verbose\" },\n );\n\n if (!eligibility.passed) {\n recordDetectorFallback(debug?.summary, \"notEligible\");\n const fallbackDebugOutcome = resolveFallbackDebugOutcome(window, options);\n const resolution: ResolvedDetectorWindow = {\n resolvedLocale: getDetectorFallbackTag(window.routeTag),\n sample,\n eligibility,\n contentGate,\n engineExecuted: false,\n engineReason: \"notEligible\",\n rawResult: null,\n rawRemappedTag: null,\n normalizedResult: null,\n normalizedRemappedTag: null,\n decision: {\n accepted: false,\n path: null,\n finalTag: fallbackDebugOutcome.finalTag,\n ...(fallbackDebugOutcome.finalLocales\n ? { finalLocales: fallbackDebugOutcome.finalLocales }\n : {}),\n fallbackReason: \"notEligible\",\n },\n };\n emitDetectorWindowEvidence({\n window,\n windowIndex,\n sample: resolution.sample,\n eligibility: resolution.eligibility,\n contentGate: resolution.contentGate,\n rawResult: resolution.rawResult,\n rawRemappedTag: resolution.rawRemappedTag,\n normalizedResult: resolution.normalizedResult,\n normalizedRemappedTag: resolution.normalizedRemappedTag,\n decision: resolution.decision,\n debug,\n });\n debug?.emit?.(\"detector.window.fallback\", {\n routeTag: window.routeTag,\n finalTag: fallbackDebugOutcome.finalTag,\n ...(fallbackDebugOutcome.finalLocales\n ? { finalLocales: fallbackDebugOutcome.finalLocales }\n : {}),\n reason: \"notEligible\",\n });\n return resolution;\n }\n\n const { rawResult, rawRemapped, normalizedResult, normalizedRemapped } =\n await executeEngineSample(sample, window.routeTag);\n debug?.emit?.(\n \"detector.window.sample\",\n {\n routeTag: window.routeTag,\n normalizedApplied: sample.normalizedApplied,\n normalizedLength: sample.normalizedText.length,\n textSource: sample.textSource,\n contentGate,\n qualityGate: contentGate.passed,\n rawTag: rawRemapped?.tag ?? null,\n rawConfidence: rawRemapped?.confidence ?? null,\n rawReliable: rawRemapped?.reliable ?? null,\n ...(sample.borrowedContext ? { borrowedContext: sample.borrowedContext } : {}),\n },\n { verbosity: \"verbose\" },\n );\n debug?.emit?.(\n \"detector.window.candidates\",\n {\n routeTag: window.routeTag,\n normalizedTag: normalizedRemapped?.tag ?? null,\n normalizedConfidence: normalizedRemapped?.confidence ?? null,\n normalizedReliable: normalizedRemapped?.reliable ?? null,\n },\n { verbosity: \"verbose\" },\n );\n\n const candidates = [rawRemapped, normalizedRemapped].filter((value) => value !== null);\n if (candidates.length === 0) {\n recordDetectorFallback(debug?.summary, \"noCandidate\");\n const fallbackDebugOutcome = resolveFallbackDebugOutcome(window, options);\n const resolution: ResolvedDetectorWindow = {\n resolvedLocale: getDetectorFallbackTag(window.routeTag),\n sample,\n eligibility,\n contentGate,\n engineExecuted: true,\n rawResult,\n rawRemappedTag: rawRemapped?.tag ?? null,\n normalizedResult,\n normalizedRemappedTag: normalizedRemapped?.tag ?? null,\n decision: {\n accepted: false,\n path: null,\n finalTag: fallbackDebugOutcome.finalTag,\n ...(fallbackDebugOutcome.finalLocales\n ? { finalLocales: fallbackDebugOutcome.finalLocales }\n : {}),\n fallbackReason: \"noCandidate\",\n },\n };\n emitDetectorWindowEvidence({\n window,\n windowIndex,\n sample: resolution.sample,\n eligibility: resolution.eligibility,\n contentGate: resolution.contentGate,\n rawResult: resolution.rawResult,\n rawRemappedTag: resolution.rawRemappedTag,\n normalizedResult: resolution.normalizedResult,\n normalizedRemappedTag: resolution.normalizedRemappedTag,\n decision: resolution.decision,\n debug,\n });\n debug?.emit?.(\"detector.window.fallback\", {\n routeTag: window.routeTag,\n finalTag: fallbackDebugOutcome.finalTag,\n ...(fallbackDebugOutcome.finalLocales\n ? { finalLocales: fallbackDebugOutcome.finalLocales }\n : {}),\n reason: \"noCandidate\",\n });\n return resolution;\n }\n\n const strongestCandidate = candidates.reduce((best, current) => {\n if (!best) {\n return current;\n }\n return (current.confidence ?? 0) > (best.confidence ?? 0) ? current : best;\n }, candidates[0]);\n\n if (strongestCandidate && contentGate.passed && routePolicy.accept(strongestCandidate)) {\n recordDetectorAccepted(debug?.summary, \"reliable\");\n const resolution: ResolvedDetectorWindow = {\n resolvedLocale: strongestCandidate.tag,\n sample,\n eligibility,\n contentGate,\n engineExecuted: true,\n rawResult,\n rawRemappedTag: rawRemapped?.tag ?? null,\n normalizedResult,\n normalizedRemappedTag: normalizedRemapped?.tag ?? null,\n decision: {\n accepted: true,\n path: \"reliable\",\n finalTag: strongestCandidate.tag,\n fallbackReason: null,\n },\n };\n emitDetectorWindowEvidence({\n window,\n windowIndex,\n sample: resolution.sample,\n eligibility: resolution.eligibility,\n contentGate: resolution.contentGate,\n rawResult: resolution.rawResult,\n rawRemappedTag: resolution.rawRemappedTag,\n normalizedResult: resolution.normalizedResult,\n normalizedRemappedTag: resolution.normalizedRemappedTag,\n decision: resolution.decision,\n debug,\n });\n debug?.emit?.(\"detector.window.accepted\", {\n routeTag: window.routeTag,\n finalTag: strongestCandidate.tag,\n acceptancePath: \"reliable\",\n confidence: strongestCandidate.confidence ?? null,\n reliable: strongestCandidate.reliable ?? null,\n });\n return resolution;\n }\n\n if (contentGate.passed && routePolicy.acceptCorroborated && rawRemapped && normalizedRemapped) {\n const corroborated = routePolicy.acceptCorroborated(rawRemapped, normalizedRemapped);\n if (corroborated.accepted) {\n recordDetectorAccepted(debug?.summary, \"corroborated\");\n const resolution: ResolvedDetectorWindow = {\n resolvedLocale: rawRemapped.tag,\n sample,\n eligibility,\n contentGate,\n engineExecuted: true,\n rawResult,\n rawRemappedTag: rawRemapped.tag,\n normalizedResult,\n normalizedRemappedTag: normalizedRemapped.tag,\n decision: {\n accepted: true,\n path: \"corroborated\",\n finalTag: rawRemapped.tag,\n fallbackReason: null,\n },\n };\n emitDetectorWindowEvidence({\n window,\n windowIndex,\n sample: resolution.sample,\n eligibility: resolution.eligibility,\n contentGate: resolution.contentGate,\n rawResult: resolution.rawResult,\n rawRemappedTag: resolution.rawRemappedTag,\n normalizedResult: resolution.normalizedResult,\n normalizedRemappedTag: resolution.normalizedRemappedTag,\n decision: resolution.decision,\n debug,\n });\n debug?.emit?.(\"detector.window.accepted\", {\n routeTag: window.routeTag,\n finalTag: rawRemapped.tag,\n acceptancePath: \"corroborated\",\n confidence: corroborated.confidence,\n reliable: corroborated.hasReliableCorroboration,\n });\n return resolution;\n }\n\n if (corroborated.reason === \"unreliable\") {\n recordDetectorFallback(debug?.summary, \"corroborationUnreliable\");\n const fallbackDebugOutcome = resolveFallbackDebugOutcome(window, options);\n const resolution: ResolvedDetectorWindow = {\n resolvedLocale: getDetectorFallbackTag(window.routeTag),\n sample,\n eligibility,\n contentGate,\n engineExecuted: true,\n rawResult,\n rawRemappedTag: rawRemapped.tag,\n normalizedResult,\n normalizedRemappedTag: normalizedRemapped.tag,\n decision: {\n accepted: false,\n path: null,\n finalTag: fallbackDebugOutcome.finalTag,\n ...(fallbackDebugOutcome.finalLocales\n ? { finalLocales: fallbackDebugOutcome.finalLocales }\n : {}),\n fallbackReason: \"corroborationUnreliable\",\n },\n };\n emitDetectorWindowEvidence({\n window,\n windowIndex,\n sample: resolution.sample,\n eligibility: resolution.eligibility,\n contentGate: resolution.contentGate,\n rawResult: resolution.rawResult,\n rawRemappedTag: resolution.rawRemappedTag,\n normalizedResult: resolution.normalizedResult,\n normalizedRemappedTag: resolution.normalizedRemappedTag,\n decision: resolution.decision,\n debug,\n });\n debug?.emit?.(\"detector.window.fallback\", {\n routeTag: window.routeTag,\n finalTag: fallbackDebugOutcome.finalTag,\n ...(fallbackDebugOutcome.finalLocales\n ? { finalLocales: fallbackDebugOutcome.finalLocales }\n : {}),\n reason: \"corroborationUnreliable\",\n });\n return resolution;\n }\n }\n\n const fallbackReason = contentGate.passed ? \"belowThreshold\" : \"qualityGate\";\n recordDetectorFallback(debug?.summary, fallbackReason);\n const fallbackDebugOutcome = resolveFallbackDebugOutcome(window, options);\n const resolution: ResolvedDetectorWindow = {\n resolvedLocale: getDetectorFallbackTag(window.routeTag),\n sample,\n eligibility,\n contentGate,\n engineExecuted: true,\n rawResult,\n rawRemappedTag: rawRemapped?.tag ?? null,\n normalizedResult,\n normalizedRemappedTag: normalizedRemapped?.tag ?? null,\n decision: {\n accepted: false,\n path: null,\n finalTag: fallbackDebugOutcome.finalTag,\n ...(fallbackDebugOutcome.finalLocales\n ? { finalLocales: fallbackDebugOutcome.finalLocales }\n : {}),\n fallbackReason,\n },\n };\n emitDetectorWindowEvidence({\n window,\n windowIndex,\n sample: resolution.sample,\n eligibility: resolution.eligibility,\n contentGate: resolution.contentGate,\n rawResult: resolution.rawResult,\n rawRemappedTag: resolution.rawRemappedTag,\n normalizedResult: resolution.normalizedResult,\n normalizedRemappedTag: resolution.normalizedRemappedTag,\n decision: resolution.decision,\n debug,\n });\n debug?.emit?.(\"detector.window.fallback\", {\n routeTag: window.routeTag,\n finalTag: fallbackDebugOutcome.finalTag,\n ...(fallbackDebugOutcome.finalLocales\n ? { finalLocales: fallbackDebugOutcome.finalLocales }\n : {}),\n reason: fallbackReason,\n });\n return resolution;\n}\n","import type { LocaleChunk } from \"../wc/types\";\nimport { isAmbiguousDetectorRoute, type DetectorWindow } from \"./policy\";\n\nexport function buildDetectorWindows(chunks: LocaleChunk[]): DetectorWindow[] {\n const windows: DetectorWindow[] = [];\n\n for (let index = 0; index < chunks.length; index += 1) {\n const chunk = chunks[index];\n if (!chunk || !isAmbiguousDetectorRoute(chunk.locale)) {\n continue;\n }\n\n const previousWindow = windows[windows.length - 1];\n if (\n previousWindow &&\n previousWindow.routeTag === chunk.locale &&\n previousWindow.endIndex === index - 1\n ) {\n previousWindow.endIndex = index;\n previousWindow.text += chunk.text;\n continue;\n }\n\n windows.push({\n routeTag: chunk.locale,\n startIndex: index,\n endIndex: index,\n text: chunk.text,\n });\n }\n\n return windows;\n}\n","import { segmentTextByLocale } from \"../wc\";\nimport { resolveLocaleDetectContext } from \"../wc/locale-detect\";\nimport { buildWordCounterResultFromChunks } from \"./result-builder\";\nimport { countSectionsWithResolvedDetector } from \"./sections\";\nimport { type DetectorWindow } from \"./policy\";\nimport { createInspectInput, segmentTextByLocaleWithTrace } from \"./inspect-helpers\";\nimport type { DetectorInspectOptions, DetectorInspectResult } from \"./inspect-types\";\nimport {\n buildEmptyEngineInspectResult,\n buildEmptyPipelineInspectResult,\n buildEngineInspectResult,\n buildPipelineInspectResult,\n} from \"./wasm-inspect\";\nimport {\n createDeferredLatinPreSegmentOptions,\n reapplyDeferredLatinFallback,\n reapplyResolvedLatinHintRules,\n} from \"./wasm-presegment\";\nimport { executeEngineSample } from \"./wasm-engine\";\nimport { resolveWindowLocale, type ResolvedDetectorWindow } from \"./wasm-resolution\";\nimport { buildDetectorWindows } from \"./wasm-windows\";\nimport { WASM_DETECTOR_RUNTIME_UNAVAILABLE_MESSAGE } from \"./whatlang-wasm\";\nimport type {\n DetectorCountSectionsOptions,\n DetectorLocaleOptions,\n DetectorWordCounterOptions,\n} from \"./types\";\n\nexport { WASM_DETECTOR_RUNTIME_UNAVAILABLE_MESSAGE };\n\nexport async function segmentTextByLocaleWithWasmDetector(\n text: string,\n options: DetectorLocaleOptions = {},\n) {\n // Validate the original hint configuration up front even though Latin hinting\n // is deferred until after detector routing in WASM mode.\n resolveLocaleDetectContext(options);\n\n const chunks = segmentTextByLocale(text, createDeferredLatinPreSegmentOptions(options));\n const resolved = [...chunks];\n const windows = buildDetectorWindows(chunks);\n\n for (const [windowIndex, window] of windows.entries()) {\n const resolution = await resolveWindowLocale(\n window,\n windowIndex,\n chunks,\n options,\n options.detectorDebug,\n );\n for (let index = window.startIndex; index <= window.endIndex; index += 1) {\n const chunk = resolved[index];\n if (!chunk) {\n continue;\n }\n resolved[index] = {\n ...chunk,\n locale: resolution.resolvedLocale,\n };\n }\n }\n\n options.detectorDebug?.emit?.(\"detector.summary\", options.detectorDebug.summary, {\n verbosity: \"compact\",\n });\n const hintRelabeled = reapplyResolvedLatinHintRules(resolved, chunks, options);\n return reapplyDeferredLatinFallback(hintRelabeled, options);\n}\n\nexport async function wordCounterWithWasmDetector(\n text: string,\n options: DetectorWordCounterOptions = {},\n) {\n const chunks = await segmentTextByLocaleWithWasmDetector(text, options);\n return buildWordCounterResultFromChunks(chunks, options);\n}\n\nexport async function countSectionsWithWasmDetector(\n input: string,\n section: Parameters<typeof countSectionsWithResolvedDetector>[1],\n options: DetectorCountSectionsOptions = {},\n) {\n return countSectionsWithResolvedDetector(input, section, options);\n}\n\nexport async function inspectTextWithWasmDetector(\n text: string,\n options: DetectorInspectOptions = {},\n): Promise<DetectorInspectResult> {\n const input = createInspectInput(text, options.input);\n const tracedChunks = segmentTextByLocaleWithTrace(\n text,\n createDeferredLatinPreSegmentOptions(options),\n );\n const chunks = tracedChunks.map(({ locale, text: chunkText }) => ({\n locale,\n text: chunkText,\n }));\n const windows = buildDetectorWindows(chunks);\n\n if (options.view === \"engine\") {\n if (windows.length === 0) {\n return buildEmptyEngineInspectResult(input);\n }\n\n const window = windows[0]!;\n return buildEngineInspectResult(input, window, chunks, executeEngineSample);\n }\n\n if (text.trim().length === 0) {\n return buildEmptyPipelineInspectResult(input);\n }\n\n const resolved = [...chunks];\n const resolvedWindows: Array<\n ResolvedDetectorWindow & { window: DetectorWindow; windowIndex: number }\n > = [];\n\n for (const [windowIndex, window] of windows.entries()) {\n const resolution = await resolveWindowLocale(window, windowIndex, chunks, options);\n resolvedWindows.push({\n ...resolution,\n window,\n windowIndex,\n });\n for (let index = window.startIndex; index <= window.endIndex; index += 1) {\n const chunk = resolved[index];\n if (!chunk) {\n continue;\n }\n resolved[index] = {\n ...chunk,\n locale: resolution.resolvedLocale,\n };\n }\n }\n\n const hintRelabeled = reapplyResolvedLatinHintRules(resolved, chunks, options);\n const finalResolved = reapplyDeferredLatinFallback(hintRelabeled, options);\n\n return buildPipelineInspectResult(input, tracedChunks, resolvedWindows, finalResolved);\n}\n","import type { SectionMode } from \"../markdown\";\nimport type { LocaleChunk } from \"../wc/types\";\nimport {\n countSectionsWithRegexDetector,\n inspectTextWithRegexDetector,\n segmentTextByLocaleWithRegexDetector,\n wordCounterWithRegexDetector,\n} from \"./none\";\nimport {\n countSectionsWithWasmDetector,\n inspectTextWithWasmDetector,\n segmentTextByLocaleWithWasmDetector,\n WASM_DETECTOR_RUNTIME_UNAVAILABLE_MESSAGE,\n wordCounterWithWasmDetector,\n} from \"./wasm\";\nimport { inspectTextWithDetector } from \"./inspect\";\nimport { createDetectorDebugSummary, mergeDetectorDebugSummaries } from \"./debug\";\nimport type {\n DetectorContentGateMode,\n DetectorContentGateOptions,\n DetectorCountSectionsOptions,\n DetectorLocaleOptions,\n DetectorMode,\n DetectorResult,\n DetectorSource,\n DetectorWordCounterOptions,\n} from \"./types\";\n\nexport type {\n DetectorContentGateMode,\n DetectorContentGateOptions,\n DetectorCountSections,\n DetectorCountSectionsOptions,\n DetectorCountResult,\n DetectorLocaleOptions,\n DetectorMode,\n DetectorResult,\n DetectorRuntimeOptions,\n DetectorSource,\n DetectorWordCounterOptions,\n} from \"./types\";\nexport type {\n DetectorInspectChunk,\n DetectorInspectDecision,\n DetectorInspectEngine,\n DetectorInspectEngineRaw,\n DetectorInspectEngineResult,\n DetectorInspectInput,\n DetectorInspectInputOptions,\n DetectorInspectInputSourceType,\n DetectorInspectKind,\n DetectorInspectOptions,\n DetectorInspectPipelineResult,\n DetectorInspectResult,\n DetectorInspectSample,\n DetectorInspectSchemaVersion,\n DetectorInspectView,\n DetectorInspectWindow,\n} from \"./inspect-types\";\nexport type {\n DetectorDebugContext,\n DetectorDebugSummary,\n DetectorDebugVerbosity,\n DetectorEvidenceConfig,\n DetectorFallbackReason,\n} from \"./debug\";\n\nexport const DETECTOR_MODES: DetectorMode[] = [\"regex\", \"wasm\"];\nexport const DEFAULT_DETECTOR_MODE: DetectorMode = \"regex\";\n\nexport function resolveDetectorMode(mode?: DetectorMode): DetectorMode {\n return mode ?? DEFAULT_DETECTOR_MODE;\n}\n\nexport function assertDetectorModeImplemented(mode?: DetectorMode): void {\n void mode;\n}\n\nexport async function segmentTextByLocaleWithDetector(\n text: string,\n options: DetectorLocaleOptions = {},\n): Promise<LocaleChunk[]> {\n const mode = resolveDetectorMode(options.detector);\n if (mode === \"wasm\") {\n return segmentTextByLocaleWithWasmDetector(text, options);\n }\n return segmentTextByLocaleWithRegexDetector(text, options);\n}\n\nexport async function wordCounterWithDetector(\n text: string,\n options: DetectorWordCounterOptions = {},\n) {\n const mode = resolveDetectorMode(options.detector);\n if (mode === \"wasm\") {\n return wordCounterWithWasmDetector(text, options);\n }\n return wordCounterWithRegexDetector(text, options);\n}\n\nexport async function countSectionsWithDetector(\n input: string,\n section: SectionMode,\n options: DetectorCountSectionsOptions = {},\n) {\n const mode = resolveDetectorMode(options.detector);\n if (mode === \"wasm\") {\n return countSectionsWithWasmDetector(input, section, options);\n }\n return countSectionsWithRegexDetector(input, section, options);\n}\n\nexport const DETECTOR_SOURCES: DetectorSource[] = [\"script\", \"hint\", \"wasm\"];\nexport const DEFAULT_DETECTOR_RESULT_SOURCE: DetectorSource = \"script\";\nexport { WASM_DETECTOR_RUNTIME_UNAVAILABLE_MESSAGE };\nexport {\n createDetectorDebugSummary,\n inspectTextWithDetector,\n inspectTextWithRegexDetector,\n inspectTextWithWasmDetector,\n mergeDetectorDebugSummaries,\n};\n\nexport function createDetectorResult(\n tag: string,\n source: DetectorSource = DEFAULT_DETECTOR_RESULT_SOURCE,\n confidence?: number,\n reliable?: boolean,\n): DetectorResult {\n return {\n tag,\n source,\n ...(confidence === undefined ? {} : { confidence }),\n ...(reliable === undefined ? {} : { reliable }),\n };\n}\n","import type { SectionedResult } from \"../../markdown\";\nimport type { WordCounterResult } from \"../../wc\";\n\nfunction stripCollectorSegmentsFromWordCounterResult(result: WordCounterResult): void {\n if (result.breakdown.mode !== \"collector\") {\n return;\n }\n\n for (const item of result.breakdown.items) {\n item.segments = [];\n }\n}\n\nfunction stripCollectorSegmentsFromSectionedResult(result: SectionedResult): void {\n for (const item of result.items) {\n stripCollectorSegmentsFromWordCounterResult(item.result);\n }\n}\n\nexport function compactCollectorSegmentsInCountResult(\n result: WordCounterResult | SectionedResult,\n): void {\n if (\"section\" in result) {\n stripCollectorSegmentsFromSectionedResult(result);\n return;\n }\n\n stripCollectorSegmentsFromWordCounterResult(result);\n}\n","import { readFile } from \"node:fs/promises\";\nimport type { BatchFileInput, BatchSkip } from \"../types\";\n\nexport function isProbablyBinary(buffer: Buffer): boolean {\n if (buffer.length === 0) {\n return false;\n }\n\n const sampleSize = Math.min(buffer.length, 1024);\n let suspicious = 0;\n\n for (let index = 0; index < sampleSize; index += 1) {\n const byte = buffer[index] ?? 0;\n\n if (byte === 0) {\n return true;\n }\n\n if (byte === 9 || byte === 10 || byte === 13) {\n continue;\n }\n\n if (byte >= 32 && byte <= 126) {\n continue;\n }\n\n if (byte >= 128) {\n continue;\n }\n\n suspicious += 1;\n }\n\n return suspicious / sampleSize > 0.3;\n}\n\nexport async function loadBatchInputs(\n filePaths: string[],\n): Promise<{ files: BatchFileInput[]; skipped: BatchSkip[] }> {\n const files: BatchFileInput[] = [];\n const skipped: BatchSkip[] = [];\n\n for (const filePath of filePaths) {\n let buffer: Buffer;\n try {\n buffer = await readFile(filePath);\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n skipped.push({ path: filePath, reason: `not readable: ${message}` });\n continue;\n }\n\n if (isProbablyBinary(buffer)) {\n skipped.push({ path: filePath, reason: \"binary file\" });\n continue;\n }\n\n files.push({\n path: filePath,\n content: buffer.toString(\"utf8\"),\n });\n }\n\n return { files, skipped };\n}\n","import { readFile } from \"node:fs/promises\";\nimport { parentPort, workerData } from \"node:worker_threads\";\nimport { countSections } from \"../../../../markdown\";\nimport { countSectionsWithDetector, wordCounterWithDetector } from \"../../../../detector\";\nimport { createDetectorDebugSummary } from \"../../../../detector/debug\";\nimport wordCounter from \"../../../../wc\";\nimport { compactCollectorSegmentsInCountResult } from \"../../aggregate\";\nimport { isProbablyBinary } from \"../../../path/load\";\nimport type { WorkerConfig, WorkerRequestMessage, WorkerResponseMessage } from \"./protocol\";\n\nconst config = workerData as WorkerConfig;\n\nif (!parentPort) {\n throw new Error(\"Worker protocol init failed: missing parentPort.\");\n}\n\nparentPort.on(\"message\", async (message: WorkerRequestMessage) => {\n if (message.type === \"shutdown\") {\n parentPort?.close();\n return;\n }\n\n const path = message.path;\n\n let buffer: Buffer;\n try {\n buffer = await readFile(path);\n } catch (error) {\n const code =\n typeof error === \"object\" && error !== null && \"code\" in error\n ? String(error.code)\n : undefined;\n const messageText = error instanceof Error ? error.message : String(error);\n\n if (code === \"EMFILE\" || code === \"ENFILE\") {\n const response: WorkerResponseMessage = {\n type: \"fatal\",\n taskId: message.taskId,\n index: message.index,\n path,\n code,\n message: messageText,\n };\n parentPort?.postMessage(response);\n return;\n }\n\n const response: WorkerResponseMessage = {\n type: \"result\",\n taskId: message.taskId,\n index: message.index,\n payload: {\n kind: \"skip\",\n skip: { path, reason: `not readable: ${messageText}` },\n },\n };\n parentPort?.postMessage(response);\n return;\n }\n\n if (config.detectBinary && isProbablyBinary(buffer)) {\n const response: WorkerResponseMessage = {\n type: \"result\",\n taskId: message.taskId,\n index: message.index,\n payload: {\n kind: \"skip\",\n skip: { path, reason: \"binary file\" },\n },\n };\n parentPort?.postMessage(response);\n return;\n }\n\n try {\n const content = buffer.toString(\"utf8\");\n const detectorDebug =\n config.detectorMode === \"wasm\" && config.debugEnabled\n ? {\n emit: (\n event: string,\n details?: Record<string, unknown>,\n options?: { verbosity?: \"compact\" | \"verbose\" },\n ) => {\n const debugEvent: WorkerResponseMessage = {\n type: \"debug-event\",\n taskId: message.taskId,\n index: message.index,\n event,\n details,\n verbosity: options?.verbosity,\n };\n parentPort?.postMessage(debugEvent);\n },\n summary: createDetectorDebugSummary(config.detectorMode),\n ...(config.detectorEvidence\n ? {\n evidence: {\n verbosity: config.debugVerbosity ?? \"compact\",\n mode: config.wcOptions.mode ?? \"chunk\",\n section: config.section,\n },\n }\n : {}),\n }\n : undefined;\n const result =\n config.detectorMode === \"regex\"\n ? config.section === \"all\"\n ? wordCounter(content, config.wcOptions)\n : countSections(content, config.section, config.wcOptions)\n : config.section === \"all\"\n ? await wordCounterWithDetector(content, {\n ...config.wcOptions,\n detector: config.detectorMode,\n detectorDebug,\n })\n : await countSectionsWithDetector(content, config.section, {\n ...config.wcOptions,\n detector: config.detectorMode,\n detectorDebug,\n });\n\n if (!config.preserveCollectorSegments) {\n compactCollectorSegmentsInCountResult(result);\n }\n\n const response: WorkerResponseMessage = {\n type: \"result\",\n taskId: message.taskId,\n index: message.index,\n payload: {\n kind: \"file\",\n file: {\n path,\n result,\n ...(detectorDebug?.summary ? { debug: { detector: detectorDebug.summary } } : {}),\n },\n },\n };\n parentPort?.postMessage(response);\n } catch (error) {\n const code =\n typeof error === \"object\" && error !== null && \"code\" in error\n ? String(error.code)\n : undefined;\n const messageText = error instanceof Error ? error.message : String(error);\n const response: WorkerResponseMessage = {\n type: \"fatal\",\n taskId: message.taskId,\n index: message.index,\n path,\n code,\n message: messageText,\n };\n parentPort?.postMessage(response);\n }\n});\n"],"mappings":";;;;;;;;AAAA,SAAgB,qBACd,QACA,KAC2B;CAC3B,MAAM,WAAW,OAAO;AACxB,KAAI,MAAM,QAAQ,SAAS,CACzB,QAAO;CAET,MAAM,OAAkC,EAAE;AAC1C,QAAO,OAAO;AACd,QAAO;;AAGT,SAAgB,mBAAmB,QAAuC;AACxE,MAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,EAAE;AACjD,MAAI,CAAC,MAAM,QAAQ,MAAM,CACvB;AASF,SAAO,OAPW,MACf,KAAK,UACJ,OAAO,QAAQ,MAAM,CAClB,KAAK,CAAC,UAAU,gBAAgB,GAAG,SAAS,GAAG,aAAa,CAC5D,KAAK,KAAK,CACd,CACA,KAAK,MAAM;;;;;ACxBlB,SAAS,eAAe,KAAqB;CAC3C,MAAM,UAAU,IAAI,MAAM;AAC1B,KACG,QAAQ,WAAW,KAAI,IAAI,QAAQ,SAAS,KAAI,IAChD,QAAQ,WAAW,IAAI,IAAI,QAAQ,SAAS,IAAI,CAEjD,QAAO,QAAQ,MAAM,GAAG,GAAG;AAE7B,QAAO;;AAGT,SAAgB,iBAAiB,KAA4B;CAC3D,MAAM,UAAU,IAAI,MAAM;AAC1B,KAAI,CAAC,QACH,QAAO;AAGT,KACG,QAAQ,WAAW,KAAI,IAAI,QAAQ,SAAS,KAAI,IAChD,QAAQ,WAAW,IAAI,IAAI,QAAQ,SAAS,IAAI,EACjD;EACA,MAAM,WAAW,eAAe,QAAQ;AACxC,SAAO,WAAW,WAAW;;CAG/B,MAAM,WAAW,QAAQ,MAAM,IAAI,CAAC,KAAK,YAAY,QAAQ,MAAM,CAAC;AACpE,KAAI,SAAS,MAAM,YAAY,CAAC,QAAQ,CACtC,QAAO;AAET,QAAO,SAAS,KAA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AM,KAAK,GAClB,KAAA;EACN,MAAM,cAAc,iBAAiB,QAAQ,MAAM,UAAU,OAAO,MAAM;EAC1E,MAAM,WAAkC;GACtC,MAAM;GACN,QAAQ,QAAQ;GAChB,OAAO,QAAQ;GACf;GACA;GACA,SAAS;GACV;AACD,cAAY,YAAY,SAAS;;EAEnC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"worker-pool.mjs","names":[],"sources":["../../src/cli/batch/jobs/worker-pool.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport { fileURLToPath } from \"node:url\";\nimport { Worker } from \"node:worker_threads\";\nimport type { SectionMode } from \"../../../markdown\";\nimport type { DetectorMode } from \"../../../detector\";\nimport type { DetectorDebugVerbosity } from \"../../../detector/debug\";\nimport type wordCounter from \"../../../wc\";\nimport type { BatchProgressSnapshot } from \"../../progress/reporter\";\nimport type { BatchFileResult, BatchSkip } from \"../../types\";\nimport type {\n WorkerRequestMessage,\n WorkerResponseMessage,\n WorkerTaskMessage,\n} from \"./worker/protocol\";\n\ntype CountBatchInputsWithWorkerPoolOptions = {\n filePaths: string[];\n jobs: number;\n section: SectionMode;\n detectorMode: DetectorMode;\n wcOptions: Parameters<typeof wordCounter>[1];\n detectBinary: boolean;\n preserveCollectorSegments: boolean;\n detectorEvidence?: boolean;\n debugVerbosity?: DetectorDebugVerbosity;\n onFileProcessed?: (snapshot: BatchProgressSnapshot) => void;\n onDetectorDebugEvent?: (\n event: string,\n details?: Record<string, unknown>,\n options?: { verbosity?: \"compact\" | \"verbose\" },\n ) => void;\n debugEnabled?: boolean;\n};\n\nexport class WorkerPoolUnavailableError extends Error {}\nexport class WorkerPoolTaskFatalError extends Error {\n path: string;\n code?: string;\n\n constructor(path: string, code: string | undefined, message: string) {\n super(message);\n this.path = path;\n this.code = code;\n }\n}\n\ntype CompletedEntry =\n | {\n kind: \"file\";\n file: BatchFileResult;\n }\n | {\n kind: \"skip\";\n skip: BatchSkip;\n };\n\ntype PendingTask = {\n index: number;\n path: string;\n workerIndex: number;\n};\n\nexport function resolveWorkerEntryUrl(): URL | null {\n const candidates = [\n new URL(\"./worker/count-worker.mjs\", import.meta.url),\n new URL(\"./worker/count-worker.js\", import.meta.url),\n new URL(\"./worker/count-worker.ts\", import.meta.url),\n ];\n\n for (const candidate of candidates) {\n if (existsSync(fileURLToPath(candidate))) {\n return candidate;\n }\n }\n\n return null;\n}\n\nexport function isWorkerThreadsAvailable(): boolean {\n return typeof Worker === \"function\";\n}\n\nfunction isWorkerResponseMessage(value: unknown): value is WorkerResponseMessage {\n if (typeof value !== \"object\" || value === null) {\n return false;\n }\n\n if (!(\"type\" in value)) {\n return false;\n }\n\n const type = (value as { type?: unknown }).type;\n return type === \"result\" || type === \"fatal\" || type === \"debug-event\";\n}\n\nexport async function countBatchInputsWithWorkerPool(\n options: CountBatchInputsWithWorkerPoolOptions,\n): Promise<{ files: BatchFileResult[]; skipped: BatchSkip[] }> {\n if (options.filePaths.length === 0) {\n return { files: [], skipped: [] };\n }\n\n const workerEntryUrl = resolveWorkerEntryUrl();\n if (!workerEntryUrl) {\n throw new WorkerPoolUnavailableError(\n \"Worker pool unavailable: count-worker entry file was not found.\",\n );\n }\n\n const safeRequestedJobs = Number.isFinite(options.jobs) ? Math.floor(options.jobs) : 1;\n const workerCount = Math.max(1, Math.min(options.filePaths.length, safeRequestedJobs));\n const workers: Worker[] = [];\n const completedEntries: Array<CompletedEntry | undefined> = Array.from({\n length: options.filePaths.length,\n });\n const pendingTasks = new Map<number, PendingTask>();\n const requestedShutdownWorkers = new Set<number>();\n let nextIndex = 0;\n let nextTaskId = 1;\n let completed = 0;\n let settled = false;\n\n const teardownWorkers = async (): Promise<void> => {\n await Promise.allSettled(workers.map((worker) => worker.terminate()));\n };\n\n return new Promise((resolve, reject) => {\n const fail = async (error: Error): Promise<void> => {\n if (settled) {\n return;\n }\n settled = true;\n await teardownWorkers();\n reject(error);\n };\n\n const complete = async (): Promise<void> => {\n if (settled) {\n return;\n }\n settled = true;\n await teardownWorkers();\n\n const files: BatchFileResult[] = [];\n const skipped: BatchSkip[] = [];\n for (const entry of completedEntries) {\n if (!entry) {\n reject(new Error(\"Worker pool finalize failed: missing completed entry.\"));\n return;\n }\n if (entry.kind === \"file\") {\n files.push(entry.file);\n continue;\n }\n skipped.push(entry.skip);\n }\n\n resolve({ files, skipped });\n };\n\n const assignNextTask = (worker: Worker, workerIndex: number): void => {\n if (settled) {\n return;\n }\n\n if (nextIndex >= options.filePaths.length) {\n requestedShutdownWorkers.add(workerIndex);\n const shutdown: WorkerRequestMessage = { type: \"shutdown\" };\n worker.postMessage(shutdown);\n return;\n }\n\n const index = nextIndex;\n nextIndex += 1;\n const path = options.filePaths[index];\n if (!path) {\n void fail(new Error(`Worker pool dispatch failed: missing path at index ${index}.`));\n return;\n }\n\n const taskId = nextTaskId;\n nextTaskId += 1;\n pendingTasks.set(taskId, { index, path, workerIndex });\n\n const message: WorkerTaskMessage = {\n type: \"task\",\n taskId,\n index,\n path,\n };\n worker.postMessage(message);\n };\n\n for (let workerIndex = 0; workerIndex < workerCount; workerIndex += 1) {\n let worker: Worker;\n try {\n worker = new Worker(workerEntryUrl, {\n workerData: {\n section: options.section,\n detectorMode: options.detectorMode,\n wcOptions: options.wcOptions,\n detectBinary: options.detectBinary,\n preserveCollectorSegments: options.preserveCollectorSegments,\n detectorEvidence: options.detectorEvidence,\n debugVerbosity: options.debugVerbosity,\n debugEnabled: options.debugEnabled,\n },\n });\n } catch (error) {\n const message = error instanceof Error ? error.message : String(error);\n void fail(new WorkerPoolUnavailableError(`Worker pool initialization failed: ${message}`));\n return;\n }\n\n workers.push(worker);\n\n worker.on(\"message\", (value: unknown) => {\n if (!isWorkerResponseMessage(value)) {\n void fail(new Error(\"Worker protocol mismatch: received unknown response payload.\"));\n return;\n }\n\n const pending = pendingTasks.get(value.taskId);\n if (!pending) {\n void fail(new Error(`Worker protocol mismatch: unknown task id ${value.taskId}.`));\n return;\n }\n\n if (value.type === \"debug-event\") {\n if (value.index !== pending.index) {\n void fail(\n new Error(\n `Worker protocol mismatch: task index mismatch for ${value.taskId} (expected ${pending.index}, got ${value.index}).`,\n ),\n );\n return;\n }\n options.onDetectorDebugEvent?.(\n value.event,\n {\n path: pending.path,\n ...value.details,\n },\n {\n verbosity: value.verbosity,\n },\n );\n return;\n }\n\n pendingTasks.delete(value.taskId);\n if (value.index !== pending.index) {\n void fail(\n new Error(\n `Worker protocol mismatch: task index mismatch for ${value.taskId} (expected ${pending.index}, got ${value.index}).`,\n ),\n );\n return;\n }\n\n if (value.type === \"fatal\") {\n void fail(\n new WorkerPoolTaskFatalError(\n value.path,\n value.code,\n `Worker task failed for ${value.path} (${value.code ?? \"UNKNOWN\"}): ${value.message}`,\n ),\n );\n return;\n }\n\n if (value.payload.kind === \"file\") {\n completedEntries[pending.index] = {\n kind: \"file\",\n file: value.payload.file,\n };\n } else {\n completedEntries[pending.index] = {\n kind: \"skip\",\n skip: value.payload.skip,\n };\n }\n\n completed += 1;\n options.onFileProcessed?.({\n completed,\n total: options.filePaths.length,\n });\n\n if (completed >= options.filePaths.length) {\n void complete();\n return;\n }\n\n assignNextTask(worker, workerIndex);\n });\n\n worker.on(\"error\", (error) => {\n const message = error instanceof Error ? error.message : String(error);\n void fail(new Error(`Worker runtime failed: ${message}`));\n });\n\n worker.on(\"exit\", (code) => {\n if (settled) {\n return;\n }\n\n const hasPendingTask = [...pendingTasks.values()].some(\n (task) => task.workerIndex === workerIndex,\n );\n const requestedShutdown = requestedShutdownWorkers.has(workerIndex);\n\n if (hasPendingTask) {\n void fail(new Error(`Worker exited before completing assigned tasks (code ${code}).`));\n return;\n }\n\n if (code !== 0) {\n void fail(new Error(`Worker exited unexpectedly with code ${code}.`));\n return;\n }\n\n if (!requestedShutdown && completed < options.filePaths.length) {\n void fail(new Error(\"Worker exited unexpectedly before completion.\"));\n }\n });\n\n assignNextTask(worker, workerIndex);\n }\n });\n}\n"],"mappings":";;;;;;AAkCA,IAAa,6BAAb,cAAgD,MAAM;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