@mdgascar/words 0.7.0 → 0.8.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 +12 -1
- package/dist/index.cjs +45 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +30 -5
- package/dist/index.d.ts +30 -5
- package/dist/index.js +45 -10
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
Write numbers in Malagasy words. Zero dependencies, ESM + CommonJS + types.
|
|
4
4
|
|
|
5
|
-
Implements spec 0.
|
|
5
|
+
Implements spec 0.7.0.
|
|
6
6
|
|
|
7
7
|
```sh
|
|
8
8
|
npm install @mdgascar/words
|
|
@@ -22,6 +22,17 @@ Takes any number from 0 to `Number.MAX_SAFE_INTEGER` (9 007 199 254 740 991), wh
|
|
|
22
22
|
Only number values are accepted: strings are not parsed. Other input throws `MdgError` with code
|
|
23
23
|
`INVALID_TYPE` or `OUT_OF_RANGE`.
|
|
24
24
|
|
|
25
|
+
## Traditional system
|
|
26
|
+
|
|
27
|
+
Malagasy also has a name for every power of ten (`safatsiroa` 10⁷, `tsitamboisa` 10⁸,
|
|
28
|
+
`lavitrisa` 10⁹, `alinkisa` 10¹⁰, …), not used in everyday writing. Ask for it with
|
|
29
|
+
`system: 'traditional'`; the default is `'standard'`:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
numberToWords(30_000_000); // 'telopolo tapitrisa'
|
|
33
|
+
numberToWords(30_000_000, { system: 'traditional' }); // 'telo safatsiroa'
|
|
34
|
+
```
|
|
35
|
+
|
|
25
36
|
Rules, sources and recorded disagreements: [spec/words](https://github.com/mdgascar/mdg-utils-spec/blob/dev/modules/words/README.md).
|
|
26
37
|
|
|
27
38
|
Docs, API reference and a live playground: [mdgascar.github.io/mdg-utils-docs/docs/words/](https://mdgascar.github.io/mdg-utils-docs/docs/words/).
|
package/dist/index.cjs
CHANGED
|
@@ -46,6 +46,24 @@ var SCALES = [
|
|
|
46
46
|
{ word: "tapitrisa", exponent: 6, countDigits: 3 },
|
|
47
47
|
{ word: "lavitrisa", exponent: 9, countDigits: null }
|
|
48
48
|
];
|
|
49
|
+
var TRADITIONAL_SCALES = [
|
|
50
|
+
{ word: "arivo", exponent: 3, countDigits: 1 },
|
|
51
|
+
{ word: "alina", exponent: 4, countDigits: 1 },
|
|
52
|
+
{ word: "hetsy", exponent: 5, countDigits: 1 },
|
|
53
|
+
{ word: "tapitrisa", exponent: 6, countDigits: 1 },
|
|
54
|
+
{ word: "safatsiroa", exponent: 7, countDigits: 1 },
|
|
55
|
+
{ word: "tsitamboisa", exponent: 8, countDigits: 1 },
|
|
56
|
+
{ word: "lavitrisa", exponent: 9, countDigits: 1 },
|
|
57
|
+
{ word: "alinkisa", exponent: 10, countDigits: 1 },
|
|
58
|
+
{ word: "tsipesimpesenina", exponent: 11, countDigits: 1 },
|
|
59
|
+
{ word: "tsitokotsiforohana", exponent: 12, countDigits: 1 },
|
|
60
|
+
{ word: "tsihitanoanoa", exponent: 13, countDigits: 1 },
|
|
61
|
+
{ word: "safatsiroafaharoa", exponent: 14, countDigits: 1 },
|
|
62
|
+
{ word: "tsitamboisafaharoa", exponent: 15, countDigits: 1 },
|
|
63
|
+
{ word: "lavitrisafaharoa", exponent: 16, countDigits: 1 },
|
|
64
|
+
{ word: "alinkisafaharoa", exponent: 17, countDigits: 1 },
|
|
65
|
+
{ word: "tsipesimpeseninafaharoa", exponent: 18, countDigits: null }
|
|
66
|
+
];
|
|
49
67
|
var DECIMAL_SEPARATOR = "faingo";
|
|
50
68
|
var JOINER_UNITS_BEFORE_TEN = "ambin'ny";
|
|
51
69
|
var JOINER_BELOW_THOUSAND = "amby";
|
|
@@ -53,7 +71,7 @@ var JOINER_BETWEEN_GROUPS = "sy";
|
|
|
53
71
|
var MIN_VALUE = 0;
|
|
54
72
|
|
|
55
73
|
// src/generated/errors.ts
|
|
56
|
-
var SPEC_VERSION = "0.
|
|
74
|
+
var SPEC_VERSION = "0.7.0";
|
|
57
75
|
var MdgError = class extends Error {
|
|
58
76
|
/** Always `'MdgError'`. */
|
|
59
77
|
name = "MdgError";
|
|
@@ -84,6 +102,12 @@ var MdgError = class extends Error {
|
|
|
84
102
|
};
|
|
85
103
|
|
|
86
104
|
// src/number-to-words.ts
|
|
105
|
+
var SCALES_BY_SYSTEM = {
|
|
106
|
+
standard: SCALES,
|
|
107
|
+
traditional: TRADITIONAL_SCALES
|
|
108
|
+
};
|
|
109
|
+
var SYSTEMS = new Set(Object.keys(SCALES_BY_SYSTEM));
|
|
110
|
+
var DEFAULT_SYSTEM = "standard";
|
|
87
111
|
var BASE = 10;
|
|
88
112
|
var HUNDRED = BASE * BASE;
|
|
89
113
|
var FIRST_SCALE_EXPONENT = SCALES[0].exponent;
|
|
@@ -116,25 +140,25 @@ function scaleCount(digits, scale) {
|
|
|
116
140
|
const start = scale.countDigits === null ? 0 : Math.max(0, end - scale.countDigits);
|
|
117
141
|
return digits.slice(start, end).replace(LEADING_ZEROS, "");
|
|
118
142
|
}
|
|
119
|
-
function scaleWords(digits, scale) {
|
|
143
|
+
function scaleWords(digits, scale, scales) {
|
|
120
144
|
const count = scaleCount(digits, scale);
|
|
121
145
|
if (count === "") return "";
|
|
122
146
|
if (count === ONE && scale.exponent === FIRST_SCALE_EXPONENT) return scale.word;
|
|
123
|
-
return `${wholeWords(count)} ${scale.word}`;
|
|
147
|
+
return `${wholeWords(count, scales)} ${scale.word}`;
|
|
124
148
|
}
|
|
125
|
-
function wholeWords(digits) {
|
|
149
|
+
function wholeWords(digits, scales) {
|
|
126
150
|
const significant = digits.replace(LEADING_ZEROS, "");
|
|
127
151
|
if (significant === "") return ZERO_WORD;
|
|
128
152
|
const groups = [
|
|
129
153
|
belowThousandWords(Number(significant.slice(-3))),
|
|
130
|
-
...
|
|
154
|
+
...scales.map((scale) => scaleWords(significant, scale, scales))
|
|
131
155
|
];
|
|
132
156
|
return groups.filter(isNotEmpty).join(` ${JOINER_BETWEEN_GROUPS} `);
|
|
133
157
|
}
|
|
134
|
-
function decimalWords(digits) {
|
|
158
|
+
function decimalWords(digits, scales) {
|
|
135
159
|
const significant = digits.replace(LEADING_ZEROS, "");
|
|
136
160
|
const zeros = Array.from({ length: digits.length - significant.length }, () => ZERO_WORD);
|
|
137
|
-
return [...zeros, wholeWords(significant)].join(" ");
|
|
161
|
+
return [...zeros, wholeWords(significant, scales)].join(" ");
|
|
138
162
|
}
|
|
139
163
|
function plainDecimal(value) {
|
|
140
164
|
const text = String(value);
|
|
@@ -154,11 +178,22 @@ function assertSupportedNumber(value) {
|
|
|
154
178
|
);
|
|
155
179
|
}
|
|
156
180
|
}
|
|
157
|
-
function
|
|
181
|
+
function scalesFor(options) {
|
|
182
|
+
if (typeof options !== "object" || options === null) {
|
|
183
|
+
throw new MdgError("INVALID_TYPE", "numberToWords options must be an object.");
|
|
184
|
+
}
|
|
185
|
+
const { system = DEFAULT_SYSTEM } = options;
|
|
186
|
+
if (!SYSTEMS.has(system)) {
|
|
187
|
+
throw new MdgError("INVALID_TYPE", "system is standard or traditional.");
|
|
188
|
+
}
|
|
189
|
+
return SCALES_BY_SYSTEM[system];
|
|
190
|
+
}
|
|
191
|
+
function numberToWords(value, options = {}) {
|
|
158
192
|
assertSupportedNumber(value);
|
|
193
|
+
const scales = scalesFor(options);
|
|
159
194
|
const [whole = "", decimals] = plainDecimal(value).split(".");
|
|
160
|
-
if (decimals === void 0) return wholeWords(whole);
|
|
161
|
-
return `${wholeWords(whole)} ${DECIMAL_SEPARATOR} ${decimalWords(decimals)}`;
|
|
195
|
+
if (decimals === void 0) return wholeWords(whole, scales);
|
|
196
|
+
return `${wholeWords(whole, scales)} ${DECIMAL_SEPARATOR} ${decimalWords(decimals, scales)}`;
|
|
162
197
|
}
|
|
163
198
|
|
|
164
199
|
exports.MdgError = MdgError;
|
package/dist/index.cjs.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,MAAA;AAAA,EACA,SAAA;AAAA,EACA,UAAA;AAAA,EACA,SAAA;AAAA,EACA,WAAA;AAAA,EACA,UAAA;AAAA,EACA,UAAA;AAAA,EACA,UAAA;AAAA,EACA;AACF,CAAA;AAGO,IAAM,aAAA,GAAgB;AAAA,EAC3B,EAAA;AAAA,EACA,MAAA;AAAA,EACA,UAAA;AAAA,EACA,WAAA;AAAA,EACA,SAAA;AAAA,EACA,WAAA;AAAA,EACA,UAAA;AAAA,EACA,WAAA;AAAA,EACA,WAAA;AAAA,EACA;AACF,CAAA;AAGO,IAAM,MAAA,GAAS;AAAA,EACpB,EAAE,IAAA,EAAM,OAAA,EAAS,QAAA,EAAU,CAAA,EAAG,aAAa,CAAA,EAAE;AAAA,EAC7C,EAAE,IAAA,EAAM,OAAA,EAAS,QAAA,EAAU,CAAA,EAAG,aAAa,CAAA,EAAE;AAAA,EAC7C,EAAE,IAAA,EAAM,OAAA,EAAS,QAAA,EAAU,CAAA,EAAG,aAAa,CAAA,EAAE;AAAA,EAC7C,EAAE,IAAA,EAAM,WAAA,EAAa,QAAA,EAAU,CAAA,EAAG,aAAa,CAAA,EAAE;AAAA,EACjD,EAAE,IAAA,EAAM,WAAA,EAAa,QAAA,EAAU,CAAA,EAAG,aAAa,IAAA;AACjD,CAAA;AAGO,IAAM,iBAAA,GAAoB,QA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YAAA,CAAa,KAAK,CAAA,CAAE,KAAA,CAAM,GAAG,CAAA;AAC5D,EAAA,IAAI,QAAA,KAAa,MAAA,EAAW,OAAO,UAAA,CAAW,KAAK,CAAA;AACnD,EAAA,OAAO,CAAA,EAAG,WAAW,KAAK,CAAC,IAAI,iBAAiB,CAAA,CAAA,EAAI,YAAA,CAAa,QAAQ,CAAC,CAAA,CAAA;AAC5E","file":"index.cjs","sourcesContent":["// GENERATED — edit spec/ instead. Run `just codegen` to refresh.\n\n// From spec/modules/words/data.yaml, spec 0.6.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 */\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.6.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 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];\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): 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)} ${scale.word}`;\n}\n\n// Works on decimal digits, not arithmetic, so counts and decimals of any length stay exact.\nfunction wholeWords(digits: string): 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)),\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): 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)].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\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 * @returns The number in Malagasy words.\n * @throws {@link MdgError} `INVALID_TYPE` if `value` is not a finite number,\n * `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 * ```\n */\nexport function numberToWords(value: number): string {\n assertSupportedNumber(value);\n const [whole = '', decimals] = plainDecimal(value).split('.');\n if (decimals === undefined) return wholeWords(whole);\n return `${wholeWords(whole)} ${DECIMAL_SEPARATOR} ${decimalWords(decimals)}`;\n}\n"]}
|
|
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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EACF;AACF;AAEA,SAAS,UAAU,OAAA,EAA8B;AAC/C,EAAA,IAAI,OAAO,OAAA,KAAY,QAAA,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.cjs","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"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,22 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How large numbers are named: `standard` (default; `telopolo tapitrisa` for 30 000 000) or
|
|
3
|
+
* `traditional`, with one name per power of ten (`telo safatsiroa`).
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const system: NumberSystem = 'traditional';
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
type NumberSystem = 'standard' | 'traditional';
|
|
11
|
+
/**
|
|
12
|
+
* Options for {@link numberToWords}.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const options: NumberToWordsOptions = { system: 'traditional' };
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
interface NumberToWordsOptions {
|
|
20
|
+
/** `standard` (default) or `traditional`. */
|
|
21
|
+
readonly system?: NumberSystem;
|
|
22
|
+
}
|
|
1
23
|
/**
|
|
2
24
|
* Writes a number in Malagasy words, smallest part first, with `faingo` before the decimals.
|
|
3
25
|
*
|
|
4
26
|
* @param value - A number from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
27
|
+
* @param options - Optional `system`: `standard` (default) or `traditional`, which names every
|
|
28
|
+
* power of ten (`telo safatsiroa` for 30 000 000).
|
|
5
29
|
* @returns The number in Malagasy words.
|
|
6
|
-
* @throws {@link MdgError} `INVALID_TYPE` if `value` is not a finite number
|
|
7
|
-
* `OUT_OF_RANGE` if it is negative or above `Number.MAX_SAFE_INTEGER`.
|
|
30
|
+
* @throws {@link MdgError} `INVALID_TYPE` if `value` is not a finite number or the options are
|
|
31
|
+
* unknown, `OUT_OF_RANGE` if it is negative or above `Number.MAX_SAFE_INTEGER`.
|
|
8
32
|
*
|
|
9
33
|
* @example
|
|
10
34
|
* ```ts
|
|
11
35
|
* numberToWords(21); // 'iraika amby roapolo'
|
|
12
36
|
* numberToWords(2026); // 'enina amby roapolo sy roa arivo'
|
|
13
37
|
* numberToWords(3.14); // "telo faingo efatra ambin'ny folo"
|
|
38
|
+
* numberToWords(30_000_000, { system: 'traditional' }); // 'telo safatsiroa'
|
|
14
39
|
* ```
|
|
15
40
|
*/
|
|
16
|
-
declare function numberToWords(value: number): string;
|
|
41
|
+
declare function numberToWords(value: number, options?: NumberToWordsOptions): string;
|
|
17
42
|
|
|
18
43
|
/** Version of the shared spec this code was generated from. */
|
|
19
|
-
declare const SPEC_VERSION = "0.
|
|
44
|
+
declare const SPEC_VERSION = "0.7.0";
|
|
20
45
|
/**
|
|
21
46
|
* Error code shared by every mdg-utils language port.
|
|
22
47
|
*
|
|
@@ -65,4 +90,4 @@ declare class MdgError extends Error {
|
|
|
65
90
|
}
|
|
66
91
|
|
|
67
92
|
export { MdgError, SPEC_VERSION, numberToWords };
|
|
68
|
-
export type { ErrorCode };
|
|
93
|
+
export type { ErrorCode, NumberSystem, NumberToWordsOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,22 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How large numbers are named: `standard` (default; `telopolo tapitrisa` for 30 000 000) or
|
|
3
|
+
* `traditional`, with one name per power of ten (`telo safatsiroa`).
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const system: NumberSystem = 'traditional';
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
type NumberSystem = 'standard' | 'traditional';
|
|
11
|
+
/**
|
|
12
|
+
* Options for {@link numberToWords}.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const options: NumberToWordsOptions = { system: 'traditional' };
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
interface NumberToWordsOptions {
|
|
20
|
+
/** `standard` (default) or `traditional`. */
|
|
21
|
+
readonly system?: NumberSystem;
|
|
22
|
+
}
|
|
1
23
|
/**
|
|
2
24
|
* Writes a number in Malagasy words, smallest part first, with `faingo` before the decimals.
|
|
3
25
|
*
|
|
4
26
|
* @param value - A number from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
27
|
+
* @param options - Optional `system`: `standard` (default) or `traditional`, which names every
|
|
28
|
+
* power of ten (`telo safatsiroa` for 30 000 000).
|
|
5
29
|
* @returns The number in Malagasy words.
|
|
6
|
-
* @throws {@link MdgError} `INVALID_TYPE` if `value` is not a finite number
|
|
7
|
-
* `OUT_OF_RANGE` if it is negative or above `Number.MAX_SAFE_INTEGER`.
|
|
30
|
+
* @throws {@link MdgError} `INVALID_TYPE` if `value` is not a finite number or the options are
|
|
31
|
+
* unknown, `OUT_OF_RANGE` if it is negative or above `Number.MAX_SAFE_INTEGER`.
|
|
8
32
|
*
|
|
9
33
|
* @example
|
|
10
34
|
* ```ts
|
|
11
35
|
* numberToWords(21); // 'iraika amby roapolo'
|
|
12
36
|
* numberToWords(2026); // 'enina amby roapolo sy roa arivo'
|
|
13
37
|
* numberToWords(3.14); // "telo faingo efatra ambin'ny folo"
|
|
38
|
+
* numberToWords(30_000_000, { system: 'traditional' }); // 'telo safatsiroa'
|
|
14
39
|
* ```
|
|
15
40
|
*/
|
|
16
|
-
declare function numberToWords(value: number): string;
|
|
41
|
+
declare function numberToWords(value: number, options?: NumberToWordsOptions): string;
|
|
17
42
|
|
|
18
43
|
/** Version of the shared spec this code was generated from. */
|
|
19
|
-
declare const SPEC_VERSION = "0.
|
|
44
|
+
declare const SPEC_VERSION = "0.7.0";
|
|
20
45
|
/**
|
|
21
46
|
* Error code shared by every mdg-utils language port.
|
|
22
47
|
*
|
|
@@ -65,4 +90,4 @@ declare class MdgError extends Error {
|
|
|
65
90
|
}
|
|
66
91
|
|
|
67
92
|
export { MdgError, SPEC_VERSION, numberToWords };
|
|
68
|
-
export type { ErrorCode };
|
|
93
|
+
export type { ErrorCode, NumberSystem, NumberToWordsOptions };
|
package/dist/index.js
CHANGED
|
@@ -44,6 +44,24 @@ var SCALES = [
|
|
|
44
44
|
{ word: "tapitrisa", exponent: 6, countDigits: 3 },
|
|
45
45
|
{ word: "lavitrisa", exponent: 9, countDigits: null }
|
|
46
46
|
];
|
|
47
|
+
var TRADITIONAL_SCALES = [
|
|
48
|
+
{ word: "arivo", exponent: 3, countDigits: 1 },
|
|
49
|
+
{ word: "alina", exponent: 4, countDigits: 1 },
|
|
50
|
+
{ word: "hetsy", exponent: 5, countDigits: 1 },
|
|
51
|
+
{ word: "tapitrisa", exponent: 6, countDigits: 1 },
|
|
52
|
+
{ word: "safatsiroa", exponent: 7, countDigits: 1 },
|
|
53
|
+
{ word: "tsitamboisa", exponent: 8, countDigits: 1 },
|
|
54
|
+
{ word: "lavitrisa", exponent: 9, countDigits: 1 },
|
|
55
|
+
{ word: "alinkisa", exponent: 10, countDigits: 1 },
|
|
56
|
+
{ word: "tsipesimpesenina", exponent: 11, countDigits: 1 },
|
|
57
|
+
{ word: "tsitokotsiforohana", exponent: 12, countDigits: 1 },
|
|
58
|
+
{ word: "tsihitanoanoa", exponent: 13, countDigits: 1 },
|
|
59
|
+
{ word: "safatsiroafaharoa", exponent: 14, countDigits: 1 },
|
|
60
|
+
{ word: "tsitamboisafaharoa", exponent: 15, countDigits: 1 },
|
|
61
|
+
{ word: "lavitrisafaharoa", exponent: 16, countDigits: 1 },
|
|
62
|
+
{ word: "alinkisafaharoa", exponent: 17, countDigits: 1 },
|
|
63
|
+
{ word: "tsipesimpeseninafaharoa", exponent: 18, countDigits: null }
|
|
64
|
+
];
|
|
47
65
|
var DECIMAL_SEPARATOR = "faingo";
|
|
48
66
|
var JOINER_UNITS_BEFORE_TEN = "ambin'ny";
|
|
49
67
|
var JOINER_BELOW_THOUSAND = "amby";
|
|
@@ -51,7 +69,7 @@ var JOINER_BETWEEN_GROUPS = "sy";
|
|
|
51
69
|
var MIN_VALUE = 0;
|
|
52
70
|
|
|
53
71
|
// src/generated/errors.ts
|
|
54
|
-
var SPEC_VERSION = "0.
|
|
72
|
+
var SPEC_VERSION = "0.7.0";
|
|
55
73
|
var MdgError = class extends Error {
|
|
56
74
|
/** Always `'MdgError'`. */
|
|
57
75
|
name = "MdgError";
|
|
@@ -82,6 +100,12 @@ var MdgError = class extends Error {
|
|
|
82
100
|
};
|
|
83
101
|
|
|
84
102
|
// src/number-to-words.ts
|
|
103
|
+
var SCALES_BY_SYSTEM = {
|
|
104
|
+
standard: SCALES,
|
|
105
|
+
traditional: TRADITIONAL_SCALES
|
|
106
|
+
};
|
|
107
|
+
var SYSTEMS = new Set(Object.keys(SCALES_BY_SYSTEM));
|
|
108
|
+
var DEFAULT_SYSTEM = "standard";
|
|
85
109
|
var BASE = 10;
|
|
86
110
|
var HUNDRED = BASE * BASE;
|
|
87
111
|
var FIRST_SCALE_EXPONENT = SCALES[0].exponent;
|
|
@@ -114,25 +138,25 @@ function scaleCount(digits, scale) {
|
|
|
114
138
|
const start = scale.countDigits === null ? 0 : Math.max(0, end - scale.countDigits);
|
|
115
139
|
return digits.slice(start, end).replace(LEADING_ZEROS, "");
|
|
116
140
|
}
|
|
117
|
-
function scaleWords(digits, scale) {
|
|
141
|
+
function scaleWords(digits, scale, scales) {
|
|
118
142
|
const count = scaleCount(digits, scale);
|
|
119
143
|
if (count === "") return "";
|
|
120
144
|
if (count === ONE && scale.exponent === FIRST_SCALE_EXPONENT) return scale.word;
|
|
121
|
-
return `${wholeWords(count)} ${scale.word}`;
|
|
145
|
+
return `${wholeWords(count, scales)} ${scale.word}`;
|
|
122
146
|
}
|
|
123
|
-
function wholeWords(digits) {
|
|
147
|
+
function wholeWords(digits, scales) {
|
|
124
148
|
const significant = digits.replace(LEADING_ZEROS, "");
|
|
125
149
|
if (significant === "") return ZERO_WORD;
|
|
126
150
|
const groups = [
|
|
127
151
|
belowThousandWords(Number(significant.slice(-3))),
|
|
128
|
-
...
|
|
152
|
+
...scales.map((scale) => scaleWords(significant, scale, scales))
|
|
129
153
|
];
|
|
130
154
|
return groups.filter(isNotEmpty).join(` ${JOINER_BETWEEN_GROUPS} `);
|
|
131
155
|
}
|
|
132
|
-
function decimalWords(digits) {
|
|
156
|
+
function decimalWords(digits, scales) {
|
|
133
157
|
const significant = digits.replace(LEADING_ZEROS, "");
|
|
134
158
|
const zeros = Array.from({ length: digits.length - significant.length }, () => ZERO_WORD);
|
|
135
|
-
return [...zeros, wholeWords(significant)].join(" ");
|
|
159
|
+
return [...zeros, wholeWords(significant, scales)].join(" ");
|
|
136
160
|
}
|
|
137
161
|
function plainDecimal(value) {
|
|
138
162
|
const text = String(value);
|
|
@@ -152,11 +176,22 @@ function assertSupportedNumber(value) {
|
|
|
152
176
|
);
|
|
153
177
|
}
|
|
154
178
|
}
|
|
155
|
-
function
|
|
179
|
+
function scalesFor(options) {
|
|
180
|
+
if (typeof options !== "object" || options === null) {
|
|
181
|
+
throw new MdgError("INVALID_TYPE", "numberToWords options must be an object.");
|
|
182
|
+
}
|
|
183
|
+
const { system = DEFAULT_SYSTEM } = options;
|
|
184
|
+
if (!SYSTEMS.has(system)) {
|
|
185
|
+
throw new MdgError("INVALID_TYPE", "system is standard or traditional.");
|
|
186
|
+
}
|
|
187
|
+
return SCALES_BY_SYSTEM[system];
|
|
188
|
+
}
|
|
189
|
+
function numberToWords(value, options = {}) {
|
|
156
190
|
assertSupportedNumber(value);
|
|
191
|
+
const scales = scalesFor(options);
|
|
157
192
|
const [whole = "", decimals] = plainDecimal(value).split(".");
|
|
158
|
-
if (decimals === void 0) return wholeWords(whole);
|
|
159
|
-
return `${wholeWords(whole)} ${DECIMAL_SEPARATOR} ${decimalWords(decimals)}`;
|
|
193
|
+
if (decimals === void 0) return wholeWords(whole, scales);
|
|
194
|
+
return `${wholeWords(whole, scales)} ${DECIMAL_SEPARATOR} ${decimalWords(decimals, scales)}`;
|
|
160
195
|
}
|
|
161
196
|
|
|
162
197
|
export { MdgError, SPEC_VERSION, numberToWords };
|
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,MAAA;AAAA,EACA,SAAA;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O,EAAA,EAAI,QAAA,GAAW,EAAE,CAAA,GAAI,KAAA;AAChD,EAAA,OAAO,CAAA,EAAA,EAAK,GAAA,CAAI,MAAA,CAAO,MAAA,CAAO,QAAQ,CAAA,GAAI,CAAC,CAAC,CAAA,EAAG,IAAI,CAAA,EAAG,IAAI,CAAA,CAAA;AAC5D;AAEA,SAAS,sBAAsB,KAAA,EAAyC;AACtE,EAAA,IAAI,OAAO,KAAA,KAAU,QAAA,IAAY,CAAC,MAAA,CAAO,QAAA,CAAS,KAAK,CAAA,EAAG;AACxD,IAAA,MAAM,IAAI,QAAA,CAAS,cAAA,EAAgB,wCAAwC,CAAA;AAAA,EAC7E;AACA,EAAA,IAAI,KAAA,GAAQ,SAAA,IAAa,KAAA,GAAQ,MAAA,CAAO,gBAAA,EAAkB;AACxD,IAAA,MAAM,IAAI,QAAA;AAAA,MACR,cAAA;AAAA,MACA,CAAA,uBAAA,EAA0B,OAAO,SAAS,CAAC,OAAO,MAAA,CAAO,MAAA,CAAO,gBAAgB,CAAC,CAAA,CAAA;AAAA,KACnF;AAAA,EACF;AACF;AAiBO,SAAS,cAAc,KAAA,EAAuB;AACnD,EAAA,qBAAA,CAAsB,KAAK,CAAA;AAC3B,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,KAAK,CAAA;AACnD,EAAA,OAAO,CAAA,EAAG,WAAW,KAAK,CAAC,IAAI,iBAAiB,CAAA,CAAA,EAAI,YAAA,CAAa,QAAQ,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.6.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 */\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.6.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 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];\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): 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)} ${scale.word}`;\n}\n\n// Works on decimal digits, not arithmetic, so counts and decimals of any length stay exact.\nfunction wholeWords(digits: string): 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)),\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): 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)].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\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 * @returns The number in Malagasy words.\n * @throws {@link MdgError} `INVALID_TYPE` if `value` is not a finite number,\n * `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 * ```\n */\nexport function numberToWords(value: number): string {\n assertSupportedNumber(value);\n const [whole = '', decimals] = plainDecimal(value).split('.');\n if (decimals === undefined) return wholeWords(whole);\n return `${wholeWords(whole)} ${DECIMAL_SEPARATOR} ${decimalWords(decimals)}`;\n}\n"]}
|
|
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,MAAA;AAAA,EACA,SAAA;AAAA,EACA,UAAA;AAAA,EACA,SAAA;AAAA,EACA,WAAA;AAAA,EACA,UAAA;AAAA,EACA,UAAA;AAAA,EACA,UAAA;AAAA,EACA;AACF,CAAA;AAGO,IAAM,aAAA,GAAgB;AAAA,EAC3B,EAAA;AAAA,EACA,MAAA;AAAA,EACA,UAAA;AAAA,EACA,WAAA;AAAA,EACA,SAAA;AAAA,EACA,WAAA;AAAA,EACA,UAAA;AAAA,EACA,WAAA;AAAA,EACA,WAAA;AAAA,EACA;AACF,CAAA;A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;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"]}
|