@hakkacl/hakka 0.0.0-stage → 0.1.0

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.
package/package.json CHANGED
@@ -1,6 +1,50 @@
1
1
  {
2
2
  "name": "@hakkacl/hakka",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Hakka phonology, multi-scheme romanization conversion, dialect metadata, and tone sandhi. Pure ESM, zero runtime dependencies.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "main": "./src/index.js",
8
+ "types": "./types/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./types/index.d.ts",
12
+ "import": "./src/index.js",
13
+ "default": "./src/index.js"
14
+ },
15
+ "./data/dialects.json": "./data/dialects.json",
16
+ "./data/*": "./data/*",
17
+ "./src/*": "./src/*",
18
+ "./package.json": "./package.json"
19
+ },
20
+ "files": [
21
+ "src",
22
+ "types",
23
+ "data",
24
+ "README.md"
25
+ ],
26
+ "scripts": {
27
+ "test": "node --test test/*.test.js"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/HakkaCL/hakka.git",
32
+ "directory": "js"
33
+ },
34
+ "engines": {
35
+ "node": ">=18"
36
+ },
37
+ "keywords": [
38
+ "hakka",
39
+ "客家",
40
+ "客家话",
41
+ "phonology",
42
+ "romanization",
43
+ "hps",
44
+ "ipa",
45
+ "sandhi",
46
+ "tone"
47
+ ],
48
+ "sideEffects": false,
49
+ "dependencies": {}
50
+ }
@@ -0,0 +1,207 @@
1
+ /**
2
+ * Dialect registry, metadata records, and alias resolution.
3
+ *
4
+ * The registry is bundled as `data/dialects.json`, so the package is fully
5
+ * self-contained and has no runtime dependencies.
6
+ */
7
+
8
+ import { readFileSync } from "node:fs";
9
+
10
+ export const DEFAULT_DIALECT = "huiyang";
11
+ export const DEFAULT_SCHEME = "hps_ext";
12
+
13
+ /** Error thrown when a dialect slug, name, or alias cannot be resolved. */
14
+ export class UnknownDialectError extends Error {
15
+ constructor(name, suggestions = []) {
16
+ const hint = suggestions.length > 0 ? ` Did you mean: ${suggestions.join(", ")}?` : "";
17
+ super(`Unknown Hakka dialect: '${name}'.${hint}`);
18
+ this.name = "UnknownDialectError";
19
+ this.dialect = name;
20
+ this.suggestions = suggestions;
21
+ }
22
+ }
23
+
24
+ const DATA_URL = new URL("../data/dialects.json", import.meta.url);
25
+ const DATA = JSON.parse(readFileSync(DATA_URL, "utf8"));
26
+
27
+ if (!Array.isArray(DATA.dialects)) {
28
+ throw new Error("Invalid dialects.json: missing dialects array");
29
+ }
30
+
31
+ function createToneSpec(toneClass, raw = {}) {
32
+ const spec = {
33
+ tone_class: String(toneClass),
34
+ name: String(raw.name ?? ""),
35
+ value: String(raw.value ?? ""),
36
+ };
37
+ Object.defineProperties(spec, {
38
+ toneClass: { enumerable: false, get: () => spec.tone_class },
39
+ });
40
+ return Object.freeze(spec);
41
+ }
42
+
43
+ function createDialect(raw, index) {
44
+ const tones = {};
45
+ for (const [toneClass, toneRaw] of Object.entries(raw.tones ?? {})) {
46
+ tones[String(toneClass)] = createToneSpec(toneClass, toneRaw);
47
+ }
48
+ Object.freeze(tones);
49
+
50
+ const aliases = Object.freeze((raw.aliases ?? []).map(String));
51
+
52
+ const dialect = {
53
+ id: index + 1,
54
+ slug: String(raw.slug),
55
+ name_trad: String(raw.name_trad ?? ""),
56
+ name_simp: String(raw.name_simp ?? ""),
57
+ tier: String(raw.tier ?? ""),
58
+ subgroup: raw.subgroup ?? null,
59
+ location: raw.location ?? null,
60
+ lat: raw.lat === null || raw.lat === undefined ? null : Number(raw.lat),
61
+ lon: raw.lon === null || raw.lon === undefined ? null : Number(raw.lon),
62
+ is_island: Boolean(raw.is_island),
63
+ is_historical: Boolean(raw.is_historical),
64
+ tones,
65
+ use_tone_value: false,
66
+ has_words: Boolean(raw.has_words),
67
+ char_count: Number(raw.char_count ?? 0),
68
+ word_count: Number(raw.word_count ?? 0),
69
+ source_license: String(raw.license ?? ""),
70
+ license: String(raw.license ?? ""),
71
+ reference: raw.reference ?? null,
72
+ aliases,
73
+ };
74
+
75
+ const toneValueFor = (toneClass) => tones[String(toneClass)]?.value ?? "";
76
+ const toneClassForValue = (toneValue) => {
77
+ if (!toneValue) return "";
78
+ for (const [toneClass, spec] of Object.entries(tones)) {
79
+ if (spec.value === toneValue) return toneClass;
80
+ }
81
+ return "";
82
+ };
83
+
84
+ Object.defineProperties(dialect, {
85
+ nameTrad: { enumerable: false, get: () => dialect.name_trad },
86
+ nameSimp: { enumerable: false, get: () => dialect.name_simp },
87
+ isIsland: { enumerable: false, get: () => dialect.is_island },
88
+ isHistorical: { enumerable: false, get: () => dialect.is_historical },
89
+ useToneValue: { enumerable: false, get: () => dialect.use_tone_value },
90
+ hasWords: { enumerable: false, get: () => dialect.has_words },
91
+ charCount: { enumerable: false, get: () => dialect.char_count },
92
+ wordCount: { enumerable: false, get: () => dialect.word_count },
93
+ sourceLicense: { enumerable: false, get: () => dialect.source_license },
94
+ toneValueFor: { enumerable: false, value: toneValueFor },
95
+ toneClassForValue: { enumerable: false, value: toneClassForValue },
96
+ tone_value_for: { enumerable: false, value: toneValueFor },
97
+ tone_class_for_value: { enumerable: false, value: toneClassForValue },
98
+ });
99
+
100
+ return Object.freeze(dialect);
101
+ }
102
+
103
+ const dialects = Object.freeze(DATA.dialects.map(createDialect));
104
+
105
+ const dialectBySlug = new Map();
106
+ const dialectByAlias = new Map();
107
+
108
+ for (const dialect of dialects) {
109
+ dialectBySlug.set(dialect.slug.toLowerCase(), dialect);
110
+ for (const alias of dialect.aliases) {
111
+ const key = alias.toLowerCase();
112
+ if (!dialectByAlias.has(key)) {
113
+ dialectByAlias.set(key, dialect);
114
+ }
115
+ }
116
+ if (!dialectByAlias.has(dialect.slug.toLowerCase())) {
117
+ dialectByAlias.set(dialect.slug.toLowerCase(), dialect);
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Resolve a dialect slug, Chinese name (traditional or simplified), or alias.
123
+ *
124
+ * @param {string} [name="huiyang"]
125
+ * @returns {object} Frozen dialect metadata record.
126
+ * @throws {Error} If the dialect is unknown.
127
+ */
128
+ export function getDialect(name = DEFAULT_DIALECT) {
129
+ const original = name;
130
+ let key = String(name ?? "").trim();
131
+ if (!key) key = DEFAULT_DIALECT;
132
+ key = key.toLowerCase();
133
+
134
+ const dialect = dialectByAlias.get(key) ?? dialectBySlug.get(key);
135
+ if (dialect) return dialect;
136
+
137
+ const suggestions = [];
138
+ for (const candidate of dialects) {
139
+ if (
140
+ candidate.slug.toLowerCase().includes(key) ||
141
+ candidate.name_trad.toLowerCase().includes(key) ||
142
+ candidate.name_simp.toLowerCase().includes(key)
143
+ ) {
144
+ if (!suggestions.includes(candidate.slug)) suggestions.push(candidate.slug);
145
+ if (suggestions.length === 5) break;
146
+ }
147
+ }
148
+ throw new UnknownDialectError(original, suggestions);
149
+ }
150
+
151
+ /**
152
+ * @typedef {object} ListDialectsOptions
153
+ * @property {string} [tier] Exact tier: `standard`, `core`, or `mcpdict`.
154
+ * @property {string} [subgroup] Substring match against the dialect subgroup.
155
+ * @property {boolean} [hasWords] Require (or exclude) dialects with word data.
156
+ * @property {boolean} [has_words] Snake-case alias of `hasWords`.
157
+ * @property {boolean} [historicalOnly] Only historical attestations.
158
+ * @property {boolean} [islandsOnly] Only island / diaspora varieties.
159
+ * @property {boolean} [historical_only] Snake-case alias of `historicalOnly`.
160
+ * @property {boolean} [islands_only] Snake-case alias of `islandsOnly`.
161
+ */
162
+
163
+ /**
164
+ * List available Hakka varieties with optional filtering.
165
+ *
166
+ * @param {ListDialectsOptions} [options]
167
+ * @returns {object[]}
168
+ */
169
+ export function listDialects(options = {}) {
170
+ const {
171
+ tier,
172
+ subgroup,
173
+ hasWords,
174
+ has_words: hasWordsSnake,
175
+ historicalOnly = false,
176
+ islandsOnly = false,
177
+ historical_only: historicalOnlySnake,
178
+ islands_only: islandsOnlySnake,
179
+ } = options ?? {};
180
+
181
+ let wantWords = null;
182
+ if (hasWords !== undefined && hasWords !== null) {
183
+ wantWords = hasWords;
184
+ } else if (hasWordsSnake !== undefined && hasWordsSnake !== null) {
185
+ wantWords = hasWordsSnake;
186
+ }
187
+ const wantHistorical = Boolean(historicalOnly || historicalOnlySnake);
188
+ const wantIslands = Boolean(islandsOnly || islandsOnlySnake);
189
+ const subgroupNeedle =
190
+ subgroup === undefined || subgroup === null ? null : String(subgroup).toLowerCase();
191
+
192
+ return dialects.filter((dialect) => {
193
+ if (tier !== undefined && tier !== null && dialect.tier !== tier) return false;
194
+ if (wantWords !== null && dialect.has_words !== Boolean(wantWords)) {
195
+ return false;
196
+ }
197
+ if (wantHistorical && !dialect.is_historical) return false;
198
+ if (wantIslands && !dialect.is_island) return false;
199
+ if (
200
+ subgroupNeedle !== null &&
201
+ !String(dialect.subgroup ?? "").toLowerCase().includes(subgroupNeedle)
202
+ ) {
203
+ return false;
204
+ }
205
+ return true;
206
+ });
207
+ }
package/src/index.js ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @hakkacl/hakka — Hakka phonology, romanization conversion, and tone sandhi.
3
+ *
4
+ * Pure ESM, zero runtime dependencies.
5
+ */
6
+
7
+ export { DEFAULT_DIALECT, DEFAULT_SCHEME, getDialect, listDialects } from "./dialects.js";
8
+ export { CANONICAL_SCHEMES } from "./phon/schemes.js";
9
+ export { parseSyllable, parseSyllables } from "./phon/parser.js";
10
+ export { formatSyllable, convert } from "./phon/converter.js";
11
+ export { validateSyllable } from "./phon/validate.js";
12
+ export { applySandhi, listSandhiDialects } from "./sandhi.js";
@@ -0,0 +1,365 @@
1
+ /**
2
+ * Formatters and bidirectional converters between Hakka phonetic schemes.
3
+ */
4
+
5
+ import { DEFAULT_DIALECT, getDialect } from "../dialects.js";
6
+ import { parseSyllables } from "./parser.js";
7
+ import {
8
+ DEFAULT_HPS_DIACRITICS,
9
+ DIGITS_TO_CHAO,
10
+ SUPERSCRIPT_DIGITS,
11
+ TONE_8_TO_6,
12
+ getPhonProfile,
13
+ normalizeScheme,
14
+ } from "./schemes.js";
15
+ import {
16
+ baseOf,
17
+ createSyllable,
18
+ hpsExtOf,
19
+ isSyllabicConsonant,
20
+ syllableFields,
21
+ } from "./syllable.js";
22
+
23
+ const ONSET_TO_IPA_BASE = Object.freeze({
24
+ "": "",
25
+ b: "p",
26
+ p: "pʰ",
27
+ bb: "b",
28
+ m: "m",
29
+ f: "f",
30
+ v: "v",
31
+ d: "t",
32
+ t: "tʰ",
33
+ dd: "d",
34
+ n: "n",
35
+ l: "l",
36
+ r: "ʝ",
37
+ g: "k",
38
+ k: "kʰ",
39
+ gg: "ɡ",
40
+ gh: "ɣ",
41
+ ng: "ŋ",
42
+ nj: "ȵ",
43
+ h: "h",
44
+ hh: "ɦ",
45
+ z: "ts",
46
+ c: "tsʰ",
47
+ s: "s",
48
+ zz: "z",
49
+ j: "tɕ",
50
+ q: "tɕʰ",
51
+ jj: "ʑ",
52
+ sl: "ɬ",
53
+ th: "θ",
54
+ "'": "ʔ",
55
+ });
56
+
57
+ const CODA_TO_IPA = Object.freeze({
58
+ "": "",
59
+ i: "i",
60
+ u: "u",
61
+ m: "m",
62
+ n: "n",
63
+ ng: "ŋ",
64
+ b: "p",
65
+ d: "t",
66
+ g: "k",
67
+ h: "ʔ",
68
+ });
69
+
70
+ const SYLLABIC_CONSONANT_IPA = Object.freeze({
71
+ m: "m̩",
72
+ n: "n̩",
73
+ ng: "ŋ̍",
74
+ v: "v̩",
75
+ });
76
+
77
+ function hasOwn(object, key) {
78
+ return Object.hasOwn(object, key);
79
+ }
80
+
81
+ function translateCharacters(text, table) {
82
+ let output = "";
83
+ for (const char of text) {
84
+ output += hasOwn(table, char) ? table[char] : char;
85
+ }
86
+ return output;
87
+ }
88
+
89
+ function onsetToIpa(onset, dialectSlug) {
90
+ const phonProfile = getPhonProfile(dialectSlug);
91
+ if (onset === "zh" || onset === "ch" || onset === "sh" || onset === "rh") {
92
+ if (phonProfile.sibilant_series === "retroflex") {
93
+ return { zh: "tʂ", ch: "tʂʰ", sh: "ʂ", rh: "ʐ" }[onset];
94
+ }
95
+ return { zh: "tʃ", ch: "tʃʰ", sh: "ʃ", rh: "ʒ" }[onset];
96
+ }
97
+ if (onset === "x") {
98
+ return phonProfile.x_ipa;
99
+ }
100
+ return hasOwn(ONSET_TO_IPA_BASE, onset) ? ONSET_TO_IPA_BASE[onset] : onset;
101
+ }
102
+
103
+ function formatIpaSegments(syllable, dialectSlug) {
104
+ const phonProfile = getPhonProfile(dialectSlug);
105
+
106
+ if (isSyllabicConsonant(syllable)) {
107
+ return SYLLABIC_CONSONANT_IPA[syllable.nucleus];
108
+ }
109
+
110
+ const onsetIpa = onsetToIpa(syllable.onset, dialectSlug);
111
+
112
+ // Special syllabic consonant with an onset (e.g. l + v).
113
+ if (
114
+ (syllable.nucleus === "m" ||
115
+ syllable.nucleus === "n" ||
116
+ syllable.nucleus === "ng" ||
117
+ syllable.nucleus === "v") &&
118
+ !syllable.medial &&
119
+ !syllable.coda
120
+ ) {
121
+ return onsetIpa + SYLLABIC_CONSONANT_IPA[syllable.nucleus];
122
+ }
123
+
124
+ const medialIpa = { "": "", i: "i", u: "u", y: "y" }[syllable.medial] ?? syllable.medial;
125
+
126
+ // Changting `eu` -> `əɯ` and `iu` -> `iəɯ`.
127
+ if (phonProfile.eu_ipa === "əɯ") {
128
+ if (syllable.medial === "" && syllable.nucleus === "e" && syllable.coda === "u") {
129
+ return `${onsetIpa}əɯ`;
130
+ }
131
+ if (syllable.medial === "i" && syllable.nucleus === "u" && syllable.coda === "") {
132
+ return `${onsetIpa}iəɯ`;
133
+ }
134
+ }
135
+
136
+ let nucleusIpa;
137
+ if (
138
+ phonProfile.ung_as_ong &&
139
+ syllable.nucleus === "u" &&
140
+ (syllable.coda === "ng" || syllable.coda === "g")
141
+ ) {
142
+ nucleusIpa = "o";
143
+ } else if (syllable.nucleus === "ii") {
144
+ if (
145
+ phonProfile.ii_closed_as_schwa &&
146
+ (syllable.coda === "m" ||
147
+ syllable.coda === "n" ||
148
+ syllable.coda === "b" ||
149
+ syllable.coda === "d")
150
+ ) {
151
+ nucleusIpa = "ə";
152
+ } else if (
153
+ syllable.onset === "zh" ||
154
+ syllable.onset === "ch" ||
155
+ syllable.onset === "sh" ||
156
+ syllable.onset === "rh"
157
+ ) {
158
+ nucleusIpa = phonProfile.ii_post_sibilant;
159
+ } else {
160
+ nucleusIpa = "ɿ";
161
+ }
162
+ } else if (syllable.nucleus === "a") {
163
+ nucleusIpa = phonProfile.a_ipa;
164
+ } else if (syllable.nucleus === "e") {
165
+ nucleusIpa = phonProfile.e_ipa;
166
+ } else if (syllable.nucleus === "ee") {
167
+ nucleusIpa = phonProfile.ee_ipa;
168
+ } else if (syllable.nucleus === "ae") {
169
+ nucleusIpa = "æ";
170
+ } else if (syllable.nucleus === "o") {
171
+ nucleusIpa = phonProfile.o_ipa;
172
+ } else if (syllable.nucleus === "oo") {
173
+ nucleusIpa = "ɔ";
174
+ } else if (syllable.nucleus === "er") {
175
+ nucleusIpa = "ə";
176
+ } else if (syllable.nucleus === "y") {
177
+ nucleusIpa = "y";
178
+ } else if (syllable.nucleus === "oe") {
179
+ nucleusIpa = "œ";
180
+ } else {
181
+ nucleusIpa = syllable.nucleus;
182
+ }
183
+
184
+ if (syllable.nasalized) {
185
+ nucleusIpa += "\u0303";
186
+ }
187
+
188
+ const codaIpa = hasOwn(CODA_TO_IPA, syllable.coda) ? CODA_TO_IPA[syllable.coda] : syllable.coda;
189
+ return `${onsetIpa}${medialIpa}${nucleusIpa}${codaIpa}`;
190
+ }
191
+
192
+ function formatGd(syllable) {
193
+ const tone6 = hasOwn(TONE_8_TO_6, syllable.tone) ? TONE_8_TO_6[syllable.tone] : syllable.tone;
194
+ if (isSyllabicConsonant(syllable)) {
195
+ return `${syllable.nucleus}${tone6}`;
196
+ }
197
+
198
+ let { onset, medial, nucleus } = syllable;
199
+ const { coda } = syllable;
200
+
201
+ // 1. Zero onset with i medial/nucleus -> y onset.
202
+ if (onset === "" && (medial === "i" || nucleus === "i")) {
203
+ onset = "y";
204
+ if (medial === "i") medial = "";
205
+ } else if ((onset === "z" || onset === "c" || onset === "s") && (medial === "i" || nucleus === "i")) {
206
+ // 2. z/c/s + i (palatalized) -> j/q/x.
207
+ onset = { z: "j", c: "q", s: "x" }[onset];
208
+ } else if ((onset === "z" || onset === "c" || onset === "s") && nucleus === "ii") {
209
+ // 3. Open ii -> i; closed iim/iin/iib/iid -> em/en/eb/ed.
210
+ nucleus = coda === "m" || coda === "n" || coda === "b" || coda === "d" ? "e" : "i";
211
+ } else if (nucleus === "e") {
212
+ // 4. [ɛ] (`e` in Meixian) -> ê.
213
+ nucleus = "ê";
214
+ }
215
+
216
+ // Normalize HPS-Ext vowels outside the GD core gracefully.
217
+ if (nucleus === "ee") {
218
+ nucleus = "ê";
219
+ } else if (nucleus === "oo") {
220
+ nucleus = "o";
221
+ } else if (nucleus === "er") {
222
+ nucleus = "e";
223
+ }
224
+
225
+ const nasal = syllable.nasalized ? "nn" : "";
226
+ return `${onset}${medial}${nucleus}${coda}${nasal}${tone6}`;
227
+ }
228
+
229
+ function formatHkilang(syllable) {
230
+ const tone6 = hasOwn(TONE_8_TO_6, syllable.tone) ? TONE_8_TO_6[syllable.tone] : syllable.tone;
231
+ if (isSyllabicConsonant(syllable)) {
232
+ return `${syllable.nucleus}${tone6}`;
233
+ }
234
+
235
+ const coda = { b: "p", d: "t", g: "k" }[syllable.coda] ?? syllable.coda;
236
+ const nasal = syllable.nasalized ? "nn" : "";
237
+
238
+ let { onset, medial, nucleus } = syllable;
239
+
240
+ if (onset === "'") {
241
+ // Glottal zero-onset ('iu1 歐) -> bare iu1 in HKILANG.
242
+ onset = "";
243
+ } else if (onset === "" && (medial === "i" || nucleus === "i")) {
244
+ // Palatal zero-onset (id7 / iu1 / ia2 / iung5) -> yit5 / yiu1 / ya2 / yung4.
245
+ onset = "y";
246
+ if (medial === "i" && !(nucleus === "u" && coda === "")) {
247
+ medial = "";
248
+ }
249
+ }
250
+
251
+ return `${onset}${medial}${nucleus}${coda}${nasal}${tone6}`;
252
+ }
253
+
254
+ function formatHpsDiacritic(syllable, dialectSlug) {
255
+ const phonProfile = getPhonProfile(dialectSlug);
256
+ const diacriticMap = phonProfile.hps_diacritics ?? DEFAULT_HPS_DIACRITICS;
257
+ const diacritic = hasOwn(diacriticMap, syllable.tone) ? diacriticMap[syllable.tone] : "";
258
+ return `${baseOf(syllable)}${diacritic}`;
259
+ }
260
+
261
+ function resolveFormatArguments(schemeOrOptions, dialect) {
262
+ if (schemeOrOptions !== null && typeof schemeOrOptions === "object") {
263
+ const options = schemeOrOptions;
264
+ return {
265
+ scheme: options.scheme ?? "hps_ext",
266
+ dialect: Object.hasOwn(options, "dialect") ? options.dialect : DEFAULT_DIALECT,
267
+ };
268
+ }
269
+ return {
270
+ scheme: schemeOrOptions ?? "hps_ext",
271
+ dialect: dialect === undefined ? DEFAULT_DIALECT : dialect,
272
+ };
273
+ }
274
+
275
+ /**
276
+ * Format a syllable into the requested transcription scheme.
277
+ *
278
+ * @param {object} syllable
279
+ * @param {string} [scheme="hps_ext"]
280
+ * @param {string | null} [dialect="huiyang"]
281
+ * @returns {string}
282
+ */
283
+ export function formatSyllable(syllable, scheme = "hps_ext", dialect = DEFAULT_DIALECT) {
284
+ const resolved = resolveFormatArguments(scheme, dialect);
285
+ const canonicalScheme = normalizeScheme(resolved.scheme);
286
+ const dialectInfo = resolved.dialect ? getDialect(resolved.dialect) : null;
287
+ const slug = dialectInfo ? dialectInfo.slug : "meixian";
288
+
289
+ const normalized = createSyllable(syllableFields(syllable));
290
+
291
+ if (canonicalScheme === "hps_ext") {
292
+ return hpsExtOf(normalized);
293
+ }
294
+ if (canonicalScheme === "hps_diacritic") {
295
+ return formatHpsDiacritic(normalized, slug);
296
+ }
297
+ if (canonicalScheme === "gd") {
298
+ return formatGd(normalized);
299
+ }
300
+ if (canonicalScheme === "hkilang") {
301
+ return formatHkilang(normalized);
302
+ }
303
+
304
+ const segmentalIpa = formatIpaSegments(normalized, slug);
305
+ if (canonicalScheme === "ipa") {
306
+ const tonePart = normalized.tone || normalized.tone_value;
307
+ return `${segmentalIpa}${tonePart}${normalized.reading_mark}`;
308
+ }
309
+
310
+ // Tone-value lookup: an explicit tone_value takes precedence (e.g. sandhi).
311
+ const toneValue =
312
+ normalized.tone_value ||
313
+ (dialectInfo && normalized.tone ? dialectInfo.toneValueFor(normalized.tone) : "");
314
+
315
+ if (canonicalScheme === "ipa_value") {
316
+ const superscript = toneValue
317
+ ? translateCharacters(toneValue, SUPERSCRIPT_DIGITS)
318
+ : translateCharacters(normalized.tone, SUPERSCRIPT_DIGITS);
319
+ return `${segmentalIpa}${superscript}${normalized.reading_mark}`;
320
+ }
321
+ if (canonicalScheme === "ipa_chao") {
322
+ const chao = toneValue
323
+ ? translateCharacters(toneValue, DIGITS_TO_CHAO)
324
+ : normalized.tone;
325
+ return `${segmentalIpa}${chao}${normalized.reading_mark}`;
326
+ }
327
+
328
+ throw new Error(`Unsupported output scheme: '${resolved.scheme}'`);
329
+ }
330
+
331
+ function resolveConvertArguments(srcOrOptions, dst, dialect, sep) {
332
+ if (srcOrOptions !== null && typeof srcOrOptions === "object") {
333
+ const options = srcOrOptions;
334
+ return {
335
+ src: options.src ?? options.from ?? "hps_ext",
336
+ dst: options.dst ?? options.to ?? "ipa",
337
+ dialect: Object.hasOwn(options, "dialect") ? options.dialect : DEFAULT_DIALECT,
338
+ sep: options.sep ?? " ",
339
+ };
340
+ }
341
+ return {
342
+ src: srcOrOptions ?? "hps_ext",
343
+ dst: dst ?? "ipa",
344
+ dialect: dialect === undefined ? DEFAULT_DIALECT : dialect,
345
+ sep: sep ?? " ",
346
+ };
347
+ }
348
+
349
+ /**
350
+ * Convert a whitespace-separated Hakka transcription between schemes.
351
+ *
352
+ * @param {string} text
353
+ * @param {string} [src="hps_ext"]
354
+ * @param {string} [dst="ipa"]
355
+ * @param {string | null} [dialect="huiyang"]
356
+ * @param {string} [sep=" "]
357
+ * @returns {string}
358
+ */
359
+ export function convert(text, src = "hps_ext", dst = "ipa", dialect = DEFAULT_DIALECT, sep = " ") {
360
+ const resolved = resolveConvertArguments(src, dst, dialect, sep);
361
+ const syllables = parseSyllables(text, resolved.src, resolved.dialect);
362
+ return syllables
363
+ .map((syllable) => formatSyllable(syllable, resolved.dst, resolved.dialect))
364
+ .join(resolved.sep);
365
+ }