@pbkware/dot-net-date-number-formatting 0.2.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.
@@ -0,0 +1,795 @@
1
+ import { Result } from '@pbkware/js-utils';
2
+
3
+ /**
4
+ * Formatter for dates and times using .NET-compatible format strings.
5
+ *
6
+ * Supports both standard format strings (d, D, f, F, g, G, M, O, s, t, T, u, Y) and
7
+ * custom format strings with specific date/time components (yyyy, MM, dd, HH, mm, ss, etc.).
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const formatter = new DotNetDateTimeFormatter();
12
+ * formatter.localeSettings = DotNetLocaleSettings.createInvariant();
13
+ *
14
+ * // Standard format strings
15
+ * formatter.trySetFormat('d'); // Short date
16
+ * console.log(formatter.toString(new Date(2024, 6, 5))); // "7/5/2024"
17
+ *
18
+ * formatter.trySetFormat('F'); // Full date/time
19
+ * console.log(formatter.toString(new Date(2024, 6, 5, 14, 30, 0)));
20
+ * // "Friday, July 5, 2024 2:30:00 PM"
21
+ *
22
+ * // Custom format strings
23
+ * formatter.trySetFormat('yyyy-MM-dd');
24
+ * console.log(formatter.toString(new Date(2024, 6, 5))); // "2024-07-05"
25
+ *
26
+ * formatter.trySetFormat("dddd, MMMM d 'at' h:mm tt");
27
+ * console.log(formatter.toString(new Date(2024, 6, 5, 14, 30, 0)));
28
+ * // "Friday, July 5 at 2:30 PM"
29
+ * ```
30
+ *
31
+ * @public
32
+ * @category DateTime Formatting
33
+ */
34
+ export declare class DotNetDateTimeFormatter {
35
+ /**
36
+ * Set of date/time style flags that are not currently supported by this implementation.
37
+ * Operations using these styles will fail.
38
+ */
39
+ static readonly unsupportedStyles: Set<DotNetDateTimeStyleId>;
40
+ private format;
41
+ /**
42
+ * The set of {@link DotNetDateTimeStyleId} flags that control date/time parsing behavior.
43
+ * Currently only used for future parsing functionality.
44
+ */
45
+ styles: DotNetDateTimeStyleSet;
46
+ /**
47
+ * The locale settings that determine date/time separators and formatting conventions.
48
+ */
49
+ localeSettings: DotNetLocaleSettings;
50
+ private formatIsStandard;
51
+ private elements;
52
+ /**
53
+ * Contains the error message from the last failed operation.
54
+ * Check this property if {@link DotNetDateTimeFormatter.trySetFormat} returns an error.
55
+ */
56
+ parseErrorText: string;
57
+ /**
58
+ * Sets the format string to use for formatting dates and times.
59
+ *
60
+ * The format string is tokenized to confirm validity and to optimize formatting/parsing operations.
61
+ *
62
+ * @param value - A standard format string (single character like 'd', 'F', 'o') or
63
+ * custom format string (e.g., "yyyy-MM-dd HH:mm:ss").
64
+ * @returns A Result indicating success or containing an error message if the format string is invalid.
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * // Standard format
69
+ * formatter.trySetFormat('d'); // Short date
70
+ *
71
+ * // Custom format
72
+ * formatter.trySetFormat('yyyy-MM-dd HH:mm:ss');
73
+ *
74
+ * // Check for errors
75
+ * const result = formatter.trySetFormat('');
76
+ * if (result.isErr()) {
77
+ * console.error(result.error); // "Format text cannot be empty"
78
+ * }
79
+ * ```
80
+ */
81
+ trySetFormat(value: string): Result<void>;
82
+ /**
83
+ * Formats a Date using the current format string.
84
+ *
85
+ * @param value - The Date to format.
86
+ * @returns The formatted date/time string.
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * const date = new Date(2024, 6, 5, 14, 30, 45);
91
+ *
92
+ * formatter.trySetFormat('d');
93
+ * console.log(formatter.toString(date)); // "7/5/2024"
94
+ *
95
+ * formatter.trySetFormat('yyyy-MM-dd HH:mm:ss');
96
+ * console.log(formatter.toString(date)); // "2024-07-05 14:30:45"
97
+ * ```
98
+ */
99
+ toString(value: Date): string;
100
+ tryFromString(strValue: string): Result<Date>;
101
+ }
102
+
103
+ /**
104
+ * Individual style flags that control date/time parsing behavior.
105
+ *
106
+ * These flags can be combined to create custom parsing rules.
107
+ *
108
+ * @example
109
+ * ```typescript
110
+ * // Combine individual flags
111
+ * formatter.styles = new Set([
112
+ * DotNetDateTimeStyleId.AllowLeadingWhite,
113
+ * DotNetDateTimeStyleId.AllowTrailingWhite
114
+ * ]);
115
+ * ```
116
+ *
117
+ * @public
118
+ * @category DateTime Styles
119
+ */
120
+ export declare enum DotNetDateTimeStyleId {
121
+ /** Allow leading whitespace characters. */
122
+ AllowLeadingWhite = "AllowLeadingWhite",
123
+ /** Allow trailing whitespace characters. */
124
+ AllowTrailingWhite = "AllowTrailingWhite",
125
+ /** Allow whitespace within the date/time string. */
126
+ AllowInnerWhite = "AllowInnerWhite",
127
+ /** Do not use current date for missing date components. */
128
+ NoCurrentDateDefault = "NoCurrentDateDefault",
129
+ /** Adjust date/time to UTC (not implemented). */
130
+ AdjustToUniversal = "AdjustToUniversal",
131
+ /** Assume local time zone if not specified (not implemented). */
132
+ AssumeLocal = "AssumeLocal",
133
+ /** Assume UTC time zone if not specified (not implemented). */
134
+ AssumeUniversal = "AssumeUniversal",
135
+ /** Preserve DateTimeKind when parsing (not implemented). */
136
+ RoundTripKind = "RoundTripKind"
137
+ }
138
+
139
+ /**
140
+ * Predefined date/time style combinations for common parsing scenarios.
141
+ *
142
+ * @internal
143
+ * @category DateTime Styles
144
+ */
145
+ export declare const DotNetDateTimeStyles: {
146
+ /** No styles - strict parsing. */
147
+ none: Set<DotNetDateTimeStyleId>;
148
+ /**
149
+ * Allow whitespace in date/time strings.
150
+ * Includes: AllowLeadingWhite, AllowTrailingWhite, AllowInnerWhite.
151
+ */
152
+ allowWhiteSpaces: Set<DotNetDateTimeStyleId>;
153
+ };
154
+
155
+ /**
156
+ * A set of {@link DotNetDateTimeStyleId} flags.
157
+ *
158
+ * @public
159
+ * @category DateTime Styles
160
+ */
161
+ export declare type DotNetDateTimeStyleSet = Set<DotNetDateTimeStyleId>;
162
+
163
+ /** @internal */
164
+ export declare class DotNetDateTimeStylesInfo {
165
+ static toString(styles: DotNetDateTimeStyleSet): string;
166
+ static toXmlValue(styles: DotNetDateTimeStyleSet): string;
167
+ static tryFromString(value: string): Result<DotNetDateTimeStyleSet>;
168
+ static tryFromXmlValue(value: string): Result<DotNetDateTimeStyleSet>;
169
+ }
170
+
171
+ /**
172
+ * Formatter for decimal numbers with high precision using .NET-compatible format strings.
173
+ *
174
+ * Similar to {@link DotNetFloatFormatter} but with different default precision behavior.
175
+ * When no format is specified, trailing zeros after the decimal point are automatically trimmed.
176
+ *
177
+ * @example
178
+ * ```typescript
179
+ * const formatter = new DotNetDecimalFormatter();
180
+ * formatter.localeSettings = DotNetLocaleSettings.createInvariant();
181
+ *
182
+ * // High precision formatting
183
+ * formatter.trySetFormat('F6');
184
+ * console.log(formatter.toString(123.456)); // "123.456000"
185
+ *
186
+ * // Default behavior (trims trailing zeros)
187
+ * console.log(formatter.toString(123.400)); // "123.4"
188
+ * ```
189
+ *
190
+ * @public
191
+ * @category Numeric Formatting
192
+ */
193
+ export declare class DotNetDecimalFormatter extends DotNetNumberFormatter {
194
+ /**
195
+ * Formats a number using the current format string.
196
+ * When no format is specified, trailing zeros are trimmed.
197
+ *
198
+ * @param value - The number to format.
199
+ * @returns The formatted number string.
200
+ */
201
+ toString(value: number): string;
202
+ /**
203
+ * Attempts to parse a number from a string using the current parsing styles.
204
+ *
205
+ * @param strValue - The string to parse.
206
+ * @returns A Result containing the parsed number if successful, or an error if parsing fails.
207
+ */
208
+ tryFromString(strValue: string): Result<number>;
209
+ }
210
+
211
+ /**
212
+ * Formatter for floating-point numbers using .NET-compatible format strings.
213
+ *
214
+ * Supports all standard numeric format strings (C, D, E, F, G, N, P, R, X, B) and
215
+ * custom format strings with digit placeholders, separators, and sections.
216
+ *
217
+ * @example
218
+ * ```typescript
219
+ * const formatter = new DotNetFloatFormatter();
220
+ * formatter.localeSettings = DotNetLocaleSettings.createInvariant();
221
+ *
222
+ * // Currency format
223
+ * formatter.trySetFormat('C2');
224
+ * console.log(formatter.toString(1234.56)); // "$1,234.56"
225
+ *
226
+ * // Percentage format
227
+ * formatter.trySetFormat('P1');
228
+ * console.log(formatter.toString(0.1234)); // "12.3%"
229
+ *
230
+ * // Custom format with sections
231
+ * formatter.trySetFormat('#,##0.00;(#,##0.00)');
232
+ * console.log(formatter.toString(-1234.56)); // "(1,234.56)"
233
+ *
234
+ * // Parsing
235
+ * formatter.styles = DotNetNumberStyles.number;
236
+ * const result = formatter.tryFromString('1,234.56');
237
+ * if (result.isOk()) {
238
+ * console.log(result.value); // 1234.56
239
+ * }
240
+ * ```
241
+ *
242
+ * @public
243
+ * @category Numeric Formatting
244
+ */
245
+ export declare class DotNetFloatFormatter extends DotNetNumberFormatter {
246
+ /**
247
+ * Formats a number using the current format string.
248
+ *
249
+ * @param value - The number to format.
250
+ * @returns The formatted number string.
251
+ */
252
+ toString(value: number): string;
253
+ /**
254
+ * Attempts to parse a number from a string using the current parsing styles.
255
+ *
256
+ * @param strValue - The string to parse.
257
+ * @returns A Result containing the parsed number if successful, or an error if parsing fails.
258
+ *
259
+ * @example
260
+ * ```typescript
261
+ * formatter.styles = DotNetNumberStyles.number;
262
+ * const result = formatter.tryFromString(' 1,234.56 ');
263
+ * if (result.isOk()) {
264
+ * console.log(result.value); // 1234.56
265
+ * }
266
+ * ```
267
+ */
268
+ tryFromString(strValue: string): Result<number>;
269
+ }
270
+
271
+ /**
272
+ * Formatter for integer values (bigint) using .NET-compatible format strings.
273
+ *
274
+ * Supports formatting integers with standard format strings like D (decimal), X (hexadecimal),
275
+ * B (binary), and custom format strings.
276
+ *
277
+ * @example
278
+ * ```typescript
279
+ * const formatter = new DotNetIntegerFormatter();
280
+ * formatter.localeSettings = DotNetLocaleSettings.createInvariant();
281
+ *
282
+ * // Decimal with padding
283
+ * formatter.trySetFormat('D8');
284
+ * console.log(formatter.toString(123n)); // "00000123"
285
+ *
286
+ * // Hexadecimal
287
+ * formatter.trySetFormat('X');
288
+ * console.log(formatter.toString(255n)); // "FF"
289
+ *
290
+ * // Parsing
291
+ * formatter.styles = DotNetNumberStyles.integer;
292
+ * const result = formatter.tryFromString('-12345');
293
+ * if (result.isOk()) {
294
+ * console.log(result.value); // -12345n
295
+ * }
296
+ * ```
297
+ *
298
+ * @public
299
+ * @category Numeric Formatting
300
+ */
301
+ export declare class DotNetIntegerFormatter extends DotNetNumberFormatter {
302
+ /**
303
+ * Formats a bigint value using the current format string.
304
+ *
305
+ * @param value - The bigint value to format.
306
+ * @returns The formatted number string.
307
+ */
308
+ toString(value: bigint): string;
309
+ /**
310
+ * Attempts to parse a bigint value from a string using the current parsing styles.
311
+ *
312
+ * @param strValue - The string to parse.
313
+ * @returns A Result containing the parsed bigint if successful, or an error if parsing fails.
314
+ *
315
+ * @example
316
+ * ```typescript
317
+ * formatter.styles = DotNetNumberStyles.integer;
318
+ * const result = formatter.tryFromString(' -123 ');
319
+ * if (result.isOk()) {
320
+ * console.log(result.value); // -123n
321
+ * }
322
+ * ```
323
+ */
324
+ tryFromString(strValue: string): Result<bigint>;
325
+ }
326
+
327
+ /**
328
+ * Represents locale-specific formatting settings for dates, times, and numbers.
329
+ *
330
+ * Provides culture-specific separators and formatting options that determine how
331
+ * numbers, currencies, dates, and times are formatted and parsed.
332
+ *
333
+ * @example
334
+ * ```typescript
335
+ * // Use invariant culture (en-US)
336
+ * const invariant = DotNetLocaleSettings.createInvariant();
337
+ *
338
+ * // Use specific locale
339
+ * const french = DotNetLocaleSettings.create('fr-FR');
340
+ * console.log(french.decimalSeparator); // ","
341
+ * console.log(french.thousandSeparator); // " "
342
+ *
343
+ * // Use current system locale
344
+ * const current = DotNetLocaleSettings.current;
345
+ * ```
346
+ *
347
+ * @public
348
+ * @category Locale Settings
349
+ */
350
+ export declare class DotNetLocaleSettings {
351
+ /**
352
+ * Singleton instance for invariant culture (en-US).
353
+ * Provides consistent formatting across all systems.
354
+ */
355
+ static readonly invariant: DotNetLocaleSettings;
356
+ /**
357
+ * Singleton instance for current system locale.
358
+ * Uses the locale settings from the runtime environment.
359
+ */
360
+ static readonly current: DotNetLocaleSettings;
361
+ /** The locale identifier (same as name). */
362
+ readonly id: string;
363
+ /** The locale name (e.g., "en-US", "fr-FR"). */
364
+ readonly name: string;
365
+ /** Character used for decimal point (e.g., "." or ","). */
366
+ readonly decimalSeparator: string;
367
+ /** Character used for thousands grouping (e.g., "," or "." or " "). */
368
+ readonly thousandSeparator: string;
369
+ /** Currency symbol for this locale (e.g., "$", "€", "£"). */
370
+ readonly currencyString: string;
371
+ /** Character used between date components (e.g., "/", "-", "."). */
372
+ readonly dateSeparator: string;
373
+ /** Character used between time components (typically ":"). */
374
+ readonly timeSeparator: string;
375
+ /** Pre-configured number formatter for floating-point values. */
376
+ readonly defaultFloat: Intl.NumberFormat;
377
+ /** Pre-configured number formatter for high-precision decimal values. */
378
+ readonly defaultDecimal: Intl.NumberFormat;
379
+ /** Pre-configured number formatter for currency values. */
380
+ readonly defaultCurrency: Intl.NumberFormat;
381
+ /**
382
+ * Creates a new DotNetLocaleSettings instance.
383
+ *
384
+ * @param localeName - Optional locale name (e.g., "en-US", "fr-FR").
385
+ * If undefined, uses the system's current locale.
386
+ *
387
+ * @example
388
+ * ```typescript
389
+ * // System locale
390
+ * const settings = new DotNetLocaleSettings(undefined);
391
+ *
392
+ * // Specific locale
393
+ * const french = new DotNetLocaleSettings('fr-FR');
394
+ * ```
395
+ */
396
+ constructor(localeName?: string);
397
+ /**
398
+ * Creates a DotNetLocaleSettings instance for the specified locale.
399
+ *
400
+ * @param localeName - The locale name (e.g., "en-US", "fr-FR", "de-DE").
401
+ * @returns A new DotNetLocaleSettings instance configured for the specified locale.
402
+ *
403
+ * @example
404
+ * ```typescript
405
+ * const usSettings = DotNetLocaleSettings.create('en-US');
406
+ * const frSettings = DotNetLocaleSettings.create('fr-FR');
407
+ * ```
408
+ */
409
+ static create(localeName: string): DotNetLocaleSettings;
410
+ /**
411
+ * Creates a DotNetLocaleSettings instance for the invariant culture (en-US).
412
+ * The invariant culture provides consistent formatting across all systems.
413
+ *
414
+ * @returns A new DotNetLocaleSettings instance configured for invariant culture.
415
+ *
416
+ * @example
417
+ * ```typescript
418
+ * const settings = DotNetLocaleSettings.createInvariant();
419
+ * console.log(settings.decimalSeparator); // "."
420
+ * console.log(settings.thousandSeparator); // ","
421
+ * ```
422
+ */
423
+ static createInvariant(): DotNetLocaleSettings;
424
+ /**
425
+ * Converts a number or bigint to a string using locale-specific formatting.
426
+ *
427
+ * @param value - The number or bigint to format.
428
+ * @param options - Optional Intl.NumberFormatOptions for custom formatting.
429
+ * @returns The formatted number string.
430
+ *
431
+ * @example
432
+ * ```typescript
433
+ * const settings = DotNetLocaleSettings.create('en-US');
434
+ * settings.numberToStr(1234.56); // "1,234.56"
435
+ * settings.numberToStr(1234.56, { minimumFractionDigits: 2 }); // "1,234.56"
436
+ * ```
437
+ */
438
+ numberToStr(value: number | bigint, options?: Intl.NumberFormatOptions): string;
439
+ /**
440
+ * Converts a Date to a string using locale-specific formatting.
441
+ *
442
+ * @param value - The Date to format.
443
+ * @param options - Optional Intl.DateTimeFormatOptions for custom formatting.
444
+ * @returns The formatted date string.
445
+ *
446
+ * @example
447
+ * ```typescript
448
+ * const settings = DotNetLocaleSettings.create('en-US');
449
+ * settings.dateToStr(new Date(2024, 6, 5), { dateStyle: 'short' }); // "7/5/2024"
450
+ * ```
451
+ */
452
+ dateToStr(value: Date, options?: Intl.DateTimeFormatOptions): string;
453
+ /**
454
+ * Converts a boolean to a string.
455
+ *
456
+ * @param value - The boolean value.
457
+ * @returns "true" or "false".
458
+ *
459
+ * @example
460
+ * ```typescript
461
+ * settings.boolToStr(true); // "true"
462
+ * settings.boolToStr(false); // "false"
463
+ * ```
464
+ */
465
+ boolToStr(value: boolean): string;
466
+ /**
467
+ * Attempts to parse a boolean value from a string.
468
+ *
469
+ * @param value - The string to parse. Accepts: "true", "1", "yes" (case-insensitive) for true,
470
+ * and "false", "0", "no" for false.
471
+ * @returns A Result containing the boolean value if successful, or an error if parsing fails.
472
+ *
473
+ * @example
474
+ * ```typescript
475
+ * settings.tryStrToBool('yes'); // Ok(true)
476
+ * settings.tryStrToBool('false'); // Ok(false)
477
+ * settings.tryStrToBool('invalid'); // Err("Invalid boolean string")
478
+ * ```
479
+ */
480
+ tryStrToBool(value: string): Result<boolean>;
481
+ /**
482
+ * Attempts to parse an integer from a string.
483
+ *
484
+ * @param value - The string to parse. Must contain only digits and an optional leading +/- sign.
485
+ * @returns A Result containing the parsed integer if successful, or an error if parsing fails.
486
+ *
487
+ * @example
488
+ * ```typescript
489
+ * settings.tryStrToInt('123'); // Ok(123)
490
+ * settings.tryStrToInt('-456'); // Ok(-456)
491
+ * settings.tryStrToInt('12.34'); // Err("Invalid integer string")
492
+ * ```
493
+ */
494
+ tryStrToInt(value: string): Result<number>;
495
+ /**
496
+ * Attempts to parse a BigInt from a string.
497
+ *
498
+ * @param value - The string to parse. Must contain only digits and an optional leading +/- sign.
499
+ * @returns A Result containing the parsed BigInt if successful, or an error if parsing fails.
500
+ *
501
+ * @example
502
+ * ```typescript
503
+ * settings.tryStrToBigInt('12345678901234567890'); // Ok(12345678901234567890n)
504
+ * settings.tryStrToBigInt('-999'); // Ok(-999n)
505
+ * ```
506
+ */
507
+ tryStrToBigInt(value: string): Result<bigint>;
508
+ /**
509
+ * Attempts to parse a number from a string using locale-specific formatting.
510
+ * Handles currency symbols, thousands separators, and locale-specific decimal separators.
511
+ *
512
+ * @param value - The string to parse.
513
+ * @returns A Result containing the parsed number if successful, or an error if parsing fails.
514
+ *
515
+ * @example
516
+ * ```typescript
517
+ * const usSettings = DotNetLocaleSettings.create('en-US');
518
+ * usSettings.tryStrToNumber('1,234.56'); // Ok(1234.56)
519
+ * usSettings.tryStrToNumber('$1,234.56'); // Ok(1234.56)
520
+ *
521
+ * const frSettings = DotNetLocaleSettings.create('fr-FR');
522
+ * frSettings.tryStrToNumber('1 234,56'); // Ok(1234.56)
523
+ * ```
524
+ */
525
+ tryStrToNumber(value: string): Result<number>;
526
+ /**
527
+ * Attempts to parse a Date from a string.
528
+ *
529
+ * @param value - The string to parse. Accepts various date formats understood by JavaScript Date constructor.
530
+ * @returns A Result containing the parsed Date if successful, or an error if parsing fails.
531
+ *
532
+ * @example
533
+ * ```typescript
534
+ * settings.tryStrToDate('2024-07-05'); // Ok(Date)
535
+ * settings.tryStrToDate('July 5, 2024'); // Ok(Date)
536
+ * settings.tryStrToDate('invalid'); // Err("Invalid date string")
537
+ * ```
538
+ */
539
+ tryStrToDate(value: string): Result<Date>;
540
+ /**
541
+ * Convert a single character to uppercase using culture-specific rules.
542
+ * Handles special cases like Turkish 'i' → 'İ' vs standard 'i' → 'I'.
543
+ *
544
+ * @param char - The character to convert to uppercase.
545
+ * @returns The uppercased character.
546
+ *
547
+ * @example
548
+ * ```typescript
549
+ * const usSettings = DotNetLocaleSettings.create('en-US');
550
+ * usSettings.toUpperChar('i'); // "I"
551
+ *
552
+ * const trSettings = DotNetLocaleSettings.create('tr-TR');
553
+ * trSettings.toUpperChar('i'); // "İ" (Turkish dotted I)
554
+ * ```
555
+ */
556
+ toUpperChar(char: string): string;
557
+ /**
558
+ * Convert a single character to lowercase using culture-specific rules.
559
+ * Handles special cases like Turkish 'I' → 'ı' vs Turkish 'İ' → 'i'.
560
+ *
561
+ * @param char - The character to convert to lowercase.
562
+ * @returns The lowercased character.
563
+ *
564
+ * @example
565
+ * ```typescript
566
+ * const usSettings = DotNetLocaleSettings.create('en-US');
567
+ * usSettings.toLowerChar('I'); // "i"
568
+ *
569
+ * const trSettings = DotNetLocaleSettings.create('tr-TR');
570
+ * trSettings.toLowerChar('I'); // "ı" (Turkish dotless i)
571
+ * trSettings.toLowerChar('İ'); // "i" (Turkish dotted i)
572
+ * ```
573
+ */
574
+ toLowerChar(char: string): string;
575
+ }
576
+
577
+ /**
578
+ * Base class for formatting and parsing numbers using .NET-compatible format strings.
579
+ *
580
+ * Supports both standard format strings (C, D, E, F, G, N, P, R, X, B) and custom format strings
581
+ * with fine-grained control over digit placeholders, separators, and sections.
582
+ *
583
+ * @remarks
584
+ * This is a base class - use {@link DotNetIntegerFormatter}, {@link DotNetFloatFormatter},
585
+ * or {@link DotNetDecimalFormatter} for specific number types.
586
+ *
587
+ * @example
588
+ * ```typescript
589
+ * // Use derived classes instead
590
+ * const formatter = new DotNetFloatFormatter();
591
+ * formatter.localeSettings = DotNetLocaleSettings.createInvariant();
592
+ * formatter.trySetFormat('C2');
593
+ * console.log(formatter.toString(1234.56)); // "$1,234.56"
594
+ * ```
595
+ *
596
+ * @public
597
+ * @category Numeric Formatting
598
+ */
599
+ export declare class DotNetNumberFormatter {
600
+ protected format: string;
601
+ private formatIsStandard;
602
+ private precision;
603
+ private sections;
604
+ /**
605
+ * The set of {@link DotNetNumberStyleId} flags that control which number formats are allowed during parsing.
606
+ *
607
+ * @example
608
+ * ```typescript
609
+ * // Use predefined styles
610
+ * formatter.styles = DotNetNumberStyles.number;
611
+ *
612
+ * // Or combine individual flags
613
+ * formatter.styles = new Set([
614
+ * DotNetNumberStyleId.AllowLeadingSign,
615
+ * DotNetNumberStyleId.AllowDecimalPoint
616
+ * ]);
617
+ * ```
618
+ */
619
+ styles: DotNetNumberStyleSet;
620
+ /**
621
+ * The locale settings that determine decimal/thousands separators and other culture-specific formatting.
622
+ */
623
+ localeSettings: DotNetLocaleSettings;
624
+ /**
625
+ * Contains the error message from the last failed operation.
626
+ * Check this property if {@link DotNetNumberFormatter.trySetFormat} or parsing methods return an error.
627
+ */
628
+ parseErrorText: string;
629
+ protected setParseErrorText(value: string): false;
630
+ /**
631
+ * Sets the format string to use for formatting numbers.
632
+ *
633
+ * The format string is tokenized to confirm validity and to optimize formatting/parsing operations.
634
+ *
635
+ * @param value - A standard format string (e.g., "C", "N2", "E3") or custom format string (e.g., "#,##0.00").
636
+ * @returns A Result indicating success or containing an error message if the format string is invalid.
637
+ *
638
+ * @example
639
+ * ```typescript
640
+ * // Standard format
641
+ * formatter.trySetFormat('C2'); // Currency with 2 decimal places
642
+ *
643
+ * // Custom format
644
+ * formatter.trySetFormat('#,##0.00'); // Number with thousands separator
645
+ *
646
+ * // Check for errors
647
+ * const result = formatter.trySetFormat('INVALID');
648
+ * if (result.isErr()) {
649
+ * console.error(result.error);
650
+ * }
651
+ * ```
652
+ */
653
+ trySetFormat(value: string): Result<void>;
654
+ private parseCustomFormat;
655
+ private analyzeSection;
656
+ protected formatNumber(value: number | bigint, allowDecimal?: boolean): string;
657
+ private formatStandard;
658
+ private formatCurrency;
659
+ private formatDecimal;
660
+ private formatExponential;
661
+ private formatFixedPoint;
662
+ private formatGeneral;
663
+ private formatNumber_;
664
+ private formatPercent;
665
+ private formatHex;
666
+ private formatBinary;
667
+ private formatCustom;
668
+ private formatWithSection;
669
+ private formatCustomExponential;
670
+ protected trimTrailingPadZeros(value: string): string;
671
+ hasExponentChar(value: string): boolean;
672
+ hasDecimalChar(value: string): boolean;
673
+ hasDigitChar(value: string): boolean;
674
+ static tryHexToInt64(hex: string): Result<bigint>;
675
+ protected unstyleNumberString(value: string): Result<{
676
+ unstyled: string;
677
+ negated: boolean;
678
+ }>;
679
+ }
680
+
681
+ /**
682
+ * Individual style flags that control which number formats are allowed during parsing.
683
+ *
684
+ * These flags can be combined to create custom parsing behavior.
685
+ *
686
+ * @example
687
+ * ```typescript
688
+ * // Combine individual flags
689
+ * formatter.styles = new Set([
690
+ * DotNetNumberStyleId.AllowLeadingSign,
691
+ * DotNetNumberStyleId.AllowDecimalPoint,
692
+ * DotNetNumberStyleId.AllowThousands
693
+ * ]);
694
+ * ```
695
+ *
696
+ * @public
697
+ * @category Number Styles
698
+ */
699
+ export declare enum DotNetNumberStyleId {
700
+ /** Allow currency symbol in the number string. */
701
+ AllowCurrencySymbol = "AllowCurrencySymbol",
702
+ /** Allow decimal point in the number string. */
703
+ AllowDecimalPoint = "AllowDecimalPoint",
704
+ /** Allow exponential notation (e.g., 1.23e+10). */
705
+ AllowExponent = "AllowExponent",
706
+ /** Parse the number as hexadecimal. */
707
+ AllowHexSpecifier = "AllowHexSpecifier",
708
+ /** Allow a leading plus (+) or minus (-) sign. */
709
+ AllowLeadingSign = "AllowLeadingSign",
710
+ /** Allow leading whitespace characters. */
711
+ AllowLeadingWhite = "AllowLeadingWhite",
712
+ /** Allow parentheses to indicate negative numbers. */
713
+ AllowParentheses = "AllowParentheses",
714
+ /** Allow thousands separator characters. */
715
+ AllowThousands = "AllowThousands",
716
+ /** Allow a trailing plus (+) or minus (-) sign. */
717
+ AllowTrailingSign = "AllowTrailingSign",
718
+ /** Allow trailing whitespace characters. */
719
+ AllowTrailingWhite = "AllowTrailingWhite"
720
+ }
721
+
722
+ /**
723
+ * Predefined number style combinations for common parsing scenarios.
724
+ *
725
+ * @example
726
+ * ```typescript
727
+ * // Use predefined style
728
+ * formatter.styles = DotNetNumberStyles.number;
729
+ * formatter.tryFromString('1,234.56'); // Parses successfully
730
+ *
731
+ * // Use currency style
732
+ * formatter.styles = DotNetNumberStyles.currency;
733
+ * formatter.tryFromString('$1,234.56'); // Parses successfully
734
+ * ```
735
+ *
736
+ * @internal
737
+ * @category Number Styles
738
+ */
739
+ export declare const DotNetNumberStyles: {
740
+ /** No styles - only basic digits allowed. */
741
+ none: Set<DotNetNumberStyleId>;
742
+ /**
743
+ * All styles allowed (except hex).
744
+ * Includes: AllowCurrencySymbol, AllowDecimalPoint, AllowExponent, AllowLeadingSign,
745
+ * AllowLeadingWhite, AllowParentheses, AllowThousands, AllowTrailingSign, AllowTrailingWhite.
746
+ */
747
+ any: Set<DotNetNumberStyleId>;
748
+ /**
749
+ * Currency parsing style.
750
+ * Includes: AllowCurrencySymbol, AllowDecimalPoint, AllowLeadingSign, AllowLeadingWhite,
751
+ * AllowParentheses, AllowThousands, AllowTrailingSign, AllowTrailingWhite.
752
+ */
753
+ currency: Set<DotNetNumberStyleId>;
754
+ /**
755
+ * Floating-point parsing style.
756
+ * Includes: AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign,
757
+ * AllowDecimalPoint, AllowExponent.
758
+ */
759
+ float: Set<DotNetNumberStyleId>;
760
+ /**
761
+ * Hexadecimal parsing style.
762
+ * Includes: AllowLeadingWhite, AllowTrailingWhite, AllowHexSpecifier.
763
+ */
764
+ hexNumber: Set<DotNetNumberStyleId>;
765
+ /**
766
+ * Basic integer parsing style.
767
+ * Includes: AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign.
768
+ */
769
+ integer: Set<DotNetNumberStyleId>;
770
+ /**
771
+ * Standard number parsing style.
772
+ * Includes: AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign, AllowTrailingSign,
773
+ * AllowDecimalPoint, AllowThousands.
774
+ */
775
+ number: Set<DotNetNumberStyleId>;
776
+ };
777
+
778
+ /**
779
+ * A set of {@link DotNetNumberStyleId} flags.
780
+ *
781
+ * @public
782
+ * @category Number Styles
783
+ */
784
+ export declare type DotNetNumberStyleSet = Set<DotNetNumberStyleId>;
785
+
786
+ /** @internal */
787
+ export declare class DotNetNumberStylesInfo {
788
+ static toString(styles: DotNetNumberStyleSet): string;
789
+ static toXmlValue(styles: DotNetNumberStyleSet): string;
790
+ static tryFromString(value: string): Result<DotNetNumberStyleSet>;
791
+ static tryFromXmlValue(value: string): Result<DotNetNumberStyleSet>;
792
+ static tryFromXmlValueWithDefault(value: string, defaultStyles: DotNetNumberStyleSet): Result<DotNetNumberStyleSet>;
793
+ }
794
+
795
+ export { }