@mdgascar/ariary 0.0.0-stage → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +48 -2
- package/dist/index.cjs +306 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +248 -0
- package/dist/index.d.ts +248 -0
- package/dist/index.js +295 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -3
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes an Ariary amount in Malagasy words.
|
|
3
|
+
*
|
|
4
|
+
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
5
|
+
* @returns The amount in words followed by `ariary`; decimals follow `faingo`.
|
|
6
|
+
* @throws {@link MdgError} `INVALID_TYPE` or `OUT_OF_RANGE`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* ariaryToWords(1500); // 'dimanjato sy arivo ariary'
|
|
11
|
+
* ariaryToWords(1500.5); // 'dimanjato sy arivo faingo dimy ariary'
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
declare function ariaryToWords(amount: number): string;
|
|
15
|
+
/**
|
|
16
|
+
* Converts old Malagasy francs (FMG) to ariary: 5 FMG = 1 Ar, exactly in decimal.
|
|
17
|
+
*
|
|
18
|
+
* @param francs - Francs from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
19
|
+
* @returns The amount in ariary: `7501` → `1500.2`.
|
|
20
|
+
* @throws {@link MdgError} `INVALID_TYPE` if `francs` is not a finite number, `OUT_OF_RANGE` when
|
|
21
|
+
* it is negative or above `Number.MAX_SAFE_INTEGER`.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* fmgToAriary(7500); // 1500
|
|
26
|
+
* fmgToAriary(7501); // 1500.2
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
declare function fmgToAriary(francs: number): number;
|
|
30
|
+
/**
|
|
31
|
+
* Converts ariary to old Malagasy francs (FMG): 1 Ar = 5 FMG, exactly in decimal.
|
|
32
|
+
*
|
|
33
|
+
* @param amount - Ariary from 0 up, whole or decimal.
|
|
34
|
+
* @returns The amount in francs: `0.07` → `0.35`, never `0.35000000000000003`.
|
|
35
|
+
* @throws {@link MdgError} `INVALID_TYPE` if `amount` is not a finite number, `OUT_OF_RANGE` when
|
|
36
|
+
* it is negative or the francs would exceed `Number.MAX_SAFE_INTEGER`.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* ariaryToFmg(1500); // 7500
|
|
41
|
+
* ariaryToFmg(0.07); // 0.35
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
declare function ariaryToFmg(amount: number): number;
|
|
45
|
+
|
|
46
|
+
/** Thousand separator used by {@link formatAriary}: `space` (default) or `dot`. */
|
|
47
|
+
type AriaryGrouping = 'space' | 'dot' | 'comma';
|
|
48
|
+
/** Decimal separator used by {@link formatAriary} and the parsers: `comma` (default) or `dot`. */
|
|
49
|
+
type AriaryDecimal = 'comma' | 'dot';
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Separator options for formatting and reading amounts.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* const english: AriarySeparatorOptions = { grouping: 'comma', decimal: 'dot' };
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
interface AriarySeparatorOptions {
|
|
60
|
+
/** Thousand separator: `space` (default), `dot`, or `comma` for another convention. */
|
|
61
|
+
readonly grouping?: AriaryGrouping;
|
|
62
|
+
/** Decimal separator: `comma` (default), or `dot` for another convention. */
|
|
63
|
+
readonly decimal?: AriaryDecimal;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Where the currency symbol goes: `before` (default, `Ar 1 500`) or `after` (`1 500 Ar`).
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* const position: AriarySymbolPosition = 'after';
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
type AriarySymbolPosition = 'before' | 'after';
|
|
75
|
+
/**
|
|
76
|
+
* Layout options for {@link formatAriary} and {@link formatFmg}.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* const options: AriaryFormatOptions = { grouping: 'dot', symbolPosition: 'after' };
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
interface AriaryFormatOptions extends AriarySeparatorOptions {
|
|
84
|
+
/** Symbol `before` (default) or `after` the amount. */
|
|
85
|
+
readonly symbolPosition?: AriarySymbolPosition;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Writes an Ariary amount the Malagasy way: symbol `Ar`, digits grouped by no-break spaces.
|
|
89
|
+
*
|
|
90
|
+
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
91
|
+
* @param options - Optional `grouping` (`space`, `dot` or `comma`), `decimal` (`comma` or `dot`)
|
|
92
|
+
* and `symbolPosition` (`before` or `after`). The defaults are the Malagasy way.
|
|
93
|
+
* @returns The formatted amount, e.g. `Ar 1 500` or `Ar 1 500,5` (the spaces are U+00A0).
|
|
94
|
+
* @throws {@link MdgError} `INVALID_TYPE` for a value that is not a finite number or unknown options,
|
|
95
|
+
* `OUT_OF_RANGE` outside 0 to `Number.MAX_SAFE_INTEGER`.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* formatAriary(1500); // 'Ar 1 500'
|
|
100
|
+
* formatAriary(1500, { grouping: 'dot', symbolPosition: 'after' }); // '1.500 Ar'
|
|
101
|
+
* formatAriary(1500.5); // 'Ar 1 500,5'
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
declare function formatAriary(amount: number, options?: AriaryFormatOptions): string;
|
|
105
|
+
/**
|
|
106
|
+
* Writes an amount in old Malagasy francs, with the same layout as {@link formatAriary}.
|
|
107
|
+
*
|
|
108
|
+
* @param francs - Francs from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
109
|
+
* @param options - Same options as {@link formatAriary}.
|
|
110
|
+
* @returns The formatted amount, e.g. `Fmg 7 500`.
|
|
111
|
+
* @throws {@link MdgError} `INVALID_TYPE` or `OUT_OF_RANGE`, as {@link formatAriary}.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* formatFmg(7500); // 'Fmg 7 500'
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
declare function formatFmg(francs: number, options?: AriaryFormatOptions): string;
|
|
119
|
+
|
|
120
|
+
/** Version of the shared spec this code was generated from. */
|
|
121
|
+
declare const SPEC_VERSION = "0.6.0";
|
|
122
|
+
/**
|
|
123
|
+
* Error code shared by every mdg-utils language port.
|
|
124
|
+
*
|
|
125
|
+
* - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).
|
|
126
|
+
* - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.
|
|
127
|
+
* - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).
|
|
128
|
+
* - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to a mobile operator.
|
|
129
|
+
*/
|
|
130
|
+
type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR';
|
|
131
|
+
/**
|
|
132
|
+
* Outcome of a `safeParse…` function: the value, or the error that would have been thrown.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* const result = safeParsePhoneNumber(input);
|
|
137
|
+
* if (result.success) console.log(result.data);
|
|
138
|
+
* else console.log(result.error.code);
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
type SafeParseResult<T> = {
|
|
142
|
+
readonly success: true;
|
|
143
|
+
readonly data: T;
|
|
144
|
+
} | {
|
|
145
|
+
readonly success: false;
|
|
146
|
+
readonly error: MdgError;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Error thrown by mdg-utils functions. Check `code`, which is the same in every language.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* try {
|
|
154
|
+
* numberToWords(-1);
|
|
155
|
+
* } catch (error) {
|
|
156
|
+
* if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') {
|
|
157
|
+
* // handle it
|
|
158
|
+
* }
|
|
159
|
+
* }
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
declare class MdgError extends Error {
|
|
163
|
+
/** Always `'MdgError'`. */
|
|
164
|
+
readonly name = "MdgError";
|
|
165
|
+
/** Why the call failed; the same code in every language. */
|
|
166
|
+
readonly code: ErrorCode;
|
|
167
|
+
/**
|
|
168
|
+
* Creates an error with a shared spec code.
|
|
169
|
+
*
|
|
170
|
+
* @param code - Shared spec error code.
|
|
171
|
+
* @param message - Human-readable explanation, in English.
|
|
172
|
+
*/
|
|
173
|
+
constructor(code: ErrorCode, message: string);
|
|
174
|
+
/**
|
|
175
|
+
* Makes `instanceof MdgError` true for errors from any mdg-utils package or build.
|
|
176
|
+
*
|
|
177
|
+
* @remarks Each package, and its ESM and CommonJS builds, has its own copy of this class,
|
|
178
|
+
* so the check looks at the error's shape instead of its prototype chain.
|
|
179
|
+
*
|
|
180
|
+
* @param value - Any value, usually a caught error.
|
|
181
|
+
* @returns `true` when `value` is an mdg-utils error.
|
|
182
|
+
*/
|
|
183
|
+
static [Symbol.hasInstance](value: unknown): boolean;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Options for reading amounts: which `grouping` and `decimal` separators the text uses.
|
|
188
|
+
* Without `grouping`, every Malagasy one is accepted (spaces or dot); `decimal` defaults to a
|
|
189
|
+
* comma. Say so for text in another convention: `{ grouping: 'comma', decimal: 'dot' }`.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* const english: AriaryParseOptions = { grouping: 'comma', decimal: 'dot' };
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
type AriaryParseOptions = AriarySeparatorOptions;
|
|
197
|
+
/**
|
|
198
|
+
* Reads an Ariary amount without throwing.
|
|
199
|
+
*
|
|
200
|
+
* @param text - An amount such as `Ar 1 500`, `1.500 Ar`, `MGA 1500` or `1 500,50`.
|
|
201
|
+
* @param options - Optional `grouping` and `decimal`; see {@link AriaryParseOptions}.
|
|
202
|
+
* @returns `{ success: true, data }` with the amount, or `{ success: false, error }`.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```ts
|
|
206
|
+
* const result = safeParseAriary('1 500 Ar');
|
|
207
|
+
* if (result.success) result.data; // 1500
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number>;
|
|
211
|
+
/**
|
|
212
|
+
* Reads an Ariary amount written in any common way.
|
|
213
|
+
*
|
|
214
|
+
* @param text - An amount such as `Ar 1 500`, `1.500 Ar`, `MGA 1500` or `1 500,50`.
|
|
215
|
+
* @param options - Optional `grouping` and `decimal`; see {@link AriaryParseOptions}.
|
|
216
|
+
* @returns The amount, whole or decimal.
|
|
217
|
+
* @throws {@link MdgError} `INVALID_TYPE` for non-strings or bad options, `INVALID_FORMAT` for text
|
|
218
|
+
* that is not an amount (signs, a dot as decimal point by default), `OUT_OF_RANGE` above
|
|
219
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* parseAriary('Ar 1 500'); // 1500
|
|
224
|
+
* parseAriary('1.500 Ar'); // 1500
|
|
225
|
+
* parseAriary('1 500,50 Ar'); // 1500.5
|
|
226
|
+
* parseAriary('1,500.50', { grouping: 'comma', decimal: 'dot' }); // 1500.5
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
declare function parseAriary(text: string, options?: AriaryParseOptions): number;
|
|
230
|
+
/**
|
|
231
|
+
* Reads an Ariary amount, or gives `undefined` instead of throwing.
|
|
232
|
+
*
|
|
233
|
+
* Use {@link safeParseAriary} when you need to know why the text was rejected.
|
|
234
|
+
*
|
|
235
|
+
* @param text - An amount such as `Ar 1 500`, `1.500 Ar`, `MGA 1500` or `1 500,50`.
|
|
236
|
+
* @param options - Optional `grouping` and `decimal`; see {@link AriaryParseOptions}.
|
|
237
|
+
* @returns The amount, or `undefined` when {@link parseAriary} would throw.
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* tryParseAriary('1.500 Ar'); // 1500
|
|
242
|
+
* tryParseAriary('1.5'); // undefined
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | undefined;
|
|
246
|
+
|
|
247
|
+
export { MdgError, SPEC_VERSION, ariaryToFmg, ariaryToWords, fmgToAriary, formatAriary, formatFmg, parseAriary, safeParseAriary, tryParseAriary };
|
|
248
|
+
export type { AriaryDecimal, AriaryFormatOptions, AriaryGrouping, AriaryParseOptions, AriarySeparatorOptions, AriarySymbolPosition, ErrorCode, SafeParseResult };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Writes an Ariary amount in Malagasy words.
|
|
3
|
+
*
|
|
4
|
+
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
5
|
+
* @returns The amount in words followed by `ariary`; decimals follow `faingo`.
|
|
6
|
+
* @throws {@link MdgError} `INVALID_TYPE` or `OUT_OF_RANGE`.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* ariaryToWords(1500); // 'dimanjato sy arivo ariary'
|
|
11
|
+
* ariaryToWords(1500.5); // 'dimanjato sy arivo faingo dimy ariary'
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
declare function ariaryToWords(amount: number): string;
|
|
15
|
+
/**
|
|
16
|
+
* Converts old Malagasy francs (FMG) to ariary: 5 FMG = 1 Ar, exactly in decimal.
|
|
17
|
+
*
|
|
18
|
+
* @param francs - Francs from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
19
|
+
* @returns The amount in ariary: `7501` → `1500.2`.
|
|
20
|
+
* @throws {@link MdgError} `INVALID_TYPE` if `francs` is not a finite number, `OUT_OF_RANGE` when
|
|
21
|
+
* it is negative or above `Number.MAX_SAFE_INTEGER`.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* fmgToAriary(7500); // 1500
|
|
26
|
+
* fmgToAriary(7501); // 1500.2
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
declare function fmgToAriary(francs: number): number;
|
|
30
|
+
/**
|
|
31
|
+
* Converts ariary to old Malagasy francs (FMG): 1 Ar = 5 FMG, exactly in decimal.
|
|
32
|
+
*
|
|
33
|
+
* @param amount - Ariary from 0 up, whole or decimal.
|
|
34
|
+
* @returns The amount in francs: `0.07` → `0.35`, never `0.35000000000000003`.
|
|
35
|
+
* @throws {@link MdgError} `INVALID_TYPE` if `amount` is not a finite number, `OUT_OF_RANGE` when
|
|
36
|
+
* it is negative or the francs would exceed `Number.MAX_SAFE_INTEGER`.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* ariaryToFmg(1500); // 7500
|
|
41
|
+
* ariaryToFmg(0.07); // 0.35
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
declare function ariaryToFmg(amount: number): number;
|
|
45
|
+
|
|
46
|
+
/** Thousand separator used by {@link formatAriary}: `space` (default) or `dot`. */
|
|
47
|
+
type AriaryGrouping = 'space' | 'dot' | 'comma';
|
|
48
|
+
/** Decimal separator used by {@link formatAriary} and the parsers: `comma` (default) or `dot`. */
|
|
49
|
+
type AriaryDecimal = 'comma' | 'dot';
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Separator options for formatting and reading amounts.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* const english: AriarySeparatorOptions = { grouping: 'comma', decimal: 'dot' };
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
interface AriarySeparatorOptions {
|
|
60
|
+
/** Thousand separator: `space` (default), `dot`, or `comma` for another convention. */
|
|
61
|
+
readonly grouping?: AriaryGrouping;
|
|
62
|
+
/** Decimal separator: `comma` (default), or `dot` for another convention. */
|
|
63
|
+
readonly decimal?: AriaryDecimal;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Where the currency symbol goes: `before` (default, `Ar 1 500`) or `after` (`1 500 Ar`).
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* const position: AriarySymbolPosition = 'after';
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
type AriarySymbolPosition = 'before' | 'after';
|
|
75
|
+
/**
|
|
76
|
+
* Layout options for {@link formatAriary} and {@link formatFmg}.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* const options: AriaryFormatOptions = { grouping: 'dot', symbolPosition: 'after' };
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
interface AriaryFormatOptions extends AriarySeparatorOptions {
|
|
84
|
+
/** Symbol `before` (default) or `after` the amount. */
|
|
85
|
+
readonly symbolPosition?: AriarySymbolPosition;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Writes an Ariary amount the Malagasy way: symbol `Ar`, digits grouped by no-break spaces.
|
|
89
|
+
*
|
|
90
|
+
* @param amount - Ariary from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
91
|
+
* @param options - Optional `grouping` (`space`, `dot` or `comma`), `decimal` (`comma` or `dot`)
|
|
92
|
+
* and `symbolPosition` (`before` or `after`). The defaults are the Malagasy way.
|
|
93
|
+
* @returns The formatted amount, e.g. `Ar 1 500` or `Ar 1 500,5` (the spaces are U+00A0).
|
|
94
|
+
* @throws {@link MdgError} `INVALID_TYPE` for a value that is not a finite number or unknown options,
|
|
95
|
+
* `OUT_OF_RANGE` outside 0 to `Number.MAX_SAFE_INTEGER`.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```ts
|
|
99
|
+
* formatAriary(1500); // 'Ar 1 500'
|
|
100
|
+
* formatAriary(1500, { grouping: 'dot', symbolPosition: 'after' }); // '1.500 Ar'
|
|
101
|
+
* formatAriary(1500.5); // 'Ar 1 500,5'
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
declare function formatAriary(amount: number, options?: AriaryFormatOptions): string;
|
|
105
|
+
/**
|
|
106
|
+
* Writes an amount in old Malagasy francs, with the same layout as {@link formatAriary}.
|
|
107
|
+
*
|
|
108
|
+
* @param francs - Francs from 0 to `Number.MAX_SAFE_INTEGER`, whole or decimal.
|
|
109
|
+
* @param options - Same options as {@link formatAriary}.
|
|
110
|
+
* @returns The formatted amount, e.g. `Fmg 7 500`.
|
|
111
|
+
* @throws {@link MdgError} `INVALID_TYPE` or `OUT_OF_RANGE`, as {@link formatAriary}.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* formatFmg(7500); // 'Fmg 7 500'
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
declare function formatFmg(francs: number, options?: AriaryFormatOptions): string;
|
|
119
|
+
|
|
120
|
+
/** Version of the shared spec this code was generated from. */
|
|
121
|
+
declare const SPEC_VERSION = "0.6.0";
|
|
122
|
+
/**
|
|
123
|
+
* Error code shared by every mdg-utils language port.
|
|
124
|
+
*
|
|
125
|
+
* - `INVALID_TYPE`: The input is not of the accepted type (for example a string or a non-integer where an integer is expected).
|
|
126
|
+
* - `OUT_OF_RANGE`: The input has the right type but lies outside the supported range.
|
|
127
|
+
* - `INVALID_FORMAT`: The input text cannot be read as a value of this module (wrong characters or wrong length).
|
|
128
|
+
* - `UNKNOWN_OPERATOR`: The phone number is well formed but its operator code is not assigned to a mobile operator.
|
|
129
|
+
*/
|
|
130
|
+
type ErrorCode = 'INVALID_TYPE' | 'OUT_OF_RANGE' | 'INVALID_FORMAT' | 'UNKNOWN_OPERATOR';
|
|
131
|
+
/**
|
|
132
|
+
* Outcome of a `safeParse…` function: the value, or the error that would have been thrown.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* const result = safeParsePhoneNumber(input);
|
|
137
|
+
* if (result.success) console.log(result.data);
|
|
138
|
+
* else console.log(result.error.code);
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
type SafeParseResult<T> = {
|
|
142
|
+
readonly success: true;
|
|
143
|
+
readonly data: T;
|
|
144
|
+
} | {
|
|
145
|
+
readonly success: false;
|
|
146
|
+
readonly error: MdgError;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Error thrown by mdg-utils functions. Check `code`, which is the same in every language.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* try {
|
|
154
|
+
* numberToWords(-1);
|
|
155
|
+
* } catch (error) {
|
|
156
|
+
* if (error instanceof MdgError && error.code === 'OUT_OF_RANGE') {
|
|
157
|
+
* // handle it
|
|
158
|
+
* }
|
|
159
|
+
* }
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
declare class MdgError extends Error {
|
|
163
|
+
/** Always `'MdgError'`. */
|
|
164
|
+
readonly name = "MdgError";
|
|
165
|
+
/** Why the call failed; the same code in every language. */
|
|
166
|
+
readonly code: ErrorCode;
|
|
167
|
+
/**
|
|
168
|
+
* Creates an error with a shared spec code.
|
|
169
|
+
*
|
|
170
|
+
* @param code - Shared spec error code.
|
|
171
|
+
* @param message - Human-readable explanation, in English.
|
|
172
|
+
*/
|
|
173
|
+
constructor(code: ErrorCode, message: string);
|
|
174
|
+
/**
|
|
175
|
+
* Makes `instanceof MdgError` true for errors from any mdg-utils package or build.
|
|
176
|
+
*
|
|
177
|
+
* @remarks Each package, and its ESM and CommonJS builds, has its own copy of this class,
|
|
178
|
+
* so the check looks at the error's shape instead of its prototype chain.
|
|
179
|
+
*
|
|
180
|
+
* @param value - Any value, usually a caught error.
|
|
181
|
+
* @returns `true` when `value` is an mdg-utils error.
|
|
182
|
+
*/
|
|
183
|
+
static [Symbol.hasInstance](value: unknown): boolean;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Options for reading amounts: which `grouping` and `decimal` separators the text uses.
|
|
188
|
+
* Without `grouping`, every Malagasy one is accepted (spaces or dot); `decimal` defaults to a
|
|
189
|
+
* comma. Say so for text in another convention: `{ grouping: 'comma', decimal: 'dot' }`.
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* const english: AriaryParseOptions = { grouping: 'comma', decimal: 'dot' };
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
type AriaryParseOptions = AriarySeparatorOptions;
|
|
197
|
+
/**
|
|
198
|
+
* Reads an Ariary amount without throwing.
|
|
199
|
+
*
|
|
200
|
+
* @param text - An amount such as `Ar 1 500`, `1.500 Ar`, `MGA 1500` or `1 500,50`.
|
|
201
|
+
* @param options - Optional `grouping` and `decimal`; see {@link AriaryParseOptions}.
|
|
202
|
+
* @returns `{ success: true, data }` with the amount, or `{ success: false, error }`.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* ```ts
|
|
206
|
+
* const result = safeParseAriary('1 500 Ar');
|
|
207
|
+
* if (result.success) result.data; // 1500
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
declare function safeParseAriary(text: unknown, options?: AriaryParseOptions): SafeParseResult<number>;
|
|
211
|
+
/**
|
|
212
|
+
* Reads an Ariary amount written in any common way.
|
|
213
|
+
*
|
|
214
|
+
* @param text - An amount such as `Ar 1 500`, `1.500 Ar`, `MGA 1500` or `1 500,50`.
|
|
215
|
+
* @param options - Optional `grouping` and `decimal`; see {@link AriaryParseOptions}.
|
|
216
|
+
* @returns The amount, whole or decimal.
|
|
217
|
+
* @throws {@link MdgError} `INVALID_TYPE` for non-strings or bad options, `INVALID_FORMAT` for text
|
|
218
|
+
* that is not an amount (signs, a dot as decimal point by default), `OUT_OF_RANGE` above
|
|
219
|
+
* `Number.MAX_SAFE_INTEGER`.
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* parseAriary('Ar 1 500'); // 1500
|
|
224
|
+
* parseAriary('1.500 Ar'); // 1500
|
|
225
|
+
* parseAriary('1 500,50 Ar'); // 1500.5
|
|
226
|
+
* parseAriary('1,500.50', { grouping: 'comma', decimal: 'dot' }); // 1500.5
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
declare function parseAriary(text: string, options?: AriaryParseOptions): number;
|
|
230
|
+
/**
|
|
231
|
+
* Reads an Ariary amount, or gives `undefined` instead of throwing.
|
|
232
|
+
*
|
|
233
|
+
* Use {@link safeParseAriary} when you need to know why the text was rejected.
|
|
234
|
+
*
|
|
235
|
+
* @param text - An amount such as `Ar 1 500`, `1.500 Ar`, `MGA 1500` or `1 500,50`.
|
|
236
|
+
* @param options - Optional `grouping` and `decimal`; see {@link AriaryParseOptions}.
|
|
237
|
+
* @returns The amount, or `undefined` when {@link parseAriary} would throw.
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* tryParseAriary('1.500 Ar'); // 1500
|
|
242
|
+
* tryParseAriary('1.5'); // undefined
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
declare function tryParseAriary(text: unknown, options?: AriaryParseOptions): number | undefined;
|
|
246
|
+
|
|
247
|
+
export { MdgError, SPEC_VERSION, ariaryToFmg, ariaryToWords, fmgToAriary, formatAriary, formatFmg, parseAriary, safeParseAriary, tryParseAriary };
|
|
248
|
+
export type { AriaryDecimal, AriaryFormatOptions, AriaryGrouping, AriaryParseOptions, AriarySeparatorOptions, AriarySymbolPosition, ErrorCode, SafeParseResult };
|