@mdgascar/words 0.8.0 → 0.10.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 +29 -16
- package/dist/index.cjs +367 -71
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +146 -16
- package/dist/index.d.ts +146 -16
- package/dist/index.js +363 -71
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/dist/index.js
CHANGED
|
@@ -60,58 +60,30 @@ var TRADITIONAL_SCALES = [
|
|
|
60
60
|
{ word: "tsitamboisafaharoa", exponent: 15, countDigits: 1 },
|
|
61
61
|
{ word: "lavitrisafaharoa", exponent: 16, countDigits: 1 },
|
|
62
62
|
{ word: "alinkisafaharoa", exponent: 17, countDigits: 1 },
|
|
63
|
-
{ word: "tsipesimpeseninafaharoa", exponent: 18, countDigits:
|
|
63
|
+
{ word: "tsipesimpeseninafaharoa", exponent: 18, countDigits: 1 }
|
|
64
64
|
];
|
|
65
65
|
var DECIMAL_SEPARATOR = "faingo";
|
|
66
66
|
var JOINER_UNITS_BEFORE_TEN = "ambin'ny";
|
|
67
67
|
var JOINER_BELOW_THOUSAND = "amby";
|
|
68
68
|
var JOINER_BETWEEN_GROUPS = "sy";
|
|
69
69
|
var MIN_VALUE = 0;
|
|
70
|
+
var ORDINAL_FIRST = "voalohany";
|
|
71
|
+
var ORDINAL_PREFIX = "faha";
|
|
72
|
+
var ORDINAL_PREFIX_BEFORE_VOWEL = "fah";
|
|
73
|
+
var VOWELS = ["a", "e", "i", "o", "y"];
|
|
74
|
+
var MAX_TEXT_LENGTH = 100;
|
|
75
|
+
var MAX_WORDS_LENGTH = 4096;
|
|
70
76
|
|
|
71
|
-
// src/
|
|
72
|
-
var SPEC_VERSION = "0.7.0";
|
|
73
|
-
var MdgError = class extends Error {
|
|
74
|
-
/** Always `'MdgError'`. */
|
|
75
|
-
name = "MdgError";
|
|
76
|
-
/** Why the call failed; the same code in every language. */
|
|
77
|
-
code;
|
|
78
|
-
/**
|
|
79
|
-
* Creates an error with a shared spec code.
|
|
80
|
-
*
|
|
81
|
-
* @param code - Shared spec error code.
|
|
82
|
-
* @param message - Human-readable explanation, in English.
|
|
83
|
-
*/
|
|
84
|
-
constructor(code, message) {
|
|
85
|
-
super(message);
|
|
86
|
-
this.code = code;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Makes `instanceof MdgError` true for errors from any mdg-utils package or build.
|
|
90
|
-
*
|
|
91
|
-
* @remarks Each package, and its ESM and CommonJS builds, has its own copy of this class,
|
|
92
|
-
* so the check looks at the error's shape instead of its prototype chain.
|
|
93
|
-
*
|
|
94
|
-
* @param value - Any value, usually a caught error.
|
|
95
|
-
* @returns `true` when `value` is an mdg-utils error.
|
|
96
|
-
*/
|
|
97
|
-
static [Symbol.hasInstance](value) {
|
|
98
|
-
return value instanceof Error && value.name === "MdgError" && "code" in value;
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
// src/number-to-words.ts
|
|
103
|
-
var SCALES_BY_SYSTEM = {
|
|
104
|
-
standard: SCALES,
|
|
105
|
-
traditional: TRADITIONAL_SCALES
|
|
106
|
-
};
|
|
107
|
-
var SYSTEMS = new Set(Object.keys(SCALES_BY_SYSTEM));
|
|
108
|
-
var DEFAULT_SYSTEM = "standard";
|
|
77
|
+
// src/cardinal.ts
|
|
109
78
|
var BASE = 10;
|
|
110
79
|
var HUNDRED = BASE * BASE;
|
|
111
|
-
var
|
|
112
|
-
var ONE = "1";
|
|
80
|
+
var BARE_ONE_EXPONENT = SCALES[0].exponent;
|
|
113
81
|
var LEADING_ZEROS = /^0+/;
|
|
114
|
-
var
|
|
82
|
+
var ONE = "1";
|
|
83
|
+
var JOINERS = {
|
|
84
|
+
number: JOINER_BETWEEN_GROUPS,
|
|
85
|
+
count: JOINER_BELOW_THOUSAND
|
|
86
|
+
};
|
|
115
87
|
function digitAt(value, place) {
|
|
116
88
|
return Math.floor(value / place) % BASE;
|
|
117
89
|
}
|
|
@@ -122,16 +94,24 @@ function belowHundredWords(unitWord, units, tens) {
|
|
|
122
94
|
if (tens === 1 && units > 0) return `${unitWord} ${JOINER_UNITS_BEFORE_TEN} ${TEN_WORDS[1]}`;
|
|
123
95
|
return [unitWord, TEN_WORDS[tens]].filter(isNotEmpty).join(` ${JOINER_BELOW_THOUSAND} `);
|
|
124
96
|
}
|
|
125
|
-
function belowThousandWords(value) {
|
|
97
|
+
function belowThousandWords({ value, role, nextJoiner }) {
|
|
126
98
|
const units = digitAt(value, 1);
|
|
127
99
|
const tens = digitAt(value, BASE);
|
|
128
100
|
const hundreds = digitAt(value, HUNDRED);
|
|
129
|
-
const
|
|
101
|
+
const joiner = role === "count" || hundreds === 1 ? JOINER_BELOW_THOUSAND : JOINER_BETWEEN_GROUPS;
|
|
102
|
+
const afterUnits = tens > 0 || hundreds > 0 ? joiner : nextJoiner;
|
|
103
|
+
const isOneBeforeAmby = units === 1 && (tens > 0 || afterUnits === JOINER_BELOW_THOUSAND);
|
|
130
104
|
const unitWord = isOneBeforeAmby ? UNIT_ONE_IN_COMPOUND : UNIT_WORDS[units];
|
|
131
105
|
const belowHundred = belowHundredWords(unitWord, units, tens);
|
|
132
|
-
const joiner = hundreds === 1 ? JOINER_BELOW_THOUSAND : JOINER_BETWEEN_GROUPS;
|
|
133
106
|
return [belowHundred, HUNDRED_WORDS[hundreds]].filter(isNotEmpty).join(` ${joiner} `);
|
|
134
107
|
}
|
|
108
|
+
var TRADITIONAL_REACH = TRADITIONAL_SCALES.reduce(
|
|
109
|
+
(most, scale) => Math.max(most, scale.exponent + scale.countDigits),
|
|
110
|
+
0
|
|
111
|
+
);
|
|
112
|
+
function scalesFor(digits, system) {
|
|
113
|
+
return system === "traditional" && digits.length <= TRADITIONAL_REACH ? TRADITIONAL_SCALES : SCALES;
|
|
114
|
+
}
|
|
135
115
|
function scaleCount(digits, scale) {
|
|
136
116
|
const end = digits.length - scale.exponent;
|
|
137
117
|
if (end <= 0) return "";
|
|
@@ -141,23 +121,64 @@ function scaleCount(digits, scale) {
|
|
|
141
121
|
function scaleWords(digits, scale, scales) {
|
|
142
122
|
const count = scaleCount(digits, scale);
|
|
143
123
|
if (count === "") return "";
|
|
144
|
-
if (count === ONE && scale.exponent ===
|
|
145
|
-
return `${
|
|
124
|
+
if (count === ONE && scale.exponent === BARE_ONE_EXPONENT) return scale.word;
|
|
125
|
+
return `${numberWords(count, scales, "count")} ${scale.word}`;
|
|
146
126
|
}
|
|
147
|
-
function
|
|
127
|
+
function numberWords(significant, scales, role) {
|
|
128
|
+
const upper = scales.map((scale) => scaleWords(significant, scale, scales)).filter(isNotEmpty);
|
|
129
|
+
const joiner = JOINERS[role];
|
|
130
|
+
const lowest = belowThousandWords({
|
|
131
|
+
value: Number(significant.slice(-3)),
|
|
132
|
+
role,
|
|
133
|
+
nextJoiner: upper.length > 0 ? joiner : ""
|
|
134
|
+
});
|
|
135
|
+
return [lowest, ...upper].filter(isNotEmpty).join(` ${joiner} `);
|
|
136
|
+
}
|
|
137
|
+
function wholeWords(digits, system) {
|
|
148
138
|
const significant = digits.replace(LEADING_ZEROS, "");
|
|
149
139
|
if (significant === "") return ZERO_WORD;
|
|
150
|
-
|
|
151
|
-
belowThousandWords(Number(significant.slice(-3))),
|
|
152
|
-
...scales.map((scale) => scaleWords(significant, scale, scales))
|
|
153
|
-
];
|
|
154
|
-
return groups.filter(isNotEmpty).join(` ${JOINER_BETWEEN_GROUPS} `);
|
|
140
|
+
return numberWords(significant, scalesFor(significant, system), "number");
|
|
155
141
|
}
|
|
156
|
-
function
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
return [...zeros, wholeWords(significant, scales)].join(" ");
|
|
142
|
+
function countWords(count, system) {
|
|
143
|
+
const scales = system === "traditional" ? TRADITIONAL_SCALES : SCALES;
|
|
144
|
+
return numberWords(count.toString(), scales, "count");
|
|
160
145
|
}
|
|
146
|
+
|
|
147
|
+
// src/generated/errors.ts
|
|
148
|
+
var SPEC_VERSION = "0.9.0";
|
|
149
|
+
var MdgError = class extends Error {
|
|
150
|
+
/** Always `'MdgError'`. */
|
|
151
|
+
name = "MdgError";
|
|
152
|
+
/** Why the call failed; the same code in every language. */
|
|
153
|
+
code;
|
|
154
|
+
/**
|
|
155
|
+
* Creates an error with a shared spec code.
|
|
156
|
+
*
|
|
157
|
+
* @param code - Shared spec error code.
|
|
158
|
+
* @param message - Human-readable explanation, in English.
|
|
159
|
+
*/
|
|
160
|
+
constructor(code, message) {
|
|
161
|
+
super(message);
|
|
162
|
+
this.code = code;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Makes `instanceof MdgError` true for errors from any mdg-utils package or build.
|
|
166
|
+
*
|
|
167
|
+
* @remarks Each package, and its ESM and CommonJS builds, has its own copy of this class,
|
|
168
|
+
* so the check looks at the error's shape instead of its prototype chain.
|
|
169
|
+
*
|
|
170
|
+
* @param value - Any value, usually a caught error.
|
|
171
|
+
* @returns `true` when `value` is an mdg-utils error.
|
|
172
|
+
*/
|
|
173
|
+
static [Symbol.hasInstance](value) {
|
|
174
|
+
return value instanceof Error && value.name === "MdgError" && "code" in value;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// src/decimal-text.ts
|
|
179
|
+
var TRAILING_ZEROS = /0+$/;
|
|
180
|
+
var DECIMAL_TEXT = /^([0-9]+)(?:\.([0-9]+))?$/;
|
|
181
|
+
var NEGATIVE_EXPONENT = /^(\d)(?:\.(\d+))?e-(\d+)$/;
|
|
161
182
|
function plainDecimal(value) {
|
|
162
183
|
const text = String(value);
|
|
163
184
|
const match = NEGATIVE_EXPONENT.exec(text);
|
|
@@ -165,35 +186,306 @@ function plainDecimal(value) {
|
|
|
165
186
|
const [, lead = "", rest = "", exponent = ""] = match;
|
|
166
187
|
return `0.${"0".repeat(Number(exponent) - 1)}${lead}${rest}`;
|
|
167
188
|
}
|
|
168
|
-
function
|
|
189
|
+
function decimalTextParts(text, caller) {
|
|
190
|
+
const match = text.length <= MAX_TEXT_LENGTH ? DECIMAL_TEXT.exec(text) : null;
|
|
191
|
+
if (match === null) {
|
|
192
|
+
throw new MdgError(
|
|
193
|
+
"INVALID_FORMAT",
|
|
194
|
+
`${caller} reads digits with an optional .decimals, at most ${String(MAX_TEXT_LENGTH)} characters.`
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
const [, whole = "", decimals = ""] = match;
|
|
198
|
+
const significantDecimals = decimals.replace(TRAILING_ZEROS, "");
|
|
199
|
+
return [whole, significantDecimals === "" ? void 0 : significantDecimals];
|
|
200
|
+
}
|
|
201
|
+
function assertSupportedNumber(value, caller) {
|
|
169
202
|
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
170
|
-
throw new MdgError("INVALID_TYPE",
|
|
203
|
+
throw new MdgError("INVALID_TYPE", `${caller} expects a finite number or decimal text.`);
|
|
171
204
|
}
|
|
172
205
|
if (value < MIN_VALUE || value > Number.MAX_SAFE_INTEGER) {
|
|
173
206
|
throw new MdgError(
|
|
174
207
|
"OUT_OF_RANGE",
|
|
175
|
-
|
|
208
|
+
`${caller} supports ${String(MIN_VALUE)} to ${String(Number.MAX_SAFE_INTEGER)}.`
|
|
176
209
|
);
|
|
177
210
|
}
|
|
178
211
|
}
|
|
179
|
-
function
|
|
212
|
+
function decimalParts(value, caller) {
|
|
213
|
+
if (typeof value === "string") return decimalTextParts(value, caller);
|
|
214
|
+
assertSupportedNumber(value, caller);
|
|
215
|
+
const [whole = "", decimals] = plainDecimal(value).split(".");
|
|
216
|
+
return [whole, decimals];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// src/system.ts
|
|
220
|
+
var SYSTEMS = ["standard", "traditional"];
|
|
221
|
+
var DEFAULT_SYSTEM = "standard";
|
|
222
|
+
function systemOf(options) {
|
|
180
223
|
if (typeof options !== "object" || options === null) {
|
|
181
|
-
throw new MdgError("INVALID_TYPE", "
|
|
224
|
+
throw new MdgError("INVALID_TYPE", "spell options must be an object.");
|
|
182
225
|
}
|
|
183
226
|
const { system = DEFAULT_SYSTEM } = options;
|
|
184
|
-
if (!SYSTEMS.
|
|
227
|
+
if (!SYSTEMS.includes(system)) {
|
|
185
228
|
throw new MdgError("INVALID_TYPE", "system is standard or traditional.");
|
|
186
229
|
}
|
|
187
|
-
return
|
|
230
|
+
return system;
|
|
188
231
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
232
|
+
|
|
233
|
+
// src/spell.ts
|
|
234
|
+
var LEADING_ZEROS2 = /^0+/;
|
|
235
|
+
function decimalWords(digits, system) {
|
|
236
|
+
const significant = digits.replace(LEADING_ZEROS2, "");
|
|
237
|
+
const zeros = Array.from({ length: digits.length - significant.length }, () => ZERO_WORD);
|
|
238
|
+
return [...zeros, wholeWords(significant, system)].join(" ");
|
|
239
|
+
}
|
|
240
|
+
function partsWords(whole, decimals, system) {
|
|
241
|
+
const wholePart = wholeWords(whole, system);
|
|
242
|
+
if (decimals === void 0) return wholePart;
|
|
243
|
+
return `${wholePart} ${DECIMAL_SEPARATOR} ${decimalWords(decimals, system)}`;
|
|
244
|
+
}
|
|
245
|
+
function spell(value, options = {}) {
|
|
246
|
+
const [whole, decimals] = decimalParts(value, "spell");
|
|
247
|
+
return partsWords(whole, decimals, systemOf(options));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// src/ordinal.ts
|
|
251
|
+
var STARTS_WITH_VOWEL = new Set(VOWELS);
|
|
252
|
+
var LEADING_ZEROS3 = /^0+/;
|
|
253
|
+
var ONE2 = "1";
|
|
254
|
+
var POINT = ".";
|
|
255
|
+
function ordinalDigits(value) {
|
|
256
|
+
if (typeof value === "string" && value.includes(POINT)) {
|
|
257
|
+
throw new MdgError("INVALID_FORMAT", "spellOrdinal reads whole digits, with no point.");
|
|
258
|
+
}
|
|
259
|
+
const [whole, decimals] = decimalParts(value, "spellOrdinal");
|
|
260
|
+
if (decimals !== void 0) {
|
|
261
|
+
throw new MdgError("INVALID_TYPE", "spellOrdinal expects a whole number.");
|
|
262
|
+
}
|
|
263
|
+
const significant = whole.replace(LEADING_ZEROS3, "");
|
|
264
|
+
if (significant === "") {
|
|
265
|
+
throw new MdgError("OUT_OF_RANGE", "Ordinals start at 1 (voalohany).");
|
|
266
|
+
}
|
|
267
|
+
return significant;
|
|
268
|
+
}
|
|
269
|
+
function spellOrdinal(value, options = {}) {
|
|
270
|
+
const digits = ordinalDigits(value);
|
|
271
|
+
const cardinal = wholeWords(digits, systemOf(options));
|
|
272
|
+
if (digits === ONE2) return ORDINAL_FIRST;
|
|
273
|
+
const prefix = STARTS_WITH_VOWEL.has(cardinal.charAt(0)) ? ORDINAL_PREFIX_BEFORE_VOWEL : ORDINAL_PREFIX;
|
|
274
|
+
return `${prefix}${cardinal}`;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// src/read-words.ts
|
|
278
|
+
var BASE2 = 10;
|
|
279
|
+
var TOKENS_PER_STEP = 2;
|
|
280
|
+
var TEN = BigInt(BASE2);
|
|
281
|
+
var HUNDRED2 = BASE2 * BASE2;
|
|
282
|
+
var MAX_READINGS = 8;
|
|
283
|
+
var PHRASE_JOINERS = /* @__PURE__ */ new Set([
|
|
284
|
+
JOINER_BELOW_THOUSAND,
|
|
285
|
+
JOINER_UNITS_BEFORE_TEN
|
|
286
|
+
]);
|
|
287
|
+
function wordValues(words, step) {
|
|
288
|
+
return words.flatMap((word, digit) => word === "" ? [] : [[word, digit * step]]);
|
|
289
|
+
}
|
|
290
|
+
var PHRASE_VALUES = new Map([
|
|
291
|
+
...wordValues(UNIT_WORDS, 1),
|
|
292
|
+
[UNIT_ONE_IN_COMPOUND, 1],
|
|
293
|
+
...wordValues(TEN_WORDS, BASE2),
|
|
294
|
+
...wordValues(HUNDRED_WORDS, HUNDRED2)
|
|
295
|
+
]);
|
|
296
|
+
var MAX_PHRASE_TOKENS = 5;
|
|
297
|
+
function groupWord(exponent, countDigits) {
|
|
298
|
+
const factor = TEN ** BigInt(exponent);
|
|
299
|
+
const maxLeftTokens = countWords(factor - BigInt(1), "standard").split(" ").length;
|
|
300
|
+
return { factor, hasPhraseCount: countDigits !== null, maxLeftTokens };
|
|
301
|
+
}
|
|
302
|
+
var GROUP_WORDS = new Map(
|
|
303
|
+
[...TRADITIONAL_SCALES, ...SCALES].map((scale) => [
|
|
304
|
+
scale.word,
|
|
305
|
+
groupWord(scale.exponent, scale.countDigits)
|
|
306
|
+
])
|
|
307
|
+
);
|
|
308
|
+
function readPhrase(tokens) {
|
|
309
|
+
let total = 0;
|
|
310
|
+
for (const [index, token] of tokens.entries()) {
|
|
311
|
+
const isJoinerPlace = index % TOKENS_PER_STEP === 1;
|
|
312
|
+
if (isJoinerPlace !== PHRASE_JOINERS.has(token)) return void 0;
|
|
313
|
+
if (!isJoinerPlace) total += PHRASE_VALUES.get(token) ?? Number.NaN;
|
|
314
|
+
}
|
|
315
|
+
return tokens.length % TOKENS_PER_STEP === 1 && !Number.isNaN(total) ? BigInt(total) : void 0;
|
|
316
|
+
}
|
|
317
|
+
function sums(lefts, rights, factor) {
|
|
318
|
+
return [...lefts].flatMap((left) => [...rights].map((right) => left + right * factor));
|
|
319
|
+
}
|
|
320
|
+
function groupSums(lefts, counts, factor) {
|
|
321
|
+
return sums(
|
|
322
|
+
[...lefts].filter((left) => left < factor),
|
|
323
|
+
counts,
|
|
324
|
+
factor
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
function limited(values) {
|
|
328
|
+
return [...new Set(values)].slice(0, MAX_READINGS);
|
|
329
|
+
}
|
|
330
|
+
var CountReader = class {
|
|
331
|
+
spans = /* @__PURE__ */ new Map();
|
|
332
|
+
tokens;
|
|
333
|
+
constructor(tokens) {
|
|
334
|
+
this.tokens = tokens;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Counts written exactly as spell writes a count before a group word.
|
|
338
|
+
*
|
|
339
|
+
* @param start - First token of the count.
|
|
340
|
+
* @param end - Token after the count.
|
|
341
|
+
* @returns The counts those tokens spell.
|
|
342
|
+
*/
|
|
343
|
+
counts(start, end) {
|
|
344
|
+
const text = this.tokens.slice(start, end).join(" ");
|
|
345
|
+
return this.values(start, end).filter(
|
|
346
|
+
(count) => SYSTEMS.some((system) => countWords(count, system) === text)
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
// Every value of tokens[start, end): a phrase, or parts joined by amby ending in a group.
|
|
350
|
+
values(start, end) {
|
|
351
|
+
const key = `${String(start)}:${String(end)}`;
|
|
352
|
+
const known = this.spans.get(key);
|
|
353
|
+
if (known !== void 0) return known;
|
|
354
|
+
const phrase = readPhrase(this.tokens.slice(start, end));
|
|
355
|
+
const found = limited([...phrase === void 0 ? [] : [phrase], ...this.grouped(start, end)]);
|
|
356
|
+
this.spans.set(key, found);
|
|
357
|
+
return found;
|
|
358
|
+
}
|
|
359
|
+
// "dimy arivo amby roa alina": whatever comes before an amby, plus a count of the last group.
|
|
360
|
+
grouped(start, end) {
|
|
361
|
+
const group = GROUP_WORDS.get(String(this.tokens[end - 1]));
|
|
362
|
+
if (group === void 0) return [];
|
|
363
|
+
const first = group.hasPhraseCount ? Math.max(start, end - 1 - MAX_PHRASE_TOKENS) : start;
|
|
364
|
+
const last = Math.min(end - 1, start + group.maxLeftTokens + 1);
|
|
365
|
+
const found = [];
|
|
366
|
+
for (let split = first; split <= last; split += 1) {
|
|
367
|
+
if (split > start && this.tokens[split - 1] !== JOINER_BELOW_THOUSAND) continue;
|
|
368
|
+
const lefts = split === start ? [BigInt(0)] : this.values(start, split - 1);
|
|
369
|
+
const counts = split === end - 1 ? [BigInt(1)] : this.counts(split, end - 1);
|
|
370
|
+
found.push(...groupSums(lefts, counts, group.factor));
|
|
371
|
+
}
|
|
372
|
+
return found;
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
function splitOn(tokens, separator) {
|
|
376
|
+
const parts = [[]];
|
|
377
|
+
for (const token of tokens) {
|
|
378
|
+
if (token === separator) parts.push([]);
|
|
379
|
+
else parts[parts.length - 1]?.push(token);
|
|
380
|
+
}
|
|
381
|
+
return parts;
|
|
382
|
+
}
|
|
383
|
+
function groupValues(tokens) {
|
|
384
|
+
const factor = GROUP_WORDS.get(String(tokens[tokens.length - 1]))?.factor;
|
|
385
|
+
if (factor === void 0) {
|
|
386
|
+
const phrase = readPhrase(tokens);
|
|
387
|
+
return phrase === void 0 ? [] : [phrase];
|
|
388
|
+
}
|
|
389
|
+
const counts = tokens.length === 1 ? [BigInt(1)] : new CountReader(tokens).counts(0, tokens.length - 1);
|
|
390
|
+
return counts.map((count) => count * factor);
|
|
391
|
+
}
|
|
392
|
+
function wholeValues(tokens) {
|
|
393
|
+
if (tokens.length === 1 && tokens[0] === ZERO_WORD) return [BigInt(0)];
|
|
394
|
+
return splitOn(tokens, JOINER_BETWEEN_GROUPS).reduce(
|
|
395
|
+
(totals, group) => limited(sums(totals, groupValues(group), BigInt(1))),
|
|
396
|
+
[BigInt(0)]
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
function decimalValues(tokens) {
|
|
400
|
+
const zeros = tokens.findIndex((token) => token !== ZERO_WORD);
|
|
401
|
+
if (zeros < 0) return [];
|
|
402
|
+
const prefix = "0".repeat(zeros);
|
|
403
|
+
return wholeValues(tokens.slice(zeros)).map((value) => `${prefix}${value.toString()}`);
|
|
404
|
+
}
|
|
405
|
+
function readWords(words) {
|
|
406
|
+
const [wholeTokens = [], decimalTokens, ...extra] = splitOn(words.split(" "), DECIMAL_SEPARATOR);
|
|
407
|
+
if (extra.length > 0) return { wholes: [], decimals: void 0 };
|
|
408
|
+
return {
|
|
409
|
+
wholes: wholeValues(wholeTokens),
|
|
410
|
+
decimals: decimalTokens === void 0 ? void 0 : decimalValues(decimalTokens)
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// src/parse.ts
|
|
415
|
+
var RESULT_KINDS = /* @__PURE__ */ new Set(["number", "text"]);
|
|
416
|
+
var OPTION_KEYS = /* @__PURE__ */ new Set(["as"]);
|
|
417
|
+
var APOSTROPHES = /[‘’ʼ`´]/g;
|
|
418
|
+
var SPACES = /\s+/gu;
|
|
419
|
+
var SPLIT_AMBINNY = /\bambin' ny\b|\bamby ny\b/g;
|
|
420
|
+
var AMBINNY = "ambin'ny";
|
|
421
|
+
var TRAILING_ZEROS2 = /0+$/;
|
|
422
|
+
function failure(code, message) {
|
|
423
|
+
return { success: false, error: new MdgError(code, message) };
|
|
424
|
+
}
|
|
425
|
+
function resultKind(options) {
|
|
426
|
+
if (typeof options !== "object" || options === null) return void 0;
|
|
427
|
+
if (Object.keys(options).some((key) => !OPTION_KEYS.has(key))) return void 0;
|
|
428
|
+
const { as = "number" } = options;
|
|
429
|
+
return RESULT_KINDS.has(as) ? as : void 0;
|
|
430
|
+
}
|
|
431
|
+
function normalise(text) {
|
|
432
|
+
return text.toLowerCase().replace(APOSTROPHES, "'").replace(SPACES, " ").trim().replace(SPLIT_AMBINNY, AMBINNY);
|
|
433
|
+
}
|
|
434
|
+
function spelledReadings(words) {
|
|
435
|
+
const { wholes, decimals } = readWords(words);
|
|
436
|
+
const texts = wholes.flatMap(
|
|
437
|
+
(whole) => (decimals ?? [""]).map((digits) => {
|
|
438
|
+
const significant = digits.replace(TRAILING_ZEROS2, "");
|
|
439
|
+
return significant === "" ? whole.toString() : `${whole.toString()}.${significant}`;
|
|
440
|
+
})
|
|
441
|
+
);
|
|
442
|
+
return [...new Set(texts)].filter((text) => {
|
|
443
|
+
const [whole = "", decimal] = text.split(".");
|
|
444
|
+
return SYSTEMS.some((system) => partsWords(whole, decimal, system) === words);
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
function readExact(words) {
|
|
448
|
+
const readings = spelledReadings(words);
|
|
449
|
+
const [only, ...others] = readings;
|
|
450
|
+
if (only === void 0) {
|
|
451
|
+
return failure("INVALID_FORMAT", "Expected Malagasy number words, as spell writes them.");
|
|
452
|
+
}
|
|
453
|
+
if (others.length > 0) {
|
|
454
|
+
return failure("AMBIGUOUS", `These words read as ${readings.join(" and as ")}.`);
|
|
455
|
+
}
|
|
456
|
+
return { success: true, data: only };
|
|
457
|
+
}
|
|
458
|
+
function asNumber(text) {
|
|
459
|
+
const value = Number(text);
|
|
460
|
+
if (value <= Number.MAX_SAFE_INTEGER && plainDecimal(value) === text) {
|
|
461
|
+
return { success: true, data: value };
|
|
462
|
+
}
|
|
463
|
+
return failure(
|
|
464
|
+
"PRECISION_LOSS",
|
|
465
|
+
`${text} does not fit a number exactly; pass { as: 'text' } for the exact digits.`
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
function safeParseWords(text, options = {}) {
|
|
469
|
+
if (typeof text !== "string") return failure("INVALID_TYPE", "Expected the words as text.");
|
|
470
|
+
const kind = resultKind(options);
|
|
471
|
+
if (kind === void 0) return failure("INVALID_TYPE", "as is 'number' or 'text'.");
|
|
472
|
+
if (text.length > MAX_WORDS_LENGTH) {
|
|
473
|
+
return failure("INVALID_FORMAT", `Words are at most ${String(MAX_WORDS_LENGTH)} characters.`);
|
|
474
|
+
}
|
|
475
|
+
const exact = readExact(normalise(text));
|
|
476
|
+
if (!exact.success || kind === "text") return exact;
|
|
477
|
+
return asNumber(exact.data);
|
|
478
|
+
}
|
|
479
|
+
function parseWords(text, options = {}) {
|
|
480
|
+
const result = safeParseWords(text, options);
|
|
481
|
+
if (!result.success) throw result.error;
|
|
482
|
+
return result.data;
|
|
483
|
+
}
|
|
484
|
+
function tryParseWords(text, options = {}) {
|
|
485
|
+
const result = safeParseWords(text, options);
|
|
486
|
+
return result.success ? result.data : void 0;
|
|
195
487
|
}
|
|
196
488
|
|
|
197
|
-
export { MdgError, SPEC_VERSION,
|
|
489
|
+
export { MdgError, SPEC_VERSION, parseWords, safeParseWords, spell, spellOrdinal, tryParseWords };
|
|
198
490
|
//# sourceMappingURL=index.js.map
|
|
199
491
|
//# sourceMappingURL=index.js.map
|