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/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: "\u07CA",
5
- e: "\u07CB",
6
- i: "\u07CC",
7
- "ɛ": "\u07CD",
8
- u: "\u07CE",
9
- o: "\u07CF",
10
- "ɔ": "\u07D0",
4
+ a: "ߊ",
5
+ e: "ߋ",
6
+ i: "ߌ",
7
+ "ɛ": "ߍ",
8
+ u: "ߎ",
9
+ o: "ߏ",
10
+ "ɔ": "ߐ",
11
11
  };
12
12
  const LATIN_CONSONANTS = {
13
- b: "\u07D3",
14
- p: "\u07D4",
15
- t: "\u07D5",
16
- j: "\u07D6",
17
- c: "\u07D7",
18
- d: "\u07D8",
19
- r: "\u07D9",
20
- s: "\u07DB",
21
- g: "\u07DC",
22
- f: "\u07DD",
23
- k: "\u07DE",
24
- l: "\u07DF",
25
- m: "\u07E1",
26
- "ɲ": "\u07E2",
27
- n: "\u07E3",
28
- h: "\u07E4",
29
- w: "\u07E5",
30
- y: "\u07E6",
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
- ",": "\u07F8",
34
- "!": "\u07F9",
35
- "?": "\u061F",
36
- ";": "\u061B",
33
+ ",": "߸",
34
+ "!": "߹",
35
+ "?": "؟",
36
+ ";": "؛",
37
37
  };
38
+ const GRAVE = "̀";
38
39
  const ACCENT_TONES = {
39
- "\u0301": "high",
40
- "\u0300": "low",
41
- "\u030C": "rising",
42
- "\u0302": "falling",
40
+ "́": "high",
41
+ [GRAVE]: "low",
42
+ "̌": "rising",
43
+ "̂": "falling",
43
44
  };
44
45
  const SHORT_MARKS = {
45
- high: "\u07EB",
46
- low: "\u07EC",
47
- rising: "\u07ED",
46
+ high: "߫",
47
+ low: "߬",
48
+ rising: "߭",
48
49
  falling: "", // short falling is the unmarked form
49
50
  };
50
51
  const LONG_MARKS = {
51
- high: "\u07EF",
52
- low: "\u07F0",
53
- rising: "\u07F1",
54
- falling: "\u07EE",
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 (the N'Ko rule that drops a repeated
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
- const chars = Array.from(source.normalize("NFD").toLowerCase());
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
- let missingTones = 0;
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
- if (isVowel(c)) {
86
- const start = i;
87
- let [tone, j] = readTone(i + 1);
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
- if (chars[j] === c) {
137
+ const second = readVowel(j);
138
+ if (second && second[0] === latin) {
90
139
  long = true;
91
- const [tone2, k] = readTone(j + 1);
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
- let mark = "";
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
- out += SYLLABIC_N + (tone ? SHORT_MARKS[tone] : "");
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
- out += "\u07E2";
162
+ units.push({ kind: "consonant", nko: "ߢ" });
127
163
  i += 2;
128
164
  continue;
129
165
  }
130
166
  if (two === "rr") {
131
- out += "\u07DA";
167
+ units.push({ kind: "consonant", nko: "ߚ" });
132
168
  i += 2;
133
169
  continue;
134
170
  }
135
171
  if (two === "gb") {
136
- out += "\u07DC";
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 \u07DB (s): check." });
142
- out += "\u07DB";
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 \u07D2: check." });
148
- out += SYLLABIC_N;
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
- out += LATIN_CONSONANTS[c];
189
+ units.push({ kind: "consonant", nko: LATIN_CONSONANTS[c] });
154
190
  i++;
155
191
  continue;
156
192
  }
157
193
  if (c in PUNCT) {
158
- out += PUNCT[c];
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
- out += c;
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.1.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
- "devDependencies": {
28
- "typescript": "^5.9.3",
29
- "vitest": "^2.1.9"
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/MomoOuologuem/nko-kit.git"
30
21
  },
31
22
  "type": "module",
32
- "types": "dist/index.d.ts",
23
+ "main": "./dist/cjs/index.js",
24
+ "module": "./dist/index.js",
25
+ "types": "./dist/index.d.ts",
33
26
  "exports": {
34
27
  ".": {
35
- "types": "./dist/index.d.ts",
36
- "import": "./dist/index.js"
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
- "repository": {
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
  }