@mdgascar/words 0.7.0 → 0.9.0

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