@mdgascar/ariary 0.9.0 → 0.10.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 +4 -3
- package/dist/index.cjs +115 -63
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +78 -32
- package/dist/index.d.ts +78 -32
- package/dist/index.js +115 -63
- 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';
|
|
@@ -94,12 +130,14 @@ interface AriaryFormatOptions extends AriarySeparatorOptions {
|
|
|
94
130
|
/**
|
|
95
131
|
* Writes an Ariary amount the Malagasy way: symbol `Ar`, digits grouped by no-break spaces.
|
|
96
132
|
*
|
|
97
|
-
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal
|
|
133
|
+
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or decimal
|
|
134
|
+
* text such as `'1500.25'`, written exactly.
|
|
98
135
|
* @param options - Optional `grouping` (`space`, `dot` or `comma`), `decimal` (`comma` or `dot`)
|
|
99
136
|
* and `symbolPosition` (`before` or `after`). The defaults are the Malagasy way.
|
|
100
137
|
* @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
|
-
* `
|
|
138
|
+
* @throws {@link MdgError} `INVALID_TYPE` for a value that is not a finite number or text, or
|
|
139
|
+
* unknown options; `INVALID_FORMAT` for text that is not plain decimal; `OUT_OF_RANGE` for a
|
|
140
|
+
* number outside 0 to `Number.MAX_SAFE_INTEGER`.
|
|
103
141
|
*
|
|
104
142
|
* @example
|
|
105
143
|
* ```ts
|
|
@@ -108,33 +146,35 @@ interface AriaryFormatOptions extends AriarySeparatorOptions {
|
|
|
108
146
|
* formatAriary(1500.5); // 'Ar 1 500,5'
|
|
109
147
|
* ```
|
|
110
148
|
*/
|
|
111
|
-
declare function formatAriary(amount: number, options?: AriaryFormatOptions): string;
|
|
149
|
+
declare function formatAriary(amount: number | string, options?: AriaryFormatOptions): string;
|
|
112
150
|
/**
|
|
113
151
|
* Writes an amount in old Malagasy francs, with the same layout as {@link formatAriary}.
|
|
114
152
|
*
|
|
115
|
-
* @param francs - Francs from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
153
|
+
* @param francs - Francs from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or decimal text.
|
|
116
154
|
* @param options - Same options as {@link formatAriary}.
|
|
117
155
|
* @returns The formatted amount, e.g. `Fmg 7 500`.
|
|
118
|
-
* @throws {@link MdgError}
|
|
156
|
+
* @throws {@link MdgError} as {@link formatAriary}.
|
|
119
157
|
*
|
|
120
158
|
* @example
|
|
121
159
|
* ```ts
|
|
122
160
|
* formatFmg(7500); // 'Fmg 7 500'
|
|
123
161
|
* ```
|
|
124
162
|
*/
|
|
125
|
-
declare function formatFmg(francs: number, options?: AriaryFormatOptions): string;
|
|
163
|
+
declare function formatFmg(francs: number | string, options?: AriaryFormatOptions): string;
|
|
126
164
|
|
|
127
165
|
/** Version of the shared spec this code was generated from. */
|
|
128
|
-
declare const SPEC_VERSION = "0.
|
|
166
|
+
declare const SPEC_VERSION = "0.9.0";
|
|
129
167
|
/**
|
|
130
168
|
* Error code shared by every mdg-utils language port.
|
|
131
169
|
*
|
|
132
170
|
* - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).
|
|
133
171
|
* - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.
|
|
134
172
|
* - `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
|
|
173
|
+
* - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to an operator.
|
|
174
|
+
* - `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.
|
|
175
|
+
* - `AMBIGUOUS`: The text reads as more than one value, so none is chosen.
|
|
136
176
|
*/
|
|
137
|
-
type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR';
|
|
177
|
+
type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR' | 'PRECISION_LOSS' | 'AMBIGUOUS';
|
|
138
178
|
/**
|
|
139
179
|
* Outcome of a `safeParse…` function: the value, or the error that would have been thrown.
|
|
140
180
|
*
|
|
@@ -191,16 +231,20 @@ declare class MdgError extends Error {
|
|
|
191
231
|
}
|
|
192
232
|
|
|
193
233
|
/**
|
|
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:
|
|
234
|
+
* Options for reading amounts: which `grouping` and `decimal` separators the text uses, and
|
|
235
|
+
* what to give back. Without `grouping`, every Malagasy one is accepted (spaces or dot);
|
|
236
|
+
* `decimal` defaults to a comma. Say so for text in another convention:
|
|
237
|
+
* `{ grouping: 'comma', decimal: 'dot' }`.
|
|
197
238
|
*
|
|
198
239
|
* @example
|
|
199
240
|
* ```ts
|
|
200
241
|
* const english: AriaryParseOptions = { grouping: 'comma', decimal: 'dot' };
|
|
201
242
|
* ```
|
|
202
243
|
*/
|
|
203
|
-
|
|
244
|
+
interface AriaryParseOptions extends AriarySeparatorOptions {
|
|
245
|
+
/** `number` (default), or `text` for the exact decimal digits of any amount. */
|
|
246
|
+
readonly as?: AmountResultKind;
|
|
247
|
+
}
|
|
204
248
|
/**
|
|
205
249
|
* Reads an Ariary amount without throwing.
|
|
206
250
|
*
|
|
@@ -214,7 +258,7 @@ type AriaryParseOptions = AriarySeparatorOptions;
|
|
|
214
258
|
* if (result.success) result.data; // 1500
|
|
215
259
|
* ```
|
|
216
260
|
*/
|
|
217
|
-
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number>;
|
|
261
|
+
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number | string>;
|
|
218
262
|
/**
|
|
219
263
|
* Reads an Ariary amount written in any common way.
|
|
220
264
|
*
|
|
@@ -222,8 +266,8 @@ declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): S
|
|
|
222
266
|
* @param options - Optional `grouping` and `decimal`; see {@link AriaryParseOptions}.
|
|
223
267
|
* @returns The amount, whole or decimal.
|
|
224
268
|
* @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
|
-
* `
|
|
269
|
+
* that is not an amount (signs, a dot as decimal point by default), `PRECISION_LOSS` when a
|
|
270
|
+
* number cannot hold the amount exactly (pass `{ as: 'text' }` for the exact digits).
|
|
227
271
|
*
|
|
228
272
|
* @example
|
|
229
273
|
* ```ts
|
|
@@ -233,7 +277,9 @@ declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): S
|
|
|
233
277
|
* parseAriary('1,500.50', { grouping: 'comma', decimal: 'dot' }); // 1500.5
|
|
234
278
|
* ```
|
|
235
279
|
*/
|
|
236
|
-
declare function parseAriary(text: string, options?:
|
|
280
|
+
declare function parseAriary<Kind extends AmountResultKind = 'number'>(text: string, options?: AriarySeparatorOptions & {
|
|
281
|
+
readonly as?: Kind;
|
|
282
|
+
}): AmountValue<Kind>;
|
|
237
283
|
/**
|
|
238
284
|
* Reads an Ariary amount, or gives `undefined` instead of throwing.
|
|
239
285
|
*
|
|
@@ -249,7 +295,7 @@ declare function parseAriary(text: string, options?: AriaryParseOptions): number
|
|
|
249
295
|
* tryParseAriary('1.5'); // undefined
|
|
250
296
|
* ```
|
|
251
297
|
*/
|
|
252
|
-
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | undefined;
|
|
298
|
+
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | string | undefined;
|
|
253
299
|
|
|
254
300
|
export { MdgError, SPEC_VERSION, ariaryToFmg, fmgToAriary, formatAriary, formatFmg, parseAriary, safeParseAriary, spellAriary, tryParseAriary };
|
|
255
|
-
export type { AriaryDecimal, AriaryFormatOptions, AriaryGrouping, AriaryParseOptions, AriarySeparatorOptions, AriarySymbolPosition, ErrorCode, SafeParseResult };
|
|
301
|
+
export type { AmountResultKind, AmountResultOptions, AmountValue, 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';
|
|
@@ -94,12 +130,14 @@ interface AriaryFormatOptions extends AriarySeparatorOptions {
|
|
|
94
130
|
/**
|
|
95
131
|
* Writes an Ariary amount the Malagasy way: symbol `Ar`, digits grouped by no-break spaces.
|
|
96
132
|
*
|
|
97
|
-
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal
|
|
133
|
+
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or decimal
|
|
134
|
+
* text such as `'1500.25'`, written exactly.
|
|
98
135
|
* @param options - Optional `grouping` (`space`, `dot` or `comma`), `decimal` (`comma` or `dot`)
|
|
99
136
|
* and `symbolPosition` (`before` or `after`). The defaults are the Malagasy way.
|
|
100
137
|
* @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
|
-
* `
|
|
138
|
+
* @throws {@link MdgError} `INVALID_TYPE` for a value that is not a finite number or text, or
|
|
139
|
+
* unknown options; `INVALID_FORMAT` for text that is not plain decimal; `OUT_OF_RANGE` for a
|
|
140
|
+
* number outside 0 to `Number.MAX_SAFE_INTEGER`.
|
|
103
141
|
*
|
|
104
142
|
* @example
|
|
105
143
|
* ```ts
|
|
@@ -108,33 +146,35 @@ interface AriaryFormatOptions extends AriarySeparatorOptions {
|
|
|
108
146
|
* formatAriary(1500.5); // 'Ar 1 500,5'
|
|
109
147
|
* ```
|
|
110
148
|
*/
|
|
111
|
-
declare function formatAriary(amount: number, options?: AriaryFormatOptions): string;
|
|
149
|
+
declare function formatAriary(amount: number | string, options?: AriaryFormatOptions): string;
|
|
112
150
|
/**
|
|
113
151
|
* Writes an amount in old Malagasy francs, with the same layout as {@link formatAriary}.
|
|
114
152
|
*
|
|
115
|
-
* @param francs - Francs from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
153
|
+
* @param francs - Francs from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or decimal text.
|
|
116
154
|
* @param options - Same options as {@link formatAriary}.
|
|
117
155
|
* @returns The formatted amount, e.g. `Fmg 7 500`.
|
|
118
|
-
* @throws {@link MdgError}
|
|
156
|
+
* @throws {@link MdgError} as {@link formatAriary}.
|
|
119
157
|
*
|
|
120
158
|
* @example
|
|
121
159
|
* ```ts
|
|
122
160
|
* formatFmg(7500); // 'Fmg 7 500'
|
|
123
161
|
* ```
|
|
124
162
|
*/
|
|
125
|
-
declare function formatFmg(francs: number, options?: AriaryFormatOptions): string;
|
|
163
|
+
declare function formatFmg(francs: number | string, options?: AriaryFormatOptions): string;
|
|
126
164
|
|
|
127
165
|
/** Version of the shared spec this code was generated from. */
|
|
128
|
-
declare const SPEC_VERSION = "0.
|
|
166
|
+
declare const SPEC_VERSION = "0.9.0";
|
|
129
167
|
/**
|
|
130
168
|
* Error code shared by every mdg-utils language port.
|
|
131
169
|
*
|
|
132
170
|
* - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).
|
|
133
171
|
* - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.
|
|
134
172
|
* - `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
|
|
173
|
+
* - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to an operator.
|
|
174
|
+
* - `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.
|
|
175
|
+
* - `AMBIGUOUS`: The text reads as more than one value, so none is chosen.
|
|
136
176
|
*/
|
|
137
|
-
type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR';
|
|
177
|
+
type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR' | 'PRECISION_LOSS' | 'AMBIGUOUS';
|
|
138
178
|
/**
|
|
139
179
|
* Outcome of a `safeParse…` function: the value, or the error that would have been thrown.
|
|
140
180
|
*
|
|
@@ -191,16 +231,20 @@ declare class MdgError extends Error {
|
|
|
191
231
|
}
|
|
192
232
|
|
|
193
233
|
/**
|
|
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:
|
|
234
|
+
* Options for reading amounts: which `grouping` and `decimal` separators the text uses, and
|
|
235
|
+
* what to give back. Without `grouping`, every Malagasy one is accepted (spaces or dot);
|
|
236
|
+
* `decimal` defaults to a comma. Say so for text in another convention:
|
|
237
|
+
* `{ grouping: 'comma', decimal: 'dot' }`.
|
|
197
238
|
*
|
|
198
239
|
* @example
|
|
199
240
|
* ```ts
|
|
200
241
|
* const english: AriaryParseOptions = { grouping: 'comma', decimal: 'dot' };
|
|
201
242
|
* ```
|
|
202
243
|
*/
|
|
203
|
-
|
|
244
|
+
interface AriaryParseOptions extends AriarySeparatorOptions {
|
|
245
|
+
/** `number` (default), or `text` for the exact decimal digits of any amount. */
|
|
246
|
+
readonly as?: AmountResultKind;
|
|
247
|
+
}
|
|
204
248
|
/**
|
|
205
249
|
* Reads an Ariary amount without throwing.
|
|
206
250
|
*
|
|
@@ -214,7 +258,7 @@ type AriaryParseOptions = AriarySeparatorOptions;
|
|
|
214
258
|
* if (result.success) result.data; // 1500
|
|
215
259
|
* ```
|
|
216
260
|
*/
|
|
217
|
-
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number>;
|
|
261
|
+
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number | string>;
|
|
218
262
|
/**
|
|
219
263
|
* Reads an Ariary amount written in any common way.
|
|
220
264
|
*
|
|
@@ -222,8 +266,8 @@ declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): S
|
|
|
222
266
|
* @param options - Optional `grouping` and `decimal`; see {@link AriaryParseOptions}.
|
|
223
267
|
* @returns The amount, whole or decimal.
|
|
224
268
|
* @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
|
-
* `
|
|
269
|
+
* that is not an amount (signs, a dot as decimal point by default), `PRECISION_LOSS` when a
|
|
270
|
+
* number cannot hold the amount exactly (pass `{ as: 'text' }` for the exact digits).
|
|
227
271
|
*
|
|
228
272
|
* @example
|
|
229
273
|
* ```ts
|
|
@@ -233,7 +277,9 @@ declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): S
|
|
|
233
277
|
* parseAriary('1,500.50', { grouping: 'comma', decimal: 'dot' }); // 1500.5
|
|
234
278
|
* ```
|
|
235
279
|
*/
|
|
236
|
-
declare function parseAriary(text: string, options?:
|
|
280
|
+
declare function parseAriary<Kind extends AmountResultKind = 'number'>(text: string, options?: AriarySeparatorOptions & {
|
|
281
|
+
readonly as?: Kind;
|
|
282
|
+
}): AmountValue<Kind>;
|
|
237
283
|
/**
|
|
238
284
|
* Reads an Ariary amount, or gives `undefined` instead of throwing.
|
|
239
285
|
*
|
|
@@ -249,7 +295,7 @@ declare function parseAriary(text: string, options?: AriaryParseOptions): number
|
|
|
249
295
|
* tryParseAriary('1.5'); // undefined
|
|
250
296
|
* ```
|
|
251
297
|
*/
|
|
252
|
-
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | undefined;
|
|
298
|
+
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | string | undefined;
|
|
253
299
|
|
|
254
300
|
export { MdgError, SPEC_VERSION, ariaryToFmg, fmgToAriary, formatAriary, formatFmg, parseAriary, safeParseAriary, spellAriary, tryParseAriary };
|
|
255
|
-
export type { AriaryDecimal, AriaryFormatOptions, AriaryGrouping, AriaryParseOptions, AriarySeparatorOptions, AriarySymbolPosition, ErrorCode, SafeParseResult };
|
|
301
|
+
export type { AmountResultKind, AmountResultOptions, AmountValue, AriaryDecimal, AriaryFormatOptions, AriaryGrouping, AriaryParseOptions, AriarySeparatorOptions, AriarySymbolPosition, ErrorCode, SafeParseResult };
|