nko-kit 0.0.0-stage → 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,132 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.containsNko = containsNko;
4
+ exports.isNko = isNko;
5
+ exports.normalizeNko = normalizeNko;
6
+ exports.stripTones = stripTones;
7
+ exports.stripDiacritics = stripDiacritics;
8
+ exports.toLatinDigits = toLatinDigits;
9
+ exports.toNkoDigits = toNkoDigits;
10
+ exports.isolateRtl = isolateRtl;
11
+ exports.textDirection = textDirection;
12
+ const chars_js_1 = require("./chars.js");
13
+ const NKO_DIGIT_ZERO = 0x07c0;
14
+ function isNkoCodePoint(cp) {
15
+ return cp >= chars_js_1.NKO_RANGE_START && cp <= chars_js_1.NKO_RANGE_END;
16
+ }
17
+ /** True if the text contains at least one N'Ko character. */
18
+ function containsNko(text) {
19
+ for (const ch of text) {
20
+ if (isNkoCodePoint(ch.codePointAt(0)))
21
+ return true;
22
+ }
23
+ return false;
24
+ }
25
+ /**
26
+ * True if every letter of the text is N'Ko. Spaces, digits and
27
+ * punctuation are ignored.
28
+ */
29
+ function isNko(text) {
30
+ let letters = 0;
31
+ for (const ch of text) {
32
+ const cp = ch.codePointAt(0);
33
+ if (isNkoCodePoint(cp)) {
34
+ letters++;
35
+ continue;
36
+ }
37
+ if (/\p{L}/u.test(ch))
38
+ return false;
39
+ }
40
+ return letters > 0;
41
+ }
42
+ /**
43
+ * Puts combining marks in canonical order (nasalization before tone)
44
+ * so that two spellings of the same word compare equal.
45
+ */
46
+ function normalizeNko(text) {
47
+ return text.normalize("NFC");
48
+ }
49
+ /** Removes tone marks, keeps nasalization. Useful for search. */
50
+ function stripTones(text) {
51
+ let out = "";
52
+ for (const ch of text) {
53
+ if (ch in chars_js_1.TONE_MARKS)
54
+ continue;
55
+ out += ch;
56
+ }
57
+ return out;
58
+ }
59
+ /** Removes tone marks and nasalization. */
60
+ function stripDiacritics(text) {
61
+ return stripTones(text).split(chars_js_1.NASALIZATION).join("");
62
+ }
63
+ /** ߁߉߄߉ → 1949 */
64
+ function toLatinDigits(text) {
65
+ return text.replace(/[\u07C0-\u07C9]/g, (d) => String(d.charCodeAt(0) - NKO_DIGIT_ZERO));
66
+ }
67
+ /** 1949 → ߁߉߄߉ */
68
+ function toNkoDigits(text) {
69
+ return text.replace(/[0-9]/g, (d) => String.fromCharCode(NKO_DIGIT_ZERO + Number(d)));
70
+ }
71
+ /**
72
+ * Wraps N'Ko text in Unicode directional isolates (RLI … PDI) so it
73
+ * displays right-to-left correctly inside left-to-right text, as the
74
+ * W3C recommends.
75
+ */
76
+ function isolateRtl(text) {
77
+ return `\u2067${text}\u2069`;
78
+ }
79
+ /**
80
+ * Unicode blocks of right-to-left scripts. Every letter inside them is
81
+ * right-to-left and every letter outside is not (checked against the
82
+ * Unicode 17 character database).
83
+ */
84
+ const RTL_BLOCKS = [
85
+ [0x0590, 0x08ff], // Hebrew, Arabic, Syriac, Thaana, N'Ko, Samaritan, Mandaic
86
+ [0xfb1d, 0xfdff], // Hebrew and Arabic presentation forms
87
+ [0xfe70, 0xfeff], // Arabic presentation forms B
88
+ [0x10800, 0x10fff], // Garay, Hanifi Rohingya, Yezidi and older scripts
89
+ [0x1e800, 0x1efff], // Adlam, Mende Kikakui, Arabic mathematical letters
90
+ ];
91
+ const LETTER = /\p{L}/u;
92
+ /** Modifier letters that are neutral for the bidi algorithm (Unicode 17). */
93
+ const NEUTRAL_LETTER = /[\u02B9\u02BA\u02C6-\u02CF\u02EC\u0374\u2E2F\uA67F\uA717-\uA71F\uA788]/u;
94
+ function isRtlBlock(cp) {
95
+ return RTL_BLOCKS.some(([start, end]) => cp >= start && cp <= end);
96
+ }
97
+ /**
98
+ * Direction of a text from its first strong character, like the HTML
99
+ * attribute dir="auto" and rule P2 of the Unicode bidi algorithm: N'Ko,
100
+ * Arabic, Hebrew, Adlam… give "rtl", Latin and other letters give "ltr".
101
+ * N'Ko and Adlam digits and the marks LRM, RLM and ALM also decide; other
102
+ * digits, punctuation, emoji and text inside directional isolates are
103
+ * skipped. A text with no strong character gives "neutral".
104
+ */
105
+ function textDirection(text) {
106
+ let isolates = 0;
107
+ for (const ch of text) {
108
+ const cp = ch.codePointAt(0);
109
+ if (cp >= 0x2066 && cp <= 0x2068) {
110
+ isolates++; // LRI, RLI, FSI
111
+ continue;
112
+ }
113
+ if (cp === 0x2069) {
114
+ if (isolates > 0)
115
+ isolates--; // PDI
116
+ continue;
117
+ }
118
+ if (isolates > 0)
119
+ continue;
120
+ if (cp === 0x200e)
121
+ return "ltr"; // LEFT-TO-RIGHT MARK
122
+ if (cp === 0x200f || cp === 0x061c)
123
+ return "rtl"; // RIGHT-TO-LEFT MARK, ARABIC LETTER MARK
124
+ if ((cp >= 0x07c0 && cp <= 0x07c9) || (cp >= 0x1e950 && cp <= 0x1e959)) {
125
+ return "rtl"; // N'Ko and Adlam digits are strong right-to-left
126
+ }
127
+ if (!LETTER.test(ch) || NEUTRAL_LETTER.test(ch))
128
+ continue;
129
+ return isRtlBlock(cp) ? "rtl" : "ltr";
130
+ }
131
+ return "neutral";
132
+ }
@@ -0,0 +1,31 @@
1
+ export interface ToLatinOptions {
2
+ /**
3
+ * "none" (default): no tones, like everyday Latin Bambara.
4
+ * "accents": tones as accents (á high, à low, ǎ rising).
5
+ */
6
+ tones?: "none" | "accents";
7
+ /**
8
+ * With tones: "accents", also write the default short falling tone
9
+ * (unmarked in N'Ko) as â. Off by default.
10
+ */
11
+ markFalling?: boolean;
12
+ /** Convert N'Ko digits to 0-9. Default true. */
13
+ digits?: boolean;
14
+ }
15
+ export interface Warning {
16
+ /**
17
+ * Index of the character the warning is about, counted in code points
18
+ * of the source text after NFC normalization.
19
+ */
20
+ index: number;
21
+ char: string;
22
+ message: string;
23
+ }
24
+ export interface ToLatinResult {
25
+ text: string;
26
+ warnings: Warning[];
27
+ }
28
+ /** N'Ko → Latin Bambara, with warnings for uncertain letters. */
29
+ export declare function toLatinDetailed(source: string, options?: ToLatinOptions): ToLatinResult;
30
+ /** N'Ko → Latin Bambara. */
31
+ export declare function toLatin(source: string, options?: ToLatinOptions): string;
@@ -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
+ }
@@ -0,0 +1,5 @@
1
+ export { containsNko, isNko, isolateRtl, normalizeNko, stripDiacritics, stripTones, textDirection, toLatinDigits, toNkoDigits, type TextDirection, } from "./text.js";
2
+ export { searchKey } from "./search.js";
3
+ export { toLatin, toLatinDetailed, type ToLatinOptions, type ToLatinResult, type Warning, } from "./toLatin.js";
4
+ export { toNko, toNkoDetailed, type ToNkoOptions, type ToNkoResult, } from "./toNko.js";
5
+ export type { Tone } from "./chars.js";
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { containsNko, isNko, isolateRtl, normalizeNko, stripDiacritics, stripTones, textDirection, toLatinDigits, toNkoDigits, } from "./text.js";
2
+ export { searchKey } from "./search.js";
3
+ export { toLatin, toLatinDetailed, } from "./toLatin.js";
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 ADDED
@@ -0,0 +1,36 @@
1
+ /** True if the text contains at least one N'Ko character. */
2
+ export declare function containsNko(text: string): boolean;
3
+ /**
4
+ * True if every letter of the text is N'Ko. Spaces, digits and
5
+ * punctuation are ignored.
6
+ */
7
+ export declare function isNko(text: string): boolean;
8
+ /**
9
+ * Puts combining marks in canonical order (nasalization before tone)
10
+ * so that two spellings of the same word compare equal.
11
+ */
12
+ export declare function normalizeNko(text: string): string;
13
+ /** Removes tone marks, keeps nasalization. Useful for search. */
14
+ export declare function stripTones(text: string): string;
15
+ /** Removes tone marks and nasalization. */
16
+ export declare function stripDiacritics(text: string): string;
17
+ /** ߁߉߄߉ → 1949 */
18
+ export declare function toLatinDigits(text: string): string;
19
+ /** 1949 → ߁߉߄߉ */
20
+ export declare function toNkoDigits(text: string): string;
21
+ /**
22
+ * Wraps N'Ko text in Unicode directional isolates (RLI … PDI) so it
23
+ * displays right-to-left correctly inside left-to-right text, as the
24
+ * W3C recommends.
25
+ */
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;