@mdgascar/words 0.0.0-stage → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mdgascar contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,38 @@
1
- # Temporary Holding Version
1
+ # @mdgascar/words
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Write numbers in Malagasy words. Zero dependencies, ESM + CommonJS + types.
4
+
5
+ Implements spec 0.7.0.
6
+
7
+ ```sh
8
+ npm install @mdgascar/words
9
+ ```
10
+
11
+ ```ts
12
+ import { numberToWords } from '@mdgascar/words';
13
+
14
+ numberToWords(21); // 'iraika amby roapolo'
15
+ numberToWords(2026); // 'enina amby roapolo sy roa arivo'
16
+ numberToWords(280000); // 'valo alina sy roa hetsy'
17
+ numberToWords(3.14); // "telo faingo efatra ambin'ny folo"
18
+ numberToWords(0.008); // 'aotra faingo aotra aotra valo'
19
+ ```
20
+
21
+ Takes any number from 0 to `Number.MAX_SAFE_INTEGER` (9 007 199 254 740 991), whole or decimal.
22
+ Only number values are accepted: strings are not parsed. Other input throws `MdgError` with code
23
+ `INVALID_TYPE` or `OUT_OF_RANGE`.
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
+
36
+ Rules, sources and recorded disagreements: [spec/words](https://github.com/mdgascar/mdg-utils-spec/blob/dev/modules/words/README.md).
37
+
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 ADDED
@@ -0,0 +1,203 @@
1
+ 'use strict';
2
+
3
+ // src/generated/data.ts
4
+ var ZERO_WORD = "aotra";
5
+ var UNIT_WORDS = [
6
+ "",
7
+ "iray",
8
+ "roa",
9
+ "telo",
10
+ "efatra",
11
+ "dimy",
12
+ "enina",
13
+ "fito",
14
+ "valo",
15
+ "sivy"
16
+ ];
17
+ var UNIT_ONE_IN_COMPOUND = "iraika";
18
+ var TEN_WORDS = [
19
+ "",
20
+ "folo",
21
+ "roapolo",
22
+ "telopolo",
23
+ "efapolo",
24
+ "dimampolo",
25
+ "enimpolo",
26
+ "fitopolo",
27
+ "valopolo",
28
+ "sivifolo"
29
+ ];
30
+ var HUNDRED_WORDS = [
31
+ "",
32
+ "zato",
33
+ "roanjato",
34
+ "telonjato",
35
+ "efajato",
36
+ "dimanjato",
37
+ "eninjato",
38
+ "fitonjato",
39
+ "valonjato",
40
+ "sivinjato"
41
+ ];
42
+ var SCALES = [
43
+ { word: "arivo", exponent: 3, countDigits: 1 },
44
+ { word: "alina", exponent: 4, countDigits: 1 },
45
+ { word: "hetsy", exponent: 5, countDigits: 1 },
46
+ { word: "tapitrisa", exponent: 6, countDigits: 3 },
47
+ { word: "lavitrisa", exponent: 9, countDigits: null }
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
+ ];
67
+ var DECIMAL_SEPARATOR = "faingo";
68
+ var JOINER_UNITS_BEFORE_TEN = "ambin'ny";
69
+ var JOINER_BELOW_THOUSAND = "amby";
70
+ var JOINER_BETWEEN_GROUPS = "sy";
71
+ var MIN_VALUE = 0;
72
+
73
+ // src/generated/errors.ts
74
+ var SPEC_VERSION = "0.7.0";
75
+ var MdgError = class extends Error {
76
+ /** Always `'MdgError'`. */
77
+ name = "MdgError";
78
+ /** Why the call failed; the same code in every language. */
79
+ code;
80
+ /**
81
+ * Creates an error with a shared spec code.
82
+ *
83
+ * @param code - Shared spec error code.
84
+ * @param message - Human-readable explanation, in English.
85
+ */
86
+ constructor(code, message) {
87
+ super(message);
88
+ this.code = code;
89
+ }
90
+ /**
91
+ * Makes `instanceof MdgError` true for errors from any mdg-utils package or build.
92
+ *
93
+ * @remarks Each package, and its ESM and CommonJS builds, has its own copy of this class,
94
+ * so the check looks at the error's shape instead of its prototype chain.
95
+ *
96
+ * @param value - Any value, usually a caught error.
97
+ * @returns `true` when `value` is an mdg-utils error.
98
+ */
99
+ static [Symbol.hasInstance](value) {
100
+ return value instanceof Error && value.name === "MdgError" && "code" in value;
101
+ }
102
+ };
103
+
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";
111
+ var BASE = 10;
112
+ var HUNDRED = BASE * BASE;
113
+ var FIRST_SCALE_EXPONENT = SCALES[0].exponent;
114
+ var ONE = "1";
115
+ var LEADING_ZEROS = /^0+/;
116
+ var NEGATIVE_EXPONENT = /^(\d)(?:\.(\d+))?e-(\d+)$/;
117
+ function digitAt(value, place) {
118
+ return Math.floor(value / place) % BASE;
119
+ }
120
+ function isNotEmpty(word) {
121
+ return word !== "";
122
+ }
123
+ function belowHundredWords(unitWord, units, tens) {
124
+ if (tens === 1 && units > 0) return `${unitWord} ${JOINER_UNITS_BEFORE_TEN} ${TEN_WORDS[1]}`;
125
+ return [unitWord, TEN_WORDS[tens]].filter(isNotEmpty).join(` ${JOINER_BELOW_THOUSAND} `);
126
+ }
127
+ function belowThousandWords(value) {
128
+ const units = digitAt(value, 1);
129
+ const tens = digitAt(value, BASE);
130
+ const hundreds = digitAt(value, HUNDRED);
131
+ const isOneBeforeAmby = units === 1 && (tens > 0 || hundreds === 1);
132
+ const unitWord = isOneBeforeAmby ? UNIT_ONE_IN_COMPOUND : UNIT_WORDS[units];
133
+ const belowHundred = belowHundredWords(unitWord, units, tens);
134
+ const joiner = hundreds === 1 ? JOINER_BELOW_THOUSAND : JOINER_BETWEEN_GROUPS;
135
+ return [belowHundred, HUNDRED_WORDS[hundreds]].filter(isNotEmpty).join(` ${joiner} `);
136
+ }
137
+ function scaleCount(digits, scale) {
138
+ const end = digits.length - scale.exponent;
139
+ if (end <= 0) return "";
140
+ const start = scale.countDigits === null ? 0 : Math.max(0, end - scale.countDigits);
141
+ return digits.slice(start, end).replace(LEADING_ZEROS, "");
142
+ }
143
+ function scaleWords(digits, scale, scales) {
144
+ const count = scaleCount(digits, scale);
145
+ if (count === "") return "";
146
+ if (count === ONE && scale.exponent === FIRST_SCALE_EXPONENT) return scale.word;
147
+ return `${wholeWords(count, scales)} ${scale.word}`;
148
+ }
149
+ function wholeWords(digits, scales) {
150
+ const significant = digits.replace(LEADING_ZEROS, "");
151
+ if (significant === "") return ZERO_WORD;
152
+ const groups = [
153
+ belowThousandWords(Number(significant.slice(-3))),
154
+ ...scales.map((scale) => scaleWords(significant, scale, scales))
155
+ ];
156
+ return groups.filter(isNotEmpty).join(` ${JOINER_BETWEEN_GROUPS} `);
157
+ }
158
+ function decimalWords(digits, scales) {
159
+ const significant = digits.replace(LEADING_ZEROS, "");
160
+ const zeros = Array.from({ length: digits.length - significant.length }, () => ZERO_WORD);
161
+ return [...zeros, wholeWords(significant, scales)].join(" ");
162
+ }
163
+ function plainDecimal(value) {
164
+ const text = String(value);
165
+ const match = NEGATIVE_EXPONENT.exec(text);
166
+ if (match === null) return text;
167
+ const [, lead = "", rest = "", exponent = ""] = match;
168
+ return `0.${"0".repeat(Number(exponent) - 1)}${lead}${rest}`;
169
+ }
170
+ function assertSupportedNumber(value) {
171
+ if (typeof value !== "number" || !Number.isFinite(value)) {
172
+ throw new MdgError("INVALID_TYPE", "numberToWords expects a finite number.");
173
+ }
174
+ if (value < MIN_VALUE || value > Number.MAX_SAFE_INTEGER) {
175
+ throw new MdgError(
176
+ "OUT_OF_RANGE",
177
+ `numberToWords supports ${String(MIN_VALUE)} to ${String(Number.MAX_SAFE_INTEGER)}.`
178
+ );
179
+ }
180
+ }
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 = {}) {
192
+ assertSupportedNumber(value);
193
+ const scales = scalesFor(options);
194
+ const [whole = "", decimals] = plainDecimal(value).split(".");
195
+ if (decimals === void 0) return wholeWords(whole, scales);
196
+ return `${wholeWords(whole, scales)} ${DECIMAL_SEPARATOR} ${decimalWords(decimals, scales)}`;
197
+ }
198
+
199
+ exports.MdgError = MdgError;
200
+ exports.SPEC_VERSION = SPEC_VERSION;
201
+ exports.numberToWords = numberToWords;
202
+ //# sourceMappingURL=index.cjs.map
203
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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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S,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"]}
@@ -0,0 +1,93 @@
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
+ }
23
+ /**
24
+ * Writes a number in Malagasy words, smallest part first, with `faingo` before the decimals.
25
+ *
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).
29
+ * @returns The number in Malagasy words.
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`.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * numberToWords(21); // 'iraika amby roapolo'
36
+ * numberToWords(2026); // 'enina amby roapolo sy roa arivo'
37
+ * numberToWords(3.14); // "telo faingo efatra ambin'ny folo"
38
+ * numberToWords(30_000_000, { system: 'traditional' }); // 'telo safatsiroa'
39
+ * ```
40
+ */
41
+ declare function numberToWords(value: number, options?: NumberToWordsOptions): string;
42
+
43
+ /** Version of the shared spec this code was generated from. */
44
+ declare const SPEC_VERSION = "0.7.0";
45
+ /**
46
+ * Error code shared by every mdg-utils language port.
47
+ *
48
+ * - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).
49
+ * - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.
50
+ * - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).
51
+ * - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to a mobile operator.
52
+ */
53
+ type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR';
54
+ /**
55
+ * Error thrown by mdg-utils functions. Check `code`, which is the same in every language.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * try {
60
+ * numberToWords(-1);
61
+ * } catch (error) {
62
+ * if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') {
63
+ * // handle it
64
+ * }
65
+ * }
66
+ * ```
67
+ */
68
+ declare class MdgError extends Error {
69
+ /** Always `'MdgError'`. */
70
+ readonly name = "MdgError";
71
+ /** Why the call failed; the same code in every language. */
72
+ readonly code: ErrorCode;
73
+ /**
74
+ * Creates an error with a shared spec code.
75
+ *
76
+ * @param code - Shared spec error code.
77
+ * @param message - Human-readable explanation, in English.
78
+ */
79
+ constructor(code: ErrorCode, message: string);
80
+ /**
81
+ * Makes `instanceof MdgError` true for errors from any mdg-utils package or build.
82
+ *
83
+ * @remarks Each package, and its ESM and CommonJS builds, has its own copy of this class,
84
+ * so the check looks at the error's shape instead of its prototype chain.
85
+ *
86
+ * @param value - Any value, usually a caught error.
87
+ * @returns `true` when `value` is an mdg-utils error.
88
+ */
89
+ static [Symbol.hasInstance](value: unknown): boolean;
90
+ }
91
+
92
+ export { MdgError, SPEC_VERSION, numberToWords };
93
+ export type { ErrorCode, NumberSystem, NumberToWordsOptions };
@@ -0,0 +1,93 @@
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
+ }
23
+ /**
24
+ * Writes a number in Malagasy words, smallest part first, with `faingo` before the decimals.
25
+ *
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).
29
+ * @returns The number in Malagasy words.
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`.
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * numberToWords(21); // 'iraika amby roapolo'
36
+ * numberToWords(2026); // 'enina amby roapolo sy roa arivo'
37
+ * numberToWords(3.14); // "telo faingo efatra ambin'ny folo"
38
+ * numberToWords(30_000_000, { system: 'traditional' }); // 'telo safatsiroa'
39
+ * ```
40
+ */
41
+ declare function numberToWords(value: number, options?: NumberToWordsOptions): string;
42
+
43
+ /** Version of the shared spec this code was generated from. */
44
+ declare const SPEC_VERSION = "0.7.0";
45
+ /**
46
+ * Error code shared by every mdg-utils language port.
47
+ *
48
+ * - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).
49
+ * - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.
50
+ * - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).
51
+ * - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to a mobile operator.
52
+ */
53
+ type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR';
54
+ /**
55
+ * Error thrown by mdg-utils functions. Check `code`, which is the same in every language.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * try {
60
+ * numberToWords(-1);
61
+ * } catch (error) {
62
+ * if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') {
63
+ * // handle it
64
+ * }
65
+ * }
66
+ * ```
67
+ */
68
+ declare class MdgError extends Error {
69
+ /** Always `'MdgError'`. */
70
+ readonly name = "MdgError";
71
+ /** Why the call failed; the same code in every language. */
72
+ readonly code: ErrorCode;
73
+ /**
74
+ * Creates an error with a shared spec code.
75
+ *
76
+ * @param code - Shared spec error code.
77
+ * @param message - Human-readable explanation, in English.
78
+ */
79
+ constructor(code: ErrorCode, message: string);
80
+ /**
81
+ * Makes `instanceof MdgError` true for errors from any mdg-utils package or build.
82
+ *
83
+ * @remarks Each package, and its ESM and CommonJS builds, has its own copy of this class,
84
+ * so the check looks at the error's shape instead of its prototype chain.
85
+ *
86
+ * @param value - Any value, usually a caught error.
87
+ * @returns `true` when `value` is an mdg-utils error.
88
+ */
89
+ static [Symbol.hasInstance](value: unknown): boolean;
90
+ }
91
+
92
+ export { MdgError, SPEC_VERSION, numberToWords };
93
+ export type { ErrorCode, NumberSystem, NumberToWordsOptions };
package/dist/index.js ADDED
@@ -0,0 +1,199 @@
1
+ // src/generated/data.ts
2
+ var ZERO_WORD = "aotra";
3
+ var UNIT_WORDS = [
4
+ "",
5
+ "iray",
6
+ "roa",
7
+ "telo",
8
+ "efatra",
9
+ "dimy",
10
+ "enina",
11
+ "fito",
12
+ "valo",
13
+ "sivy"
14
+ ];
15
+ var UNIT_ONE_IN_COMPOUND = "iraika";
16
+ var TEN_WORDS = [
17
+ "",
18
+ "folo",
19
+ "roapolo",
20
+ "telopolo",
21
+ "efapolo",
22
+ "dimampolo",
23
+ "enimpolo",
24
+ "fitopolo",
25
+ "valopolo",
26
+ "sivifolo"
27
+ ];
28
+ var HUNDRED_WORDS = [
29
+ "",
30
+ "zato",
31
+ "roanjato",
32
+ "telonjato",
33
+ "efajato",
34
+ "dimanjato",
35
+ "eninjato",
36
+ "fitonjato",
37
+ "valonjato",
38
+ "sivinjato"
39
+ ];
40
+ var SCALES = [
41
+ { word: "arivo", exponent: 3, countDigits: 1 },
42
+ { word: "alina", exponent: 4, countDigits: 1 },
43
+ { word: "hetsy", exponent: 5, countDigits: 1 },
44
+ { word: "tapitrisa", exponent: 6, countDigits: 3 },
45
+ { word: "lavitrisa", exponent: 9, countDigits: null }
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
+ ];
65
+ var DECIMAL_SEPARATOR = "faingo";
66
+ var JOINER_UNITS_BEFORE_TEN = "ambin'ny";
67
+ var JOINER_BELOW_THOUSAND = "amby";
68
+ var JOINER_BETWEEN_GROUPS = "sy";
69
+ var MIN_VALUE = 0;
70
+
71
+ // src/generated/errors.ts
72
+ var SPEC_VERSION = "0.7.0";
73
+ var MdgError = class extends Error {
74
+ /** Always `'MdgError'`. */
75
+ name = "MdgError";
76
+ /** Why the call failed; the same code in every language. */
77
+ code;
78
+ /**
79
+ * Creates an error with a shared spec code.
80
+ *
81
+ * @param code - Shared spec error code.
82
+ * @param message - Human-readable explanation, in English.
83
+ */
84
+ constructor(code, message) {
85
+ super(message);
86
+ this.code = code;
87
+ }
88
+ /**
89
+ * Makes `instanceof MdgError` true for errors from any mdg-utils package or build.
90
+ *
91
+ * @remarks Each package, and its ESM and CommonJS builds, has its own copy of this class,
92
+ * so the check looks at the error's shape instead of its prototype chain.
93
+ *
94
+ * @param value - Any value, usually a caught error.
95
+ * @returns `true` when `value` is an mdg-utils error.
96
+ */
97
+ static [Symbol.hasInstance](value) {
98
+ return value instanceof Error && value.name === "MdgError" && "code" in value;
99
+ }
100
+ };
101
+
102
+ // src/number-to-words.ts
103
+ var SCALES_BY_SYSTEM = {
104
+ standard: SCALES,
105
+ traditional: TRADITIONAL_SCALES
106
+ };
107
+ var SYSTEMS = new Set(Object.keys(SCALES_BY_SYSTEM));
108
+ var DEFAULT_SYSTEM = "standard";
109
+ var BASE = 10;
110
+ var HUNDRED = BASE * BASE;
111
+ var FIRST_SCALE_EXPONENT = SCALES[0].exponent;
112
+ var ONE = "1";
113
+ var LEADING_ZEROS = /^0+/;
114
+ var NEGATIVE_EXPONENT = /^(\d)(?:\.(\d+))?e-(\d+)$/;
115
+ function digitAt(value, place) {
116
+ return Math.floor(value / place) % BASE;
117
+ }
118
+ function isNotEmpty(word) {
119
+ return word !== "";
120
+ }
121
+ function belowHundredWords(unitWord, units, tens) {
122
+ if (tens === 1 && units > 0) return `${unitWord} ${JOINER_UNITS_BEFORE_TEN} ${TEN_WORDS[1]}`;
123
+ return [unitWord, TEN_WORDS[tens]].filter(isNotEmpty).join(` ${JOINER_BELOW_THOUSAND} `);
124
+ }
125
+ function belowThousandWords(value) {
126
+ const units = digitAt(value, 1);
127
+ const tens = digitAt(value, BASE);
128
+ const hundreds = digitAt(value, HUNDRED);
129
+ const isOneBeforeAmby = units === 1 && (tens > 0 || hundreds === 1);
130
+ const unitWord = isOneBeforeAmby ? UNIT_ONE_IN_COMPOUND : UNIT_WORDS[units];
131
+ const belowHundred = belowHundredWords(unitWord, units, tens);
132
+ const joiner = hundreds === 1 ? JOINER_BELOW_THOUSAND : JOINER_BETWEEN_GROUPS;
133
+ return [belowHundred, HUNDRED_WORDS[hundreds]].filter(isNotEmpty).join(` ${joiner} `);
134
+ }
135
+ function scaleCount(digits, scale) {
136
+ const end = digits.length - scale.exponent;
137
+ if (end <= 0) return "";
138
+ const start = scale.countDigits === null ? 0 : Math.max(0, end - scale.countDigits);
139
+ return digits.slice(start, end).replace(LEADING_ZEROS, "");
140
+ }
141
+ function scaleWords(digits, scale, scales) {
142
+ const count = scaleCount(digits, scale);
143
+ if (count === "") return "";
144
+ if (count === ONE && scale.exponent === FIRST_SCALE_EXPONENT) return scale.word;
145
+ return `${wholeWords(count, scales)} ${scale.word}`;
146
+ }
147
+ function wholeWords(digits, scales) {
148
+ const significant = digits.replace(LEADING_ZEROS, "");
149
+ if (significant === "") return ZERO_WORD;
150
+ const groups = [
151
+ belowThousandWords(Number(significant.slice(-3))),
152
+ ...scales.map((scale) => scaleWords(significant, scale, scales))
153
+ ];
154
+ return groups.filter(isNotEmpty).join(` ${JOINER_BETWEEN_GROUPS} `);
155
+ }
156
+ function decimalWords(digits, scales) {
157
+ const significant = digits.replace(LEADING_ZEROS, "");
158
+ const zeros = Array.from({ length: digits.length - significant.length }, () => ZERO_WORD);
159
+ return [...zeros, wholeWords(significant, scales)].join(" ");
160
+ }
161
+ function plainDecimal(value) {
162
+ const text = String(value);
163
+ const match = NEGATIVE_EXPONENT.exec(text);
164
+ if (match === null) return text;
165
+ const [, lead = "", rest = "", exponent = ""] = match;
166
+ return `0.${"0".repeat(Number(exponent) - 1)}${lead}${rest}`;
167
+ }
168
+ function assertSupportedNumber(value) {
169
+ if (typeof value !== "number" || !Number.isFinite(value)) {
170
+ throw new MdgError("INVALID_TYPE", "numberToWords expects a finite number.");
171
+ }
172
+ if (value < MIN_VALUE || value > Number.MAX_SAFE_INTEGER) {
173
+ throw new MdgError(
174
+ "OUT_OF_RANGE",
175
+ `numberToWords supports ${String(MIN_VALUE)} to ${String(Number.MAX_SAFE_INTEGER)}.`
176
+ );
177
+ }
178
+ }
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 = {}) {
190
+ assertSupportedNumber(value);
191
+ const scales = scalesFor(options);
192
+ const [whole = "", decimals] = plainDecimal(value).split(".");
193
+ if (decimals === void 0) return wholeWords(whole, scales);
194
+ return `${wholeWords(whole, scales)} ${DECIMAL_SEPARATOR} ${decimalWords(decimals, scales)}`;
195
+ }
196
+
197
+ export { MdgError, SPEC_VERSION, numberToWords };
198
+ //# sourceMappingURL=index.js.map
199
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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,QAA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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"]}
package/package.json CHANGED
@@ -1,6 +1,53 @@
1
1
  {
2
2
  "name": "@mdgascar/words",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.8.0",
4
+ "description": "Malagasy number words: 1234 → efatra amby telopolo sy roanjato sy arivo.",
5
+ "license": "MIT",
6
+ "author": "Mdgascar contributors",
7
+ "homepage": "https://github.com/mdgascar/mdg-utils-js/tree/main/packages/words#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/mdgascar/mdg-utils-js.git",
11
+ "directory": "packages/words"
12
+ },
13
+ "bugs": "https://github.com/mdgascar/mdg-utils-js/issues",
14
+ "keywords": [
15
+ "madagascar",
16
+ "malagasy",
17
+ "numbers",
18
+ "words",
19
+ "number-to-words"
20
+ ],
21
+ "type": "module",
22
+ "sideEffects": false,
23
+ "exports": {
24
+ ".": {
25
+ "import": {
26
+ "types": "./dist/index.d.ts",
27
+ "default": "./dist/index.js"
28
+ },
29
+ "require": {
30
+ "types": "./dist/index.d.cts",
31
+ "default": "./dist/index.cjs"
32
+ }
33
+ },
34
+ "./package.json": "./package.json"
35
+ },
36
+ "main": "./dist/index.cjs",
37
+ "module": "./dist/index.js",
38
+ "types": "./dist/index.d.ts",
39
+ "files": [
40
+ "dist"
41
+ ],
42
+ "engines": {
43
+ "node": ">=22.18.0"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public",
47
+ "provenance": true
48
+ },
49
+ "scripts": {
50
+ "build": "tsup",
51
+ "api": "api-extractor run --local"
52
+ }
6
53
  }