@mdgascar/ariary 0.8.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 +7 -5
- package/dist/index.cjs +119 -67
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +88 -40
- package/dist/index.d.ts +88 -40
- package/dist/index.js +120 -68
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -1,29 +1,64 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { NumberSystem,
|
|
1
|
+
import { SpellOptions } from '@mdgascar/words';
|
|
2
|
+
export { NumberSystem, SpellOptions } from '@mdgascar/words';
|
|
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;
|
|
3
36
|
|
|
4
37
|
/**
|
|
5
38
|
* Writes an Ariary amount in Malagasy words.
|
|
6
39
|
*
|
|
7
|
-
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal
|
|
40
|
+
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or plain
|
|
41
|
+
* decimal text such as `'1500.25'`, read exactly.
|
|
8
42
|
* @param options - The words `system`: `standard` (default) or `traditional`.
|
|
9
43
|
* @returns The amount in words followed by `ariary`; decimals follow `faingo`.
|
|
10
|
-
* @throws {@link MdgError} `INVALID_TYPE` or `OUT_OF_RANGE`.
|
|
44
|
+
* @throws {@link MdgError} `INVALID_TYPE`, `INVALID_FORMAT` (text) or `OUT_OF_RANGE`.
|
|
11
45
|
*
|
|
12
46
|
* @example
|
|
13
47
|
* ```ts
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* ariaryToWords(30_000_000, { system: 'traditional' }); // 'telo safatsiroa ariary'
|
|
48
|
+
* spellAriary(1500); // 'dimanjato sy arivo ariary'
|
|
49
|
+
* spellAriary(1500.5); // 'dimanjato sy arivo faingo dimy ariary'
|
|
17
50
|
* ```
|
|
18
51
|
*/
|
|
19
|
-
declare function
|
|
52
|
+
declare function spellAriary(amount: number | string, options?: SpellOptions): string;
|
|
20
53
|
/**
|
|
21
54
|
* Converts old Malagasy francs (FMG) to ariary: 5 FMG = 1 Ar, exactly in decimal.
|
|
22
55
|
*
|
|
23
|
-
* @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.
|
|
24
58
|
* @returns The amount in ariary: `7501` → `1500.2`.
|
|
25
|
-
* @throws {@link MdgError} `INVALID_TYPE`
|
|
26
|
-
*
|
|
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.
|
|
27
62
|
*
|
|
28
63
|
* @example
|
|
29
64
|
* ```ts
|
|
@@ -31,22 +66,25 @@ declare function ariaryToWords(amount: number, options?: NumberToWordsOptions):
|
|
|
31
66
|
* fmgToAriary(7501); // 1500.2
|
|
32
67
|
* ```
|
|
33
68
|
*/
|
|
34
|
-
declare function fmgToAriary(francs: number
|
|
69
|
+
declare function fmgToAriary<Kind extends AmountResultKind = 'number'>(francs: number | string, options?: {
|
|
70
|
+
readonly as?: Kind;
|
|
71
|
+
}): AmountValue<Kind>;
|
|
35
72
|
/**
|
|
36
73
|
* Converts ariary to old Malagasy francs (FMG): 1 Ar = 5 FMG, exactly in decimal.
|
|
37
74
|
*
|
|
38
|
-
* @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.
|
|
39
77
|
* @returns The amount in francs: `0.07` → `0.35`, never `0.35000000000000003`.
|
|
40
|
-
* @throws {@link MdgError}
|
|
41
|
-
* it is negative or the francs would exceed `Number.MAX_SAFE_INTEGER`.
|
|
78
|
+
* @throws {@link MdgError} as {@link fmgToAriary}.
|
|
42
79
|
*
|
|
43
80
|
* @example
|
|
44
81
|
* ```ts
|
|
45
82
|
* ariaryToFmg(1500); // 7500
|
|
46
|
-
* ariaryToFmg(0.07); // 0.35
|
|
47
83
|
* ```
|
|
48
84
|
*/
|
|
49
|
-
declare function ariaryToFmg(amount: number
|
|
85
|
+
declare function ariaryToFmg<Kind extends AmountResultKind = 'number'>(amount: number | string, options?: {
|
|
86
|
+
readonly as?: Kind;
|
|
87
|
+
}): AmountValue<Kind>;
|
|
50
88
|
|
|
51
89
|
/** Thousand separator used by {@link formatAriary}: `space` (default) or `dot`. */
|
|
52
90
|
type AriaryGrouping = 'space' | 'dot' | 'comma';
|
|
@@ -92,12 +130,14 @@ interface AriaryFormatOptions extends AriarySeparatorOptions {
|
|
|
92
130
|
/**
|
|
93
131
|
* Writes an Ariary amount the Malagasy way: symbol `Ar`, digits grouped by no-break spaces.
|
|
94
132
|
*
|
|
95
|
-
* @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.
|
|
96
135
|
* @param options - Optional `grouping` (`space`, `dot` or `comma`), `decimal` (`comma` or `dot`)
|
|
97
136
|
* and `symbolPosition` (`before` or `after`). The defaults are the Malagasy way.
|
|
98
137
|
* @returns The formatted amount, e.g. `Ar 1 500` or `Ar 1 500,5` (the spaces are U+00A0).
|
|
99
|
-
* @throws {@link MdgError} `INVALID_TYPE` for a value that is not a finite number or
|
|
100
|
-
* `
|
|
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`.
|
|
101
141
|
*
|
|
102
142
|
* @example
|
|
103
143
|
* ```ts
|
|
@@ -106,39 +146,41 @@ interface AriaryFormatOptions extends AriarySeparatorOptions {
|
|
|
106
146
|
* formatAriary(1500.5); // 'Ar 1 500,5'
|
|
107
147
|
* ```
|
|
108
148
|
*/
|
|
109
|
-
declare function formatAriary(amount: number, options?: AriaryFormatOptions): string;
|
|
149
|
+
declare function formatAriary(amount: number | string, options?: AriaryFormatOptions): string;
|
|
110
150
|
/**
|
|
111
151
|
* Writes an amount in old Malagasy francs, with the same layout as {@link formatAriary}.
|
|
112
152
|
*
|
|
113
|
-
* @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.
|
|
114
154
|
* @param options - Same options as {@link formatAriary}.
|
|
115
155
|
* @returns The formatted amount, e.g. `Fmg 7 500`.
|
|
116
|
-
* @throws {@link MdgError}
|
|
156
|
+
* @throws {@link MdgError} as {@link formatAriary}.
|
|
117
157
|
*
|
|
118
158
|
* @example
|
|
119
159
|
* ```ts
|
|
120
160
|
* formatFmg(7500); // 'Fmg 7 500'
|
|
121
161
|
* ```
|
|
122
162
|
*/
|
|
123
|
-
declare function formatFmg(francs: number, options?: AriaryFormatOptions): string;
|
|
163
|
+
declare function formatFmg(francs: number | string, options?: AriaryFormatOptions): string;
|
|
124
164
|
|
|
125
165
|
/** Version of the shared spec this code was generated from. */
|
|
126
|
-
declare const SPEC_VERSION = "0.
|
|
166
|
+
declare const SPEC_VERSION = "0.9.0";
|
|
127
167
|
/**
|
|
128
168
|
* Error code shared by every mdg-utils language port.
|
|
129
169
|
*
|
|
130
170
|
* - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).
|
|
131
171
|
* - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.
|
|
132
172
|
* - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).
|
|
133
|
-
* - `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.
|
|
134
176
|
*/
|
|
135
|
-
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';
|
|
136
178
|
/**
|
|
137
179
|
* Outcome of a `safeParse…` function: the value, or the error that would have been thrown.
|
|
138
180
|
*
|
|
139
181
|
* @example
|
|
140
182
|
* ```ts
|
|
141
|
-
* const result =
|
|
183
|
+
* const result = safeParsePhone(input);
|
|
142
184
|
* if (result.success) console.log(result.data);
|
|
143
185
|
* else console.log(result.error.code);
|
|
144
186
|
* ```
|
|
@@ -156,7 +198,7 @@ type SafeParseResult<T> = {
|
|
|
156
198
|
* @example
|
|
157
199
|
* ```ts
|
|
158
200
|
* try {
|
|
159
|
-
*
|
|
201
|
+
* spell(-1);
|
|
160
202
|
* } catch (error) {
|
|
161
203
|
* if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') {
|
|
162
204
|
* // handle it
|
|
@@ -189,16 +231,20 @@ declare class MdgError extends Error {
|
|
|
189
231
|
}
|
|
190
232
|
|
|
191
233
|
/**
|
|
192
|
-
* Options for reading amounts: which `grouping` and `decimal` separators the text uses
|
|
193
|
-
* Without `grouping`, every Malagasy one is accepted (spaces or dot);
|
|
194
|
-
* 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' }`.
|
|
195
238
|
*
|
|
196
239
|
* @example
|
|
197
240
|
* ```ts
|
|
198
241
|
* const english: AriaryParseOptions = { grouping: 'comma', decimal: 'dot' };
|
|
199
242
|
* ```
|
|
200
243
|
*/
|
|
201
|
-
|
|
244
|
+
interface AriaryParseOptions extends AriarySeparatorOptions {
|
|
245
|
+
/** `number` (default), or `text` for the exact decimal digits of any amount. */
|
|
246
|
+
readonly as?: AmountResultKind;
|
|
247
|
+
}
|
|
202
248
|
/**
|
|
203
249
|
* Reads an Ariary amount without throwing.
|
|
204
250
|
*
|
|
@@ -212,7 +258,7 @@ type AriaryParseOptions = AriarySeparatorOptions;
|
|
|
212
258
|
* if (result.success) result.data; // 1500
|
|
213
259
|
* ```
|
|
214
260
|
*/
|
|
215
|
-
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number>;
|
|
261
|
+
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number | string>;
|
|
216
262
|
/**
|
|
217
263
|
* Reads an Ariary amount written in any common way.
|
|
218
264
|
*
|
|
@@ -220,8 +266,8 @@ declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): S
|
|
|
220
266
|
* @param options - Optional `grouping` and `decimal`; see {@link AriaryParseOptions}.
|
|
221
267
|
* @returns The amount, whole or decimal.
|
|
222
268
|
* @throws {@link MdgError} `INVALID_TYPE` for non-strings or bad options, `INVALID_FORMAT` for text
|
|
223
|
-
* that is not an amount (signs, a dot as decimal point by default), `
|
|
224
|
-
* `
|
|
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).
|
|
225
271
|
*
|
|
226
272
|
* @example
|
|
227
273
|
* ```ts
|
|
@@ -231,7 +277,9 @@ declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): S
|
|
|
231
277
|
* parseAriary('1,500.50', { grouping: 'comma', decimal: 'dot' }); // 1500.5
|
|
232
278
|
* ```
|
|
233
279
|
*/
|
|
234
|
-
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>;
|
|
235
283
|
/**
|
|
236
284
|
* Reads an Ariary amount, or gives `undefined` instead of throwing.
|
|
237
285
|
*
|
|
@@ -247,7 +295,7 @@ declare function parseAriary(text: string, options?: AriaryParseOptions): number
|
|
|
247
295
|
* tryParseAriary('1.5'); // undefined
|
|
248
296
|
* ```
|
|
249
297
|
*/
|
|
250
|
-
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | undefined;
|
|
298
|
+
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | string | undefined;
|
|
251
299
|
|
|
252
|
-
export { MdgError, SPEC_VERSION, ariaryToFmg,
|
|
253
|
-
export type { AriaryDecimal, AriaryFormatOptions, AriaryGrouping, AriaryParseOptions, AriarySeparatorOptions, AriarySymbolPosition, ErrorCode, SafeParseResult };
|
|
300
|
+
export { MdgError, SPEC_VERSION, ariaryToFmg, fmgToAriary, formatAriary, formatFmg, parseAriary, safeParseAriary, spellAriary, tryParseAriary };
|
|
301
|
+
export type { AmountResultKind, AmountResultOptions, AmountValue, AriaryDecimal, AriaryFormatOptions, AriaryGrouping, AriaryParseOptions, AriarySeparatorOptions, AriarySymbolPosition, ErrorCode, SafeParseResult };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,29 +1,64 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { NumberSystem,
|
|
1
|
+
import { SpellOptions } from '@mdgascar/words';
|
|
2
|
+
export { NumberSystem, SpellOptions } from '@mdgascar/words';
|
|
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;
|
|
3
36
|
|
|
4
37
|
/**
|
|
5
38
|
* Writes an Ariary amount in Malagasy words.
|
|
6
39
|
*
|
|
7
|
-
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal
|
|
40
|
+
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal, or plain
|
|
41
|
+
* decimal text such as `'1500.25'`, read exactly.
|
|
8
42
|
* @param options - The words `system`: `standard` (default) or `traditional`.
|
|
9
43
|
* @returns The amount in words followed by `ariary`; decimals follow `faingo`.
|
|
10
|
-
* @throws {@link MdgError} `INVALID_TYPE` or `OUT_OF_RANGE`.
|
|
44
|
+
* @throws {@link MdgError} `INVALID_TYPE`, `INVALID_FORMAT` (text) or `OUT_OF_RANGE`.
|
|
11
45
|
*
|
|
12
46
|
* @example
|
|
13
47
|
* ```ts
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* ariaryToWords(30_000_000, { system: 'traditional' }); // 'telo safatsiroa ariary'
|
|
48
|
+
* spellAriary(1500); // 'dimanjato sy arivo ariary'
|
|
49
|
+
* spellAriary(1500.5); // 'dimanjato sy arivo faingo dimy ariary'
|
|
17
50
|
* ```
|
|
18
51
|
*/
|
|
19
|
-
declare function
|
|
52
|
+
declare function spellAriary(amount: number | string, options?: SpellOptions): string;
|
|
20
53
|
/**
|
|
21
54
|
* Converts old Malagasy francs (FMG) to ariary: 5 FMG = 1 Ar, exactly in decimal.
|
|
22
55
|
*
|
|
23
|
-
* @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.
|
|
24
58
|
* @returns The amount in ariary: `7501` → `1500.2`.
|
|
25
|
-
* @throws {@link MdgError} `INVALID_TYPE`
|
|
26
|
-
*
|
|
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.
|
|
27
62
|
*
|
|
28
63
|
* @example
|
|
29
64
|
* ```ts
|
|
@@ -31,22 +66,25 @@ declare function ariaryToWords(amount: number, options?: NumberToWordsOptions):
|
|
|
31
66
|
* fmgToAriary(7501); // 1500.2
|
|
32
67
|
* ```
|
|
33
68
|
*/
|
|
34
|
-
declare function fmgToAriary(francs: number
|
|
69
|
+
declare function fmgToAriary<Kind extends AmountResultKind = 'number'>(francs: number | string, options?: {
|
|
70
|
+
readonly as?: Kind;
|
|
71
|
+
}): AmountValue<Kind>;
|
|
35
72
|
/**
|
|
36
73
|
* Converts ariary to old Malagasy francs (FMG): 1 Ar = 5 FMG, exactly in decimal.
|
|
37
74
|
*
|
|
38
|
-
* @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.
|
|
39
77
|
* @returns The amount in francs: `0.07` → `0.35`, never `0.35000000000000003`.
|
|
40
|
-
* @throws {@link MdgError}
|
|
41
|
-
* it is negative or the francs would exceed `Number.MAX_SAFE_INTEGER`.
|
|
78
|
+
* @throws {@link MdgError} as {@link fmgToAriary}.
|
|
42
79
|
*
|
|
43
80
|
* @example
|
|
44
81
|
* ```ts
|
|
45
82
|
* ariaryToFmg(1500); // 7500
|
|
46
|
-
* ariaryToFmg(0.07); // 0.35
|
|
47
83
|
* ```
|
|
48
84
|
*/
|
|
49
|
-
declare function ariaryToFmg(amount: number
|
|
85
|
+
declare function ariaryToFmg<Kind extends AmountResultKind = 'number'>(amount: number | string, options?: {
|
|
86
|
+
readonly as?: Kind;
|
|
87
|
+
}): AmountValue<Kind>;
|
|
50
88
|
|
|
51
89
|
/** Thousand separator used by {@link formatAriary}: `space` (default) or `dot`. */
|
|
52
90
|
type AriaryGrouping = 'space' | 'dot' | 'comma';
|
|
@@ -92,12 +130,14 @@ interface AriaryFormatOptions extends AriarySeparatorOptions {
|
|
|
92
130
|
/**
|
|
93
131
|
* Writes an Ariary amount the Malagasy way: symbol `Ar`, digits grouped by no-break spaces.
|
|
94
132
|
*
|
|
95
|
-
* @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.
|
|
96
135
|
* @param options - Optional `grouping` (`space`, `dot` or `comma`), `decimal` (`comma` or `dot`)
|
|
97
136
|
* and `symbolPosition` (`before` or `after`). The defaults are the Malagasy way.
|
|
98
137
|
* @returns The formatted amount, e.g. `Ar 1 500` or `Ar 1 500,5` (the spaces are U+00A0).
|
|
99
|
-
* @throws {@link MdgError} `INVALID_TYPE` for a value that is not a finite number or
|
|
100
|
-
* `
|
|
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`.
|
|
101
141
|
*
|
|
102
142
|
* @example
|
|
103
143
|
* ```ts
|
|
@@ -106,39 +146,41 @@ interface AriaryFormatOptions extends AriarySeparatorOptions {
|
|
|
106
146
|
* formatAriary(1500.5); // 'Ar 1 500,5'
|
|
107
147
|
* ```
|
|
108
148
|
*/
|
|
109
|
-
declare function formatAriary(amount: number, options?: AriaryFormatOptions): string;
|
|
149
|
+
declare function formatAriary(amount: number | string, options?: AriaryFormatOptions): string;
|
|
110
150
|
/**
|
|
111
151
|
* Writes an amount in old Malagasy francs, with the same layout as {@link formatAriary}.
|
|
112
152
|
*
|
|
113
|
-
* @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.
|
|
114
154
|
* @param options - Same options as {@link formatAriary}.
|
|
115
155
|
* @returns The formatted amount, e.g. `Fmg 7 500`.
|
|
116
|
-
* @throws {@link MdgError}
|
|
156
|
+
* @throws {@link MdgError} as {@link formatAriary}.
|
|
117
157
|
*
|
|
118
158
|
* @example
|
|
119
159
|
* ```ts
|
|
120
160
|
* formatFmg(7500); // 'Fmg 7 500'
|
|
121
161
|
* ```
|
|
122
162
|
*/
|
|
123
|
-
declare function formatFmg(francs: number, options?: AriaryFormatOptions): string;
|
|
163
|
+
declare function formatFmg(francs: number | string, options?: AriaryFormatOptions): string;
|
|
124
164
|
|
|
125
165
|
/** Version of the shared spec this code was generated from. */
|
|
126
|
-
declare const SPEC_VERSION = "0.
|
|
166
|
+
declare const SPEC_VERSION = "0.9.0";
|
|
127
167
|
/**
|
|
128
168
|
* Error code shared by every mdg-utils language port.
|
|
129
169
|
*
|
|
130
170
|
* - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).
|
|
131
171
|
* - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.
|
|
132
172
|
* - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).
|
|
133
|
-
* - `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.
|
|
134
176
|
*/
|
|
135
|
-
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';
|
|
136
178
|
/**
|
|
137
179
|
* Outcome of a `safeParse…` function: the value, or the error that would have been thrown.
|
|
138
180
|
*
|
|
139
181
|
* @example
|
|
140
182
|
* ```ts
|
|
141
|
-
* const result =
|
|
183
|
+
* const result = safeParsePhone(input);
|
|
142
184
|
* if (result.success) console.log(result.data);
|
|
143
185
|
* else console.log(result.error.code);
|
|
144
186
|
* ```
|
|
@@ -156,7 +198,7 @@ type SafeParseResult<T> = {
|
|
|
156
198
|
* @example
|
|
157
199
|
* ```ts
|
|
158
200
|
* try {
|
|
159
|
-
*
|
|
201
|
+
* spell(-1);
|
|
160
202
|
* } catch (error) {
|
|
161
203
|
* if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') {
|
|
162
204
|
* // handle it
|
|
@@ -189,16 +231,20 @@ declare class MdgError extends Error {
|
|
|
189
231
|
}
|
|
190
232
|
|
|
191
233
|
/**
|
|
192
|
-
* Options for reading amounts: which `grouping` and `decimal` separators the text uses
|
|
193
|
-
* Without `grouping`, every Malagasy one is accepted (spaces or dot);
|
|
194
|
-
* 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' }`.
|
|
195
238
|
*
|
|
196
239
|
* @example
|
|
197
240
|
* ```ts
|
|
198
241
|
* const english: AriaryParseOptions = { grouping: 'comma', decimal: 'dot' };
|
|
199
242
|
* ```
|
|
200
243
|
*/
|
|
201
|
-
|
|
244
|
+
interface AriaryParseOptions extends AriarySeparatorOptions {
|
|
245
|
+
/** `number` (default), or `text` for the exact decimal digits of any amount. */
|
|
246
|
+
readonly as?: AmountResultKind;
|
|
247
|
+
}
|
|
202
248
|
/**
|
|
203
249
|
* Reads an Ariary amount without throwing.
|
|
204
250
|
*
|
|
@@ -212,7 +258,7 @@ type AriaryParseOptions = AriarySeparatorOptions;
|
|
|
212
258
|
* if (result.success) result.data; // 1500
|
|
213
259
|
* ```
|
|
214
260
|
*/
|
|
215
|
-
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number>;
|
|
261
|
+
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number | string>;
|
|
216
262
|
/**
|
|
217
263
|
* Reads an Ariary amount written in any common way.
|
|
218
264
|
*
|
|
@@ -220,8 +266,8 @@ declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): S
|
|
|
220
266
|
* @param options - Optional `grouping` and `decimal`; see {@link AriaryParseOptions}.
|
|
221
267
|
* @returns The amount, whole or decimal.
|
|
222
268
|
* @throws {@link MdgError} `INVALID_TYPE` for non-strings or bad options, `INVALID_FORMAT` for text
|
|
223
|
-
* that is not an amount (signs, a dot as decimal point by default), `
|
|
224
|
-
* `
|
|
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).
|
|
225
271
|
*
|
|
226
272
|
* @example
|
|
227
273
|
* ```ts
|
|
@@ -231,7 +277,9 @@ declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): S
|
|
|
231
277
|
* parseAriary('1,500.50', { grouping: 'comma', decimal: 'dot' }); // 1500.5
|
|
232
278
|
* ```
|
|
233
279
|
*/
|
|
234
|
-
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>;
|
|
235
283
|
/**
|
|
236
284
|
* Reads an Ariary amount, or gives `undefined` instead of throwing.
|
|
237
285
|
*
|
|
@@ -247,7 +295,7 @@ declare function parseAriary(text: string, options?: AriaryParseOptions): number
|
|
|
247
295
|
* tryParseAriary('1.5'); // undefined
|
|
248
296
|
* ```
|
|
249
297
|
*/
|
|
250
|
-
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | undefined;
|
|
298
|
+
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | string | undefined;
|
|
251
299
|
|
|
252
|
-
export { MdgError, SPEC_VERSION, ariaryToFmg,
|
|
253
|
-
export type { AriaryDecimal, AriaryFormatOptions, AriaryGrouping, AriaryParseOptions, AriarySeparatorOptions, AriarySymbolPosition, ErrorCode, SafeParseResult };
|
|
300
|
+
export { MdgError, SPEC_VERSION, ariaryToFmg, fmgToAriary, formatAriary, formatFmg, parseAriary, safeParseAriary, spellAriary, tryParseAriary };
|
|
301
|
+
export type { AmountResultKind, AmountResultOptions, AmountValue, AriaryDecimal, AriaryFormatOptions, AriaryGrouping, AriaryParseOptions, AriarySeparatorOptions, AriarySymbolPosition, ErrorCode, SafeParseResult };
|