@mdgascar/ariary 0.9.0 → 0.11.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 +5 -3
- package/dist/index.cjs +126 -65
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +93 -34
- package/dist/index.d.ts +93 -34
- package/dist/index.js +126 -65
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -1,11 +1,44 @@
|
|
|
1
1
|
import { SpellOptions } from '@mdgascar/words';
|
|
2
2
|
export { NumberSystem, SpellOptions } from '@mdgascar/words';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* What the functions that compute or read an amount give back: a `number` (default), or the
|
|
6
|
+
* exact decimal `text`, of any size.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const exact: AmountResultKind = 'text';
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
type AmountResultKind = 'number' | 'text';
|
|
14
|
+
/**
|
|
15
|
+
* Options for the functions that give back an amount.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const options: AmountResultOptions = { as: 'text' };
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
interface AmountResultOptions {
|
|
23
|
+
/** `number` (default), or `text` for the exact decimal digits. */
|
|
24
|
+
readonly as?: AmountResultKind;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The value an amount function gives back for an {@link AmountResultKind}: `string` for `text`,
|
|
28
|
+
* otherwise `number`.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const digits: AmountValue<'text'> = '1500.5';
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
type AmountValue<Kind extends AmountResultKind> = Kind extends 'text' ? string : number;
|
|
36
|
+
|
|
4
37
|
/**
|
|
5
38
|
* Writes an Ariary amount in Malagasy words.
|
|
6
39
|
*
|
|
7
40
|
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or plain
|
|
8
|
-
* decimal text such as `'
|
|
41
|
+
* decimal text such as `'1500.25'`, read exactly.
|
|
9
42
|
* @param options - The words `system`: `standard` (default) or `traditional`.
|
|
10
43
|
* @returns The amount in words followed by `ariary`; decimals follow `faingo`.
|
|
11
44
|
* @throws {@link MdgError} `INVALID_TYPE`, `INVALID_FORMAT` (text) or `OUT_OF_RANGE`.
|
|
@@ -14,18 +47,18 @@ export { NumberSystem, SpellOptions } from '@mdgascar/words';
|
|
|
14
47
|
* ```ts
|
|
15
48
|
* spellAriary(1500); // 'dimanjato sy arivo ariary'
|
|
16
49
|
* spellAriary(1500.5); // 'dimanjato sy arivo faingo dimy ariary'
|
|
17
|
-
* spellAriary(30_000_000, { system: 'traditional' }); // 'telo safatsiroa ariary'
|
|
18
|
-
* spellAriary('2000000000000000.9'); // 'roa tapitrisa lavitrisa faingo sivy ariary'
|
|
19
50
|
* ```
|
|
20
51
|
*/
|
|
21
52
|
declare function spellAriary(amount: number | string, options?: SpellOptions): string;
|
|
22
53
|
/**
|
|
23
54
|
* Converts old Malagasy francs (FMG) to ariary: 5 FMG = 1 Ar, exactly in decimal.
|
|
24
55
|
*
|
|
25
|
-
* @param francs - Francs from 0
|
|
56
|
+
* @param francs - Francs from 0 up, whole or decimal, as a number or decimal text.
|
|
57
|
+
* @param options - Optional `as`: `number` (default) or `text` for the exact digits.
|
|
26
58
|
* @returns The amount in ariary: `7501` → `1500.2`.
|
|
27
|
-
* @throws {@link MdgError} `INVALID_TYPE`
|
|
28
|
-
*
|
|
59
|
+
* @throws {@link MdgError} `INVALID_TYPE` for a value that is not a finite number or text,
|
|
60
|
+
* `INVALID_FORMAT` for text that is not plain decimal, `OUT_OF_RANGE` for a negative number,
|
|
61
|
+
* `PRECISION_LOSS` when a number cannot hold the result exactly.
|
|
29
62
|
*
|
|
30
63
|
* @example
|
|
31
64
|
* ```ts
|
|
@@ -33,22 +66,25 @@ declare function spellAriary(amount: number | string, options?: SpellOptions): s
|
|
|
33
66
|
* fmgToAriary(7501); // 1500.2
|
|
34
67
|
* ```
|
|
35
68
|
*/
|
|
36
|
-
declare function fmgToAriary(francs: number
|
|
69
|
+
declare function fmgToAriary<Kind extends AmountResultKind = 'number'>(francs: number | string, options?: {
|
|
70
|
+
readonly as?: Kind;
|
|
71
|
+
}): AmountValue<Kind>;
|
|
37
72
|
/**
|
|
38
73
|
* Converts ariary to old Malagasy francs (FMG): 1 Ar = 5 FMG, exactly in decimal.
|
|
39
74
|
*
|
|
40
|
-
* @param amount - Ariary from 0 up, whole or decimal.
|
|
75
|
+
* @param amount - Ariary from 0 up, whole or decimal, as a number or decimal text.
|
|
76
|
+
* @param options - Optional `as`: `number` (default) or `text` for the exact digits.
|
|
41
77
|
* @returns The amount in francs: `0.07` → `0.35`, never `0.35000000000000003`.
|
|
42
|
-
* @throws {@link MdgError}
|
|
43
|
-
* it is negative or the francs would exceed `Number.MAX_SAFE_INTEGER`.
|
|
78
|
+
* @throws {@link MdgError} as {@link fmgToAriary}.
|
|
44
79
|
*
|
|
45
80
|
* @example
|
|
46
81
|
* ```ts
|
|
47
82
|
* ariaryToFmg(1500); // 7500
|
|
48
|
-
* ariaryToFmg(0.07); // 0.35
|
|
49
83
|
* ```
|
|
50
84
|
*/
|
|
51
|
-
declare function ariaryToFmg(amount: number
|
|
85
|
+
declare function ariaryToFmg<Kind extends AmountResultKind = 'number'>(amount: number | string, options?: {
|
|
86
|
+
readonly as?: Kind;
|
|
87
|
+
}): AmountValue<Kind>;
|
|
52
88
|
|
|
53
89
|
/** Thousand separator used by {@link formatAriary}: `space` (default) or `dot`. */
|
|
54
90
|
type AriaryGrouping = 'space' | 'dot' | 'comma';
|
|
@@ -79,6 +115,16 @@ interface AriarySeparatorOptions {
|
|
|
79
115
|
* ```
|
|
80
116
|
*/
|
|
81
117
|
type AriarySymbolPosition = 'before' | 'after';
|
|
118
|
+
/**
|
|
119
|
+
* How the currency is shown: `symbol` (default, `Ar 1 500`) or the ISO 4217 `code`
|
|
120
|
+
* (`MGA 1 500`; `MGF` for old francs).
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* const display: AriaryCurrencyDisplay = 'code';
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
type AriaryCurrencyDisplay = 'symbol' | 'code';
|
|
82
128
|
/**
|
|
83
129
|
* Layout options for {@link formatAriary} and {@link formatFmg}.
|
|
84
130
|
*
|
|
@@ -90,51 +136,58 @@ type AriarySymbolPosition = 'before' | 'after';
|
|
|
90
136
|
interface AriaryFormatOptions extends AriarySeparatorOptions {
|
|
91
137
|
/** Symbol `before` (default) or `after` the amount. */
|
|
92
138
|
readonly symbolPosition?: AriarySymbolPosition;
|
|
139
|
+
/** `symbol` (default, `Ar`) or the ISO `code` (`MGA`). */
|
|
140
|
+
readonly currencyDisplay?: AriaryCurrencyDisplay;
|
|
93
141
|
}
|
|
94
142
|
/**
|
|
95
143
|
* Writes an Ariary amount the Malagasy way: symbol `Ar`, digits grouped by no-break spaces.
|
|
96
144
|
*
|
|
97
|
-
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal
|
|
98
|
-
*
|
|
99
|
-
*
|
|
145
|
+
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or decimal
|
|
146
|
+
* text such as `'1500.25'`, written exactly.
|
|
147
|
+
* @param options - Optional `grouping` (`space`, `dot` or `comma`), `decimal` (`comma` or `dot`),
|
|
148
|
+
* `symbolPosition` (`before` or `after`) and `currencyDisplay` (`symbol` or `code`). The defaults are the Malagasy way.
|
|
100
149
|
* @returns The formatted amount, e.g. `Ar 1 500` or `Ar 1 500,5` (the spaces are U+00A0).
|
|
101
|
-
* @throws {@link MdgError} `INVALID_TYPE` for a value that is not a finite number or
|
|
102
|
-
* `
|
|
150
|
+
* @throws {@link MdgError} `INVALID_TYPE` for a value that is not a finite number or text, or
|
|
151
|
+
* unknown options; `INVALID_FORMAT` for text that is not plain decimal; `OUT_OF_RANGE` for a
|
|
152
|
+
* number outside 0 to `Number.MAX_SAFE_INTEGER`.
|
|
103
153
|
*
|
|
104
154
|
* @example
|
|
105
155
|
* ```ts
|
|
106
156
|
* formatAriary(1500); // 'Ar 1 500'
|
|
107
157
|
* formatAriary(1500, { grouping: 'dot', symbolPosition: 'after' }); // '1.500 Ar'
|
|
108
158
|
* formatAriary(1500.5); // 'Ar 1 500,5'
|
|
159
|
+
* formatAriary(1500, { currencyDisplay: 'code' }); // 'MGA 1 500'
|
|
109
160
|
* ```
|
|
110
161
|
*/
|
|
111
|
-
declare function formatAriary(amount: number, options?: AriaryFormatOptions): string;
|
|
162
|
+
declare function formatAriary(amount: number | string, options?: AriaryFormatOptions): string;
|
|
112
163
|
/**
|
|
113
164
|
* Writes an amount in old Malagasy francs, with the same layout as {@link formatAriary}.
|
|
114
165
|
*
|
|
115
|
-
* @param francs - Francs from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
166
|
+
* @param francs - Francs from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or decimal text.
|
|
116
167
|
* @param options - Same options as {@link formatAriary}.
|
|
117
168
|
* @returns The formatted amount, e.g. `Fmg 7 500`.
|
|
118
|
-
* @throws {@link MdgError}
|
|
169
|
+
* @throws {@link MdgError} as {@link formatAriary}.
|
|
119
170
|
*
|
|
120
171
|
* @example
|
|
121
172
|
* ```ts
|
|
122
173
|
* formatFmg(7500); // 'Fmg 7 500'
|
|
123
174
|
* ```
|
|
124
175
|
*/
|
|
125
|
-
declare function formatFmg(francs: number, options?: AriaryFormatOptions): string;
|
|
176
|
+
declare function formatFmg(francs: number | string, options?: AriaryFormatOptions): string;
|
|
126
177
|
|
|
127
178
|
/** Version of the shared spec this code was generated from. */
|
|
128
|
-
declare const SPEC_VERSION = "0.
|
|
179
|
+
declare const SPEC_VERSION = "0.10.0";
|
|
129
180
|
/**
|
|
130
181
|
* Error code shared by every mdg-utils language port.
|
|
131
182
|
*
|
|
132
183
|
* - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).
|
|
133
184
|
* - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.
|
|
134
185
|
* - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).
|
|
135
|
-
* - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to
|
|
186
|
+
* - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to an operator.
|
|
187
|
+
* - `PRECISION_LOSS`: The value is exact in the text but the port's number type cannot hold it exactly; ask for the exact text instead.
|
|
188
|
+
* - `AMBIGUOUS`: The text reads as more than one value, so none is chosen.
|
|
136
189
|
*/
|
|
137
|
-
type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR';
|
|
190
|
+
type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR' | 'PRECISION_LOSS' | 'AMBIGUOUS';
|
|
138
191
|
/**
|
|
139
192
|
* Outcome of a `safeParse…` function: the value, or the error that would have been thrown.
|
|
140
193
|
*
|
|
@@ -191,16 +244,20 @@ declare class MdgError extends Error {
|
|
|
191
244
|
}
|
|
192
245
|
|
|
193
246
|
/**
|
|
194
|
-
* Options for reading amounts: which `grouping` and `decimal` separators the text uses
|
|
195
|
-
* Without `grouping`, every Malagasy one is accepted (spaces or dot);
|
|
196
|
-
* comma. Say so for text in another convention:
|
|
247
|
+
* Options for reading amounts: which `grouping` and `decimal` separators the text uses, and
|
|
248
|
+
* what to give back. Without `grouping`, every Malagasy one is accepted (spaces or dot);
|
|
249
|
+
* `decimal` defaults to a comma. Say so for text in another convention:
|
|
250
|
+
* `{ grouping: 'comma', decimal: 'dot' }`.
|
|
197
251
|
*
|
|
198
252
|
* @example
|
|
199
253
|
* ```ts
|
|
200
254
|
* const english: AriaryParseOptions = { grouping: 'comma', decimal: 'dot' };
|
|
201
255
|
* ```
|
|
202
256
|
*/
|
|
203
|
-
|
|
257
|
+
interface AriaryParseOptions extends AriarySeparatorOptions {
|
|
258
|
+
/** `number` (default), or `text` for the exact decimal digits of any amount. */
|
|
259
|
+
readonly as?: AmountResultKind;
|
|
260
|
+
}
|
|
204
261
|
/**
|
|
205
262
|
* Reads an Ariary amount without throwing.
|
|
206
263
|
*
|
|
@@ -214,7 +271,7 @@ type AriaryParseOptions = AriarySeparatorOptions;
|
|
|
214
271
|
* if (result.success) result.data; // 1500
|
|
215
272
|
* ```
|
|
216
273
|
*/
|
|
217
|
-
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number>;
|
|
274
|
+
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number | string>;
|
|
218
275
|
/**
|
|
219
276
|
* Reads an Ariary amount written in any common way.
|
|
220
277
|
*
|
|
@@ -222,8 +279,8 @@ declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): S
|
|
|
222
279
|
* @param options - Optional `grouping` and `decimal`; see {@link AriaryParseOptions}.
|
|
223
280
|
* @returns The amount, whole or decimal.
|
|
224
281
|
* @throws {@link MdgError} `INVALID_TYPE` for non-strings or bad options, `INVALID_FORMAT` for text
|
|
225
|
-
* that is not an amount (signs, a dot as decimal point by default), `
|
|
226
|
-
* `
|
|
282
|
+
* that is not an amount (signs, a dot as decimal point by default), `PRECISION_LOSS` when a
|
|
283
|
+
* number cannot hold the amount exactly (pass `{ as: 'text' }` for the exact digits).
|
|
227
284
|
*
|
|
228
285
|
* @example
|
|
229
286
|
* ```ts
|
|
@@ -233,7 +290,9 @@ declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): S
|
|
|
233
290
|
* parseAriary('1,500.50', { grouping: 'comma', decimal: 'dot' }); // 1500.5
|
|
234
291
|
* ```
|
|
235
292
|
*/
|
|
236
|
-
declare function parseAriary(text: string, options?:
|
|
293
|
+
declare function parseAriary<Kind extends AmountResultKind = 'number'>(text: string, options?: AriarySeparatorOptions & {
|
|
294
|
+
readonly as?: Kind;
|
|
295
|
+
}): AmountValue<Kind>;
|
|
237
296
|
/**
|
|
238
297
|
* Reads an Ariary amount, or gives `undefined` instead of throwing.
|
|
239
298
|
*
|
|
@@ -249,7 +308,7 @@ declare function parseAriary(text: string, options?: AriaryParseOptions): number
|
|
|
249
308
|
* tryParseAriary('1.5'); // undefined
|
|
250
309
|
* ```
|
|
251
310
|
*/
|
|
252
|
-
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | undefined;
|
|
311
|
+
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | string | undefined;
|
|
253
312
|
|
|
254
313
|
export { MdgError, SPEC_VERSION, ariaryToFmg, fmgToAriary, formatAriary, formatFmg, parseAriary, safeParseAriary, spellAriary, tryParseAriary };
|
|
255
|
-
export type { AriaryDecimal, AriaryFormatOptions, AriaryGrouping, AriaryParseOptions, AriarySeparatorOptions, AriarySymbolPosition, ErrorCode, SafeParseResult };
|
|
314
|
+
export type { AmountResultKind, AmountResultOptions, AmountValue, AriaryCurrencyDisplay, AriaryDecimal, AriaryFormatOptions, AriaryGrouping, AriaryParseOptions, AriarySeparatorOptions, AriarySymbolPosition, ErrorCode, SafeParseResult };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,44 @@
|
|
|
1
1
|
import { SpellOptions } from '@mdgascar/words';
|
|
2
2
|
export { NumberSystem, SpellOptions } from '@mdgascar/words';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* What the functions that compute or read an amount give back: a `number` (default), or the
|
|
6
|
+
* exact decimal `text`, of any size.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* const exact: AmountResultKind = 'text';
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
type AmountResultKind = 'number' | 'text';
|
|
14
|
+
/**
|
|
15
|
+
* Options for the functions that give back an amount.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const options: AmountResultOptions = { as: 'text' };
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
interface AmountResultOptions {
|
|
23
|
+
/** `number` (default), or `text` for the exact decimal digits. */
|
|
24
|
+
readonly as?: AmountResultKind;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The value an amount function gives back for an {@link AmountResultKind}: `string` for `text`,
|
|
28
|
+
* otherwise `number`.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const digits: AmountValue<'text'> = '1500.5';
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
type AmountValue<Kind extends AmountResultKind> = Kind extends 'text' ? string : number;
|
|
36
|
+
|
|
4
37
|
/**
|
|
5
38
|
* Writes an Ariary amount in Malagasy words.
|
|
6
39
|
*
|
|
7
40
|
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or plain
|
|
8
|
-
* decimal text such as `'
|
|
41
|
+
* decimal text such as `'1500.25'`, read exactly.
|
|
9
42
|
* @param options - The words `system`: `standard` (default) or `traditional`.
|
|
10
43
|
* @returns The amount in words followed by `ariary`; decimals follow `faingo`.
|
|
11
44
|
* @throws {@link MdgError} `INVALID_TYPE`, `INVALID_FORMAT` (text) or `OUT_OF_RANGE`.
|
|
@@ -14,18 +47,18 @@ export { NumberSystem, SpellOptions } from '@mdgascar/words';
|
|
|
14
47
|
* ```ts
|
|
15
48
|
* spellAriary(1500); // 'dimanjato sy arivo ariary'
|
|
16
49
|
* spellAriary(1500.5); // 'dimanjato sy arivo faingo dimy ariary'
|
|
17
|
-
* spellAriary(30_000_000, { system: 'traditional' }); // 'telo safatsiroa ariary'
|
|
18
|
-
* spellAriary('2000000000000000.9'); // 'roa tapitrisa lavitrisa faingo sivy ariary'
|
|
19
50
|
* ```
|
|
20
51
|
*/
|
|
21
52
|
declare function spellAriary(amount: number | string, options?: SpellOptions): string;
|
|
22
53
|
/**
|
|
23
54
|
* Converts old Malagasy francs (FMG) to ariary: 5 FMG = 1 Ar, exactly in decimal.
|
|
24
55
|
*
|
|
25
|
-
* @param francs - Francs from 0
|
|
56
|
+
* @param francs - Francs from 0 up, whole or decimal, as a number or decimal text.
|
|
57
|
+
* @param options - Optional `as`: `number` (default) or `text` for the exact digits.
|
|
26
58
|
* @returns The amount in ariary: `7501` → `1500.2`.
|
|
27
|
-
* @throws {@link MdgError} `INVALID_TYPE`
|
|
28
|
-
*
|
|
59
|
+
* @throws {@link MdgError} `INVALID_TYPE` for a value that is not a finite number or text,
|
|
60
|
+
* `INVALID_FORMAT` for text that is not plain decimal, `OUT_OF_RANGE` for a negative number,
|
|
61
|
+
* `PRECISION_LOSS` when a number cannot hold the result exactly.
|
|
29
62
|
*
|
|
30
63
|
* @example
|
|
31
64
|
* ```ts
|
|
@@ -33,22 +66,25 @@ declare function spellAriary(amount: number | string, options?: SpellOptions): s
|
|
|
33
66
|
* fmgToAriary(7501); // 1500.2
|
|
34
67
|
* ```
|
|
35
68
|
*/
|
|
36
|
-
declare function fmgToAriary(francs: number
|
|
69
|
+
declare function fmgToAriary<Kind extends AmountResultKind = 'number'>(francs: number | string, options?: {
|
|
70
|
+
readonly as?: Kind;
|
|
71
|
+
}): AmountValue<Kind>;
|
|
37
72
|
/**
|
|
38
73
|
* Converts ariary to old Malagasy francs (FMG): 1 Ar = 5 FMG, exactly in decimal.
|
|
39
74
|
*
|
|
40
|
-
* @param amount - Ariary from 0 up, whole or decimal.
|
|
75
|
+
* @param amount - Ariary from 0 up, whole or decimal, as a number or decimal text.
|
|
76
|
+
* @param options - Optional `as`: `number` (default) or `text` for the exact digits.
|
|
41
77
|
* @returns The amount in francs: `0.07` → `0.35`, never `0.35000000000000003`.
|
|
42
|
-
* @throws {@link MdgError}
|
|
43
|
-
* it is negative or the francs would exceed `Number.MAX_SAFE_INTEGER`.
|
|
78
|
+
* @throws {@link MdgError} as {@link fmgToAriary}.
|
|
44
79
|
*
|
|
45
80
|
* @example
|
|
46
81
|
* ```ts
|
|
47
82
|
* ariaryToFmg(1500); // 7500
|
|
48
|
-
* ariaryToFmg(0.07); // 0.35
|
|
49
83
|
* ```
|
|
50
84
|
*/
|
|
51
|
-
declare function ariaryToFmg(amount: number
|
|
85
|
+
declare function ariaryToFmg<Kind extends AmountResultKind = 'number'>(amount: number | string, options?: {
|
|
86
|
+
readonly as?: Kind;
|
|
87
|
+
}): AmountValue<Kind>;
|
|
52
88
|
|
|
53
89
|
/** Thousand separator used by {@link formatAriary}: `space` (default) or `dot`. */
|
|
54
90
|
type AriaryGrouping = 'space' | 'dot' | 'comma';
|
|
@@ -79,6 +115,16 @@ interface AriarySeparatorOptions {
|
|
|
79
115
|
* ```
|
|
80
116
|
*/
|
|
81
117
|
type AriarySymbolPosition = 'before' | 'after';
|
|
118
|
+
/**
|
|
119
|
+
* How the currency is shown: `symbol` (default, `Ar 1 500`) or the ISO 4217 `code`
|
|
120
|
+
* (`MGA 1 500`; `MGF` for old francs).
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* const display: AriaryCurrencyDisplay = 'code';
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
type AriaryCurrencyDisplay = 'symbol' | 'code';
|
|
82
128
|
/**
|
|
83
129
|
* Layout options for {@link formatAriary} and {@link formatFmg}.
|
|
84
130
|
*
|
|
@@ -90,51 +136,58 @@ type AriarySymbolPosition = 'before' | 'after';
|
|
|
90
136
|
interface AriaryFormatOptions extends AriarySeparatorOptions {
|
|
91
137
|
/** Symbol `before` (default) or `after` the amount. */
|
|
92
138
|
readonly symbolPosition?: AriarySymbolPosition;
|
|
139
|
+
/** `symbol` (default, `Ar`) or the ISO `code` (`MGA`). */
|
|
140
|
+
readonly currencyDisplay?: AriaryCurrencyDisplay;
|
|
93
141
|
}
|
|
94
142
|
/**
|
|
95
143
|
* Writes an Ariary amount the Malagasy way: symbol `Ar`, digits grouped by no-break spaces.
|
|
96
144
|
*
|
|
97
|
-
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal
|
|
98
|
-
*
|
|
99
|
-
*
|
|
145
|
+
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or decimal
|
|
146
|
+
* text such as `'1500.25'`, written exactly.
|
|
147
|
+
* @param options - Optional `grouping` (`space`, `dot` or `comma`), `decimal` (`comma` or `dot`),
|
|
148
|
+
* `symbolPosition` (`before` or `after`) and `currencyDisplay` (`symbol` or `code`). The defaults are the Malagasy way.
|
|
100
149
|
* @returns The formatted amount, e.g. `Ar 1 500` or `Ar 1 500,5` (the spaces are U+00A0).
|
|
101
|
-
* @throws {@link MdgError} `INVALID_TYPE` for a value that is not a finite number or
|
|
102
|
-
* `
|
|
150
|
+
* @throws {@link MdgError} `INVALID_TYPE` for a value that is not a finite number or text, or
|
|
151
|
+
* unknown options; `INVALID_FORMAT` for text that is not plain decimal; `OUT_OF_RANGE` for a
|
|
152
|
+
* number outside 0 to `Number.MAX_SAFE_INTEGER`.
|
|
103
153
|
*
|
|
104
154
|
* @example
|
|
105
155
|
* ```ts
|
|
106
156
|
* formatAriary(1500); // 'Ar 1 500'
|
|
107
157
|
* formatAriary(1500, { grouping: 'dot', symbolPosition: 'after' }); // '1.500 Ar'
|
|
108
158
|
* formatAriary(1500.5); // 'Ar 1 500,5'
|
|
159
|
+
* formatAriary(1500, { currencyDisplay: 'code' }); // 'MGA 1 500'
|
|
109
160
|
* ```
|
|
110
161
|
*/
|
|
111
|
-
declare function formatAriary(amount: number, options?: AriaryFormatOptions): string;
|
|
162
|
+
declare function formatAriary(amount: number | string, options?: AriaryFormatOptions): string;
|
|
112
163
|
/**
|
|
113
164
|
* Writes an amount in old Malagasy francs, with the same layout as {@link formatAriary}.
|
|
114
165
|
*
|
|
115
|
-
* @param francs - Francs from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
166
|
+
* @param francs - Francs from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or decimal text.
|
|
116
167
|
* @param options - Same options as {@link formatAriary}.
|
|
117
168
|
* @returns The formatted amount, e.g. `Fmg 7 500`.
|
|
118
|
-
* @throws {@link MdgError}
|
|
169
|
+
* @throws {@link MdgError} as {@link formatAriary}.
|
|
119
170
|
*
|
|
120
171
|
* @example
|
|
121
172
|
* ```ts
|
|
122
173
|
* formatFmg(7500); // 'Fmg 7 500'
|
|
123
174
|
* ```
|
|
124
175
|
*/
|
|
125
|
-
declare function formatFmg(francs: number, options?: AriaryFormatOptions): string;
|
|
176
|
+
declare function formatFmg(francs: number | string, options?: AriaryFormatOptions): string;
|
|
126
177
|
|
|
127
178
|
/** Version of the shared spec this code was generated from. */
|
|
128
|
-
declare const SPEC_VERSION = "0.
|
|
179
|
+
declare const SPEC_VERSION = "0.10.0";
|
|
129
180
|
/**
|
|
130
181
|
* Error code shared by every mdg-utils language port.
|
|
131
182
|
*
|
|
132
183
|
* - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).
|
|
133
184
|
* - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.
|
|
134
185
|
* - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).
|
|
135
|
-
* - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to
|
|
186
|
+
* - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to an operator.
|
|
187
|
+
* - `PRECISION_LOSS`: The value is exact in the text but the port's number type cannot hold it exactly; ask for the exact text instead.
|
|
188
|
+
* - `AMBIGUOUS`: The text reads as more than one value, so none is chosen.
|
|
136
189
|
*/
|
|
137
|
-
type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR';
|
|
190
|
+
type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR' | 'PRECISION_LOSS' | 'AMBIGUOUS';
|
|
138
191
|
/**
|
|
139
192
|
* Outcome of a `safeParse…` function: the value, or the error that would have been thrown.
|
|
140
193
|
*
|
|
@@ -191,16 +244,20 @@ declare class MdgError extends Error {
|
|
|
191
244
|
}
|
|
192
245
|
|
|
193
246
|
/**
|
|
194
|
-
* Options for reading amounts: which `grouping` and `decimal` separators the text uses
|
|
195
|
-
* Without `grouping`, every Malagasy one is accepted (spaces or dot);
|
|
196
|
-
* comma. Say so for text in another convention:
|
|
247
|
+
* Options for reading amounts: which `grouping` and `decimal` separators the text uses, and
|
|
248
|
+
* what to give back. Without `grouping`, every Malagasy one is accepted (spaces or dot);
|
|
249
|
+
* `decimal` defaults to a comma. Say so for text in another convention:
|
|
250
|
+
* `{ grouping: 'comma', decimal: 'dot' }`.
|
|
197
251
|
*
|
|
198
252
|
* @example
|
|
199
253
|
* ```ts
|
|
200
254
|
* const english: AriaryParseOptions = { grouping: 'comma', decimal: 'dot' };
|
|
201
255
|
* ```
|
|
202
256
|
*/
|
|
203
|
-
|
|
257
|
+
interface AriaryParseOptions extends AriarySeparatorOptions {
|
|
258
|
+
/** `number` (default), or `text` for the exact decimal digits of any amount. */
|
|
259
|
+
readonly as?: AmountResultKind;
|
|
260
|
+
}
|
|
204
261
|
/**
|
|
205
262
|
* Reads an Ariary amount without throwing.
|
|
206
263
|
*
|
|
@@ -214,7 +271,7 @@ type AriaryParseOptions = AriarySeparatorOptions;
|
|
|
214
271
|
* if (result.success) result.data; // 1500
|
|
215
272
|
* ```
|
|
216
273
|
*/
|
|
217
|
-
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number>;
|
|
274
|
+
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number | string>;
|
|
218
275
|
/**
|
|
219
276
|
* Reads an Ariary amount written in any common way.
|
|
220
277
|
*
|
|
@@ -222,8 +279,8 @@ declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): S
|
|
|
222
279
|
* @param options - Optional `grouping` and `decimal`; see {@link AriaryParseOptions}.
|
|
223
280
|
* @returns The amount, whole or decimal.
|
|
224
281
|
* @throws {@link MdgError} `INVALID_TYPE` for non-strings or bad options, `INVALID_FORMAT` for text
|
|
225
|
-
* that is not an amount (signs, a dot as decimal point by default), `
|
|
226
|
-
* `
|
|
282
|
+
* that is not an amount (signs, a dot as decimal point by default), `PRECISION_LOSS` when a
|
|
283
|
+
* number cannot hold the amount exactly (pass `{ as: 'text' }` for the exact digits).
|
|
227
284
|
*
|
|
228
285
|
* @example
|
|
229
286
|
* ```ts
|
|
@@ -233,7 +290,9 @@ declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): S
|
|
|
233
290
|
* parseAriary('1,500.50', { grouping: 'comma', decimal: 'dot' }); // 1500.5
|
|
234
291
|
* ```
|
|
235
292
|
*/
|
|
236
|
-
declare function parseAriary(text: string, options?:
|
|
293
|
+
declare function parseAriary<Kind extends AmountResultKind = 'number'>(text: string, options?: AriarySeparatorOptions & {
|
|
294
|
+
readonly as?: Kind;
|
|
295
|
+
}): AmountValue<Kind>;
|
|
237
296
|
/**
|
|
238
297
|
* Reads an Ariary amount, or gives `undefined` instead of throwing.
|
|
239
298
|
*
|
|
@@ -249,7 +308,7 @@ declare function parseAriary(text: string, options?: AriaryParseOptions): number
|
|
|
249
308
|
* tryParseAriary('1.5'); // undefined
|
|
250
309
|
* ```
|
|
251
310
|
*/
|
|
252
|
-
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | undefined;
|
|
311
|
+
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | string | undefined;
|
|
253
312
|
|
|
254
313
|
export { MdgError, SPEC_VERSION, ariaryToFmg, fmgToAriary, formatAriary, formatFmg, parseAriary, safeParseAriary, spellAriary, tryParseAriary };
|
|
255
|
-
export type { AriaryDecimal, AriaryFormatOptions, AriaryGrouping, AriaryParseOptions, AriarySeparatorOptions, AriarySymbolPosition, ErrorCode, SafeParseResult };
|
|
314
|
+
export type { AmountResultKind, AmountResultOptions, AmountValue, AriaryCurrencyDisplay, AriaryDecimal, AriaryFormatOptions, AriaryGrouping, AriaryParseOptions, AriarySeparatorOptions, AriarySymbolPosition, ErrorCode, SafeParseResult };
|