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.
- package/README.md +34 -7
- package/dist/cjs/chars.d.ts +38 -0
- package/dist/cjs/chars.js +100 -0
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.js +21 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/search.d.ts +13 -0
- package/dist/cjs/search.js +33 -0
- package/dist/cjs/text.d.ts +36 -0
- package/dist/cjs/text.js +132 -0
- package/dist/cjs/toLatin.d.ts +31 -0
- package/dist/cjs/toLatin.js +128 -0
- package/dist/cjs/toNko.d.ts +48 -0
- package/dist/cjs/toNko.js +246 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/search.d.ts +13 -0
- package/dist/search.js +30 -0
- package/dist/text.d.ts +10 -0
- package/dist/text.js +54 -0
- package/dist/toLatin.d.ts +4 -1
- package/dist/toLatin.js +4 -3
- package/dist/toNko.d.ts +24 -2
- package/dist/toNko.js +140 -77
- package/package.json +32 -21
package/dist/toNko.js
CHANGED
|
@@ -1,75 +1,112 @@
|
|
|
1
1
|
import { NASALIZATION, SYLLABIC_N } from "./chars.js";
|
|
2
2
|
import { toNkoDigits } from "./text.js";
|
|
3
3
|
const LATIN_VOWELS = {
|
|
4
|
-
a: "
|
|
5
|
-
e: "
|
|
6
|
-
i: "
|
|
7
|
-
"ɛ": "
|
|
8
|
-
u: "
|
|
9
|
-
o: "
|
|
10
|
-
"ɔ": "
|
|
4
|
+
a: "ߊ",
|
|
5
|
+
e: "ߋ",
|
|
6
|
+
i: "ߌ",
|
|
7
|
+
"ɛ": "ߍ",
|
|
8
|
+
u: "ߎ",
|
|
9
|
+
o: "ߏ",
|
|
10
|
+
"ɔ": "ߐ",
|
|
11
11
|
};
|
|
12
12
|
const LATIN_CONSONANTS = {
|
|
13
|
-
b: "
|
|
14
|
-
p: "
|
|
15
|
-
t: "
|
|
16
|
-
j: "
|
|
17
|
-
c: "
|
|
18
|
-
d: "
|
|
19
|
-
r: "
|
|
20
|
-
s: "
|
|
21
|
-
g: "
|
|
22
|
-
f: "
|
|
23
|
-
k: "
|
|
24
|
-
l: "
|
|
25
|
-
m: "
|
|
26
|
-
"ɲ": "
|
|
27
|
-
n: "
|
|
28
|
-
h: "
|
|
29
|
-
w: "
|
|
30
|
-
y: "
|
|
13
|
+
b: "ߓ",
|
|
14
|
+
p: "ߔ",
|
|
15
|
+
t: "ߕ",
|
|
16
|
+
j: "ߖ",
|
|
17
|
+
c: "ߗ",
|
|
18
|
+
d: "ߘ",
|
|
19
|
+
r: "ߙ",
|
|
20
|
+
s: "ߛ",
|
|
21
|
+
g: "ߜ",
|
|
22
|
+
f: "ߝ",
|
|
23
|
+
k: "ߞ",
|
|
24
|
+
l: "ߟ",
|
|
25
|
+
m: "ߡ",
|
|
26
|
+
"ɲ": "ߢ",
|
|
27
|
+
n: "ߣ",
|
|
28
|
+
h: "ߤ",
|
|
29
|
+
w: "ߥ",
|
|
30
|
+
y: "ߦ",
|
|
31
31
|
};
|
|
32
32
|
const PUNCT = {
|
|
33
|
-
",": "
|
|
34
|
-
"!": "
|
|
35
|
-
"?": "
|
|
36
|
-
";": "
|
|
33
|
+
",": "߸",
|
|
34
|
+
"!": "߹",
|
|
35
|
+
"?": "؟",
|
|
36
|
+
";": "؛",
|
|
37
37
|
};
|
|
38
|
+
const GRAVE = "̀";
|
|
38
39
|
const ACCENT_TONES = {
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
"
|
|
42
|
-
"
|
|
40
|
+
"́": "high",
|
|
41
|
+
[GRAVE]: "low",
|
|
42
|
+
"̌": "rising",
|
|
43
|
+
"̂": "falling",
|
|
43
44
|
};
|
|
44
45
|
const SHORT_MARKS = {
|
|
45
|
-
high: "
|
|
46
|
-
low: "
|
|
47
|
-
rising: "
|
|
46
|
+
high: "߫",
|
|
47
|
+
low: "߬",
|
|
48
|
+
rising: "߭",
|
|
48
49
|
falling: "", // short falling is the unmarked form
|
|
49
50
|
};
|
|
50
51
|
const LONG_MARKS = {
|
|
51
|
-
high: "
|
|
52
|
-
low: "
|
|
53
|
-
rising: "
|
|
54
|
-
falling: "
|
|
52
|
+
high: "߯",
|
|
53
|
+
low: "߰",
|
|
54
|
+
rising: "߱",
|
|
55
|
+
falling: "߮",
|
|
55
56
|
};
|
|
56
57
|
const isVowel = (c) => c !== undefined && c in LATIN_VOWELS;
|
|
57
58
|
const isLetter = (c) => c !== undefined && /\p{L}/u.test(c);
|
|
59
|
+
/**
|
|
60
|
+
* Applies the repeated-vowel rule (see ToNkoOptions.elideVowels). Works
|
|
61
|
+
* from the end so that a vowel is only dropped before a vowel that stays.
|
|
62
|
+
*/
|
|
63
|
+
function dropRepeatedVowels(units, warnings) {
|
|
64
|
+
for (let k = units.length - 4; k >= 0; k--) {
|
|
65
|
+
const c1 = units[k];
|
|
66
|
+
const v1 = units[k + 1];
|
|
67
|
+
const c2 = units[k + 2];
|
|
68
|
+
const v2 = units[k + 3];
|
|
69
|
+
if (c1?.kind !== "consonant" || v1?.kind !== "vowel")
|
|
70
|
+
continue;
|
|
71
|
+
if (c2?.kind !== "consonant" || v2?.kind !== "vowel")
|
|
72
|
+
continue;
|
|
73
|
+
if (c1.nko === c2.nko || v1.latin !== v2.latin || v2.dropped)
|
|
74
|
+
continue;
|
|
75
|
+
if (v1.long || v2.long || v1.nasal || v2.nasal || v1.tone !== v2.tone)
|
|
76
|
+
continue;
|
|
77
|
+
v1.dropped = true;
|
|
78
|
+
if (v1.tone === undefined) {
|
|
79
|
+
warnings.push({
|
|
80
|
+
index: v1.index,
|
|
81
|
+
char: v1.latin,
|
|
82
|
+
message: "Vowel dropped assuming both syllables have the same tone: check.",
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
58
87
|
/**
|
|
59
88
|
* Latin Bambara → N'Ko, assisted mode.
|
|
60
89
|
*
|
|
61
90
|
* Everyday Latin Bambara does not write tones, but N'Ko must. Tones
|
|
62
91
|
* given as accents (á à ǎ â) are converted; every other vowel is left
|
|
63
92
|
* unmarked and counted in `missingTones` so a person can finish the job.
|
|
64
|
-
* All vowels are written in full
|
|
65
|
-
* vowel is not applied yet).
|
|
93
|
+
* All vowels are written in full unless `elideVowels` is set.
|
|
66
94
|
*/
|
|
67
95
|
export function toNkoDetailed(source, options = {}) {
|
|
68
|
-
const opts = { digits: true, ...options };
|
|
69
|
-
|
|
96
|
+
const opts = { digits: true, elideVowels: false, legacyLatin: false, ...options };
|
|
97
|
+
// Work on decomposed, lower-case text so accents can be read as tone
|
|
98
|
+
// marks, but remember where each piece came from in the NFC text so
|
|
99
|
+
// warning indices match what toLatinDetailed reports.
|
|
100
|
+
const chars = [];
|
|
101
|
+
const origin = [];
|
|
102
|
+
Array.from(source.normalize("NFC")).forEach((ch, pos) => {
|
|
103
|
+
for (const piece of ch.normalize("NFD").toLowerCase()) {
|
|
104
|
+
chars.push(piece);
|
|
105
|
+
origin.push(pos);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
70
108
|
const warnings = [];
|
|
71
|
-
|
|
72
|
-
let out = "";
|
|
109
|
+
const units = [];
|
|
73
110
|
const readTone = (i) => {
|
|
74
111
|
let tone;
|
|
75
112
|
while (i < chars.length && ACCENT_TONES[chars[i]] !== undefined) {
|
|
@@ -78,17 +115,29 @@ export function toNkoDetailed(source, options = {}) {
|
|
|
78
115
|
}
|
|
79
116
|
return [tone, i];
|
|
80
117
|
};
|
|
118
|
+
/** The Latin vowel letter at i and the index after it, if there is one. */
|
|
119
|
+
const readVowel = (i) => {
|
|
120
|
+
const c = chars[i];
|
|
121
|
+
if (c === undefined || !isVowel(c))
|
|
122
|
+
return undefined;
|
|
123
|
+
if (opts.legacyLatin && (c === "e" || c === "o") && chars[i + 1] === GRAVE) {
|
|
124
|
+
return [c === "e" ? "ɛ" : "ɔ", i + 2];
|
|
125
|
+
}
|
|
126
|
+
return [c, i + 1];
|
|
127
|
+
};
|
|
81
128
|
let i = 0;
|
|
82
129
|
while (i < chars.length) {
|
|
83
130
|
const c = chars[i];
|
|
84
131
|
const prev = chars[i - 1];
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
132
|
+
const vowel = readVowel(i);
|
|
133
|
+
if (vowel) {
|
|
134
|
+
const [latin, afterLetter] = vowel;
|
|
135
|
+
let [tone, j] = readTone(afterLetter);
|
|
88
136
|
let long = false;
|
|
89
|
-
|
|
137
|
+
const second = readVowel(j);
|
|
138
|
+
if (second && second[0] === latin) {
|
|
90
139
|
long = true;
|
|
91
|
-
const [tone2, k] = readTone(
|
|
140
|
+
const [tone2, k] = readTone(second[1]);
|
|
92
141
|
tone = tone ?? tone2;
|
|
93
142
|
j = k;
|
|
94
143
|
}
|
|
@@ -97,80 +146,94 @@ export function toNkoDetailed(source, options = {}) {
|
|
|
97
146
|
nasal = true;
|
|
98
147
|
j++;
|
|
99
148
|
}
|
|
100
|
-
|
|
101
|
-
if (tone)
|
|
102
|
-
mark = long ? LONG_MARKS[tone] : SHORT_MARKS[tone];
|
|
103
|
-
else {
|
|
104
|
-
missingTones++;
|
|
105
|
-
if (long) {
|
|
106
|
-
warnings.push({
|
|
107
|
-
index: start,
|
|
108
|
-
char: c,
|
|
109
|
-
message: "Long vowel without a tone: the tone mark must be added by hand.",
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
out += LATIN_VOWELS[c] + (nasal ? NASALIZATION : "") + mark;
|
|
149
|
+
units.push({ kind: "vowel", latin, tone, long, nasal, index: origin[i], dropped: false });
|
|
114
150
|
i = j;
|
|
115
151
|
continue;
|
|
116
152
|
}
|
|
117
153
|
// Syllabic n: a word-initial n before a consonant, or n alone.
|
|
118
154
|
if (c === "n" && !isLetter(prev) && !isVowel(chars[i + 1]) && chars[i + 1] !== "y") {
|
|
119
155
|
const [tone, j] = readTone(i + 1);
|
|
120
|
-
|
|
156
|
+
units.push({ kind: "text", nko: SYLLABIC_N + (tone ? SHORT_MARKS[tone] : "") });
|
|
121
157
|
i = j;
|
|
122
158
|
continue;
|
|
123
159
|
}
|
|
124
160
|
const two = c + (chars[i + 1] ?? "");
|
|
125
161
|
if (two === "ny" && isVowel(chars[i + 2])) {
|
|
126
|
-
|
|
162
|
+
units.push({ kind: "consonant", nko: "ߢ" });
|
|
127
163
|
i += 2;
|
|
128
164
|
continue;
|
|
129
165
|
}
|
|
130
166
|
if (two === "rr") {
|
|
131
|
-
|
|
167
|
+
units.push({ kind: "consonant", nko: "ߚ" });
|
|
132
168
|
i += 2;
|
|
133
169
|
continue;
|
|
134
170
|
}
|
|
135
171
|
if (two === "gb") {
|
|
136
|
-
|
|
172
|
+
units.push({ kind: "consonant", nko: "ߜ" });
|
|
137
173
|
i += 2;
|
|
138
174
|
continue;
|
|
139
175
|
}
|
|
140
176
|
if (two === "sh") {
|
|
141
|
-
warnings.push({ index: i, char: "sh", message: "'sh' written as
|
|
142
|
-
|
|
177
|
+
warnings.push({ index: origin[i], char: "sh", message: "'sh' written as ߛ (s): check." });
|
|
178
|
+
units.push({ kind: "consonant", nko: "ߛ" });
|
|
143
179
|
i += 2;
|
|
144
180
|
continue;
|
|
145
181
|
}
|
|
146
182
|
if (c === "ŋ") {
|
|
147
|
-
warnings.push({ index: i, char: c, message: "'ŋ' written as
|
|
148
|
-
|
|
183
|
+
warnings.push({ index: origin[i], char: c, message: "'ŋ' written as ߒ: check." });
|
|
184
|
+
units.push({ kind: "text", nko: SYLLABIC_N });
|
|
149
185
|
i++;
|
|
150
186
|
continue;
|
|
151
187
|
}
|
|
152
188
|
if (c in LATIN_CONSONANTS) {
|
|
153
|
-
|
|
189
|
+
units.push({ kind: "consonant", nko: LATIN_CONSONANTS[c] });
|
|
154
190
|
i++;
|
|
155
191
|
continue;
|
|
156
192
|
}
|
|
157
193
|
if (c in PUNCT) {
|
|
158
|
-
|
|
194
|
+
units.push({ kind: "text", nko: PUNCT[c] });
|
|
159
195
|
i++;
|
|
160
196
|
continue;
|
|
161
197
|
}
|
|
162
198
|
if (isLetter(c)) {
|
|
163
199
|
warnings.push({
|
|
164
|
-
index: i,
|
|
200
|
+
index: origin[i],
|
|
165
201
|
char: c,
|
|
166
202
|
message: `No N'Ko letter for '${c}' yet: left as is.`,
|
|
167
203
|
});
|
|
168
204
|
}
|
|
169
|
-
|
|
205
|
+
units.push({ kind: "text", nko: c });
|
|
170
206
|
i++;
|
|
171
207
|
}
|
|
208
|
+
if (opts.elideVowels)
|
|
209
|
+
dropRepeatedVowels(units, warnings);
|
|
210
|
+
let out = "";
|
|
211
|
+
let missingTones = 0;
|
|
212
|
+
for (const u of units) {
|
|
213
|
+
if (u.kind !== "vowel") {
|
|
214
|
+
out += u.nko;
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (u.dropped)
|
|
218
|
+
continue;
|
|
219
|
+
let mark = "";
|
|
220
|
+
if (u.tone)
|
|
221
|
+
mark = u.long ? LONG_MARKS[u.tone] : SHORT_MARKS[u.tone];
|
|
222
|
+
else {
|
|
223
|
+
missingTones++;
|
|
224
|
+
if (u.long) {
|
|
225
|
+
warnings.push({
|
|
226
|
+
index: u.index,
|
|
227
|
+
char: u.latin,
|
|
228
|
+
message: "Long vowel without a tone: the tone mark must be added by hand.",
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
out += LATIN_VOWELS[u.latin] + (u.nasal ? NASALIZATION : "") + mark;
|
|
233
|
+
}
|
|
172
234
|
if (opts.digits)
|
|
173
235
|
out = toNkoDigits(out);
|
|
236
|
+
warnings.sort((a, b) => a.index - b.index);
|
|
174
237
|
return { text: out.normalize("NFC"), warnings, missingTones };
|
|
175
238
|
}
|
|
176
239
|
/** Latin Bambara → N'Ko (assisted mode, see toNkoDetailed). */
|
package/package.json
CHANGED
|
@@ -1,16 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nko-kit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "N'Ko and Bambara toolkit: N'Ko ↔ Latin transliteration, tones, digits, RTL helpers. Zero dependencies.",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"directories": {
|
|
7
|
-
"test": "test"
|
|
8
|
-
},
|
|
9
|
-
"scripts": {
|
|
10
|
-
"build": "tsc",
|
|
11
|
-
"test": "vitest run",
|
|
12
|
-
"prepublishOnly": "npm run build && npm test"
|
|
13
|
-
},
|
|
14
5
|
"keywords": [
|
|
15
6
|
"nko",
|
|
16
7
|
"n'ko",
|
|
@@ -24,26 +15,46 @@
|
|
|
24
15
|
],
|
|
25
16
|
"author": "Hamed Domo Ouologuem",
|
|
26
17
|
"license": "MIT",
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/MomoOuologuem/nko-kit.git"
|
|
30
21
|
},
|
|
31
22
|
"type": "module",
|
|
32
|
-
"
|
|
23
|
+
"main": "./dist/cjs/index.js",
|
|
24
|
+
"module": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
33
26
|
"exports": {
|
|
34
27
|
".": {
|
|
35
|
-
"
|
|
36
|
-
|
|
37
|
-
|
|
28
|
+
"import": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./dist/cjs/index.d.ts",
|
|
34
|
+
"default": "./dist/cjs/index.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
38
|
},
|
|
39
39
|
"files": [
|
|
40
40
|
"dist"
|
|
41
41
|
],
|
|
42
|
-
"
|
|
43
|
-
"type": "git",
|
|
44
|
-
"url": "https://github.com/MomoOuologuem/nko-kit"
|
|
45
|
-
},
|
|
42
|
+
"sideEffects": false,
|
|
46
43
|
"engines": {
|
|
47
44
|
"node": ">=18"
|
|
45
|
+
},
|
|
46
|
+
"directories": {
|
|
47
|
+
"test": "test"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "node scripts/clean.mjs && tsc -p tsconfig.json && tsc -p tsconfig.cjs.json && node scripts/mark-cjs.mjs",
|
|
51
|
+
"test": "vitest run",
|
|
52
|
+
"prepublishOnly": "npm run build && npm test && npm run test:dist",
|
|
53
|
+
"clean": "node scripts/clean.mjs",
|
|
54
|
+
"test:dist": "node scripts/check-dist.mjs"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"typescript": "^5.9.3",
|
|
58
|
+
"vitest": "^2.1.9"
|
|
48
59
|
}
|
|
49
60
|
}
|