kirundi_text 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +44 -2
- package/kirundi.d.ts +49 -0
- package/kirundi.mjs +598 -0
- package/package.json +21 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Blixy4
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,45 @@
|
|
|
1
|
-
#
|
|
1
|
+
# kirundi_text
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Numbers, dates and text tools for **Kirundi** (Ikirundi), the language of Burundi.
|
|
4
|
+
Zero dependencies, ES module, TypeScript types included. Node 18+ and browsers.
|
|
5
|
+
The same library exists for [Python](https://pypi.org/project/kirundi-text/) and [Dart](https://pub.dev/packages/kirundi_text) with identical behaviour.
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install kirundi_text
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import {
|
|
15
|
+
toWords, fromWords, decimalToWords, decimalFromWords,
|
|
16
|
+
fractionToWords, fractionFromWords, ordinalToWords, ordinalFromWords,
|
|
17
|
+
moneyWords, formatDate, dateParts, parseMonth,
|
|
18
|
+
normalize, fold, tokens, expandNumbers,
|
|
19
|
+
} from "kirundi_text";
|
|
20
|
+
|
|
21
|
+
toWords(1500); // "igihumbi n'amajana atanu"
|
|
22
|
+
toWords(-6); // "haze gatandatu"
|
|
23
|
+
toWords(3, 2); // "batatu" (abantu batatu)
|
|
24
|
+
fromWords("mirongo ibiri n'icenda"); // 29
|
|
25
|
+
decimalToWords("45.4"); // "mirongo ine na gatanu n'ibice bine"
|
|
26
|
+
fractionToWords("-4/5"); // "haze bine vya gatanu"
|
|
27
|
+
ordinalToWords(3); // "ica gatatu"
|
|
28
|
+
moneyWords(15000); // "amafaranga ibihumbi cumi na bitanu"
|
|
29
|
+
formatDate(2026, 12, 3); // "Kuwa kane, 3 Kigarama 2026"
|
|
30
|
+
formatDate(2026, 12, 3, "itariki"); // "Itariki 3 Kigarama 2026"
|
|
31
|
+
formatDate(2026, 12, 3, "{Weekday} {day} {month}"); // your own pattern
|
|
32
|
+
parseMonth("kigarama"); // 12
|
|
33
|
+
tokens("n'amajana abiri"); // ["n'", "amajana", "abiri"]
|
|
34
|
+
expandNumbers("Nafashe 3 abantu"); // "Nafashe gatatu abantu"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Numbers go from -999,999,999,999 to 999,999,999,999. The optional second argument of `toWords` is a noun class (1 to 15):
|
|
38
|
+
a singular class for exactly one, a plural class (2, 4, 6, 8, 10, 13) for two or more.
|
|
39
|
+
`fromWords(toWords(n)) === n` for every number. Decimals and fractions are passed as strings (`"45.4"`, `"13/454"`)
|
|
40
|
+
and come back as strings. Bad input throws `RangeError` or `TypeError`.
|
|
41
|
+
|
|
42
|
+
- [Grammar and conventions](https://github.com/Blixy4/kirundi_text/blob/main/docs/grammar.md)
|
|
43
|
+
- [API reference](https://github.com/Blixy4/kirundi_text/blob/main/docs/api.md)
|
|
44
|
+
|
|
45
|
+
MIT licensed.
|
package/kirundi.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export const VERSION: string;
|
|
2
|
+
export const MAX_VALUE: number;
|
|
3
|
+
export const PLURAL_CLASSES: readonly number[];
|
|
4
|
+
export const SINGULAR_CLASSES: readonly number[];
|
|
5
|
+
export const MONTHS: readonly string[];
|
|
6
|
+
export const WEEKDAYS: readonly string[];
|
|
7
|
+
|
|
8
|
+
/** Spell n (0 <= n < 10**12) in Kirundi. cls is an optional noun class 1 to 15. */
|
|
9
|
+
export function toWords(n: number, cls?: number | null): string;
|
|
10
|
+
/** Parse Kirundi number words back to an integer. Throws RangeError on bad input. */
|
|
11
|
+
export function fromWords(text: string): number;
|
|
12
|
+
/** "45.4" -> "mirongo ine na gatanu n'ibice bine". Throws RangeError on bad input. */
|
|
13
|
+
export function decimalToWords(text: string): string;
|
|
14
|
+
/** Inverse of decimalToWords; returns a string such as "45.4". */
|
|
15
|
+
export function decimalFromWords(text: string): string;
|
|
16
|
+
/** "13/454" -> "cumi na bitatu vy'amajana ane na mirongo itanu na kane". Throws RangeError on bad input. */
|
|
17
|
+
export function fractionToWords(text: string): string;
|
|
18
|
+
/** Inverse of fractionToWords; returns a string such as "13/454". */
|
|
19
|
+
export function fractionFromWords(text: string): string;
|
|
20
|
+
/** 1 -> "ifaranga rimwe", 2 -> "amafaranga abiri", 0 -> "nta mafaranga". */
|
|
21
|
+
export function moneyWords(amount: number): string;
|
|
22
|
+
|
|
23
|
+
export interface DateParts {
|
|
24
|
+
weekday: string; Weekday: string; day: number;
|
|
25
|
+
month: string; month_number: number; year: number;
|
|
26
|
+
}
|
|
27
|
+
/** The pieces of a date, to lay out in any order. */
|
|
28
|
+
export function dateParts(year: number, month: number, day: number): DateParts;
|
|
29
|
+
export function dateParts(date: Date): DateParts;
|
|
30
|
+
|
|
31
|
+
export function isLeap(year: number): boolean;
|
|
32
|
+
export function daysInMonth(year: number, month: number): number;
|
|
33
|
+
/** Day of week, Monday = 0 ... Sunday = 6. */
|
|
34
|
+
export function weekday(year: number, month: number, day: number): number;
|
|
35
|
+
export function monthName(month: number): string;
|
|
36
|
+
export function weekdayName(year: number, month: number, day: number): string;
|
|
37
|
+
export function parseMonth(name: string): number;
|
|
38
|
+
export type DateStyle = "long" | "short";
|
|
39
|
+
export function formatDate(date: Date, style?: DateStyle): string;
|
|
40
|
+
export function formatDate(year: number, month: number, day: number, style?: DateStyle): string;
|
|
41
|
+
|
|
42
|
+
export function normalize(text: string): string;
|
|
43
|
+
export function fold(text: string): string;
|
|
44
|
+
export function tokens(text: string): string[];
|
|
45
|
+
export function expandNumbers(text: string): string;
|
|
46
|
+
/** 1 is "ica mbere", 2 is "ica kabiri", then "ica" + the counting number. */
|
|
47
|
+
export function ordinalToWords(n: number): string;
|
|
48
|
+
/** Inverse of ordinalToWords. */
|
|
49
|
+
export function ordinalFromWords(text: string): number;
|
package/kirundi.mjs
ADDED
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
// kirundi_text for JavaScript: numbers, dates and text tools for Kirundi.
|
|
2
|
+
// Zero dependencies. ES module. Mirrors the Python reference exactly; both are
|
|
3
|
+
// tested against the same tests/vectors.json.
|
|
4
|
+
|
|
5
|
+
export const VERSION = "1.0.0";
|
|
6
|
+
export const MAX_VALUE = 999999999999;
|
|
7
|
+
|
|
8
|
+
const VOWELS = "aeiou";
|
|
9
|
+
|
|
10
|
+
const COUNT = {
|
|
11
|
+
1: "rimwe", 2: "kabiri", 3: "gatatu", 4: "kane", 5: "gatanu",
|
|
12
|
+
6: "gatandatu", 7: "indwi", 8: "umunani", 9: "icenda",
|
|
13
|
+
};
|
|
14
|
+
const TENS = {
|
|
15
|
+
2: "ibiri", 3: "itatu", 4: "ine", 5: "itanu", 6: "itandatu", 7: "indwi",
|
|
16
|
+
8: "inani", 9: "icenda",
|
|
17
|
+
};
|
|
18
|
+
const ONE = {
|
|
19
|
+
1: "umwe", 3: "umwe", 5: "rimwe", 7: "kimwe", 9: "imwe",
|
|
20
|
+
11: "rumwe", 12: "kamwe", 14: "bumwe", 15: "kumwe",
|
|
21
|
+
};
|
|
22
|
+
const PLURAL_TO_SINGULAR = { 2: 1, 4: 3, 6: 5, 8: 7, 10: 9, 13: 12 };
|
|
23
|
+
const PREFIX = { 2: "ba", 4: "i", 6: "a", 8: "bi", 10: "zi", 13: "tu" };
|
|
24
|
+
const STEM = { 2: "biri", 3: "tatu", 4: "ne", 5: "tanu", 6: "tandatu", 7: "rindwi" };
|
|
25
|
+
// 7 is plain "indwi" for these classes and prefix + "rindwi" for the others.
|
|
26
|
+
const INDWI_CLASSES = [4, 6, 10];
|
|
27
|
+
|
|
28
|
+
export const PLURAL_CLASSES = Object.freeze(Object.keys(PREFIX).map(Number));
|
|
29
|
+
export const SINGULAR_CLASSES = Object.freeze(Object.keys(ONE).map(Number));
|
|
30
|
+
|
|
31
|
+
const has = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
|
|
32
|
+
const isInt = (v) => typeof v === "number" && Number.isInteger(v);
|
|
33
|
+
|
|
34
|
+
function join(parts) {
|
|
35
|
+
let out = parts[0];
|
|
36
|
+
for (let i = 1; i < parts.length; i++) {
|
|
37
|
+
const p = parts[i];
|
|
38
|
+
out += (VOWELS.includes(p[0]) ? " n'" : " na ") + p;
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function unit(d, cls, compound) {
|
|
44
|
+
if (cls === null) return COUNT[d];
|
|
45
|
+
if (d === 1) {
|
|
46
|
+
if (has(ONE, cls)) return ONE[cls];
|
|
47
|
+
if (compound && has(PLURAL_TO_SINGULAR, cls)) return ONE[PLURAL_TO_SINGULAR[cls]];
|
|
48
|
+
throw new RangeError(`class ${cls} cannot count exactly one`);
|
|
49
|
+
}
|
|
50
|
+
if (d === 8 || d === 9) return COUNT[d];
|
|
51
|
+
if (!has(PREFIX, cls)) throw new RangeError(`class ${cls} is not a plural class`);
|
|
52
|
+
if (d === 7 && INDWI_CLASSES.includes(cls)) return COUNT[7];
|
|
53
|
+
return PREFIX[cls] + STEM[d];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function below1000(n, cls, compound) {
|
|
57
|
+
const parts = [];
|
|
58
|
+
const h = Math.floor(n / 100);
|
|
59
|
+
const r = n % 100;
|
|
60
|
+
if (h === 1) parts.push("ijana");
|
|
61
|
+
else if (h > 1) parts.push("amajana " + unit(h, 6, true));
|
|
62
|
+
if (r) {
|
|
63
|
+
if (r === 10) parts.push("icumi");
|
|
64
|
+
else if (r > 10 && r < 20) parts.push(join(["cumi", unit(r - 10, cls, compound)]));
|
|
65
|
+
else if (r < 10) parts.push(unit(r, cls, compound));
|
|
66
|
+
else {
|
|
67
|
+
const t = Math.floor(r / 10);
|
|
68
|
+
const u = r % 10;
|
|
69
|
+
let s = "mirongo " + TENS[t];
|
|
70
|
+
if (u) s = join([s, unit(u, cls, compound)]);
|
|
71
|
+
parts.push(s);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return join(parts);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const GROUPS = [
|
|
78
|
+
[1e9, "miliyari imwe", "miliyari", 10],
|
|
79
|
+
[1e6, "miliyoni imwe", "miliyoni", 10],
|
|
80
|
+
[1000, "igihumbi", "ibihumbi", 8],
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
function words(n, cls, compound) {
|
|
84
|
+
for (const [size, oneWord, noun, gcls] of GROUPS) {
|
|
85
|
+
const q = Math.floor(n / size);
|
|
86
|
+
if (!q) continue;
|
|
87
|
+
const rem = n % size;
|
|
88
|
+
let rest = rem ? words(rem, cls, compound) : "";
|
|
89
|
+
let word;
|
|
90
|
+
if (q === 1) {
|
|
91
|
+
word = oneWord;
|
|
92
|
+
} else {
|
|
93
|
+
// A multiplier with hundreds and a smaller part repeats the noun:
|
|
94
|
+
// 218,000 is "ibihumbi amajana abiri n'ibihumbi cumi n'umunani".
|
|
95
|
+
const h = Math.floor(q / 100);
|
|
96
|
+
const r = q % 100;
|
|
97
|
+
const mults = h && r > 0 && r < 20 ? [h * 100, r] : [q];
|
|
98
|
+
word = join(mults.map((m) => noun + " " + below1000(m, gcls, true)));
|
|
99
|
+
if (rest) {
|
|
100
|
+
const last = mults[mults.length - 1];
|
|
101
|
+
if (last % 100 === 0 && rem >= 20 && rem <= 99) {
|
|
102
|
+
// Hundreds then tens: "mirongo" is dropped and the tens digit keeps
|
|
103
|
+
// its agreeing form. 200,030 is "ibihumbi amajana abiri n'itatu".
|
|
104
|
+
const t = Math.floor(rem / 10);
|
|
105
|
+
const u = rem % 10;
|
|
106
|
+
let tens = t === 7 ? "irindwi" : TENS[t]; // "indwi" is seven
|
|
107
|
+
if (u) tens = join([tens, unit(u, cls, compound)]);
|
|
108
|
+
const h9 = last / 100;
|
|
109
|
+
if (h9 === 9) word = word.slice(0, word.length - unit(9, 6, true).length) + "cenda";
|
|
110
|
+
return join([word, tens]);
|
|
111
|
+
}
|
|
112
|
+
// 29,000 "ibihumbi mirongo ibiri n'icenda" vs 20,009 "... na cenda"; 200,009
|
|
113
|
+
// is "... amajana abiri na cenda" because "n'icenda" there would be 90.
|
|
114
|
+
if ((last % 100 >= 20 && last % 100 <= 90 && last % 10 === 0 && (rem === 8 || rem === 9)) ||
|
|
115
|
+
(last % 100 === 0 && rem === 9)) {
|
|
116
|
+
rest = rem === 8 ? "munani" : "cenda";
|
|
117
|
+
}
|
|
118
|
+
// Anything still readable two ways gets a comma.
|
|
119
|
+
const firstRest = rest.split(" ")[0];
|
|
120
|
+
if (last % 100 === 0 && has(TENS_REV, firstRest) && firstRest !== "indwi") word += ",";
|
|
121
|
+
const mult = below1000(last, gcls, true);
|
|
122
|
+
const toks = tokenize(mult + " " + rest);
|
|
123
|
+
if (chunk(toks, 0, GROUP_NOUNS[noun][1], true)[1] !== tokenize(mult).length) word += ",";
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return rest ? join([word, rest]) : word;
|
|
127
|
+
}
|
|
128
|
+
return below1000(n, cls, compound);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function classMsg(cls, need, options) {
|
|
132
|
+
const kind = has(PREFIX, cls) ? "a plural" : has(ONE, cls) ? "a singular" : "not a valid";
|
|
133
|
+
return `class ${cls} is ${kind} class; use one of ${options.join(", ")} for ${need}`;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Spell n (0 <= n < 10**12) in Kirundi. cls is an optional noun class 1 to 15. */
|
|
137
|
+
export function toWords(n, cls = null) {
|
|
138
|
+
if (typeof n !== "number" || !Number.isInteger(n)) throw new TypeError("n must be an integer");
|
|
139
|
+
if (n < -MAX_VALUE || n > MAX_VALUE) throw new RangeError(`n must be between -${MAX_VALUE} and ${MAX_VALUE}`);
|
|
140
|
+
if (n < 0) return "haze " + toWords(-n, cls);
|
|
141
|
+
if (cls !== null && cls !== undefined) {
|
|
142
|
+
if (!isInt(cls)) throw new TypeError("cls must be an integer or null");
|
|
143
|
+
if (n === 1 && !has(ONE, cls)) throw new RangeError(classMsg(cls, "exactly one", SINGULAR_CLASSES));
|
|
144
|
+
if (n >= 2 && !has(PREFIX, cls)) throw new RangeError(classMsg(cls, "two or more", PLURAL_CLASSES));
|
|
145
|
+
} else {
|
|
146
|
+
cls = null;
|
|
147
|
+
}
|
|
148
|
+
if (n === 0) return "ubusa";
|
|
149
|
+
return words(n, cls, n > 1);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ---------------------------------------------------------------- parsing
|
|
153
|
+
|
|
154
|
+
const forms = (cls) => {
|
|
155
|
+
const out = {};
|
|
156
|
+
for (const d of [2, 3, 4, 5, 6]) out[PREFIX[cls] + STEM[d]] = d;
|
|
157
|
+
out[unit(7, cls, false)] = 7;
|
|
158
|
+
return out;
|
|
159
|
+
};
|
|
160
|
+
const INVARIANT = { umunani: 8, icenda: 9 };
|
|
161
|
+
const ALL_UNITS = {};
|
|
162
|
+
for (const [d, w] of Object.entries(COUNT)) ALL_UNITS[w] = Number(d);
|
|
163
|
+
for (const c of Object.keys(PREFIX)) Object.assign(ALL_UNITS, forms(Number(c)));
|
|
164
|
+
for (const w of Object.values(ONE)) ALL_UNITS[w] = 1;
|
|
165
|
+
// After "na" a separate 8 or 9 can drop its first vowel: "... mirongo ibiri na cenda".
|
|
166
|
+
ALL_UNITS.munani = 8;
|
|
167
|
+
ALL_UNITS.cenda = 9;
|
|
168
|
+
const HUND_UNITS = { ...forms(6), munani: 8, cenda: 9, ...INVARIANT };
|
|
169
|
+
const THOUSAND_UNITS = { ...forms(8), kimwe: 1, ...INVARIANT };
|
|
170
|
+
const MILLION_UNITS = { ...forms(10), imwe: 1, ...INVARIANT };
|
|
171
|
+
const TENS_REV = {};
|
|
172
|
+
for (const [d, w] of Object.entries(TENS)) TENS_REV[w] = Number(d);
|
|
173
|
+
// "mirongo irindwi" is also in common use for 70; it is read but not produced.
|
|
174
|
+
TENS_REV.irindwi = 7;
|
|
175
|
+
ALL_UNITS.irindwi = 7;
|
|
176
|
+
const GROUP_NOUNS = {
|
|
177
|
+
igihumbi: [1000, null],
|
|
178
|
+
ibihumbi: [1000, THOUSAND_UNITS],
|
|
179
|
+
miliyoni: [1e6, MILLION_UNITS],
|
|
180
|
+
miliyari: [1e9, MILLION_UNITS],
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const APOSTROPHES = /[\u2019\u2018\u02bc\u02bb`\u00b4\u2032]/g;
|
|
184
|
+
|
|
185
|
+
function tokenize(text) {
|
|
186
|
+
const s = text.trim().toLowerCase().replace(APOSTROPHES, "'").replace(/,/g, " , ");
|
|
187
|
+
const out = [];
|
|
188
|
+
for (let t of s.split(/\s+/)) {
|
|
189
|
+
if (!t || t === "na" || t === "n'") continue;
|
|
190
|
+
if (t.startsWith("n'")) t = t.slice(2);
|
|
191
|
+
out.push(unglue(t));
|
|
192
|
+
}
|
|
193
|
+
return out;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
let KNOWN = null;
|
|
197
|
+
|
|
198
|
+
// Undo a join written without a space or apostrophe: "nabitanu" is "na bitanu"
|
|
199
|
+
// and "nicenda" is "n'icenda".
|
|
200
|
+
function unglue(t) {
|
|
201
|
+
if (KNOWN === null) {
|
|
202
|
+
KNOWN = new Set([
|
|
203
|
+
...Object.keys(ALL_UNITS), ...Object.keys(GROUP_NOUNS), ...Object.keys(TENS_REV),
|
|
204
|
+
...Object.keys(DENOMINATOR_LEN),
|
|
205
|
+
"ijana", "amajana", "mirongo", "cumi", "icumi", "ubusa", "zeru", "ibice", "haze",
|
|
206
|
+
]);
|
|
207
|
+
}
|
|
208
|
+
if (KNOWN.has(t)) return t;
|
|
209
|
+
if (t.startsWith("na") && KNOWN.has(t.slice(2))) return t.slice(2);
|
|
210
|
+
if (t.startsWith("n") && VOWELS.includes(t[1] ?? "x") && KNOWN.has(t.slice(1))) return t.slice(1);
|
|
211
|
+
return t;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// A multiplier is hundreds or a part below 100, never both.
|
|
215
|
+
function chunk(toks, i, allowed, multiplier = false) {
|
|
216
|
+
let val = 0;
|
|
217
|
+
let stage = 0;
|
|
218
|
+
const n = toks.length;
|
|
219
|
+
if (i < n && toks[i] === "ijana") {
|
|
220
|
+
val += 100; stage = 1; i += 1;
|
|
221
|
+
} else if (i < n && toks[i] === "amajana") {
|
|
222
|
+
if (i + 1 >= n || !has(HUND_UNITS, toks[i + 1])) {
|
|
223
|
+
throw new RangeError("expected a number after 'amajana'");
|
|
224
|
+
}
|
|
225
|
+
val += 100 * HUND_UNITS[toks[i + 1]]; stage = 1; i += 2;
|
|
226
|
+
}
|
|
227
|
+
if (multiplier && stage === 1 && !(i < n && toks[i] === "mirongo")) return [val, i];
|
|
228
|
+
if (stage < 2 && i < n) {
|
|
229
|
+
const t = toks[i];
|
|
230
|
+
if (t === "mirongo") {
|
|
231
|
+
if (i + 1 >= n || !has(TENS_REV, toks[i + 1])) {
|
|
232
|
+
throw new RangeError("expected a tens digit after 'mirongo'");
|
|
233
|
+
}
|
|
234
|
+
val += 10 * TENS_REV[toks[i + 1]]; stage = 2; i += 2;
|
|
235
|
+
} else if (t === "icumi") {
|
|
236
|
+
val += 10; stage = 4; i += 1;
|
|
237
|
+
// "icumi na bitanu" is also said for 15.
|
|
238
|
+
if (!multiplier && i < n && has(allowed, toks[i])) { val += allowed[toks[i]]; i += 1; }
|
|
239
|
+
} else if (t === "cumi") {
|
|
240
|
+
if (i + 1 < n && has(allowed, toks[i + 1])) {
|
|
241
|
+
val += 10 + allowed[toks[i + 1]]; stage = 4; i += 2;
|
|
242
|
+
} else {
|
|
243
|
+
// "cumi" alone is a common spoken form of "icumi" (10).
|
|
244
|
+
val += 10; stage = 4; i += 1;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (stage <= 2 && i < n && has(allowed, toks[i])) {
|
|
249
|
+
val += allowed[toks[i]]; i += 1;
|
|
250
|
+
}
|
|
251
|
+
return [val, i];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** Parse Kirundi number words back to an integer. Throws RangeError on bad input. */
|
|
255
|
+
export function fromWords(text) {
|
|
256
|
+
if (typeof text !== "string") throw new TypeError("text must be a string");
|
|
257
|
+
const toks = tokenize(text);
|
|
258
|
+
if (!toks.length) throw new RangeError("empty input");
|
|
259
|
+
if (toks.length === 1 && (toks[0] === "ubusa" || toks[0] === "zeru")) return 0;
|
|
260
|
+
if (toks[0] === "haze") {
|
|
261
|
+
if (toks.length < 2 || ["haze", "ubusa", "zeru"].includes(toks[1])) {
|
|
262
|
+
throw new RangeError("haze must be followed by a number above zero");
|
|
263
|
+
}
|
|
264
|
+
return -fromWords(toks.slice(1).join(" "));
|
|
265
|
+
}
|
|
266
|
+
let total = 0;
|
|
267
|
+
let i = 0;
|
|
268
|
+
let last = 1e12;
|
|
269
|
+
const n = toks.length;
|
|
270
|
+
while (i < n && has(GROUP_NOUNS, toks[i])) {
|
|
271
|
+
const [size, allowed] = GROUP_NOUNS[toks[i]];
|
|
272
|
+
i += 1;
|
|
273
|
+
let mult = 1;
|
|
274
|
+
if (allowed !== null) {
|
|
275
|
+
const [m, j] = chunk(toks, i, allowed, true);
|
|
276
|
+
if (j === i) throw new RangeError(`expected a number after '${toks[i - 1]}'`);
|
|
277
|
+
mult = m;
|
|
278
|
+
i = j;
|
|
279
|
+
}
|
|
280
|
+
if (size > last) throw new RangeError("groups out of order");
|
|
281
|
+
last = size;
|
|
282
|
+
total += size * mult;
|
|
283
|
+
// After plain hundreds a bare tens digit is a tens count.
|
|
284
|
+
if (allowed !== null && mult % 100 === 0 && i < n && has(TENS_REV, toks[i]) && toks[i] !== "indwi") {
|
|
285
|
+
total += 10 * TENS_REV[toks[i]];
|
|
286
|
+
i += 1;
|
|
287
|
+
}
|
|
288
|
+
if (i < n && toks[i] === ",") i += 1;
|
|
289
|
+
}
|
|
290
|
+
const [rest, j] = chunk(toks, i, ALL_UNITS);
|
|
291
|
+
total += rest;
|
|
292
|
+
i = j;
|
|
293
|
+
if (i < n) throw new RangeError(`cannot parse '${toks[i]}'`);
|
|
294
|
+
if (i === 0) throw new RangeError(`cannot parse '${text}'`);
|
|
295
|
+
return total;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** 1 -> "ifaranga rimwe", 2 -> "amafaranga abiri", 0 -> "nta mafaranga". */
|
|
299
|
+
export function moneyWords(amount) {
|
|
300
|
+
if (typeof amount !== "number" || !Number.isInteger(amount)) {
|
|
301
|
+
throw new TypeError("amount must be an integer");
|
|
302
|
+
}
|
|
303
|
+
if (amount < 0) throw new RangeError("amount must not be negative");
|
|
304
|
+
if (amount === 0) return "nta mafaranga";
|
|
305
|
+
if (amount === 1) return "ifaranga " + toWords(1, 5);
|
|
306
|
+
return "amafaranga " + toWords(amount, 6);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const DECIMAL = /^([0-9]+)\.([0-9]+)$/;
|
|
310
|
+
const DENOMINATORS = { 2: "ivyijana", 3: "ivyigihumbi", 6: "ivyumuliyoni", 9: "ivyamiliyaridi" };
|
|
311
|
+
// "ivyamiliyare" and "ivyamiliyari" are also read, not written.
|
|
312
|
+
const DENOMINATOR_LEN = { ivyijana: 2, ivyigihumbi: 3, ivyumuliyoni: 6, ivyamiliyaridi: 9, ivyamiliyare: 9, ivyamiliyari: 9 };
|
|
313
|
+
|
|
314
|
+
// A count of ibice (class 8); exactly one is kimwe (class 7).
|
|
315
|
+
const ibice = (v) => toWords(v, v === 1 ? 7 : 8);
|
|
316
|
+
|
|
317
|
+
// A count of things: a final 1 is kimwe and a final 4 is bine. Plain counting
|
|
318
|
+
// (rimwe, kane) is for the number on its own.
|
|
319
|
+
function things(n) {
|
|
320
|
+
if (n === 1) return toWords(1, 7);
|
|
321
|
+
if (n > 1 && (n % 10 === 1 || n % 10 === 4)) return toWords(n, 8);
|
|
322
|
+
return toWords(n);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// "-6.5" gives "haze gatandatu n'ibice bitanu" (haze = minus).
|
|
326
|
+
export function decimalToWords(text) {
|
|
327
|
+
if (typeof text !== "string") throw new TypeError("text must be a string");
|
|
328
|
+
const t = text.trim();
|
|
329
|
+
if (t[0] === "-" || t[0] === "\u2212") return "haze " + decimalUnsigned(t.slice(1));
|
|
330
|
+
return decimalUnsigned(t);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function decimalUnsigned(text) {
|
|
334
|
+
const m = DECIMAL.exec(text.trim());
|
|
335
|
+
if (!m) throw new RangeError("expected digits, a point, then digits: " + JSON.stringify(text));
|
|
336
|
+
const head = toWords(Number(m[1]));
|
|
337
|
+
const frac = m[2];
|
|
338
|
+
const zeros = frac.length - frac.replace(/^0+/, "").length;
|
|
339
|
+
const rest = frac.slice(zeros);
|
|
340
|
+
if (zeros === 0) return head + " n'ibice " + ibice(Number(frac));
|
|
341
|
+
if (!rest) return head + " n'ibice " + Array(zeros).fill("ubusa").join(" ");
|
|
342
|
+
if (DENOMINATORS[frac.length]) {
|
|
343
|
+
return head + " n'" + DENOMINATORS[frac.length] + " " + ibice(Number(rest));
|
|
344
|
+
}
|
|
345
|
+
const last = Number(rest) === 1 ? "rimwe" : ibice(Number(rest));
|
|
346
|
+
return head + " n'ibice " + Array(zeros).fill("ubusa").join(" ") + " " + last;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export function decimalFromWords(text) {
|
|
350
|
+
if (typeof text !== "string") throw new TypeError("text must be a string");
|
|
351
|
+
let toks = tokenize(text);
|
|
352
|
+
const negative = toks[0] === "haze";
|
|
353
|
+
if (negative) toks = toks.slice(1);
|
|
354
|
+
const k = toks.findIndex((t) => t === "ibice" || Object.hasOwn(DENOMINATOR_LEN, t));
|
|
355
|
+
if (k < 0) {
|
|
356
|
+
throw new RangeError("expected n'ibice, n'ivyijana, n'ivyigihumbi or n'ivyumuliyoni: " + JSON.stringify(text));
|
|
357
|
+
}
|
|
358
|
+
const left = toks.slice(0, k);
|
|
359
|
+
const right = toks.slice(k + 1);
|
|
360
|
+
if (!left.length || !right.length) throw new RangeError("a number is needed on both sides of " + toks[k]);
|
|
361
|
+
let frac;
|
|
362
|
+
if (toks[k] === "ibice") {
|
|
363
|
+
let zeros = 0;
|
|
364
|
+
while (zeros < right.length && right[zeros] === "ubusa") zeros++;
|
|
365
|
+
const rest = right.slice(zeros);
|
|
366
|
+
frac = "0".repeat(zeros) + (rest.length ? String(fromWords(rest.join(" "))) : "");
|
|
367
|
+
} else {
|
|
368
|
+
const size = DENOMINATOR_LEN[toks[k]];
|
|
369
|
+
const v = fromWords(right.join(" "));
|
|
370
|
+
if (!(v >= 1 && v < 10 ** (size - 1))) {
|
|
371
|
+
throw new RangeError(toks[k] + " takes a number from 1 to " + (10 ** (size - 1) - 1));
|
|
372
|
+
}
|
|
373
|
+
frac = String(v).padStart(size, "0");
|
|
374
|
+
}
|
|
375
|
+
return (negative ? "-" : "") + fromWords(left.join(" ")) + "." + frac;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const FRACTION = /^([-\u2212]?)([0-9]+)\/([0-9]+)$/;
|
|
379
|
+
|
|
380
|
+
// "13/454" gives "cumi na bitatu vy'amajana ane na mirongo itanu na kane";
|
|
381
|
+
// 1/2 is "icakabiri", 1/3 "icagatatu", -4/5 "haze bine vya gatanu".
|
|
382
|
+
export function fractionToWords(text) {
|
|
383
|
+
if (typeof text !== "string") throw new TypeError("text must be a string");
|
|
384
|
+
const m = FRACTION.exec(text.trim());
|
|
385
|
+
if (!m) throw new RangeError("expected digits, a slash, then digits: " + JSON.stringify(text));
|
|
386
|
+
const top = Number(m[2]);
|
|
387
|
+
const bottom = Number(m[3]);
|
|
388
|
+
if (bottom < 2) throw new RangeError("the bottom of a fraction must be at least 2: " + JSON.stringify(text));
|
|
389
|
+
const low = things(bottom);
|
|
390
|
+
let body;
|
|
391
|
+
if (top === 1 && (bottom === 2 || bottom === 3)) {
|
|
392
|
+
body = "ica" + low;
|
|
393
|
+
} else {
|
|
394
|
+
const vowel = VOWELS.includes(low[0]);
|
|
395
|
+
const link = top === 1 ? (vowel ? "c'" : "ca ") : (vowel ? "vy'" : "vya ");
|
|
396
|
+
body = ibice(top) + " " + link + low;
|
|
397
|
+
}
|
|
398
|
+
return (m[1] ? "haze " : "") + body;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export function fractionFromWords(text) {
|
|
402
|
+
if (typeof text !== "string") throw new TypeError("text must be a string");
|
|
403
|
+
let toks = tokenize(text);
|
|
404
|
+
const negative = toks[0] === "haze";
|
|
405
|
+
if (negative) toks = toks.slice(1);
|
|
406
|
+
if (!toks.length) throw new RangeError("empty input");
|
|
407
|
+
unglue("ubusa"); // makes sure the word list exists
|
|
408
|
+
const first = toks[0];
|
|
409
|
+
let left;
|
|
410
|
+
let right;
|
|
411
|
+
if (first === "ica" && toks.length > 1) {
|
|
412
|
+
left = ["kimwe"]; right = toks.slice(1);
|
|
413
|
+
} else if (first.startsWith("ica") && KNOWN.has(first.slice(3))) {
|
|
414
|
+
left = ["kimwe"]; right = [first.slice(3), ...toks.slice(1)];
|
|
415
|
+
} else {
|
|
416
|
+
const k = toks.findIndex((t) => t === "ca" || t === "vya" || t.startsWith("c'") || t.startsWith("vy'"));
|
|
417
|
+
if (k < 0) throw new RangeError("expected ca, vya, c' or vy' between the two parts: " + JSON.stringify(text));
|
|
418
|
+
const at = toks[k].indexOf("'");
|
|
419
|
+
const glued = at < 0 ? "" : toks[k].slice(at + 1);
|
|
420
|
+
left = toks.slice(0, k);
|
|
421
|
+
right = (glued ? [glued] : []).concat(toks.slice(k + 1));
|
|
422
|
+
}
|
|
423
|
+
if (!left.length || !right.length) throw new RangeError("a number is needed on both sides of the fraction");
|
|
424
|
+
const top = fromWords(left.join(" "));
|
|
425
|
+
const bottom = fromWords(right.join(" "));
|
|
426
|
+
if (bottom < 2) throw new RangeError("the bottom of a fraction must be at least 2");
|
|
427
|
+
return (negative ? "-" : "") + top + "/" + bottom;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
/** First, second, third: 1 is "ica mbere", 2 is "ica kabiri", then "ica" + count. */
|
|
432
|
+
export function ordinalToWords(n) {
|
|
433
|
+
if (!Number.isInteger(n)) throw new TypeError("n must be an integer");
|
|
434
|
+
if (n < 1) throw new RangeError("an ordinal starts at 1: " + n);
|
|
435
|
+
if (n === 1) return "ica mbere";
|
|
436
|
+
const body = n === 10 ? "cumi" : toWords(n);
|
|
437
|
+
return ("aeiou".includes(body[0]) ? "ic'" : "ica ") + body;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/** Inverse of ordinalToWords. Also reads glued spellings such as "icakabiri". */
|
|
441
|
+
export function ordinalFromWords(text) {
|
|
442
|
+
if (typeof text !== "string") throw new TypeError("text must be a string");
|
|
443
|
+
let toks = tokenize(text);
|
|
444
|
+
if (!toks.length) throw new RangeError("empty input");
|
|
445
|
+
unglue("ubusa"); // makes sure the word list exists
|
|
446
|
+
const first = toks[0];
|
|
447
|
+
if (first.startsWith("ic'") && first.length > 3) {
|
|
448
|
+
toks = ["ica", first.slice(3), ...toks.slice(1)];
|
|
449
|
+
} else if (first !== "ica" && first.startsWith("ica") && (first.slice(3) === "mbere" || KNOWN.has(first.slice(3)))) {
|
|
450
|
+
toks = ["ica", first.slice(3), ...toks.slice(1)];
|
|
451
|
+
}
|
|
452
|
+
if (toks[0] !== "ica" || toks.length < 2) throw new RangeError("an ordinal starts with ica: " + JSON.stringify(text));
|
|
453
|
+
const rest = toks.slice(1);
|
|
454
|
+
if (rest.length === 1 && rest[0] === "mbere") return 1;
|
|
455
|
+
const v = fromWords(rest.join(" "));
|
|
456
|
+
if (v < 2) throw new RangeError("ica mbere is the first: " + JSON.stringify(text));
|
|
457
|
+
return v;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
// ------------------------------------------------------------------ dates
|
|
461
|
+
|
|
462
|
+
export const MONTHS = Object.freeze([
|
|
463
|
+
"Nzero", "Ruhuhuma", "Ntwarante", "Ndamukiza", "Rusama", "Ruheshi",
|
|
464
|
+
"Mukakaro", "Myandagaro", "Nyakanga", "Gitugutu", "Munyonyo", "Kigarama",
|
|
465
|
+
]);
|
|
466
|
+
// Index 0 is Monday.
|
|
467
|
+
export const WEEKDAYS = Object.freeze([
|
|
468
|
+
"kuwa mbere", "kuwa kabiri", "kuwa gatatu", "kuwa kane", "kuwa gatanu",
|
|
469
|
+
"kuwa gatandatu", "ku wamungu",
|
|
470
|
+
]);
|
|
471
|
+
const T = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4];
|
|
472
|
+
|
|
473
|
+
export const isLeap = (y) => y % 4 === 0 && (y % 100 !== 0 || y % 400 === 0);
|
|
474
|
+
|
|
475
|
+
export function daysInMonth(year, month) {
|
|
476
|
+
if (!isInt(month) || month < 1 || month > 12) throw new RangeError("month must be 1 to 12");
|
|
477
|
+
if (month === 2) return isLeap(year) ? 29 : 28;
|
|
478
|
+
return [4, 6, 9, 11].includes(month) ? 30 : 31;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function checkDate(y, m, d) {
|
|
482
|
+
for (const v of [y, m, d]) {
|
|
483
|
+
if (!isInt(v)) throw new TypeError("year, month and day must be integers");
|
|
484
|
+
}
|
|
485
|
+
if (y < 1) throw new RangeError("year must be 1 or later");
|
|
486
|
+
if (d < 1 || d > daysInMonth(y, m)) throw new RangeError(`invalid date ${y}-${m}-${d}`);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/** Day of week, Monday = 0 ... Sunday = 6. */
|
|
490
|
+
export function weekday(year, month, day) {
|
|
491
|
+
checkDate(year, month, day);
|
|
492
|
+
const y = year - (month < 3 ? 1 : 0);
|
|
493
|
+
const sunday0 = (y + Math.floor(y / 4) - Math.floor(y / 100) + Math.floor(y / 400) + T[month - 1] + day) % 7;
|
|
494
|
+
return (sunday0 + 6) % 7;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
export function monthName(month) {
|
|
498
|
+
if (!isInt(month) || month < 1 || month > 12) throw new RangeError("month must be an integer from 1 to 12");
|
|
499
|
+
return MONTHS[month - 1];
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
export const weekdayName = (y, m, d) => WEEKDAYS[weekday(y, m, d)];
|
|
503
|
+
|
|
504
|
+
const foldMarks = (s) => s.normalize("NFD").replace(/\p{M}/gu, "");
|
|
505
|
+
const MONTH_KEYS = {};
|
|
506
|
+
MONTHS.forEach((m, i) => { MONTH_KEYS[foldMarks(m.toLowerCase())] = i + 1; });
|
|
507
|
+
|
|
508
|
+
export function parseMonth(name) {
|
|
509
|
+
const key = foldMarks(String(name).trim().toLowerCase());
|
|
510
|
+
if (!has(MONTH_KEYS, key)) throw new RangeError(`unknown month '${name}'`);
|
|
511
|
+
return MONTH_KEYS[key];
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* formatDate(new Date(), style) or formatDate(y, m, d, style).
|
|
516
|
+
* style is "long" ("Kuwa kane, 3 Kigarama 2026") or "short" ("3 Kigarama 2026").
|
|
517
|
+
* A Date uses its local calendar day.
|
|
518
|
+
*/
|
|
519
|
+
export function dateParts(a, b, c) {
|
|
520
|
+
let y, m, day;
|
|
521
|
+
if (a instanceof Date) {
|
|
522
|
+
y = a.getFullYear(); m = a.getMonth() + 1; day = a.getDate();
|
|
523
|
+
} else {
|
|
524
|
+
y = a; m = b; day = c;
|
|
525
|
+
}
|
|
526
|
+
checkDate(y, m, day);
|
|
527
|
+
const wd = WEEKDAYS[weekday(y, m, day)];
|
|
528
|
+
return {
|
|
529
|
+
weekday: wd, Weekday: wd[0].toUpperCase() + wd.slice(1), day,
|
|
530
|
+
month: MONTHS[m - 1], month_number: m, year: y,
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
export function formatDate(a, b, c, d) {
|
|
535
|
+
let parts, style;
|
|
536
|
+
if (a instanceof Date) {
|
|
537
|
+
parts = dateParts(a);
|
|
538
|
+
style = b === undefined ? "long" : b;
|
|
539
|
+
} else {
|
|
540
|
+
parts = dateParts(a, b, c);
|
|
541
|
+
style = d === undefined ? "long" : d;
|
|
542
|
+
}
|
|
543
|
+
const core = `${parts.day} ${parts.month} ${parts.year}`;
|
|
544
|
+
if (style === "short") return core;
|
|
545
|
+
if (style === "itariki") return "Itariki " + core;
|
|
546
|
+
if (style === "long") return parts.Weekday + ", " + core;
|
|
547
|
+
if (style.includes("{")) {
|
|
548
|
+
return style.replace(/\{(\w+)\}/g, (_, key) => {
|
|
549
|
+
if (!Object.hasOwn(parts, key)) {
|
|
550
|
+
throw new RangeError(`unknown date field {${key}}; use one of ${Object.keys(parts).map((k) => "{" + k + "}").join(", ")}`);
|
|
551
|
+
}
|
|
552
|
+
return String(parts[key]);
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
throw new RangeError("style must be 'long', 'short', 'itariki' or a pattern such as '{day} {month} {year}'");
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
// ------------------------------------------------------------------- text
|
|
559
|
+
|
|
560
|
+
const APOS_RE = /[\u2019\u2018\u02bc\u02bb`\u00b4\u2032]/g;
|
|
561
|
+
const INVISIBLE_RE = /[\u200b\u200c\u200d\u2060\ufeff\u00ad]/g;
|
|
562
|
+
const SPACE_RE = /[\s\u00a0\u2007\u202f]+/g;
|
|
563
|
+
const WORD_RE = /[\p{L}\p{N}]+(?:'[\p{L}\p{N}]+)*/gu;
|
|
564
|
+
const DIGITS_RE = /[0-9]+/g;
|
|
565
|
+
|
|
566
|
+
/** NFC, one apostrophe character, single spaces, trimmed. Case is kept. */
|
|
567
|
+
export function normalize(text) {
|
|
568
|
+
return text.normalize("NFC").replace(INVISIBLE_RE, "").replace(APOS_RE, "'")
|
|
569
|
+
.replace(SPACE_RE, " ").trim();
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/** Search key: normalize, lower-case, drop accents and tone marks. */
|
|
573
|
+
export function fold(text) {
|
|
574
|
+
return foldMarks(normalize(text).toLowerCase()).normalize("NFC");
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/** Split into words, keeping the elision apostrophe on the left piece. */
|
|
578
|
+
export function tokens(text) {
|
|
579
|
+
const out = [];
|
|
580
|
+
for (const word of normalize(text).match(WORD_RE) || []) {
|
|
581
|
+
if (word.includes("'")) {
|
|
582
|
+
const pieces = word.split("'");
|
|
583
|
+
for (let i = 0; i < pieces.length - 1; i++) out.push(pieces[i] + "'");
|
|
584
|
+
out.push(pieces[pieces.length - 1]);
|
|
585
|
+
} else {
|
|
586
|
+
out.push(word);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
return out;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
/** Replace each run of ASCII digits with Kirundi words (for speech output). */
|
|
593
|
+
export function expandNumbers(text) {
|
|
594
|
+
return text.replace(DIGITS_RE, (m) => {
|
|
595
|
+
const t = m.replace(/^0+/, "");
|
|
596
|
+
return t.length > 12 ? m : toWords(t === "" ? 0 : Number(t));
|
|
597
|
+
});
|
|
598
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kirundi_text",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Numbers, dates and text tools for Kirundi (Ikirundi): digits to words and back, decimals, fractions, ordinals, dates, search folding. Zero dependencies.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "kirundi.mjs",
|
|
7
|
+
"exports": "./kirundi.mjs",
|
|
8
|
+
"types": "kirundi.d.ts",
|
|
9
|
+
"files": ["kirundi.mjs", "kirundi.d.ts", "README.md", "LICENSE"],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "node --test test/kirundi.test.mjs"
|
|
12
|
+
},
|
|
13
|
+
"engines": { "node": ">=18" },
|
|
14
|
+
"keywords": ["kirundi", "ikirundi", "burundi", "numbers", "nlp", "i18n"],
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/Blixy4/kirundi_text.git",
|
|
18
|
+
"directory": "js"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/Blixy4/kirundi_text#readme",
|
|
21
|
+
"bugs": { "url": "https://github.com/Blixy4/kirundi_text/issues" },
|
|
22
|
+
"license": "MIT"
|
|
23
|
+
}
|