@mdgascar/words 0.9.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 +20 -20
- package/dist/index.cjs +351 -82
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +132 -6
- package/dist/index.d.ts +132 -6
- package/dist/index.js +348 -83
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -67,54 +67,23 @@ 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"];
|
|
70
74
|
var MAX_TEXT_LENGTH = 100;
|
|
75
|
+
var MAX_WORDS_LENGTH = 4096;
|
|
71
76
|
|
|
72
|
-
// src/
|
|
73
|
-
var SPEC_VERSION = "0.8.0";
|
|
74
|
-
var MdgError = class extends Error {
|
|
75
|
-
/** Always `'MdgError'`. */
|
|
76
|
-
name = "MdgError";
|
|
77
|
-
/** Why the call failed; the same code in every language. */
|
|
78
|
-
code;
|
|
79
|
-
/**
|
|
80
|
-
* Creates an error with a shared spec code.
|
|
81
|
-
*
|
|
82
|
-
* @param code - Shared spec error code.
|
|
83
|
-
* @param message - Human-readable explanation, in English.
|
|
84
|
-
*/
|
|
85
|
-
constructor(code, message) {
|
|
86
|
-
super(message);
|
|
87
|
-
this.code = code;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Makes `instanceof MdgError` true for errors from any mdg-utils package or build.
|
|
91
|
-
*
|
|
92
|
-
* @remarks Each package, and its ESM and CommonJS builds, has its own copy of this class,
|
|
93
|
-
* so the check looks at the error's shape instead of its prototype chain.
|
|
94
|
-
*
|
|
95
|
-
* @param value - Any value, usually a caught error.
|
|
96
|
-
* @returns `true` when `value` is an mdg-utils error.
|
|
97
|
-
*/
|
|
98
|
-
static [Symbol.hasInstance](value) {
|
|
99
|
-
return value instanceof Error && value.name === "MdgError" && "code" in value;
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
// src/spell.ts
|
|
104
|
-
var SCALES_BY_SYSTEM = {
|
|
105
|
-
standard: SCALES,
|
|
106
|
-
traditional: TRADITIONAL_SCALES
|
|
107
|
-
};
|
|
108
|
-
var SYSTEMS = new Set(Object.keys(SCALES_BY_SYSTEM));
|
|
109
|
-
var DEFAULT_SYSTEM = "standard";
|
|
77
|
+
// src/cardinal.ts
|
|
110
78
|
var BASE = 10;
|
|
111
79
|
var HUNDRED = BASE * BASE;
|
|
112
|
-
var
|
|
113
|
-
var ONE = "1";
|
|
80
|
+
var BARE_ONE_EXPONENT = SCALES[0].exponent;
|
|
114
81
|
var LEADING_ZEROS = /^0+/;
|
|
115
|
-
var
|
|
116
|
-
var
|
|
117
|
-
|
|
82
|
+
var ONE = "1";
|
|
83
|
+
var JOINERS = {
|
|
84
|
+
number: JOINER_BETWEEN_GROUPS,
|
|
85
|
+
count: JOINER_BELOW_THOUSAND
|
|
86
|
+
};
|
|
118
87
|
function digitAt(value, place) {
|
|
119
88
|
return Math.floor(value / place) % BASE;
|
|
120
89
|
}
|
|
@@ -125,16 +94,24 @@ function belowHundredWords(unitWord, units, tens) {
|
|
|
125
94
|
if (tens === 1 && units > 0) return `${unitWord} ${JOINER_UNITS_BEFORE_TEN} ${TEN_WORDS[1]}`;
|
|
126
95
|
return [unitWord, TEN_WORDS[tens]].filter(isNotEmpty).join(` ${JOINER_BELOW_THOUSAND} `);
|
|
127
96
|
}
|
|
128
|
-
function belowThousandWords(value) {
|
|
97
|
+
function belowThousandWords({ value, role, nextJoiner }) {
|
|
129
98
|
const units = digitAt(value, 1);
|
|
130
99
|
const tens = digitAt(value, BASE);
|
|
131
100
|
const hundreds = digitAt(value, HUNDRED);
|
|
132
|
-
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);
|
|
133
104
|
const unitWord = isOneBeforeAmby ? UNIT_ONE_IN_COMPOUND : UNIT_WORDS[units];
|
|
134
105
|
const belowHundred = belowHundredWords(unitWord, units, tens);
|
|
135
|
-
const joiner = hundreds === 1 ? JOINER_BELOW_THOUSAND : JOINER_BETWEEN_GROUPS;
|
|
136
106
|
return [belowHundred, HUNDRED_WORDS[hundreds]].filter(isNotEmpty).join(` ${joiner} `);
|
|
137
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
|
+
}
|
|
138
115
|
function scaleCount(digits, scale) {
|
|
139
116
|
const end = digits.length - scale.exponent;
|
|
140
117
|
if (end <= 0) return "";
|
|
@@ -144,30 +121,64 @@ function scaleCount(digits, scale) {
|
|
|
144
121
|
function scaleWords(digits, scale, scales) {
|
|
145
122
|
const count = scaleCount(digits, scale);
|
|
146
123
|
if (count === "") return "";
|
|
147
|
-
if (count === ONE && scale.exponent ===
|
|
148
|
-
return `${
|
|
149
|
-
}
|
|
150
|
-
function reach(scales) {
|
|
151
|
-
const largest = scales[scales.length - 1];
|
|
152
|
-
return largest?.countDigits == null ? Infinity : largest.exponent + largest.countDigits;
|
|
124
|
+
if (count === ONE && scale.exponent === BARE_ONE_EXPONENT) return scale.word;
|
|
125
|
+
return `${numberWords(count, scales, "count")} ${scale.word}`;
|
|
153
126
|
}
|
|
154
|
-
function
|
|
155
|
-
|
|
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} `);
|
|
156
136
|
}
|
|
157
|
-
function wholeWords(digits,
|
|
137
|
+
function wholeWords(digits, system) {
|
|
158
138
|
const significant = digits.replace(LEADING_ZEROS, "");
|
|
159
139
|
if (significant === "") return ZERO_WORD;
|
|
160
|
-
|
|
161
|
-
belowThousandWords(Number(significant.slice(-3))),
|
|
162
|
-
...scales.map((scale) => scaleWords(significant, scale, scales))
|
|
163
|
-
];
|
|
164
|
-
return groups.filter(isNotEmpty).join(` ${JOINER_BETWEEN_GROUPS} `);
|
|
140
|
+
return numberWords(significant, scalesFor(significant, system), "number");
|
|
165
141
|
}
|
|
166
|
-
function
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
return [...zeros, wholeWords(significant, tableFor(significant, scales))].join(" ");
|
|
142
|
+
function countWords(count, system) {
|
|
143
|
+
const scales = system === "traditional" ? TRADITIONAL_SCALES : SCALES;
|
|
144
|
+
return numberWords(count.toString(), scales, "count");
|
|
170
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+)$/;
|
|
171
182
|
function plainDecimal(value) {
|
|
172
183
|
const text = String(value);
|
|
173
184
|
const match = NEGATIVE_EXPONENT.exec(text);
|
|
@@ -175,52 +186,306 @@ function plainDecimal(value) {
|
|
|
175
186
|
const [, lead = "", rest = "", exponent = ""] = match;
|
|
176
187
|
return `0.${"0".repeat(Number(exponent) - 1)}${lead}${rest}`;
|
|
177
188
|
}
|
|
178
|
-
function decimalTextParts(text) {
|
|
189
|
+
function decimalTextParts(text, caller) {
|
|
179
190
|
const match = text.length <= MAX_TEXT_LENGTH ? DECIMAL_TEXT.exec(text) : null;
|
|
180
191
|
if (match === null) {
|
|
181
192
|
throw new MdgError(
|
|
182
193
|
"INVALID_FORMAT",
|
|
183
|
-
|
|
194
|
+
`${caller} reads digits with an optional .decimals, at most ${String(MAX_TEXT_LENGTH)} characters.`
|
|
184
195
|
);
|
|
185
196
|
}
|
|
186
197
|
const [, whole = "", decimals = ""] = match;
|
|
187
198
|
const significantDecimals = decimals.replace(TRAILING_ZEROS, "");
|
|
188
199
|
return [whole, significantDecimals === "" ? void 0 : significantDecimals];
|
|
189
200
|
}
|
|
190
|
-
function
|
|
191
|
-
assertSupportedNumber(value);
|
|
192
|
-
const [whole = "", decimals] = plainDecimal(value).split(".");
|
|
193
|
-
return [whole, decimals];
|
|
194
|
-
}
|
|
195
|
-
function assertSupportedNumber(value) {
|
|
201
|
+
function assertSupportedNumber(value, caller) {
|
|
196
202
|
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
197
|
-
throw new MdgError("INVALID_TYPE",
|
|
203
|
+
throw new MdgError("INVALID_TYPE", `${caller} expects a finite number or decimal text.`);
|
|
198
204
|
}
|
|
199
205
|
if (value < MIN_VALUE || value > Number.MAX_SAFE_INTEGER) {
|
|
200
206
|
throw new MdgError(
|
|
201
207
|
"OUT_OF_RANGE",
|
|
202
|
-
|
|
208
|
+
`${caller} supports ${String(MIN_VALUE)} to ${String(Number.MAX_SAFE_INTEGER)}.`
|
|
203
209
|
);
|
|
204
210
|
}
|
|
205
211
|
}
|
|
206
|
-
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) {
|
|
207
223
|
if (typeof options !== "object" || options === null) {
|
|
208
224
|
throw new MdgError("INVALID_TYPE", "spell options must be an object.");
|
|
209
225
|
}
|
|
210
226
|
const { system = DEFAULT_SYSTEM } = options;
|
|
211
|
-
if (!SYSTEMS.
|
|
227
|
+
if (!SYSTEMS.includes(system)) {
|
|
212
228
|
throw new MdgError("INVALID_TYPE", "system is standard or traditional.");
|
|
213
229
|
}
|
|
214
|
-
return
|
|
230
|
+
return system;
|
|
215
231
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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);
|
|
220
242
|
if (decimals === void 0) return wholePart;
|
|
221
|
-
return `${wholePart} ${DECIMAL_SEPARATOR} ${decimalWords(decimals,
|
|
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;
|
|
222
487
|
}
|
|
223
488
|
|
|
224
|
-
export { MdgError, SPEC_VERSION, spell };
|
|
489
|
+
export { MdgError, SPEC_VERSION, parseWords, safeParseWords, spell, spellOrdinal, tryParseWords };
|
|
225
490
|
//# sourceMappingURL=index.js.map
|
|
226
491
|
//# sourceMappingURL=index.js.map
|