nko-kit 0.1.0 → 0.2.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.
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toLatinDetailed = toLatinDetailed;
4
+ exports.toLatin = toLatin;
5
+ const chars_js_1 = require("./chars.js");
6
+ const text_js_1 = require("./text.js");
7
+ function tokenize(source) {
8
+ const chars = Array.from(source.normalize("NFC"));
9
+ const units = [];
10
+ for (let i = 0; i < chars.length; i++) {
11
+ const ch = chars[i];
12
+ const start = i;
13
+ if (ch in chars_js_1.VOWELS || ch === chars_js_1.SYLLABIC_N) {
14
+ let tone;
15
+ let nasal = false;
16
+ while (i + 1 < chars.length) {
17
+ const next = chars[i + 1];
18
+ if (next in chars_js_1.TONE_MARKS)
19
+ tone = chars_js_1.TONE_MARKS[next];
20
+ else if (next === chars_js_1.NASALIZATION)
21
+ nasal = true;
22
+ else if (next === chars_js_1.DOUBLE_DOT_ABOVE) {
23
+ /* ignored on vowels */
24
+ }
25
+ else
26
+ break;
27
+ i++;
28
+ }
29
+ units.push(ch === chars_js_1.SYLLABIC_N
30
+ ? { kind: "syllabicN", tone, nasal, index: start }
31
+ : { kind: "vowel", base: chars_js_1.VOWELS[ch], tone, nasal, index: start });
32
+ }
33
+ else if (ch in chars_js_1.CONSONANTS) {
34
+ let dotted = false;
35
+ while (i + 1 < chars.length && chars[i + 1] === chars_js_1.DOUBLE_DOT_ABOVE) {
36
+ dotted = true;
37
+ i++;
38
+ }
39
+ units.push({ kind: "consonant", base: ch, dotted, index: start });
40
+ }
41
+ else if (ch === chars_js_1.DAGBASINNA) {
42
+ units.push({ kind: "noVowel", index: i });
43
+ }
44
+ else if (chars_js_1.DROPPED.has(ch)) {
45
+ continue;
46
+ }
47
+ else {
48
+ units.push({ kind: "other", text: chars_js_1.PUNCTUATION[ch] ?? ch, index: i });
49
+ }
50
+ }
51
+ return units;
52
+ }
53
+ function accentFor(tone, opts) {
54
+ if (opts.tones !== "accents")
55
+ return "";
56
+ if (!tone)
57
+ return opts.markFalling ? chars_js_1.LATIN_TONE_ACCENTS.falling : "";
58
+ return chars_js_1.LATIN_TONE_ACCENTS[tone.tone];
59
+ }
60
+ function writeVowel(base, tone, nasal, opts) {
61
+ const accent = accentFor(tone, opts);
62
+ const long = tone?.long ? base : "";
63
+ return base + accent + long + (nasal ? "n" : "");
64
+ }
65
+ /**
66
+ * In N'Ko, when two syllables in a row share the same vowel, the first
67
+ * vowel is not written: ߓߡߊߣߊ߲ is read "bamanan". This finds the vowel
68
+ * a bare consonant borrows from the next syllable.
69
+ */
70
+ function borrowedVowel(units, from) {
71
+ const next = units[from + 1];
72
+ if (!next || next.kind !== "consonant")
73
+ return undefined;
74
+ for (let j = from + 1; j < units.length; j++) {
75
+ const u = units[j];
76
+ if (u.kind === "vowel")
77
+ return u;
78
+ if (u.kind !== "consonant")
79
+ return undefined;
80
+ }
81
+ return undefined;
82
+ }
83
+ /** N'Ko → Latin Bambara, with warnings for uncertain letters. */
84
+ function toLatinDetailed(source, options = {}) {
85
+ const opts = { tones: "none", digits: true, ...options };
86
+ const units = tokenize(source);
87
+ const warnings = [];
88
+ let out = "";
89
+ units.forEach((u, idx) => {
90
+ switch (u.kind) {
91
+ case "vowel":
92
+ out += writeVowel(u.base, u.tone, u.nasal, opts);
93
+ break;
94
+ case "syllabicN":
95
+ out += "n" + accentFor(u.tone, opts) + (u.nasal ? "n" : "");
96
+ break;
97
+ case "consonant": {
98
+ out += chars_js_1.CONSONANTS[u.base];
99
+ if (chars_js_1.AMBIGUOUS_LETTERS.has(u.base) || u.dotted) {
100
+ warnings.push({
101
+ index: u.index,
102
+ char: u.base,
103
+ message: u.dotted
104
+ ? "Letter with double dot (borrowed sound): check the reading."
105
+ : "This letter can be read several ways depending on the dialect.",
106
+ });
107
+ }
108
+ const borrowed = borrowedVowel(units, idx);
109
+ if (borrowed && borrowed.kind === "vowel") {
110
+ out += writeVowel(borrowed.base, borrowed.tone, false, opts);
111
+ }
112
+ break;
113
+ }
114
+ case "noVowel":
115
+ break;
116
+ case "other":
117
+ out += u.text;
118
+ break;
119
+ }
120
+ });
121
+ if (opts.digits)
122
+ out = (0, text_js_1.toLatinDigits)(out);
123
+ return { text: out.normalize("NFC"), warnings };
124
+ }
125
+ /** N'Ko → Latin Bambara. */
126
+ function toLatin(source, options = {}) {
127
+ return toLatinDetailed(source, options).text;
128
+ }
@@ -0,0 +1,48 @@
1
+ import type { Warning } from "./toLatin.js";
2
+ export interface ToNkoOptions {
3
+ /** Convert 0-9 to N'Ko digits. Default true. */
4
+ digits?: boolean;
5
+ /**
6
+ * Drop a repeated vowel the way N'Ko writing does: when two adjacent
7
+ * consonants are followed by the same vowel, the vowel after the first
8
+ * consonant is not written ("bolo" → ߓߟߏ). Default false.
9
+ *
10
+ * Source: Richard Ishida, N'Ko orthography notes, "Vowel absence",
11
+ * https://r12a.github.io/scripts/nkoo/nqo.html. The rule does not apply
12
+ * when the two consonants are the same ("fafa" → ߝߊߝߊ). The source says
13
+ * nothing about tones, nasal vowels, long vowels or chains, so in those
14
+ * cases the vowel is kept: nasal or long vowels, different tones, a tone
15
+ * given on only one of the two vowels, and a vowel whose following
16
+ * syllable already lost its own. When neither vowel has a tone, the
17
+ * vowel is dropped with a warning, because the two tones could differ.
18
+ */
19
+ elideVowels?: boolean;
20
+ /**
21
+ * Read the older Latin spellings è for ɛ and ò for ɔ. A grave accent on
22
+ * e or o then marks the vowel, not a low tone. Default false.
23
+ *
24
+ * Source: An ka taa, "Bambara's Latin Alphabet", notes 8 and 9,
25
+ * https://kalan.ankataa.com/bambara-beg/grammar-i/bambaras-latin-alphabet.
26
+ */
27
+ legacyLatin?: boolean;
28
+ }
29
+ export interface ToNkoResult {
30
+ text: string;
31
+ warnings: Warning[];
32
+ /**
33
+ * Number of vowels written without a tone mark because the Latin
34
+ * text did not give the tone. A fluent writer has to add them.
35
+ */
36
+ missingTones: number;
37
+ }
38
+ /**
39
+ * Latin Bambara → N'Ko, assisted mode.
40
+ *
41
+ * Everyday Latin Bambara does not write tones, but N'Ko must. Tones
42
+ * given as accents (á à ǎ â) are converted; every other vowel is left
43
+ * unmarked and counted in `missingTones` so a person can finish the job.
44
+ * All vowels are written in full unless `elideVowels` is set.
45
+ */
46
+ export declare function toNkoDetailed(source: string, options?: ToNkoOptions): ToNkoResult;
47
+ /** Latin Bambara → N'Ko (assisted mode, see toNkoDetailed). */
48
+ export declare function toNko(source: string, options?: ToNkoOptions): string;
@@ -0,0 +1,246 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toNkoDetailed = toNkoDetailed;
4
+ exports.toNko = toNko;
5
+ const chars_js_1 = require("./chars.js");
6
+ const text_js_1 = require("./text.js");
7
+ const LATIN_VOWELS = {
8
+ a: "ߊ",
9
+ e: "ߋ",
10
+ i: "ߌ",
11
+ "ɛ": "ߍ",
12
+ u: "ߎ",
13
+ o: "ߏ",
14
+ "ɔ": "ߐ",
15
+ };
16
+ const LATIN_CONSONANTS = {
17
+ b: "ߓ",
18
+ p: "ߔ",
19
+ t: "ߕ",
20
+ j: "ߖ",
21
+ c: "ߗ",
22
+ d: "ߘ",
23
+ r: "ߙ",
24
+ s: "ߛ",
25
+ g: "ߜ",
26
+ f: "ߝ",
27
+ k: "ߞ",
28
+ l: "ߟ",
29
+ m: "ߡ",
30
+ "ɲ": "ߢ",
31
+ n: "ߣ",
32
+ h: "ߤ",
33
+ w: "ߥ",
34
+ y: "ߦ",
35
+ };
36
+ const PUNCT = {
37
+ ",": "߸",
38
+ "!": "߹",
39
+ "?": "؟",
40
+ ";": "؛",
41
+ };
42
+ const GRAVE = "̀";
43
+ const ACCENT_TONES = {
44
+ "́": "high",
45
+ [GRAVE]: "low",
46
+ "̌": "rising",
47
+ "̂": "falling",
48
+ };
49
+ const SHORT_MARKS = {
50
+ high: "߫",
51
+ low: "߬",
52
+ rising: "߭",
53
+ falling: "", // short falling is the unmarked form
54
+ };
55
+ const LONG_MARKS = {
56
+ high: "߯",
57
+ low: "߰",
58
+ rising: "߱",
59
+ falling: "߮",
60
+ };
61
+ const isVowel = (c) => c !== undefined && c in LATIN_VOWELS;
62
+ const isLetter = (c) => c !== undefined && /\p{L}/u.test(c);
63
+ /**
64
+ * Applies the repeated-vowel rule (see ToNkoOptions.elideVowels). Works
65
+ * from the end so that a vowel is only dropped before a vowel that stays.
66
+ */
67
+ function dropRepeatedVowels(units, warnings) {
68
+ for (let k = units.length - 4; k >= 0; k--) {
69
+ const c1 = units[k];
70
+ const v1 = units[k + 1];
71
+ const c2 = units[k + 2];
72
+ const v2 = units[k + 3];
73
+ if (c1?.kind !== "consonant" || v1?.kind !== "vowel")
74
+ continue;
75
+ if (c2?.kind !== "consonant" || v2?.kind !== "vowel")
76
+ continue;
77
+ if (c1.nko === c2.nko || v1.latin !== v2.latin || v2.dropped)
78
+ continue;
79
+ if (v1.long || v2.long || v1.nasal || v2.nasal || v1.tone !== v2.tone)
80
+ continue;
81
+ v1.dropped = true;
82
+ if (v1.tone === undefined) {
83
+ warnings.push({
84
+ index: v1.index,
85
+ char: v1.latin,
86
+ message: "Vowel dropped assuming both syllables have the same tone: check.",
87
+ });
88
+ }
89
+ }
90
+ }
91
+ /**
92
+ * Latin Bambara → N'Ko, assisted mode.
93
+ *
94
+ * Everyday Latin Bambara does not write tones, but N'Ko must. Tones
95
+ * given as accents (á à ǎ â) are converted; every other vowel is left
96
+ * unmarked and counted in `missingTones` so a person can finish the job.
97
+ * All vowels are written in full unless `elideVowels` is set.
98
+ */
99
+ function toNkoDetailed(source, options = {}) {
100
+ const opts = { digits: true, elideVowels: false, legacyLatin: false, ...options };
101
+ // Work on decomposed, lower-case text so accents can be read as tone
102
+ // marks, but remember where each piece came from in the NFC text so
103
+ // warning indices match what toLatinDetailed reports.
104
+ const chars = [];
105
+ const origin = [];
106
+ Array.from(source.normalize("NFC")).forEach((ch, pos) => {
107
+ for (const piece of ch.normalize("NFD").toLowerCase()) {
108
+ chars.push(piece);
109
+ origin.push(pos);
110
+ }
111
+ });
112
+ const warnings = [];
113
+ const units = [];
114
+ const readTone = (i) => {
115
+ let tone;
116
+ while (i < chars.length && ACCENT_TONES[chars[i]] !== undefined) {
117
+ tone = ACCENT_TONES[chars[i]];
118
+ i++;
119
+ }
120
+ return [tone, i];
121
+ };
122
+ /** The Latin vowel letter at i and the index after it, if there is one. */
123
+ const readVowel = (i) => {
124
+ const c = chars[i];
125
+ if (c === undefined || !isVowel(c))
126
+ return undefined;
127
+ if (opts.legacyLatin && (c === "e" || c === "o") && chars[i + 1] === GRAVE) {
128
+ return [c === "e" ? "ɛ" : "ɔ", i + 2];
129
+ }
130
+ return [c, i + 1];
131
+ };
132
+ let i = 0;
133
+ while (i < chars.length) {
134
+ const c = chars[i];
135
+ const prev = chars[i - 1];
136
+ const vowel = readVowel(i);
137
+ if (vowel) {
138
+ const [latin, afterLetter] = vowel;
139
+ let [tone, j] = readTone(afterLetter);
140
+ let long = false;
141
+ const second = readVowel(j);
142
+ if (second && second[0] === latin) {
143
+ long = true;
144
+ const [tone2, k] = readTone(second[1]);
145
+ tone = tone ?? tone2;
146
+ j = k;
147
+ }
148
+ let nasal = false;
149
+ if (chars[j] === "n" && !isVowel(chars[j + 1]) && chars[j + 1] !== "y") {
150
+ nasal = true;
151
+ j++;
152
+ }
153
+ units.push({ kind: "vowel", latin, tone, long, nasal, index: origin[i], dropped: false });
154
+ i = j;
155
+ continue;
156
+ }
157
+ // Syllabic n: a word-initial n before a consonant, or n alone.
158
+ if (c === "n" && !isLetter(prev) && !isVowel(chars[i + 1]) && chars[i + 1] !== "y") {
159
+ const [tone, j] = readTone(i + 1);
160
+ units.push({ kind: "text", nko: chars_js_1.SYLLABIC_N + (tone ? SHORT_MARKS[tone] : "") });
161
+ i = j;
162
+ continue;
163
+ }
164
+ const two = c + (chars[i + 1] ?? "");
165
+ if (two === "ny" && isVowel(chars[i + 2])) {
166
+ units.push({ kind: "consonant", nko: "ߢ" });
167
+ i += 2;
168
+ continue;
169
+ }
170
+ if (two === "rr") {
171
+ units.push({ kind: "consonant", nko: "ߚ" });
172
+ i += 2;
173
+ continue;
174
+ }
175
+ if (two === "gb") {
176
+ units.push({ kind: "consonant", nko: "ߜ" });
177
+ i += 2;
178
+ continue;
179
+ }
180
+ if (two === "sh") {
181
+ warnings.push({ index: origin[i], char: "sh", message: "'sh' written as ߛ (s): check." });
182
+ units.push({ kind: "consonant", nko: "ߛ" });
183
+ i += 2;
184
+ continue;
185
+ }
186
+ if (c === "ŋ") {
187
+ warnings.push({ index: origin[i], char: c, message: "'ŋ' written as ߒ: check." });
188
+ units.push({ kind: "text", nko: chars_js_1.SYLLABIC_N });
189
+ i++;
190
+ continue;
191
+ }
192
+ if (c in LATIN_CONSONANTS) {
193
+ units.push({ kind: "consonant", nko: LATIN_CONSONANTS[c] });
194
+ i++;
195
+ continue;
196
+ }
197
+ if (c in PUNCT) {
198
+ units.push({ kind: "text", nko: PUNCT[c] });
199
+ i++;
200
+ continue;
201
+ }
202
+ if (isLetter(c)) {
203
+ warnings.push({
204
+ index: origin[i],
205
+ char: c,
206
+ message: `No N'Ko letter for '${c}' yet: left as is.`,
207
+ });
208
+ }
209
+ units.push({ kind: "text", nko: c });
210
+ i++;
211
+ }
212
+ if (opts.elideVowels)
213
+ dropRepeatedVowels(units, warnings);
214
+ let out = "";
215
+ let missingTones = 0;
216
+ for (const u of units) {
217
+ if (u.kind !== "vowel") {
218
+ out += u.nko;
219
+ continue;
220
+ }
221
+ if (u.dropped)
222
+ continue;
223
+ let mark = "";
224
+ if (u.tone)
225
+ mark = u.long ? LONG_MARKS[u.tone] : SHORT_MARKS[u.tone];
226
+ else {
227
+ missingTones++;
228
+ if (u.long) {
229
+ warnings.push({
230
+ index: u.index,
231
+ char: u.latin,
232
+ message: "Long vowel without a tone: the tone mark must be added by hand.",
233
+ });
234
+ }
235
+ }
236
+ out += LATIN_VOWELS[u.latin] + (u.nasal ? chars_js_1.NASALIZATION : "") + mark;
237
+ }
238
+ if (opts.digits)
239
+ out = (0, text_js_1.toNkoDigits)(out);
240
+ warnings.sort((a, b) => a.index - b.index);
241
+ return { text: out.normalize("NFC"), warnings, missingTones };
242
+ }
243
+ /** Latin Bambara → N'Ko (assisted mode, see toNkoDetailed). */
244
+ function toNko(source, options = {}) {
245
+ return toNkoDetailed(source, options).text;
246
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- export { containsNko, isNko, isolateRtl, normalizeNko, stripDiacritics, stripTones, toLatinDigits, toNkoDigits, } from "./text.js";
1
+ export { containsNko, isNko, isolateRtl, normalizeNko, stripDiacritics, stripTones, textDirection, toLatinDigits, toNkoDigits, type TextDirection, } from "./text.js";
2
+ export { searchKey } from "./search.js";
2
3
  export { toLatin, toLatinDetailed, type ToLatinOptions, type ToLatinResult, type Warning, } from "./toLatin.js";
3
4
  export { toNko, toNkoDetailed, type ToNkoOptions, type ToNkoResult, } from "./toNko.js";
4
5
  export type { Tone } from "./chars.js";
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
- export { containsNko, isNko, isolateRtl, normalizeNko, stripDiacritics, stripTones, toLatinDigits, toNkoDigits, } from "./text.js";
1
+ export { containsNko, isNko, isolateRtl, normalizeNko, stripDiacritics, stripTones, textDirection, toLatinDigits, toNkoDigits, } from "./text.js";
2
+ export { searchKey } from "./search.js";
2
3
  export { toLatin, toLatinDetailed, } from "./toLatin.js";
3
4
  export { toNko, toNkoDetailed, } from "./toNko.js";
@@ -0,0 +1,13 @@
1
+ /**
2
+ * A plain key for searching Bambara text whatever its script or spelling.
3
+ *
4
+ * N'Ko is first read as Latin (with `toLatin`). Then tones, accents and
5
+ * case are dropped, ɛ → e, ɔ → o, ɲ → ny, ŋ → ng, apostrophes vanish and
6
+ * every other run of characters that are not letters or digits becomes
7
+ * one space. Compare keys, never raw text:
8
+ *
9
+ * searchKey("ߓߡߊߣߊ߲ߞߊ߲") === "bamanankan"
10
+ * searchKey("Bámànànkàn") === "bamanankan"
11
+ * searchKey("ɲɔgɔn") === "nyogon"
12
+ */
13
+ export declare function searchKey(text: string): string;
package/dist/search.js ADDED
@@ -0,0 +1,30 @@
1
+ import { toLatin } from "./toLatin.js";
2
+ /** Letters folded to what a keyboard without them would type. */
3
+ const FOLD = {
4
+ "ɛ": "e",
5
+ "ɔ": "o",
6
+ "ɲ": "ny",
7
+ "ŋ": "ng",
8
+ };
9
+ /**
10
+ * A plain key for searching Bambara text whatever its script or spelling.
11
+ *
12
+ * N'Ko is first read as Latin (with `toLatin`). Then tones, accents and
13
+ * case are dropped, ɛ → e, ɔ → o, ɲ → ny, ŋ → ng, apostrophes vanish and
14
+ * every other run of characters that are not letters or digits becomes
15
+ * one space. Compare keys, never raw text:
16
+ *
17
+ * searchKey("ߓߡߊߣߊ߲ߞߊ߲") === "bamanankan"
18
+ * searchKey("Bámànànkàn") === "bamanankan"
19
+ * searchKey("ɲɔgɔn") === "nyogon"
20
+ */
21
+ export function searchKey(text) {
22
+ return toLatin(text)
23
+ .normalize("NFD")
24
+ .replace(/\p{M}/gu, "")
25
+ .toLowerCase()
26
+ .replace(/[ɛɔɲŋ]/gu, (ch) => FOLD[ch])
27
+ .replace(/['`´‘’ʼ]/gu, "")
28
+ .replace(/[^\p{L}\p{N}]+/gu, " ")
29
+ .trim();
30
+ }
package/dist/text.d.ts CHANGED
@@ -24,3 +24,13 @@ export declare function toNkoDigits(text: string): string;
24
24
  * W3C recommends.
25
25
  */
26
26
  export declare function isolateRtl(text: string): string;
27
+ export type TextDirection = "rtl" | "ltr" | "neutral";
28
+ /**
29
+ * Direction of a text from its first strong character, like the HTML
30
+ * attribute dir="auto" and rule P2 of the Unicode bidi algorithm: N'Ko,
31
+ * Arabic, Hebrew, Adlam… give "rtl", Latin and other letters give "ltr".
32
+ * N'Ko and Adlam digits and the marks LRM, RLM and ALM also decide; other
33
+ * digits, punctuation, emoji and text inside directional isolates are
34
+ * skipped. A text with no strong character gives "neutral".
35
+ */
36
+ export declare function textDirection(text: string): TextDirection;
package/dist/text.js CHANGED
@@ -65,3 +65,57 @@ export function toNkoDigits(text) {
65
65
  export function isolateRtl(text) {
66
66
  return `\u2067${text}\u2069`;
67
67
  }
68
+ /**
69
+ * Unicode blocks of right-to-left scripts. Every letter inside them is
70
+ * right-to-left and every letter outside is not (checked against the
71
+ * Unicode 17 character database).
72
+ */
73
+ const RTL_BLOCKS = [
74
+ [0x0590, 0x08ff], // Hebrew, Arabic, Syriac, Thaana, N'Ko, Samaritan, Mandaic
75
+ [0xfb1d, 0xfdff], // Hebrew and Arabic presentation forms
76
+ [0xfe70, 0xfeff], // Arabic presentation forms B
77
+ [0x10800, 0x10fff], // Garay, Hanifi Rohingya, Yezidi and older scripts
78
+ [0x1e800, 0x1efff], // Adlam, Mende Kikakui, Arabic mathematical letters
79
+ ];
80
+ const LETTER = /\p{L}/u;
81
+ /** Modifier letters that are neutral for the bidi algorithm (Unicode 17). */
82
+ const NEUTRAL_LETTER = /[\u02B9\u02BA\u02C6-\u02CF\u02EC\u0374\u2E2F\uA67F\uA717-\uA71F\uA788]/u;
83
+ function isRtlBlock(cp) {
84
+ return RTL_BLOCKS.some(([start, end]) => cp >= start && cp <= end);
85
+ }
86
+ /**
87
+ * Direction of a text from its first strong character, like the HTML
88
+ * attribute dir="auto" and rule P2 of the Unicode bidi algorithm: N'Ko,
89
+ * Arabic, Hebrew, Adlam… give "rtl", Latin and other letters give "ltr".
90
+ * N'Ko and Adlam digits and the marks LRM, RLM and ALM also decide; other
91
+ * digits, punctuation, emoji and text inside directional isolates are
92
+ * skipped. A text with no strong character gives "neutral".
93
+ */
94
+ export function textDirection(text) {
95
+ let isolates = 0;
96
+ for (const ch of text) {
97
+ const cp = ch.codePointAt(0);
98
+ if (cp >= 0x2066 && cp <= 0x2068) {
99
+ isolates++; // LRI, RLI, FSI
100
+ continue;
101
+ }
102
+ if (cp === 0x2069) {
103
+ if (isolates > 0)
104
+ isolates--; // PDI
105
+ continue;
106
+ }
107
+ if (isolates > 0)
108
+ continue;
109
+ if (cp === 0x200e)
110
+ return "ltr"; // LEFT-TO-RIGHT MARK
111
+ if (cp === 0x200f || cp === 0x061c)
112
+ return "rtl"; // RIGHT-TO-LEFT MARK, ARABIC LETTER MARK
113
+ if ((cp >= 0x07c0 && cp <= 0x07c9) || (cp >= 0x1e950 && cp <= 0x1e959)) {
114
+ return "rtl"; // N'Ko and Adlam digits are strong right-to-left
115
+ }
116
+ if (!LETTER.test(ch) || NEUTRAL_LETTER.test(ch))
117
+ continue;
118
+ return isRtlBlock(cp) ? "rtl" : "ltr";
119
+ }
120
+ return "neutral";
121
+ }
package/dist/toLatin.d.ts CHANGED
@@ -13,7 +13,10 @@ export interface ToLatinOptions {
13
13
  digits?: boolean;
14
14
  }
15
15
  export interface Warning {
16
- /** Index of the unit in the source text (in code points). */
16
+ /**
17
+ * Index of the character the warning is about, counted in code points
18
+ * of the source text after NFC normalization.
19
+ */
17
20
  index: number;
18
21
  char: string;
19
22
  message: string;
package/dist/toLatin.js CHANGED
@@ -5,6 +5,7 @@ function tokenize(source) {
5
5
  const units = [];
6
6
  for (let i = 0; i < chars.length; i++) {
7
7
  const ch = chars[i];
8
+ const start = i;
8
9
  if (ch in VOWELS || ch === SYLLABIC_N) {
9
10
  let tone;
10
11
  let nasal = false;
@@ -22,8 +23,8 @@ function tokenize(source) {
22
23
  i++;
23
24
  }
24
25
  units.push(ch === SYLLABIC_N
25
- ? { kind: "syllabicN", tone, nasal, index: i }
26
- : { kind: "vowel", base: VOWELS[ch], tone, nasal, index: i });
26
+ ? { kind: "syllabicN", tone, nasal, index: start }
27
+ : { kind: "vowel", base: VOWELS[ch], tone, nasal, index: start });
27
28
  }
28
29
  else if (ch in CONSONANTS) {
29
30
  let dotted = false;
@@ -31,7 +32,7 @@ function tokenize(source) {
31
32
  dotted = true;
32
33
  i++;
33
34
  }
34
- units.push({ kind: "consonant", base: ch, dotted, index: i });
35
+ units.push({ kind: "consonant", base: ch, dotted, index: start });
35
36
  }
36
37
  else if (ch === DAGBASINNA) {
37
38
  units.push({ kind: "noVowel", index: i });
package/dist/toNko.d.ts CHANGED
@@ -2,6 +2,29 @@ import type { Warning } from "./toLatin.js";
2
2
  export interface ToNkoOptions {
3
3
  /** Convert 0-9 to N'Ko digits. Default true. */
4
4
  digits?: boolean;
5
+ /**
6
+ * Drop a repeated vowel the way N'Ko writing does: when two adjacent
7
+ * consonants are followed by the same vowel, the vowel after the first
8
+ * consonant is not written ("bolo" → ߓߟߏ). Default false.
9
+ *
10
+ * Source: Richard Ishida, N'Ko orthography notes, "Vowel absence",
11
+ * https://r12a.github.io/scripts/nkoo/nqo.html. The rule does not apply
12
+ * when the two consonants are the same ("fafa" → ߝߊߝߊ). The source says
13
+ * nothing about tones, nasal vowels, long vowels or chains, so in those
14
+ * cases the vowel is kept: nasal or long vowels, different tones, a tone
15
+ * given on only one of the two vowels, and a vowel whose following
16
+ * syllable already lost its own. When neither vowel has a tone, the
17
+ * vowel is dropped with a warning, because the two tones could differ.
18
+ */
19
+ elideVowels?: boolean;
20
+ /**
21
+ * Read the older Latin spellings è for ɛ and ò for ɔ. A grave accent on
22
+ * e or o then marks the vowel, not a low tone. Default false.
23
+ *
24
+ * Source: An ka taa, "Bambara's Latin Alphabet", notes 8 and 9,
25
+ * https://kalan.ankataa.com/bambara-beg/grammar-i/bambaras-latin-alphabet.
26
+ */
27
+ legacyLatin?: boolean;
5
28
  }
6
29
  export interface ToNkoResult {
7
30
  text: string;
@@ -18,8 +41,7 @@ export interface ToNkoResult {
18
41
  * Everyday Latin Bambara does not write tones, but N'Ko must. Tones
19
42
  * given as accents (á à ǎ â) are converted; every other vowel is left
20
43
  * unmarked and counted in `missingTones` so a person can finish the job.
21
- * All vowels are written in full (the N'Ko rule that drops a repeated
22
- * vowel is not applied yet).
44
+ * All vowels are written in full unless `elideVowels` is set.
23
45
  */
24
46
  export declare function toNkoDetailed(source: string, options?: ToNkoOptions): ToNkoResult;
25
47
  /** Latin Bambara → N'Ko (assisted mode, see toNkoDetailed). */