@mdgascar/words 0.8.0 → 0.9.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 +24 -11
- package/dist/index.cjs +40 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +18 -14
- package/dist/index.d.ts +18 -14
- package/dist/index.js +40 -13
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -2,25 +2,38 @@
|
|
|
2
2
|
|
|
3
3
|
Write numbers in Malagasy words. Zero dependencies, ESM + CommonJS + types.
|
|
4
4
|
|
|
5
|
-
Implements spec 0.
|
|
5
|
+
Implements spec 0.8.0.
|
|
6
6
|
|
|
7
7
|
```sh
|
|
8
8
|
npm install @mdgascar/words
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
```ts
|
|
12
|
-
import {
|
|
12
|
+
import { spell } from '@mdgascar/words';
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
spell(21); // 'iraika amby roapolo'
|
|
15
|
+
spell(2026); // 'enina amby roapolo sy roa arivo'
|
|
16
|
+
spell(280000); // 'valo alina sy roa hetsy'
|
|
17
|
+
spell(3.14); // "telo faingo efatra ambin'ny folo"
|
|
18
|
+
spell(0.008); // 'aotra faingo aotra aotra valo'
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
Takes any number from 0 to `Number.MAX_SAFE_INTEGER` (9 007 199 254 740 991), whole or decimal.
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
Other input throws `MdgError` with code `INVALID_TYPE` or `OUT_OF_RANGE`.
|
|
23
|
+
|
|
24
|
+
## Exact digits: decimal text
|
|
25
|
+
|
|
26
|
+
A JavaScript number keeps about 15–17 significant digits, so `2_000_000_000_000_000.9` is
|
|
27
|
+
already `2000000000000001` before any function sees it. Pass the digits as text to read them
|
|
28
|
+
exactly, at any size:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
spell(2_000_000_000_000_000.9); // 'iray sy roa tapitrisa lavitrisa' (the float)
|
|
32
|
+
spell('2000000000000000.9'); // 'roa tapitrisa lavitrisa faingo sivy'
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Text is digits with an optional `.` and decimals, at most 100 characters; anything else
|
|
36
|
+
(`'1 500'`, `'-1'`, `'1e5'`) throws `INVALID_FORMAT`.
|
|
24
37
|
|
|
25
38
|
## Traditional system
|
|
26
39
|
|
|
@@ -29,8 +42,8 @@ Malagasy also has a name for every power of ten (`safatsiroa` 10⁷, `tsitambois
|
|
|
29
42
|
`system: 'traditional'`; the default is `'standard'`:
|
|
30
43
|
|
|
31
44
|
```ts
|
|
32
|
-
|
|
33
|
-
|
|
45
|
+
spell(30_000_000); // 'telopolo tapitrisa'
|
|
46
|
+
spell(30_000_000, { system: 'traditional' }); // 'telo safatsiroa'
|
|
34
47
|
```
|
|
35
48
|
|
|
36
49
|
Rules, sources and recorded disagreements: [spec/words](https://github.com/mdgascar/mdg-utils-spec/blob/dev/modules/words/README.md).
|
package/dist/index.cjs
CHANGED
|
@@ -62,16 +62,17 @@ var TRADITIONAL_SCALES = [
|
|
|
62
62
|
{ word: "tsitamboisafaharoa", exponent: 15, countDigits: 1 },
|
|
63
63
|
{ word: "lavitrisafaharoa", exponent: 16, countDigits: 1 },
|
|
64
64
|
{ word: "alinkisafaharoa", exponent: 17, countDigits: 1 },
|
|
65
|
-
{ word: "tsipesimpeseninafaharoa", exponent: 18, countDigits:
|
|
65
|
+
{ word: "tsipesimpeseninafaharoa", exponent: 18, countDigits: 1 }
|
|
66
66
|
];
|
|
67
67
|
var DECIMAL_SEPARATOR = "faingo";
|
|
68
68
|
var JOINER_UNITS_BEFORE_TEN = "ambin'ny";
|
|
69
69
|
var JOINER_BELOW_THOUSAND = "amby";
|
|
70
70
|
var JOINER_BETWEEN_GROUPS = "sy";
|
|
71
71
|
var MIN_VALUE = 0;
|
|
72
|
+
var MAX_TEXT_LENGTH = 100;
|
|
72
73
|
|
|
73
74
|
// src/generated/errors.ts
|
|
74
|
-
var SPEC_VERSION = "0.
|
|
75
|
+
var SPEC_VERSION = "0.8.0";
|
|
75
76
|
var MdgError = class extends Error {
|
|
76
77
|
/** Always `'MdgError'`. */
|
|
77
78
|
name = "MdgError";
|
|
@@ -101,7 +102,7 @@ var MdgError = class extends Error {
|
|
|
101
102
|
}
|
|
102
103
|
};
|
|
103
104
|
|
|
104
|
-
// src/
|
|
105
|
+
// src/spell.ts
|
|
105
106
|
var SCALES_BY_SYSTEM = {
|
|
106
107
|
standard: SCALES,
|
|
107
108
|
traditional: TRADITIONAL_SCALES
|
|
@@ -113,6 +114,8 @@ var HUNDRED = BASE * BASE;
|
|
|
113
114
|
var FIRST_SCALE_EXPONENT = SCALES[0].exponent;
|
|
114
115
|
var ONE = "1";
|
|
115
116
|
var LEADING_ZEROS = /^0+/;
|
|
117
|
+
var TRAILING_ZEROS = /0+$/;
|
|
118
|
+
var DECIMAL_TEXT = /^([0-9]+)(?:\.([0-9]+))?$/;
|
|
116
119
|
var NEGATIVE_EXPONENT = /^(\d)(?:\.(\d+))?e-(\d+)$/;
|
|
117
120
|
function digitAt(value, place) {
|
|
118
121
|
return Math.floor(value / place) % BASE;
|
|
@@ -146,6 +149,13 @@ function scaleWords(digits, scale, scales) {
|
|
|
146
149
|
if (count === ONE && scale.exponent === FIRST_SCALE_EXPONENT) return scale.word;
|
|
147
150
|
return `${wholeWords(count, scales)} ${scale.word}`;
|
|
148
151
|
}
|
|
152
|
+
function reach(scales) {
|
|
153
|
+
const largest = scales[scales.length - 1];
|
|
154
|
+
return largest?.countDigits == null ? Infinity : largest.exponent + largest.countDigits;
|
|
155
|
+
}
|
|
156
|
+
function tableFor(digits, scales) {
|
|
157
|
+
return digits.replace(LEADING_ZEROS, "").length > reach(scales) ? SCALES : scales;
|
|
158
|
+
}
|
|
149
159
|
function wholeWords(digits, scales) {
|
|
150
160
|
const significant = digits.replace(LEADING_ZEROS, "");
|
|
151
161
|
if (significant === "") return ZERO_WORD;
|
|
@@ -158,7 +168,7 @@ function wholeWords(digits, scales) {
|
|
|
158
168
|
function decimalWords(digits, scales) {
|
|
159
169
|
const significant = digits.replace(LEADING_ZEROS, "");
|
|
160
170
|
const zeros = Array.from({ length: digits.length - significant.length }, () => ZERO_WORD);
|
|
161
|
-
return [...zeros, wholeWords(significant, scales)].join(" ");
|
|
171
|
+
return [...zeros, wholeWords(significant, tableFor(significant, scales))].join(" ");
|
|
162
172
|
}
|
|
163
173
|
function plainDecimal(value) {
|
|
164
174
|
const text = String(value);
|
|
@@ -167,20 +177,37 @@ function plainDecimal(value) {
|
|
|
167
177
|
const [, lead = "", rest = "", exponent = ""] = match;
|
|
168
178
|
return `0.${"0".repeat(Number(exponent) - 1)}${lead}${rest}`;
|
|
169
179
|
}
|
|
180
|
+
function decimalTextParts(text) {
|
|
181
|
+
const match = text.length <= MAX_TEXT_LENGTH ? DECIMAL_TEXT.exec(text) : null;
|
|
182
|
+
if (match === null) {
|
|
183
|
+
throw new MdgError(
|
|
184
|
+
"INVALID_FORMAT",
|
|
185
|
+
`spell reads digits with an optional .decimals, at most ${String(MAX_TEXT_LENGTH)} characters.`
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
const [, whole = "", decimals = ""] = match;
|
|
189
|
+
const significantDecimals = decimals.replace(TRAILING_ZEROS, "");
|
|
190
|
+
return [whole, significantDecimals === "" ? void 0 : significantDecimals];
|
|
191
|
+
}
|
|
192
|
+
function numberParts(value) {
|
|
193
|
+
assertSupportedNumber(value);
|
|
194
|
+
const [whole = "", decimals] = plainDecimal(value).split(".");
|
|
195
|
+
return [whole, decimals];
|
|
196
|
+
}
|
|
170
197
|
function assertSupportedNumber(value) {
|
|
171
198
|
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
172
|
-
throw new MdgError("INVALID_TYPE", "
|
|
199
|
+
throw new MdgError("INVALID_TYPE", "spell expects a finite number or decimal text.");
|
|
173
200
|
}
|
|
174
201
|
if (value < MIN_VALUE || value > Number.MAX_SAFE_INTEGER) {
|
|
175
202
|
throw new MdgError(
|
|
176
203
|
"OUT_OF_RANGE",
|
|
177
|
-
`
|
|
204
|
+
`spell supports ${String(MIN_VALUE)} to ${String(Number.MAX_SAFE_INTEGER)}.`
|
|
178
205
|
);
|
|
179
206
|
}
|
|
180
207
|
}
|
|
181
208
|
function scalesFor(options) {
|
|
182
209
|
if (typeof options !== "object" || options === null) {
|
|
183
|
-
throw new MdgError("INVALID_TYPE", "
|
|
210
|
+
throw new MdgError("INVALID_TYPE", "spell options must be an object.");
|
|
184
211
|
}
|
|
185
212
|
const { system = DEFAULT_SYSTEM } = options;
|
|
186
213
|
if (!SYSTEMS.has(system)) {
|
|
@@ -188,16 +215,16 @@ function scalesFor(options) {
|
|
|
188
215
|
}
|
|
189
216
|
return SCALES_BY_SYSTEM[system];
|
|
190
217
|
}
|
|
191
|
-
function
|
|
192
|
-
|
|
218
|
+
function spell(value, options = {}) {
|
|
219
|
+
const [whole, decimals] = typeof value === "string" ? decimalTextParts(value) : numberParts(value);
|
|
193
220
|
const scales = scalesFor(options);
|
|
194
|
-
const
|
|
195
|
-
if (decimals === void 0) return
|
|
196
|
-
return `${
|
|
221
|
+
const wholePart = wholeWords(whole, tableFor(whole, scales));
|
|
222
|
+
if (decimals === void 0) return wholePart;
|
|
223
|
+
return `${wholePart} ${DECIMAL_SEPARATOR} ${decimalWords(decimals, scales)}`;
|
|
197
224
|
}
|
|
198
225
|
|
|
199
226
|
exports.MdgError = MdgError;
|
|
200
227
|
exports.SPEC_VERSION = SPEC_VERSION;
|
|
201
|
-
exports.
|
|
228
|
+
exports.spell = spell;
|
|
202
229
|
//# sourceMappingURL=index.cjs.map
|
|
203
230
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/generated/data.ts","../src/generated/errors.ts","../src/number-to-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it spec/ instead. Run `just codegen` to refresh.\n\n// From spec/modules/words/data.yaml, spec 0.7.0.\n\n/** @internal */\nexport const ZERO_WORD = 'aotra' as const;\n\n/** @internal Index = digit; index 0 is unused. */\nexport const UNIT_WORDS = [\n '',\n 'iray',\n 'roa',\n 'telo',\n 'efatra',\n 'dimy',\n 'enina',\n 'fito',\n 'valo',\n 'sivy',\n] as const;\n\n/** @internal */\nexport const UNIT_ONE_IN_COMPOUND = 'iraika' as const;\n\n/** @internal Index = tens digit. */\nexport const TEN_WORDS = [\n '',\n 'folo',\n 'roapolo',\n 'telopolo',\n 'efapolo',\n 'dimampolo',\n 'enimpolo',\n 'fitopolo',\n 'valopolo',\n 'sivifolo',\n] as const;\n\n/** @internal Index = hundreds digit. */\nexport const HUNDRED_WORDS = [\n '',\n 'zato',\n 'roanjato',\n 'telonjato',\n 'efajato',\n 'dimanjato',\n 'eninjato',\n 'fitonjato',\n 'valonjato',\n 'sivinjato',\n] as const;\n\n/** @internal Ascending; countDigits null = no limit. */\nexport const SCALES = [\n { word: 'arivo', exponent: 3, countDigits: 1 },\n { word: 'alina', exponent: 4, countDigits: 1 },\n { word: 'hetsy', exponent: 5, countDigits: 1 },\n { word: 'tapitrisa', exponent: 6, countDigits: 3 },\n { word: 'lavitrisa', exponent: 9, countDigits: null },\n] as const;\n\n/** @internal Rule 12: one name per power of ten, ascending; countDigits null = no limit. */\nexport const TRADITIONAL_SCALES = [\n { word: 'arivo', exponent: 3, countDigits: 1 },\n { word: 'alina', exponent: 4, countDigits: 1 },\n { word: 'hetsy', exponent: 5, countDigits: 1 },\n { word: 'tapitrisa', exponent: 6, countDigits: 1 },\n { word: 'safatsiroa', exponent: 7, countDigits: 1 },\n { word: 'tsitamboisa', exponent: 8, countDigits: 1 },\n { word: 'lavitrisa', exponent: 9, countDigits: 1 },\n { word: 'alinkisa', exponent: 10, countDigits: 1 },\n { word: 'tsipesimpesenina', exponent: 11, countDigits: 1 },\n { word: 'tsitokotsiforohana', exponent: 12, countDigits: 1 },\n { word: 'tsihitanoanoa', exponent: 13, countDigits: 1 },\n { word: 'safatsiroafaharoa', exponent: 14, countDigits: 1 },\n { word: 'tsitamboisafaharoa', exponent: 15, countDigits: 1 },\n { word: 'lavitrisafaharoa', exponent: 16, countDigits: 1 },\n { word: 'alinkisafaharoa', exponent: 17, countDigits: 1 },\n { word: 'tsipesimpeseninafaharoa', exponent: 18, countDigits: null },\n] as const;\n\n/** @internal */\nexport const DECIMAL_SEPARATOR = 'faingo' as const;\n\n/** @internal */\nexport const JOINER_UNITS_BEFORE_TEN = \"ambin'ny\" as const;\n\n/** @internal */\nexport const JOINER_BELOW_THOUSAND = 'amby' as const;\n\n/** @internal */\nexport const JOINER_BETWEEN_GROUPS = 'sy' as const;\n\n/** @internal */\nexport const MIN_VALUE = 0 as const;\n","// GENERATED — edit spec/ instead. Run `just codegen` to refresh.\n\n/** Version of the shared spec this code was generated from. */\nexport const SPEC_VERSION = '0.7.0';\n\n/**\n * Error code shared by every mdg-utils language port.\n *\n * - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).\n * - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.\n * - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).\n * - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to a mobile operator.\n */\nexport type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR';\n\n/**\n * Outcome of a `safeParse…` function: the value, or the error that would have been thrown.\n *\n * @example\n * ```ts\n * const result = safeParsePhoneNumber(input);\n * if (result.success) console.log(result.data);\n * else console.log(result.error.code);\n * ```\n */\nexport type SafeParseResult<T> =\n | { readonly success: true; readonly data: T }\n | { readonly success: false; readonly error: MdgError };\n\n/**\n * Error thrown by mdg-utils functions. Check `code`, which is the same in every language.\n *\n * @example\n * ```ts\n * try {\n * numberToWords(-1);\n * } catch (error) {\n * if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') {\n * // handle it\n * }\n * }\n * ```\n */\nexport class MdgError extends Error {\n /** Always `'MdgError'`. */\n override readonly name = 'MdgError';\n /** Why the call failed; the same code in every language. */\n readonly code: ErrorCode;\n\n /**\n * Creates an error with a shared spec code.\n *\n * @param code - Shared spec error code.\n * @param message - Human-readable explanation, in English.\n */\n constructor(code: ErrorCode, message: string) {\n super(message);\n this.code = code;\n }\n\n /**\n * Makes `instanceof MdgError` true for errors from any mdg-utils package or build.\n *\n * @remarks Each package, and its ESM and CommonJS builds, has its own copy of this class,\n * so the check looks at the error's shape instead of its prototype chain.\n *\n * @param value - Any value, usually a caught error.\n * @returns `true` when `value` is an mdg-utils error.\n */\n static override [Symbol.hasInstance](value: unknown): boolean {\n return value instanceof Error && value.name === 'MdgError' && 'code' in value;\n }\n}\n","import {\n DECIMAL_SEPARATOR,\n HUNDRED_WORDS,\n JOINER_BELOW_THOUSAND,\n JOINER_BETWEEN_GROUPS,\n JOINER_UNITS_BEFORE_TEN,\n MIN_VALUE,\n SCALES,\n TEN_WORDS,\n TRADITIONAL_SCALES,\n UNIT_ONE_IN_COMPOUND,\n UNIT_WORDS,\n ZERO_WORD,\n} from './generated/data.ts';\nimport { MdgError } from './generated/errors.ts';\n\ntype Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;\ntype Scale = (typeof SCALES)[number] | (typeof TRADITIONAL_SCALES)[number];\ntype ScaleTable = readonly Scale[];\n\n/**\n * How large numbers are named: `standard` (default; `telopolo tapitrisa` for 30 000 000) or\n * `traditional`, with one name per power of ten (`telo safatsiroa`).\n *\n * @example\n * ```ts\n * const system: NumberSystem = 'traditional';\n * ```\n */\nexport type NumberSystem = 'standard' | 'traditional';\n\n/**\n * Options for {@link numberToWords}.\n *\n * @example\n * ```ts\n * const options: NumberToWordsOptions = { system: 'traditional' };\n * ```\n */\nexport interface NumberToWordsOptions {\n /** `standard` (default) or `traditional`. */\n readonly system?: NumberSystem;\n}\n\nconst SCALES_BY_SYSTEM: Readonly<Record<NumberSystem, ScaleTable>> = {\n standard: SCALES,\n traditional: TRADITIONAL_SCALES,\n};\nconst SYSTEMS: ReadonlySet<unknown> = new Set(Object.keys(SCALES_BY_SYSTEM));\nconst DEFAULT_SYSTEM: NumberSystem = 'standard';\n\nconst BASE = 10;\nconst HUNDRED = BASE * BASE;\nconst BELOW_THOUSAND_DIGITS = 3;\nconst FIRST_SCALE_EXPONENT = SCALES[0].exponent;\nconst ONE = '1';\nconst LEADING_ZEROS = /^0+/;\n// String(value) of a small number, e.g. '1e-7' or '1.5e-10'.\nconst NEGATIVE_EXPONENT = /^(\\d)(?:\\.(\\d+))?e-(\\d+)$/;\n\nfunction digitAt(value: number, place: number): Digit {\n return (Math.floor(value / place) % BASE) as Digit;\n}\n\nfunction isNotEmpty(word: string): boolean {\n return word !== '';\n}\n\n// Malagasy reads upwards: units, then tens, then hundreds. 11–19 join with \"ambin'ny\", other\n// units onto tens with \"amby\"; \"iraika\" replaces \"iray\" only right before those words.\nfunction belowHundredWords(unitWord: string, units: Digit, tens: Digit): string {\n if (tens === 1 && units > 0) return `${unitWord} ${JOINER_UNITS_BEFORE_TEN} ${TEN_WORDS[1]}`;\n return [unitWord, TEN_WORDS[tens]].filter(isNotEmpty).join(` ${JOINER_BELOW_THOUSAND} `);\n}\n\n// The smaller part joins exactly 100 with \"amby\" (\"dimy amby zato\") and 200–900 with \"sy\"\n// (\"dimy sy roanjato\"), at every size of number.\nfunction belowThousandWords(value: number): string {\n const units = digitAt(value, 1);\n const tens = digitAt(value, BASE);\n const hundreds = digitAt(value, HUNDRED);\n const isOneBeforeAmby = units === 1 && (tens > 0 || hundreds === 1);\n const unitWord = isOneBeforeAmby ? UNIT_ONE_IN_COMPOUND : UNIT_WORDS[units];\n const belowHundred = belowHundredWords(unitWord, units, tens);\n const joiner = hundreds === 1 ? JOINER_BELOW_THOUSAND : JOINER_BETWEEN_GROUPS;\n return [belowHundred, HUNDRED_WORDS[hundreds]].filter(isNotEmpty).join(` ${joiner} `);\n}\n\n// The digits a group counts, without leading zeros: '' when the group is absent. The largest\n// group takes every digit left, so its count can be any size.\nfunction scaleCount(digits: string, scale: Scale): string {\n const end = digits.length - scale.exponent;\n if (end <= 0) return '';\n const start = scale.countDigits === null ? 0 : Math.max(0, end - scale.countDigits);\n return digits.slice(start, end).replace(LEADING_ZEROS, '');\n}\n\n// A count of one is bare only for the first group: \"arivo\", but \"iray alina\", \"iray tapitrisa\".\n// Counts are read with every rule, so 1000 lavitrisa is \"arivo lavitrisa\".\nfunction scaleWords(digits: string, scale: Scale, scales: ScaleTable): string {\n const count = scaleCount(digits, scale);\n if (count === '') return '';\n if (count === ONE && scale.exponent === FIRST_SCALE_EXPONENT) return scale.word;\n return `${wholeWords(count, scales)} ${scale.word}`;\n}\n\n// Works on decimal digits, not arithmetic, so counts and decimals of any length stay exact.\nfunction wholeWords(digits: string, scales: ScaleTable): string {\n const significant = digits.replace(LEADING_ZEROS, '');\n if (significant === '') return ZERO_WORD;\n const groups = [\n belowThousandWords(Number(significant.slice(-BELOW_THOUSAND_DIGITS))),\n ...scales.map((scale) => scaleWords(significant, scale, scales)),\n ];\n return groups.filter(isNotEmpty).join(` ${JOINER_BETWEEN_GROUPS} `);\n}\n\n// Each leading zero after the point is \"aotra\"; the rest reads as a whole number.\nfunction decimalWords(digits: string, scales: ScaleTable): string {\n const significant = digits.replace(LEADING_ZEROS, '');\n const zeros = Array.from({ length: digits.length - significant.length }, () => ZERO_WORD);\n return [...zeros, wholeWords(significant, scales)].join(' ');\n}\n\n// The shortest exact decimal form of the value, never in exponent notation.\nfunction plainDecimal(value: number): string {\n const text = String(value);\n const match = NEGATIVE_EXPONENT.exec(text);\n if (match === null) return text;\n const [, lead = '', rest = '', exponent = ''] = match;\n return `0.${'0'.repeat(Number(exponent) - 1)}${lead}${rest}`;\n}\n\nfunction assertSupportedNumber(value: unknown): asserts value is number {\n if (typeof value !== 'number' || !Number.isFinite(value)) {\n throw new MdgError('INVALID_TYPE', 'numberToWords expects a finite number.');\n }\n if (value < MIN_VALUE || value > Number.MAX_SAFE_INTEGER) {\n throw new MdgError(\n 'OUT_OF_RANGE',\n `numberToWords supports ${String(MIN_VALUE)} to ${String(Number.MAX_SAFE_INTEGER)}.`,\n );\n }\n}\n\nfunction scalesFor(options: unknown): ScaleTable {\n if (typeof options !== 'object' || options === null) {\n throw new MdgError('INVALID_TYPE', 'numberToWords options must be an object.');\n }\n const { system = DEFAULT_SYSTEM } = options as NumberToWordsOptions;\n if (!SYSTEMS.has(system)) {\n throw new MdgError('INVALID_TYPE', 'system is standard or traditional.');\n }\n return SCALES_BY_SYSTEM[system];\n}\n\n/**\n * Writes a number in Malagasy words, smallest part first, with `faingo` before the decimals.\n *\n * @param value - A number from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.\n * @param options - Optional `system`: `standard` (default) or `traditional`, which names every\n * power of ten (`telo safatsiroa` for 30 000 000).\n * @returns The number in Malagasy words.\n * @throws {@link MdgError} `INVALID_TYPE` if `value` is not a finite number or the options are\n * unknown, `OUT_OF_RANGE` if it is negative or above `Number.MAX_SAFE_INTEGER`.\n *\n * @example\n * ```ts\n * numberToWords(21); // 'iraika amby roapolo'\n * numberToWords(2026); // 'enina amby roapolo sy roa arivo'\n * numberToWords(3.14); // \"telo faingo efatra ambin'ny folo\"\n * numberToWords(30_000_000, { system: 'traditional' }); // 'telo safatsiroa'\n * ```\n */\nexport function numberToWords(value: number, options: NumberToWordsOptions = {}): string {\n assertSupportedNumber(value);\n const scales = scalesFor(options);\n const [whole = '', decimals] = plainDecimal(value).split('.');\n if (decimals === undefined) return wholeWords(whole, scales);\n return `${wholeWords(whole, scales)} ${DECIMAL_SEPARATOR} ${decimalWords(decimals, scales)}`;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/generated/data.ts","../src/generated/errors.ts","../src/spell.ts"],"names":[],"mappings":";;;AAKO,IAAM,SAAA,GAAY,OAAA;AAGlB,IAAM,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5E","file":"index.cjs","sourcesContent":["// GENERATED — edit spec/ instead. Run `just codegen` to refresh.\n\n// From spec/modules/words/data.yaml, spec 0.8.0.\n\n/** @internal */\nexport const ZERO_WORD = 'aotra' as const;\n\n/** @internal Index = digit; index 0 is unused. */\nexport const UNIT_WORDS = [\n '',\n 'iray',\n 'roa',\n 'telo',\n 'efatra',\n 'dimy',\n 'enina',\n 'fito',\n 'valo',\n 'sivy',\n] as const;\n\n/** @internal */\nexport const UNIT_ONE_IN_COMPOUND = 'iraika' as const;\n\n/** @internal Index = tens digit. */\nexport const TEN_WORDS = [\n '',\n 'folo',\n 'roapolo',\n 'telopolo',\n 'efapolo',\n 'dimampolo',\n 'enimpolo',\n 'fitopolo',\n 'valopolo',\n 'sivifolo',\n] as const;\n\n/** @internal Index = hundreds digit. */\nexport const HUNDRED_WORDS = [\n '',\n 'zato',\n 'roanjato',\n 'telonjato',\n 'efajato',\n 'dimanjato',\n 'eninjato',\n 'fitonjato',\n 'valonjato',\n 'sivinjato',\n] as const;\n\n/** @internal Ascending; countDigits null = no limit. */\nexport const SCALES = [\n { word: 'arivo', exponent: 3, countDigits: 1 },\n { word: 'alina', exponent: 4, countDigits: 1 },\n { word: 'hetsy', exponent: 5, countDigits: 1 },\n { word: 'tapitrisa', exponent: 6, countDigits: 3 },\n { word: 'lavitrisa', exponent: 9, countDigits: null },\n] as const;\n\n/** @internal Rule 12: one name per power of ten, ascending, each taking one digit. */\nexport const TRADITIONAL_SCALES = [\n { word: 'arivo', exponent: 3, countDigits: 1 },\n { word: 'alina', exponent: 4, countDigits: 1 },\n { word: 'hetsy', exponent: 5, countDigits: 1 },\n { word: 'tapitrisa', exponent: 6, countDigits: 1 },\n { word: 'safatsiroa', exponent: 7, countDigits: 1 },\n { word: 'tsitamboisa', exponent: 8, countDigits: 1 },\n { word: 'lavitrisa', exponent: 9, countDigits: 1 },\n { word: 'alinkisa', exponent: 10, countDigits: 1 },\n { word: 'tsipesimpesenina', exponent: 11, countDigits: 1 },\n { word: 'tsitokotsiforohana', exponent: 12, countDigits: 1 },\n { word: 'tsihitanoanoa', exponent: 13, countDigits: 1 },\n { word: 'safatsiroafaharoa', exponent: 14, countDigits: 1 },\n { word: 'tsitamboisafaharoa', exponent: 15, countDigits: 1 },\n { word: 'lavitrisafaharoa', exponent: 16, countDigits: 1 },\n { word: 'alinkisafaharoa', exponent: 17, countDigits: 1 },\n { word: 'tsipesimpeseninafaharoa', exponent: 18, countDigits: 1 },\n] as const;\n\n/** @internal */\nexport const DECIMAL_SEPARATOR = 'faingo' as const;\n\n/** @internal */\nexport const JOINER_UNITS_BEFORE_TEN = \"ambin'ny\" as const;\n\n/** @internal */\nexport const JOINER_BELOW_THOUSAND = 'amby' as const;\n\n/** @internal */\nexport const JOINER_BETWEEN_GROUPS = 'sy' as const;\n\n/** @internal */\nexport const MIN_VALUE = 0 as const;\n\n/** @internal Longest decimal text read (rule 13). */\nexport const MAX_TEXT_LENGTH = 100 as const;\n","// GENERATED — edit spec/ instead. Run `just codegen` to refresh.\n\n/** Version of the shared spec this code was generated from. */\nexport const SPEC_VERSION = '0.8.0';\n\n/**\n * Error code shared by every mdg-utils language port.\n *\n * - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).\n * - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.\n * - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).\n * - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to a mobile operator.\n */\nexport type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR';\n\n/**\n * Outcome of a `safeParse…` function: the value, or the error that would have been thrown.\n *\n * @example\n * ```ts\n * const result = safeParsePhone(input);\n * if (result.success) console.log(result.data);\n * else console.log(result.error.code);\n * ```\n */\nexport type SafeParseResult<T> =\n | { readonly success: true; readonly data: T }\n | { readonly success: false; readonly error: MdgError };\n\n/**\n * Error thrown by mdg-utils functions. Check `code`, which is the same in every language.\n *\n * @example\n * ```ts\n * try {\n * spell(-1);\n * } catch (error) {\n * if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') {\n * // handle it\n * }\n * }\n * ```\n */\nexport class MdgError extends Error {\n /** Always `'MdgError'`. */\n override readonly name = 'MdgError';\n /** Why the call failed; the same code in every language. */\n readonly code: ErrorCode;\n\n /**\n * Creates an error with a shared spec code.\n *\n * @param code - Shared spec error code.\n * @param message - Human-readable explanation, in English.\n */\n constructor(code: ErrorCode, message: string) {\n super(message);\n this.code = code;\n }\n\n /**\n * Makes `instanceof MdgError` true for errors from any mdg-utils package or build.\n *\n * @remarks Each package, and its ESM and CommonJS builds, has its own copy of this class,\n * so the check looks at the error's shape instead of its prototype chain.\n *\n * @param value - Any value, usually a caught error.\n * @returns `true` when `value` is an mdg-utils error.\n */\n static override [Symbol.hasInstance](value: unknown): boolean {\n return value instanceof Error && value.name === 'MdgError' && 'code' in value;\n }\n}\n","import {\n DECIMAL_SEPARATOR,\n HUNDRED_WORDS,\n JOINER_BELOW_THOUSAND,\n JOINER_BETWEEN_GROUPS,\n JOINER_UNITS_BEFORE_TEN,\n MAX_TEXT_LENGTH,\n MIN_VALUE,\n SCALES,\n TEN_WORDS,\n TRADITIONAL_SCALES,\n UNIT_ONE_IN_COMPOUND,\n UNIT_WORDS,\n ZERO_WORD,\n} from './generated/data.ts';\nimport { MdgError } from './generated/errors.ts';\n\ntype Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;\ntype Scale = (typeof SCALES)[number] | (typeof TRADITIONAL_SCALES)[number];\ntype ScaleTable = readonly Scale[];\n\n/**\n * How large numbers are named: `standard` (default; `telopolo tapitrisa` for 30 000 000) or\n * `traditional`, with one name per power of ten (`telo safatsiroa`).\n *\n * @example\n * ```ts\n * const system: NumberSystem = 'traditional';\n * ```\n */\nexport type NumberSystem = 'standard' | 'traditional';\n\n/**\n * Options for {@link spell}.\n *\n * @example\n * ```ts\n * const options: SpellOptions = { system: 'traditional' };\n * ```\n */\nexport interface SpellOptions {\n /** `standard` (default) or `traditional`. */\n readonly system?: NumberSystem;\n}\n\nconst SCALES_BY_SYSTEM: Readonly<Record<NumberSystem, ScaleTable>> = {\n standard: SCALES,\n traditional: TRADITIONAL_SCALES,\n};\nconst SYSTEMS: ReadonlySet<unknown> = new Set(Object.keys(SCALES_BY_SYSTEM));\nconst DEFAULT_SYSTEM: NumberSystem = 'standard';\n\nconst BASE = 10;\nconst HUNDRED = BASE * BASE;\nconst BELOW_THOUSAND_DIGITS = 3;\nconst FIRST_SCALE_EXPONENT = SCALES[0].exponent;\nconst ONE = '1';\nconst LEADING_ZEROS = /^0+/;\nconst TRAILING_ZEROS = /0+$/;\n// Rule 13: ASCII digits, then optionally a point and more digits. Nothing else.\nconst DECIMAL_TEXT = /^([0-9]+)(?:\\.([0-9]+))?$/;\n// String(value) of a small number, e.g. '1e-7' or '1.5e-10'.\nconst NEGATIVE_EXPONENT = /^(\\d)(?:\\.(\\d+))?e-(\\d+)$/;\n\nfunction digitAt(value: number, place: number): Digit {\n return (Math.floor(value / place) % BASE) as Digit;\n}\n\nfunction isNotEmpty(word: string): boolean {\n return word !== '';\n}\n\n// Malagasy reads upwards: units, then tens, then hundreds. 11–19 join with \"ambin'ny\", other\n// units onto tens with \"amby\"; \"iraika\" replaces \"iray\" only right before those words.\nfunction belowHundredWords(unitWord: string, units: Digit, tens: Digit): string {\n if (tens === 1 && units > 0) return `${unitWord} ${JOINER_UNITS_BEFORE_TEN} ${TEN_WORDS[1]}`;\n return [unitWord, TEN_WORDS[tens]].filter(isNotEmpty).join(` ${JOINER_BELOW_THOUSAND} `);\n}\n\n// The smaller part joins exactly 100 with \"amby\" (\"dimy amby zato\") and 200–900 with \"sy\"\n// (\"dimy sy roanjato\"), at every size of number.\nfunction belowThousandWords(value: number): string {\n const units = digitAt(value, 1);\n const tens = digitAt(value, BASE);\n const hundreds = digitAt(value, HUNDRED);\n const isOneBeforeAmby = units === 1 && (tens > 0 || hundreds === 1);\n const unitWord = isOneBeforeAmby ? UNIT_ONE_IN_COMPOUND : UNIT_WORDS[units];\n const belowHundred = belowHundredWords(unitWord, units, tens);\n const joiner = hundreds === 1 ? JOINER_BELOW_THOUSAND : JOINER_BETWEEN_GROUPS;\n return [belowHundred, HUNDRED_WORDS[hundreds]].filter(isNotEmpty).join(` ${joiner} `);\n}\n\n// The digits a group counts, without leading zeros: '' when the group is absent. The largest\n// group takes every digit left, so its count can be any size.\nfunction scaleCount(digits: string, scale: Scale): string {\n const end = digits.length - scale.exponent;\n if (end <= 0) return '';\n const start = scale.countDigits === null ? 0 : Math.max(0, end - scale.countDigits);\n return digits.slice(start, end).replace(LEADING_ZEROS, '');\n}\n\n// A count of one is bare only for the first group: \"arivo\", but \"iray alina\", \"iray tapitrisa\".\n// Counts are read with every rule, so 1000 lavitrisa is \"arivo lavitrisa\".\nfunction scaleWords(digits: string, scale: Scale, scales: ScaleTable): string {\n const count = scaleCount(digits, scale);\n if (count === '') return '';\n if (count === ONE && scale.exponent === FIRST_SCALE_EXPONENT) return scale.word;\n return `${wholeWords(count, scales)} ${scale.word}`;\n}\n\n// How many digits a table can name: the largest group's count width, or no limit.\nfunction reach(scales: ScaleTable): number {\n const largest = scales[scales.length - 1];\n return largest?.countDigits == null ? Infinity : largest.exponent + largest.countDigits;\n}\n\n// Rule 12: a number longer than the traditional names reach is read with the standard scales.\nfunction tableFor(digits: string, scales: ScaleTable): ScaleTable {\n return digits.replace(LEADING_ZEROS, '').length > reach(scales) ? SCALES : scales;\n}\n\n// Works on decimal digits, not arithmetic, so counts and decimals of any length stay exact.\nfunction wholeWords(digits: string, scales: ScaleTable): string {\n const significant = digits.replace(LEADING_ZEROS, '');\n if (significant === '') return ZERO_WORD;\n const groups = [\n belowThousandWords(Number(significant.slice(-BELOW_THOUSAND_DIGITS))),\n ...scales.map((scale) => scaleWords(significant, scale, scales)),\n ];\n return groups.filter(isNotEmpty).join(` ${JOINER_BETWEEN_GROUPS} `);\n}\n\n// Each leading zero after the point is \"aotra\"; the rest reads as a whole number.\nfunction decimalWords(digits: string, scales: ScaleTable): string {\n const significant = digits.replace(LEADING_ZEROS, '');\n const zeros = Array.from({ length: digits.length - significant.length }, () => ZERO_WORD);\n return [...zeros, wholeWords(significant, tableFor(significant, scales))].join(' ');\n}\n\n// The shortest exact decimal form of the value, never in exponent notation.\nfunction plainDecimal(value: number): string {\n const text = String(value);\n const match = NEGATIVE_EXPONENT.exec(text);\n if (match === null) return text;\n const [, lead = '', rest = '', exponent = ''] = match;\n return `0.${'0'.repeat(Number(exponent) - 1)}${lead}${rest}`;\n}\n\n// Rule 13: the whole digits and the decimals of plain decimal text, trailing decimal zeros\n// dropped. Checked at the edge, so the reading below trusts its digits.\nfunction decimalTextParts(text: string): readonly [string, string | undefined] {\n const match = text.length <= MAX_TEXT_LENGTH ? DECIMAL_TEXT.exec(text) : null;\n if (match === null) {\n throw new MdgError(\n 'INVALID_FORMAT',\n `spell reads digits with an optional .decimals, at most ${String(MAX_TEXT_LENGTH)} characters.`,\n );\n }\n const [, whole = '', decimals = ''] = match;\n const significantDecimals = decimals.replace(TRAILING_ZEROS, '');\n return [whole, significantDecimals === '' ? undefined : significantDecimals];\n}\n\nfunction numberParts(value: number): readonly [string, string | undefined] {\n assertSupportedNumber(value);\n const [whole = '', decimals] = plainDecimal(value).split('.');\n return [whole, decimals];\n}\n\nfunction assertSupportedNumber(value: unknown): asserts value is number {\n if (typeof value !== 'number' || !Number.isFinite(value)) {\n throw new MdgError('INVALID_TYPE', 'spell expects a finite number or decimal text.');\n }\n if (value < MIN_VALUE || value > Number.MAX_SAFE_INTEGER) {\n throw new MdgError(\n 'OUT_OF_RANGE',\n `spell supports ${String(MIN_VALUE)} to ${String(Number.MAX_SAFE_INTEGER)}.`,\n );\n }\n}\n\nfunction scalesFor(options: unknown): ScaleTable {\n if (typeof options !== 'object' || options === null) {\n throw new MdgError('INVALID_TYPE', 'spell options must be an object.');\n }\n const { system = DEFAULT_SYSTEM } = options as SpellOptions;\n if (!SYSTEMS.has(system)) {\n throw new MdgError('INVALID_TYPE', 'system is standard or traditional.');\n }\n return SCALES_BY_SYSTEM[system];\n}\n\n/**\n * Writes a number in Malagasy words, smallest part first, with `faingo` before the decimals.\n *\n * @param value - A number from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or plain\n * decimal text such as `'2000000000000000.9'`, read exactly (a number that large has already\n * lost its decimals to floating point).\n * @param options - Optional `system`: `standard` (default) or `traditional`, which names every\n * power of ten (`telo safatsiroa` for 30 000 000).\n * @returns The number in Malagasy words.\n * @throws {@link MdgError} `INVALID_TYPE` if `value` is not a finite number or the options are\n * unknown, `INVALID_FORMAT` for text that is not plain decimal (at most 100 characters),\n * `OUT_OF_RANGE` if a number is negative or above `Number.MAX_SAFE_INTEGER`.\n *\n * @example\n * ```ts\n * spell(21); // 'iraika amby roapolo'\n * spell(2026); // 'enina amby roapolo sy roa arivo'\n * spell(3.14); // \"telo faingo efatra ambin'ny folo\"\n * spell(30_000_000, { system: 'traditional' }); // 'telo safatsiroa'\n * spell('2000000000000000.9'); // 'roa tapitrisa lavitrisa faingo sivy'\n * ```\n */\nexport function spell(value: number | string, options: SpellOptions = {}): string {\n const [whole, decimals] =\n typeof value === 'string' ? decimalTextParts(value) : numberParts(value);\n const scales = scalesFor(options);\n const wholePart = wholeWords(whole, tableFor(whole, scales));\n if (decimals === undefined) return wholePart;\n return `${wholePart} ${DECIMAL_SEPARATOR} ${decimalWords(decimals, scales)}`;\n}\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -9,39 +9,43 @@
|
|
|
9
9
|
*/
|
|
10
10
|
type NumberSystem = 'standard' | 'traditional';
|
|
11
11
|
/**
|
|
12
|
-
* Options for {@link
|
|
12
|
+
* Options for {@link spell}.
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
15
|
* ```ts
|
|
16
|
-
* const options:
|
|
16
|
+
* const options: SpellOptions = { system: 'traditional' };
|
|
17
17
|
* ```
|
|
18
18
|
*/
|
|
19
|
-
interface
|
|
19
|
+
interface SpellOptions {
|
|
20
20
|
/** `standard` (default) or `traditional`. */
|
|
21
21
|
readonly system?: NumberSystem;
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
24
|
* Writes a number in Malagasy words, smallest part first, with `faingo` before the decimals.
|
|
25
25
|
*
|
|
26
|
-
* @param value - A number from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal
|
|
26
|
+
* @param value - A number from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or plain
|
|
27
|
+
* decimal text such as `'2000000000000000.9'`, read exactly (a number that large has already
|
|
28
|
+
* lost its decimals to floating point).
|
|
27
29
|
* @param options - Optional `system`: `standard` (default) or `traditional`, which names every
|
|
28
30
|
* power of ten (`telo safatsiroa` for 30 000 000).
|
|
29
31
|
* @returns The number in Malagasy words.
|
|
30
32
|
* @throws {@link MdgError} `INVALID_TYPE` if `value` is not a finite number or the options are
|
|
31
|
-
* unknown, `
|
|
33
|
+
* unknown, `INVALID_FORMAT` for text that is not plain decimal (at most 100 characters),
|
|
34
|
+
* `OUT_OF_RANGE` if a number is negative or above `Number.MAX_SAFE_INTEGER`.
|
|
32
35
|
*
|
|
33
36
|
* @example
|
|
34
37
|
* ```ts
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
38
|
+
* spell(21); // 'iraika amby roapolo'
|
|
39
|
+
* spell(2026); // 'enina amby roapolo sy roa arivo'
|
|
40
|
+
* spell(3.14); // "telo faingo efatra ambin'ny folo"
|
|
41
|
+
* spell(30_000_000, { system: 'traditional' }); // 'telo safatsiroa'
|
|
42
|
+
* spell('2000000000000000.9'); // 'roa tapitrisa lavitrisa faingo sivy'
|
|
39
43
|
* ```
|
|
40
44
|
*/
|
|
41
|
-
declare function
|
|
45
|
+
declare function spell(value: number | string, options?: SpellOptions): string;
|
|
42
46
|
|
|
43
47
|
/** Version of the shared spec this code was generated from. */
|
|
44
|
-
declare const SPEC_VERSION = "0.
|
|
48
|
+
declare const SPEC_VERSION = "0.8.0";
|
|
45
49
|
/**
|
|
46
50
|
* Error code shared by every mdg-utils language port.
|
|
47
51
|
*
|
|
@@ -57,7 +61,7 @@ type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_O
|
|
|
57
61
|
* @example
|
|
58
62
|
* ```ts
|
|
59
63
|
* try {
|
|
60
|
-
*
|
|
64
|
+
* spell(-1);
|
|
61
65
|
* } catch (error) {
|
|
62
66
|
* if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') {
|
|
63
67
|
* // handle it
|
|
@@ -89,5 +93,5 @@ declare class MdgError extends Error {
|
|
|
89
93
|
static [Symbol.hasInstance](value: unknown): boolean;
|
|
90
94
|
}
|
|
91
95
|
|
|
92
|
-
export { MdgError, SPEC_VERSION,
|
|
93
|
-
export type { ErrorCode, NumberSystem,
|
|
96
|
+
export { MdgError, SPEC_VERSION, spell };
|
|
97
|
+
export type { ErrorCode, NumberSystem, SpellOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -9,39 +9,43 @@
|
|
|
9
9
|
*/
|
|
10
10
|
type NumberSystem = 'standard' | 'traditional';
|
|
11
11
|
/**
|
|
12
|
-
* Options for {@link
|
|
12
|
+
* Options for {@link spell}.
|
|
13
13
|
*
|
|
14
14
|
* @example
|
|
15
15
|
* ```ts
|
|
16
|
-
* const options:
|
|
16
|
+
* const options: SpellOptions = { system: 'traditional' };
|
|
17
17
|
* ```
|
|
18
18
|
*/
|
|
19
|
-
interface
|
|
19
|
+
interface SpellOptions {
|
|
20
20
|
/** `standard` (default) or `traditional`. */
|
|
21
21
|
readonly system?: NumberSystem;
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
24
24
|
* Writes a number in Malagasy words, smallest part first, with `faingo` before the decimals.
|
|
25
25
|
*
|
|
26
|
-
* @param value - A number from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal
|
|
26
|
+
* @param value - A number from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or plain
|
|
27
|
+
* decimal text such as `'2000000000000000.9'`, read exactly (a number that large has already
|
|
28
|
+
* lost its decimals to floating point).
|
|
27
29
|
* @param options - Optional `system`: `standard` (default) or `traditional`, which names every
|
|
28
30
|
* power of ten (`telo safatsiroa` for 30 000 000).
|
|
29
31
|
* @returns The number in Malagasy words.
|
|
30
32
|
* @throws {@link MdgError} `INVALID_TYPE` if `value` is not a finite number or the options are
|
|
31
|
-
* unknown, `
|
|
33
|
+
* unknown, `INVALID_FORMAT` for text that is not plain decimal (at most 100 characters),
|
|
34
|
+
* `OUT_OF_RANGE` if a number is negative or above `Number.MAX_SAFE_INTEGER`.
|
|
32
35
|
*
|
|
33
36
|
* @example
|
|
34
37
|
* ```ts
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
38
|
+
* spell(21); // 'iraika amby roapolo'
|
|
39
|
+
* spell(2026); // 'enina amby roapolo sy roa arivo'
|
|
40
|
+
* spell(3.14); // "telo faingo efatra ambin'ny folo"
|
|
41
|
+
* spell(30_000_000, { system: 'traditional' }); // 'telo safatsiroa'
|
|
42
|
+
* spell('2000000000000000.9'); // 'roa tapitrisa lavitrisa faingo sivy'
|
|
39
43
|
* ```
|
|
40
44
|
*/
|
|
41
|
-
declare function
|
|
45
|
+
declare function spell(value: number | string, options?: SpellOptions): string;
|
|
42
46
|
|
|
43
47
|
/** Version of the shared spec this code was generated from. */
|
|
44
|
-
declare const SPEC_VERSION = "0.
|
|
48
|
+
declare const SPEC_VERSION = "0.8.0";
|
|
45
49
|
/**
|
|
46
50
|
* Error code shared by every mdg-utils language port.
|
|
47
51
|
*
|
|
@@ -57,7 +61,7 @@ type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_O
|
|
|
57
61
|
* @example
|
|
58
62
|
* ```ts
|
|
59
63
|
* try {
|
|
60
|
-
*
|
|
64
|
+
* spell(-1);
|
|
61
65
|
* } catch (error) {
|
|
62
66
|
* if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') {
|
|
63
67
|
* // handle it
|
|
@@ -89,5 +93,5 @@ declare class MdgError extends Error {
|
|
|
89
93
|
static [Symbol.hasInstance](value: unknown): boolean;
|
|
90
94
|
}
|
|
91
95
|
|
|
92
|
-
export { MdgError, SPEC_VERSION,
|
|
93
|
-
export type { ErrorCode, NumberSystem,
|
|
96
|
+
export { MdgError, SPEC_VERSION, spell };
|
|
97
|
+
export type { ErrorCode, NumberSystem, SpellOptions };
|
package/dist/index.js
CHANGED
|
@@ -60,16 +60,17 @@ 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 MAX_TEXT_LENGTH = 100;
|
|
70
71
|
|
|
71
72
|
// src/generated/errors.ts
|
|
72
|
-
var SPEC_VERSION = "0.
|
|
73
|
+
var SPEC_VERSION = "0.8.0";
|
|
73
74
|
var MdgError = class extends Error {
|
|
74
75
|
/** Always `'MdgError'`. */
|
|
75
76
|
name = "MdgError";
|
|
@@ -99,7 +100,7 @@ var MdgError = class extends Error {
|
|
|
99
100
|
}
|
|
100
101
|
};
|
|
101
102
|
|
|
102
|
-
// src/
|
|
103
|
+
// src/spell.ts
|
|
103
104
|
var SCALES_BY_SYSTEM = {
|
|
104
105
|
standard: SCALES,
|
|
105
106
|
traditional: TRADITIONAL_SCALES
|
|
@@ -111,6 +112,8 @@ var HUNDRED = BASE * BASE;
|
|
|
111
112
|
var FIRST_SCALE_EXPONENT = SCALES[0].exponent;
|
|
112
113
|
var ONE = "1";
|
|
113
114
|
var LEADING_ZEROS = /^0+/;
|
|
115
|
+
var TRAILING_ZEROS = /0+$/;
|
|
116
|
+
var DECIMAL_TEXT = /^([0-9]+)(?:\.([0-9]+))?$/;
|
|
114
117
|
var NEGATIVE_EXPONENT = /^(\d)(?:\.(\d+))?e-(\d+)$/;
|
|
115
118
|
function digitAt(value, place) {
|
|
116
119
|
return Math.floor(value / place) % BASE;
|
|
@@ -144,6 +147,13 @@ function scaleWords(digits, scale, scales) {
|
|
|
144
147
|
if (count === ONE && scale.exponent === FIRST_SCALE_EXPONENT) return scale.word;
|
|
145
148
|
return `${wholeWords(count, scales)} ${scale.word}`;
|
|
146
149
|
}
|
|
150
|
+
function reach(scales) {
|
|
151
|
+
const largest = scales[scales.length - 1];
|
|
152
|
+
return largest?.countDigits == null ? Infinity : largest.exponent + largest.countDigits;
|
|
153
|
+
}
|
|
154
|
+
function tableFor(digits, scales) {
|
|
155
|
+
return digits.replace(LEADING_ZEROS, "").length > reach(scales) ? SCALES : scales;
|
|
156
|
+
}
|
|
147
157
|
function wholeWords(digits, scales) {
|
|
148
158
|
const significant = digits.replace(LEADING_ZEROS, "");
|
|
149
159
|
if (significant === "") return ZERO_WORD;
|
|
@@ -156,7 +166,7 @@ function wholeWords(digits, scales) {
|
|
|
156
166
|
function decimalWords(digits, scales) {
|
|
157
167
|
const significant = digits.replace(LEADING_ZEROS, "");
|
|
158
168
|
const zeros = Array.from({ length: digits.length - significant.length }, () => ZERO_WORD);
|
|
159
|
-
return [...zeros, wholeWords(significant, scales)].join(" ");
|
|
169
|
+
return [...zeros, wholeWords(significant, tableFor(significant, scales))].join(" ");
|
|
160
170
|
}
|
|
161
171
|
function plainDecimal(value) {
|
|
162
172
|
const text = String(value);
|
|
@@ -165,20 +175,37 @@ function plainDecimal(value) {
|
|
|
165
175
|
const [, lead = "", rest = "", exponent = ""] = match;
|
|
166
176
|
return `0.${"0".repeat(Number(exponent) - 1)}${lead}${rest}`;
|
|
167
177
|
}
|
|
178
|
+
function decimalTextParts(text) {
|
|
179
|
+
const match = text.length <= MAX_TEXT_LENGTH ? DECIMAL_TEXT.exec(text) : null;
|
|
180
|
+
if (match === null) {
|
|
181
|
+
throw new MdgError(
|
|
182
|
+
"INVALID_FORMAT",
|
|
183
|
+
`spell reads digits with an optional .decimals, at most ${String(MAX_TEXT_LENGTH)} characters.`
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
const [, whole = "", decimals = ""] = match;
|
|
187
|
+
const significantDecimals = decimals.replace(TRAILING_ZEROS, "");
|
|
188
|
+
return [whole, significantDecimals === "" ? void 0 : significantDecimals];
|
|
189
|
+
}
|
|
190
|
+
function numberParts(value) {
|
|
191
|
+
assertSupportedNumber(value);
|
|
192
|
+
const [whole = "", decimals] = plainDecimal(value).split(".");
|
|
193
|
+
return [whole, decimals];
|
|
194
|
+
}
|
|
168
195
|
function assertSupportedNumber(value) {
|
|
169
196
|
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
170
|
-
throw new MdgError("INVALID_TYPE", "
|
|
197
|
+
throw new MdgError("INVALID_TYPE", "spell expects a finite number or decimal text.");
|
|
171
198
|
}
|
|
172
199
|
if (value < MIN_VALUE || value > Number.MAX_SAFE_INTEGER) {
|
|
173
200
|
throw new MdgError(
|
|
174
201
|
"OUT_OF_RANGE",
|
|
175
|
-
`
|
|
202
|
+
`spell supports ${String(MIN_VALUE)} to ${String(Number.MAX_SAFE_INTEGER)}.`
|
|
176
203
|
);
|
|
177
204
|
}
|
|
178
205
|
}
|
|
179
206
|
function scalesFor(options) {
|
|
180
207
|
if (typeof options !== "object" || options === null) {
|
|
181
|
-
throw new MdgError("INVALID_TYPE", "
|
|
208
|
+
throw new MdgError("INVALID_TYPE", "spell options must be an object.");
|
|
182
209
|
}
|
|
183
210
|
const { system = DEFAULT_SYSTEM } = options;
|
|
184
211
|
if (!SYSTEMS.has(system)) {
|
|
@@ -186,14 +213,14 @@ function scalesFor(options) {
|
|
|
186
213
|
}
|
|
187
214
|
return SCALES_BY_SYSTEM[system];
|
|
188
215
|
}
|
|
189
|
-
function
|
|
190
|
-
|
|
216
|
+
function spell(value, options = {}) {
|
|
217
|
+
const [whole, decimals] = typeof value === "string" ? decimalTextParts(value) : numberParts(value);
|
|
191
218
|
const scales = scalesFor(options);
|
|
192
|
-
const
|
|
193
|
-
if (decimals === void 0) return
|
|
194
|
-
return `${
|
|
219
|
+
const wholePart = wholeWords(whole, tableFor(whole, scales));
|
|
220
|
+
if (decimals === void 0) return wholePart;
|
|
221
|
+
return `${wholePart} ${DECIMAL_SEPARATOR} ${decimalWords(decimals, scales)}`;
|
|
195
222
|
}
|
|
196
223
|
|
|
197
|
-
export { MdgError, SPEC_VERSION,
|
|
224
|
+
export { MdgError, SPEC_VERSION, spell };
|
|
198
225
|
//# sourceMappingURL=index.js.map
|
|
199
226
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/generated/data.ts","../src/generated/errors.ts","../src/number-to-words.ts"],"names":[],"mappings":";AAKO,IAAM,SAAA,GAAY,OAAA;AAGlB,IAAM,UAAA,GAAa;AAAA,EACxB,EAAA;AAAA,EACA,MAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AAAA,EACA,QAAA;AAAA,EACA,MAAA;AAAA,EACA,OAAA;AAAA,EACA,MAAA;AAAA,EACA,MAAA;AAAA,EACA;AACF,CAAA;AAGO,IAAM,oBAAA,GAAuB,QAAA;AAG7B,IAAM,SAAA,GAAY;AAAA,EACvB,EAAA;AAAA,EACA,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A,IAAY,OAAA,KAAY,IAAA,EAAM;AACnD,IAAA,MAAM,IAAI,QAAA,CAAS,cAAA,EAAgB,0CAA0C,CAAA;AAAA,EAC/E;AACA,EAAA,MAAM,EAAE,MAAA,GAAS,cAAA,EAAe,GAAI,OAAA;AACpC,EAAA,IAAI,CAAC,OAAA,CAAQ,GAAA,CAAI,MAAM,CAAA,EAAG;AACxB,IAAA,MAAM,IAAI,QAAA,CAAS,cAAA,EAAgB,oCAAoC,CAAA;AAAA,EACzE;AACA,EAAA,OAAO,iBAAiB,MAAM,CAAA;AAChC;AAoBO,SAAS,aAAA,CAAc,KAAA,EAAe,OAAA,GAAgC,EAAC,EAAW;AACvF,EAAA,qBAAA,CAAsB,KAAK,CAAA;AAC3B,EAAA,MAAM,MAAA,GAAS,UAAU,OAAO,CAAA;AAChC,EAAA,MAAM,CAAC,QAAQ,EAAA,EAAI,QAAQ,IAAI,YAAA,CAAa,KAAK,CAAA,CAAE,KAAA,CAAM,GAAG,CAAA;AAC5D,EAAA,IAAI,QAAA,KAAa,MAAA,EAAW,OAAO,UAAA,CAAW,OAAO,MAAM,CAAA;AAC3D,EAAA,OAAO,CAAA,EAAG,UAAA,CAAW,KAAA,EAAO,MAAM,CAAC,CAAA,CAAA,EAAI,iBAAiB,CAAA,CAAA,EAAI,YAAA,CAAa,QAAA,EAAU,MAAM,CAAC,CAAA,CAAA;AAC5F","file":"index.js","sourcesContent":["// GENERATED — edit spec/ instead. Run `just codegen` to refresh.\n\n// From spec/modules/words/data.yaml, spec 0.7.0.\n\n/** @internal */\nexport const ZERO_WORD = 'aotra' as const;\n\n/** @internal Index = digit; index 0 is unused. */\nexport const UNIT_WORDS = [\n '',\n 'iray',\n 'roa',\n 'telo',\n 'efatra',\n 'dimy',\n 'enina',\n 'fito',\n 'valo',\n 'sivy',\n] as const;\n\n/** @internal */\nexport const UNIT_ONE_IN_COMPOUND = 'iraika' as const;\n\n/** @internal Index = tens digit. */\nexport const TEN_WORDS = [\n '',\n 'folo',\n 'roapolo',\n 'telopolo',\n 'efapolo',\n 'dimampolo',\n 'enimpolo',\n 'fitopolo',\n 'valopolo',\n 'sivifolo',\n] as const;\n\n/** @internal Index = hundreds digit. */\nexport const HUNDRED_WORDS = [\n '',\n 'zato',\n 'roanjato',\n 'telonjato',\n 'efajato',\n 'dimanjato',\n 'eninjato',\n 'fitonjato',\n 'valonjato',\n 'sivinjato',\n] as const;\n\n/** @internal Ascending; countDigits null = no limit. */\nexport const SCALES = [\n { word: 'arivo', exponent: 3, countDigits: 1 },\n { word: 'alina', exponent: 4, countDigits: 1 },\n { word: 'hetsy', exponent: 5, countDigits: 1 },\n { word: 'tapitrisa', exponent: 6, countDigits: 3 },\n { word: 'lavitrisa', exponent: 9, countDigits: null },\n] as const;\n\n/** @internal Rule 12: one name per power of ten, ascending; countDigits null = no limit. */\nexport const TRADITIONAL_SCALES = [\n { word: 'arivo', exponent: 3, countDigits: 1 },\n { word: 'alina', exponent: 4, countDigits: 1 },\n { word: 'hetsy', exponent: 5, countDigits: 1 },\n { word: 'tapitrisa', exponent: 6, countDigits: 1 },\n { word: 'safatsiroa', exponent: 7, countDigits: 1 },\n { word: 'tsitamboisa', exponent: 8, countDigits: 1 },\n { word: 'lavitrisa', exponent: 9, countDigits: 1 },\n { word: 'alinkisa', exponent: 10, countDigits: 1 },\n { word: 'tsipesimpesenina', exponent: 11, countDigits: 1 },\n { word: 'tsitokotsiforohana', exponent: 12, countDigits: 1 },\n { word: 'tsihitanoanoa', exponent: 13, countDigits: 1 },\n { word: 'safatsiroafaharoa', exponent: 14, countDigits: 1 },\n { word: 'tsitamboisafaharoa', exponent: 15, countDigits: 1 },\n { word: 'lavitrisafaharoa', exponent: 16, countDigits: 1 },\n { word: 'alinkisafaharoa', exponent: 17, countDigits: 1 },\n { word: 'tsipesimpeseninafaharoa', exponent: 18, countDigits: null },\n] as const;\n\n/** @internal */\nexport const DECIMAL_SEPARATOR = 'faingo' as const;\n\n/** @internal */\nexport const JOINER_UNITS_BEFORE_TEN = \"ambin'ny\" as const;\n\n/** @internal */\nexport const JOINER_BELOW_THOUSAND = 'amby' as const;\n\n/** @internal */\nexport const JOINER_BETWEEN_GROUPS = 'sy' as const;\n\n/** @internal */\nexport const MIN_VALUE = 0 as const;\n","// GENERATED — edit spec/ instead. Run `just codegen` to refresh.\n\n/** Version of the shared spec this code was generated from. */\nexport const SPEC_VERSION = '0.7.0';\n\n/**\n * Error code shared by every mdg-utils language port.\n *\n * - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).\n * - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.\n * - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).\n * - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to a mobile operator.\n */\nexport type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR';\n\n/**\n * Outcome of a `safeParse…` function: the value, or the error that would have been thrown.\n *\n * @example\n * ```ts\n * const result = safeParsePhoneNumber(input);\n * if (result.success) console.log(result.data);\n * else console.log(result.error.code);\n * ```\n */\nexport type SafeParseResult<T> =\n | { readonly success: true; readonly data: T }\n | { readonly success: false; readonly error: MdgError };\n\n/**\n * Error thrown by mdg-utils functions. Check `code`, which is the same in every language.\n *\n * @example\n * ```ts\n * try {\n * numberToWords(-1);\n * } catch (error) {\n * if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') {\n * // handle it\n * }\n * }\n * ```\n */\nexport class MdgError extends Error {\n /** Always `'MdgError'`. */\n override readonly name = 'MdgError';\n /** Why the call failed; the same code in every language. */\n readonly code: ErrorCode;\n\n /**\n * Creates an error with a shared spec code.\n *\n * @param code - Shared spec error code.\n * @param message - Human-readable explanation, in English.\n */\n constructor(code: ErrorCode, message: string) {\n super(message);\n this.code = code;\n }\n\n /**\n * Makes `instanceof MdgError` true for errors from any mdg-utils package or build.\n *\n * @remarks Each package, and its ESM and CommonJS builds, has its own copy of this class,\n * so the check looks at the error's shape instead of its prototype chain.\n *\n * @param value - Any value, usually a caught error.\n * @returns `true` when `value` is an mdg-utils error.\n */\n static override [Symbol.hasInstance](value: unknown): boolean {\n return value instanceof Error && value.name === 'MdgError' && 'code' in value;\n }\n}\n","import {\n DECIMAL_SEPARATOR,\n HUNDRED_WORDS,\n JOINER_BELOW_THOUSAND,\n JOINER_BETWEEN_GROUPS,\n JOINER_UNITS_BEFORE_TEN,\n MIN_VALUE,\n SCALES,\n TEN_WORDS,\n TRADITIONAL_SCALES,\n UNIT_ONE_IN_COMPOUND,\n UNIT_WORDS,\n ZERO_WORD,\n} from './generated/data.ts';\nimport { MdgError } from './generated/errors.ts';\n\ntype Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;\ntype Scale = (typeof SCALES)[number] | (typeof TRADITIONAL_SCALES)[number];\ntype ScaleTable = readonly Scale[];\n\n/**\n * How large numbers are named: `standard` (default; `telopolo tapitrisa` for 30 000 000) or\n * `traditional`, with one name per power of ten (`telo safatsiroa`).\n *\n * @example\n * ```ts\n * const system: NumberSystem = 'traditional';\n * ```\n */\nexport type NumberSystem = 'standard' | 'traditional';\n\n/**\n * Options for {@link numberToWords}.\n *\n * @example\n * ```ts\n * const options: NumberToWordsOptions = { system: 'traditional' };\n * ```\n */\nexport interface NumberToWordsOptions {\n /** `standard` (default) or `traditional`. */\n readonly system?: NumberSystem;\n}\n\nconst SCALES_BY_SYSTEM: Readonly<Record<NumberSystem, ScaleTable>> = {\n standard: SCALES,\n traditional: TRADITIONAL_SCALES,\n};\nconst SYSTEMS: ReadonlySet<unknown> = new Set(Object.keys(SCALES_BY_SYSTEM));\nconst DEFAULT_SYSTEM: NumberSystem = 'standard';\n\nconst BASE = 10;\nconst HUNDRED = BASE * BASE;\nconst BELOW_THOUSAND_DIGITS = 3;\nconst FIRST_SCALE_EXPONENT = SCALES[0].exponent;\nconst ONE = '1';\nconst LEADING_ZEROS = /^0+/;\n// String(value) of a small number, e.g. '1e-7' or '1.5e-10'.\nconst NEGATIVE_EXPONENT = /^(\\d)(?:\\.(\\d+))?e-(\\d+)$/;\n\nfunction digitAt(value: number, place: number): Digit {\n return (Math.floor(value / place) % BASE) as Digit;\n}\n\nfunction isNotEmpty(word: string): boolean {\n return word !== '';\n}\n\n// Malagasy reads upwards: units, then tens, then hundreds. 11–19 join with \"ambin'ny\", other\n// units onto tens with \"amby\"; \"iraika\" replaces \"iray\" only right before those words.\nfunction belowHundredWords(unitWord: string, units: Digit, tens: Digit): string {\n if (tens === 1 && units > 0) return `${unitWord} ${JOINER_UNITS_BEFORE_TEN} ${TEN_WORDS[1]}`;\n return [unitWord, TEN_WORDS[tens]].filter(isNotEmpty).join(` ${JOINER_BELOW_THOUSAND} `);\n}\n\n// The smaller part joins exactly 100 with \"amby\" (\"dimy amby zato\") and 200–900 with \"sy\"\n// (\"dimy sy roanjato\"), at every size of number.\nfunction belowThousandWords(value: number): string {\n const units = digitAt(value, 1);\n const tens = digitAt(value, BASE);\n const hundreds = digitAt(value, HUNDRED);\n const isOneBeforeAmby = units === 1 && (tens > 0 || hundreds === 1);\n const unitWord = isOneBeforeAmby ? UNIT_ONE_IN_COMPOUND : UNIT_WORDS[units];\n const belowHundred = belowHundredWords(unitWord, units, tens);\n const joiner = hundreds === 1 ? JOINER_BELOW_THOUSAND : JOINER_BETWEEN_GROUPS;\n return [belowHundred, HUNDRED_WORDS[hundreds]].filter(isNotEmpty).join(` ${joiner} `);\n}\n\n// The digits a group counts, without leading zeros: '' when the group is absent. The largest\n// group takes every digit left, so its count can be any size.\nfunction scaleCount(digits: string, scale: Scale): string {\n const end = digits.length - scale.exponent;\n if (end <= 0) return '';\n const start = scale.countDigits === null ? 0 : Math.max(0, end - scale.countDigits);\n return digits.slice(start, end).replace(LEADING_ZEROS, '');\n}\n\n// A count of one is bare only for the first group: \"arivo\", but \"iray alina\", \"iray tapitrisa\".\n// Counts are read with every rule, so 1000 lavitrisa is \"arivo lavitrisa\".\nfunction scaleWords(digits: string, scale: Scale, scales: ScaleTable): string {\n const count = scaleCount(digits, scale);\n if (count === '') return '';\n if (count === ONE && scale.exponent === FIRST_SCALE_EXPONENT) return scale.word;\n return `${wholeWords(count, scales)} ${scale.word}`;\n}\n\n// Works on decimal digits, not arithmetic, so counts and decimals of any length stay exact.\nfunction wholeWords(digits: string, scales: ScaleTable): string {\n const significant = digits.replace(LEADING_ZEROS, '');\n if (significant === '') return ZERO_WORD;\n const groups = [\n belowThousandWords(Number(significant.slice(-BELOW_THOUSAND_DIGITS))),\n ...scales.map((scale) => scaleWords(significant, scale, scales)),\n ];\n return groups.filter(isNotEmpty).join(` ${JOINER_BETWEEN_GROUPS} `);\n}\n\n// Each leading zero after the point is \"aotra\"; the rest reads as a whole number.\nfunction decimalWords(digits: string, scales: ScaleTable): string {\n const significant = digits.replace(LEADING_ZEROS, '');\n const zeros = Array.from({ length: digits.length - significant.length }, () => ZERO_WORD);\n return [...zeros, wholeWords(significant, scales)].join(' ');\n}\n\n// The shortest exact decimal form of the value, never in exponent notation.\nfunction plainDecimal(value: number): string {\n const text = String(value);\n const match = NEGATIVE_EXPONENT.exec(text);\n if (match === null) return text;\n const [, lead = '', rest = '', exponent = ''] = match;\n return `0.${'0'.repeat(Number(exponent) - 1)}${lead}${rest}`;\n}\n\nfunction assertSupportedNumber(value: unknown): asserts value is number {\n if (typeof value !== 'number' || !Number.isFinite(value)) {\n throw new MdgError('INVALID_TYPE', 'numberToWords expects a finite number.');\n }\n if (value < MIN_VALUE || value > Number.MAX_SAFE_INTEGER) {\n throw new MdgError(\n 'OUT_OF_RANGE',\n `numberToWords supports ${String(MIN_VALUE)} to ${String(Number.MAX_SAFE_INTEGER)}.`,\n );\n }\n}\n\nfunction scalesFor(options: unknown): ScaleTable {\n if (typeof options !== 'object' || options === null) {\n throw new MdgError('INVALID_TYPE', 'numberToWords options must be an object.');\n }\n const { system = DEFAULT_SYSTEM } = options as NumberToWordsOptions;\n if (!SYSTEMS.has(system)) {\n throw new MdgError('INVALID_TYPE', 'system is standard or traditional.');\n }\n return SCALES_BY_SYSTEM[system];\n}\n\n/**\n * Writes a number in Malagasy words, smallest part first, with `faingo` before the decimals.\n *\n * @param value - A number from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.\n * @param options - Optional `system`: `standard` (default) or `traditional`, which names every\n * power of ten (`telo safatsiroa` for 30 000 000).\n * @returns The number in Malagasy words.\n * @throws {@link MdgError} `INVALID_TYPE` if `value` is not a finite number or the options are\n * unknown, `OUT_OF_RANGE` if it is negative or above `Number.MAX_SAFE_INTEGER`.\n *\n * @example\n * ```ts\n * numberToWords(21); // 'iraika amby roapolo'\n * numberToWords(2026); // 'enina amby roapolo sy roa arivo'\n * numberToWords(3.14); // \"telo faingo efatra ambin'ny folo\"\n * numberToWords(30_000_000, { system: 'traditional' }); // 'telo safatsiroa'\n * ```\n */\nexport function numberToWords(value: number, options: NumberToWordsOptions = {}): string {\n assertSupportedNumber(value);\n const scales = scalesFor(options);\n const [whole = '', decimals] = plainDecimal(value).split('.');\n if (decimals === undefined) return wholeWords(whole, scales);\n return `${wholeWords(whole, scales)} ${DECIMAL_SEPARATOR} ${decimalWords(decimals, scales)}`;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/generated/data.ts","../src/generated/errors.ts","../src/spell.ts"],"names":[],"mappings":";AAKO,IAAM,SAAA,GAAY,OAAA;AAGlB,IAAM,UAAA,GAAa;AAAA,EACxB,EAAA;AAAA,EACA,MAAA;AAAA,EACA,KAAA;AAAA,EACA,MAAA;AAAA,EACA,QAAA;AAAA,EACA,MAAA;AAAA,EACA,OAAA;AAAA,EACA,MAAA;AAAA,EACA,MAAA;AAAA,EACA;AACF,CAAA;AAGO,IAAM,oBAAA,GAAuB,QAAA;AAG7B,IAAM,SAAA,GAAY;AAAA,EACvB,EAAA;AAAA,EACA,MAAA;AAAA,EACA,SAAA;AAAA,EACA,UAAA;AAAA,EACA,SAAA;AAAA,EACA,WAA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AAO,OAAA,KAAY,QAAA,IAAY,OAAA,KAAY,IAAA,EAAM;AACnD,IAAA,MAAM,IAAI,QAAA,CAAS,cAAA,EAAgB,kCAAkC,CAAA;AAAA,EACvE;AACA,EAAA,MAAM,EAAE,MAAA,GAAS,cAAA,EAAe,GAAI,OAAA;AACpC,EAAA,IAAI,CAAC,OAAA,CAAQ,GAAA,CAAI,MAAM,CAAA,EAAG;AACxB,IAAA,MAAM,IAAI,QAAA,CAAS,cAAA,EAAgB,oCAAoC,CAAA;AAAA,EACzE;AACA,EAAA,OAAO,iBAAiB,MAAM,CAAA;AAChC;AAwBO,SAAS,KAAA,CAAM,KAAA,EAAwB,OAAA,GAAwB,EAAC,EAAW;AAChF,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GACpB,OAAO,KAAA,KAAU,QAAA,GAAW,gBAAA,CAAiB,KAAK,CAAA,GAAI,WAAA,CAAY,KAAK,CAAA;AACzE,EAAA,MAAM,MAAA,GAAS,UAAU,OAAO,CAAA;AAChC,EAAA,MAAM,YAAY,UAAA,CAAW,KAAA,EAAO,QAAA,CAAS,KAAA,EAAO,MAAM,CAAC,CAAA;AAC3D,EAAA,IAAI,QAAA,KAAa,QAAW,OAAO,SAAA;AACnC,EAAA,OAAO,CAAA,EAAG,SAAS,CAAA,CAAA,EAAI,iBAAiB,IAAI,YAAA,CAAa,QAAA,EAAU,MAAM,CAAC,CAAA,CAAA;AAC5E","file":"index.js","sourcesContent":["// GENERATED — edit spec/ instead. Run `just codegen` to refresh.\n\n// From spec/modules/words/data.yaml, spec 0.8.0.\n\n/** @internal */\nexport const ZERO_WORD = 'aotra' as const;\n\n/** @internal Index = digit; index 0 is unused. */\nexport const UNIT_WORDS = [\n '',\n 'iray',\n 'roa',\n 'telo',\n 'efatra',\n 'dimy',\n 'enina',\n 'fito',\n 'valo',\n 'sivy',\n] as const;\n\n/** @internal */\nexport const UNIT_ONE_IN_COMPOUND = 'iraika' as const;\n\n/** @internal Index = tens digit. */\nexport const TEN_WORDS = [\n '',\n 'folo',\n 'roapolo',\n 'telopolo',\n 'efapolo',\n 'dimampolo',\n 'enimpolo',\n 'fitopolo',\n 'valopolo',\n 'sivifolo',\n] as const;\n\n/** @internal Index = hundreds digit. */\nexport const HUNDRED_WORDS = [\n '',\n 'zato',\n 'roanjato',\n 'telonjato',\n 'efajato',\n 'dimanjato',\n 'eninjato',\n 'fitonjato',\n 'valonjato',\n 'sivinjato',\n] as const;\n\n/** @internal Ascending; countDigits null = no limit. */\nexport const SCALES = [\n { word: 'arivo', exponent: 3, countDigits: 1 },\n { word: 'alina', exponent: 4, countDigits: 1 },\n { word: 'hetsy', exponent: 5, countDigits: 1 },\n { word: 'tapitrisa', exponent: 6, countDigits: 3 },\n { word: 'lavitrisa', exponent: 9, countDigits: null },\n] as const;\n\n/** @internal Rule 12: one name per power of ten, ascending, each taking one digit. */\nexport const TRADITIONAL_SCALES = [\n { word: 'arivo', exponent: 3, countDigits: 1 },\n { word: 'alina', exponent: 4, countDigits: 1 },\n { word: 'hetsy', exponent: 5, countDigits: 1 },\n { word: 'tapitrisa', exponent: 6, countDigits: 1 },\n { word: 'safatsiroa', exponent: 7, countDigits: 1 },\n { word: 'tsitamboisa', exponent: 8, countDigits: 1 },\n { word: 'lavitrisa', exponent: 9, countDigits: 1 },\n { word: 'alinkisa', exponent: 10, countDigits: 1 },\n { word: 'tsipesimpesenina', exponent: 11, countDigits: 1 },\n { word: 'tsitokotsiforohana', exponent: 12, countDigits: 1 },\n { word: 'tsihitanoanoa', exponent: 13, countDigits: 1 },\n { word: 'safatsiroafaharoa', exponent: 14, countDigits: 1 },\n { word: 'tsitamboisafaharoa', exponent: 15, countDigits: 1 },\n { word: 'lavitrisafaharoa', exponent: 16, countDigits: 1 },\n { word: 'alinkisafaharoa', exponent: 17, countDigits: 1 },\n { word: 'tsipesimpeseninafaharoa', exponent: 18, countDigits: 1 },\n] as const;\n\n/** @internal */\nexport const DECIMAL_SEPARATOR = 'faingo' as const;\n\n/** @internal */\nexport const JOINER_UNITS_BEFORE_TEN = \"ambin'ny\" as const;\n\n/** @internal */\nexport const JOINER_BELOW_THOUSAND = 'amby' as const;\n\n/** @internal */\nexport const JOINER_BETWEEN_GROUPS = 'sy' as const;\n\n/** @internal */\nexport const MIN_VALUE = 0 as const;\n\n/** @internal Longest decimal text read (rule 13). */\nexport const MAX_TEXT_LENGTH = 100 as const;\n","// GENERATED — edit spec/ instead. Run `just codegen` to refresh.\n\n/** Version of the shared spec this code was generated from. */\nexport const SPEC_VERSION = '0.8.0';\n\n/**\n * Error code shared by every mdg-utils language port.\n *\n * - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).\n * - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.\n * - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).\n * - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to a mobile operator.\n */\nexport type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR';\n\n/**\n * Outcome of a `safeParse…` function: the value, or the error that would have been thrown.\n *\n * @example\n * ```ts\n * const result = safeParsePhone(input);\n * if (result.success) console.log(result.data);\n * else console.log(result.error.code);\n * ```\n */\nexport type SafeParseResult<T> =\n | { readonly success: true; readonly data: T }\n | { readonly success: false; readonly error: MdgError };\n\n/**\n * Error thrown by mdg-utils functions. Check `code`, which is the same in every language.\n *\n * @example\n * ```ts\n * try {\n * spell(-1);\n * } catch (error) {\n * if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') {\n * // handle it\n * }\n * }\n * ```\n */\nexport class MdgError extends Error {\n /** Always `'MdgError'`. */\n override readonly name = 'MdgError';\n /** Why the call failed; the same code in every language. */\n readonly code: ErrorCode;\n\n /**\n * Creates an error with a shared spec code.\n *\n * @param code - Shared spec error code.\n * @param message - Human-readable explanation, in English.\n */\n constructor(code: ErrorCode, message: string) {\n super(message);\n this.code = code;\n }\n\n /**\n * Makes `instanceof MdgError` true for errors from any mdg-utils package or build.\n *\n * @remarks Each package, and its ESM and CommonJS builds, has its own copy of this class,\n * so the check looks at the error's shape instead of its prototype chain.\n *\n * @param value - Any value, usually a caught error.\n * @returns `true` when `value` is an mdg-utils error.\n */\n static override [Symbol.hasInstance](value: unknown): boolean {\n return value instanceof Error && value.name === 'MdgError' && 'code' in value;\n }\n}\n","import {\n DECIMAL_SEPARATOR,\n HUNDRED_WORDS,\n JOINER_BELOW_THOUSAND,\n JOINER_BETWEEN_GROUPS,\n JOINER_UNITS_BEFORE_TEN,\n MAX_TEXT_LENGTH,\n MIN_VALUE,\n SCALES,\n TEN_WORDS,\n TRADITIONAL_SCALES,\n UNIT_ONE_IN_COMPOUND,\n UNIT_WORDS,\n ZERO_WORD,\n} from './generated/data.ts';\nimport { MdgError } from './generated/errors.ts';\n\ntype Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;\ntype Scale = (typeof SCALES)[number] | (typeof TRADITIONAL_SCALES)[number];\ntype ScaleTable = readonly Scale[];\n\n/**\n * How large numbers are named: `standard` (default; `telopolo tapitrisa` for 30 000 000) or\n * `traditional`, with one name per power of ten (`telo safatsiroa`).\n *\n * @example\n * ```ts\n * const system: NumberSystem = 'traditional';\n * ```\n */\nexport type NumberSystem = 'standard' | 'traditional';\n\n/**\n * Options for {@link spell}.\n *\n * @example\n * ```ts\n * const options: SpellOptions = { system: 'traditional' };\n * ```\n */\nexport interface SpellOptions {\n /** `standard` (default) or `traditional`. */\n readonly system?: NumberSystem;\n}\n\nconst SCALES_BY_SYSTEM: Readonly<Record<NumberSystem, ScaleTable>> = {\n standard: SCALES,\n traditional: TRADITIONAL_SCALES,\n};\nconst SYSTEMS: ReadonlySet<unknown> = new Set(Object.keys(SCALES_BY_SYSTEM));\nconst DEFAULT_SYSTEM: NumberSystem = 'standard';\n\nconst BASE = 10;\nconst HUNDRED = BASE * BASE;\nconst BELOW_THOUSAND_DIGITS = 3;\nconst FIRST_SCALE_EXPONENT = SCALES[0].exponent;\nconst ONE = '1';\nconst LEADING_ZEROS = /^0+/;\nconst TRAILING_ZEROS = /0+$/;\n// Rule 13: ASCII digits, then optionally a point and more digits. Nothing else.\nconst DECIMAL_TEXT = /^([0-9]+)(?:\\.([0-9]+))?$/;\n// String(value) of a small number, e.g. '1e-7' or '1.5e-10'.\nconst NEGATIVE_EXPONENT = /^(\\d)(?:\\.(\\d+))?e-(\\d+)$/;\n\nfunction digitAt(value: number, place: number): Digit {\n return (Math.floor(value / place) % BASE) as Digit;\n}\n\nfunction isNotEmpty(word: string): boolean {\n return word !== '';\n}\n\n// Malagasy reads upwards: units, then tens, then hundreds. 11–19 join with \"ambin'ny\", other\n// units onto tens with \"amby\"; \"iraika\" replaces \"iray\" only right before those words.\nfunction belowHundredWords(unitWord: string, units: Digit, tens: Digit): string {\n if (tens === 1 && units > 0) return `${unitWord} ${JOINER_UNITS_BEFORE_TEN} ${TEN_WORDS[1]}`;\n return [unitWord, TEN_WORDS[tens]].filter(isNotEmpty).join(` ${JOINER_BELOW_THOUSAND} `);\n}\n\n// The smaller part joins exactly 100 with \"amby\" (\"dimy amby zato\") and 200–900 with \"sy\"\n// (\"dimy sy roanjato\"), at every size of number.\nfunction belowThousandWords(value: number): string {\n const units = digitAt(value, 1);\n const tens = digitAt(value, BASE);\n const hundreds = digitAt(value, HUNDRED);\n const isOneBeforeAmby = units === 1 && (tens > 0 || hundreds === 1);\n const unitWord = isOneBeforeAmby ? UNIT_ONE_IN_COMPOUND : UNIT_WORDS[units];\n const belowHundred = belowHundredWords(unitWord, units, tens);\n const joiner = hundreds === 1 ? JOINER_BELOW_THOUSAND : JOINER_BETWEEN_GROUPS;\n return [belowHundred, HUNDRED_WORDS[hundreds]].filter(isNotEmpty).join(` ${joiner} `);\n}\n\n// The digits a group counts, without leading zeros: '' when the group is absent. The largest\n// group takes every digit left, so its count can be any size.\nfunction scaleCount(digits: string, scale: Scale): string {\n const end = digits.length - scale.exponent;\n if (end <= 0) return '';\n const start = scale.countDigits === null ? 0 : Math.max(0, end - scale.countDigits);\n return digits.slice(start, end).replace(LEADING_ZEROS, '');\n}\n\n// A count of one is bare only for the first group: \"arivo\", but \"iray alina\", \"iray tapitrisa\".\n// Counts are read with every rule, so 1000 lavitrisa is \"arivo lavitrisa\".\nfunction scaleWords(digits: string, scale: Scale, scales: ScaleTable): string {\n const count = scaleCount(digits, scale);\n if (count === '') return '';\n if (count === ONE && scale.exponent === FIRST_SCALE_EXPONENT) return scale.word;\n return `${wholeWords(count, scales)} ${scale.word}`;\n}\n\n// How many digits a table can name: the largest group's count width, or no limit.\nfunction reach(scales: ScaleTable): number {\n const largest = scales[scales.length - 1];\n return largest?.countDigits == null ? Infinity : largest.exponent + largest.countDigits;\n}\n\n// Rule 12: a number longer than the traditional names reach is read with the standard scales.\nfunction tableFor(digits: string, scales: ScaleTable): ScaleTable {\n return digits.replace(LEADING_ZEROS, '').length > reach(scales) ? SCALES : scales;\n}\n\n// Works on decimal digits, not arithmetic, so counts and decimals of any length stay exact.\nfunction wholeWords(digits: string, scales: ScaleTable): string {\n const significant = digits.replace(LEADING_ZEROS, '');\n if (significant === '') return ZERO_WORD;\n const groups = [\n belowThousandWords(Number(significant.slice(-BELOW_THOUSAND_DIGITS))),\n ...scales.map((scale) => scaleWords(significant, scale, scales)),\n ];\n return groups.filter(isNotEmpty).join(` ${JOINER_BETWEEN_GROUPS} `);\n}\n\n// Each leading zero after the point is \"aotra\"; the rest reads as a whole number.\nfunction decimalWords(digits: string, scales: ScaleTable): string {\n const significant = digits.replace(LEADING_ZEROS, '');\n const zeros = Array.from({ length: digits.length - significant.length }, () => ZERO_WORD);\n return [...zeros, wholeWords(significant, tableFor(significant, scales))].join(' ');\n}\n\n// The shortest exact decimal form of the value, never in exponent notation.\nfunction plainDecimal(value: number): string {\n const text = String(value);\n const match = NEGATIVE_EXPONENT.exec(text);\n if (match === null) return text;\n const [, lead = '', rest = '', exponent = ''] = match;\n return `0.${'0'.repeat(Number(exponent) - 1)}${lead}${rest}`;\n}\n\n// Rule 13: the whole digits and the decimals of plain decimal text, trailing decimal zeros\n// dropped. Checked at the edge, so the reading below trusts its digits.\nfunction decimalTextParts(text: string): readonly [string, string | undefined] {\n const match = text.length <= MAX_TEXT_LENGTH ? DECIMAL_TEXT.exec(text) : null;\n if (match === null) {\n throw new MdgError(\n 'INVALID_FORMAT',\n `spell reads digits with an optional .decimals, at most ${String(MAX_TEXT_LENGTH)} characters.`,\n );\n }\n const [, whole = '', decimals = ''] = match;\n const significantDecimals = decimals.replace(TRAILING_ZEROS, '');\n return [whole, significantDecimals === '' ? undefined : significantDecimals];\n}\n\nfunction numberParts(value: number): readonly [string, string | undefined] {\n assertSupportedNumber(value);\n const [whole = '', decimals] = plainDecimal(value).split('.');\n return [whole, decimals];\n}\n\nfunction assertSupportedNumber(value: unknown): asserts value is number {\n if (typeof value !== 'number' || !Number.isFinite(value)) {\n throw new MdgError('INVALID_TYPE', 'spell expects a finite number or decimal text.');\n }\n if (value < MIN_VALUE || value > Number.MAX_SAFE_INTEGER) {\n throw new MdgError(\n 'OUT_OF_RANGE',\n `spell supports ${String(MIN_VALUE)} to ${String(Number.MAX_SAFE_INTEGER)}.`,\n );\n }\n}\n\nfunction scalesFor(options: unknown): ScaleTable {\n if (typeof options !== 'object' || options === null) {\n throw new MdgError('INVALID_TYPE', 'spell options must be an object.');\n }\n const { system = DEFAULT_SYSTEM } = options as SpellOptions;\n if (!SYSTEMS.has(system)) {\n throw new MdgError('INVALID_TYPE', 'system is standard or traditional.');\n }\n return SCALES_BY_SYSTEM[system];\n}\n\n/**\n * Writes a number in Malagasy words, smallest part first, with `faingo` before the decimals.\n *\n * @param value - A number from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or plain\n * decimal text such as `'2000000000000000.9'`, read exactly (a number that large has already\n * lost its decimals to floating point).\n * @param options - Optional `system`: `standard` (default) or `traditional`, which names every\n * power of ten (`telo safatsiroa` for 30 000 000).\n * @returns The number in Malagasy words.\n * @throws {@link MdgError} `INVALID_TYPE` if `value` is not a finite number or the options are\n * unknown, `INVALID_FORMAT` for text that is not plain decimal (at most 100 characters),\n * `OUT_OF_RANGE` if a number is negative or above `Number.MAX_SAFE_INTEGER`.\n *\n * @example\n * ```ts\n * spell(21); // 'iraika amby roapolo'\n * spell(2026); // 'enina amby roapolo sy roa arivo'\n * spell(3.14); // \"telo faingo efatra ambin'ny folo\"\n * spell(30_000_000, { system: 'traditional' }); // 'telo safatsiroa'\n * spell('2000000000000000.9'); // 'roa tapitrisa lavitrisa faingo sivy'\n * ```\n */\nexport function spell(value: number | string, options: SpellOptions = {}): string {\n const [whole, decimals] =\n typeof value === 'string' ? decimalTextParts(value) : numberParts(value);\n const scales = scalesFor(options);\n const wholePart = wholeWords(whole, tableFor(whole, scales));\n if (decimals === undefined) return wholePart;\n return `${wholePart} ${DECIMAL_SEPARATOR} ${decimalWords(decimals, scales)}`;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mdgascar/words",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Malagasy number words: 1234 → efatra amby telopolo sy roanjato sy arivo.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Mdgascar contributors",
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"malagasy",
|
|
17
17
|
"numbers",
|
|
18
18
|
"words",
|
|
19
|
-
"number-to-words"
|
|
19
|
+
"number-to-words",
|
|
20
|
+
"spell"
|
|
20
21
|
],
|
|
21
22
|
"type": "module",
|
|
22
23
|
"sideEffects": false,
|