@temboplus/frontend-core 0.2.14 → 0.2.16

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.
@@ -2,19 +2,33 @@ import { CurrencyCode } from "../../models/currency";
2
2
  /**
3
3
  * Regular expression for validating amount strings
4
4
  * Supports both positive and negative amounts with optional comma separators and decimals
5
+ */
6
+ declare const AMOUNT_REGEX: RegExp;
7
+ /**
8
+ * Represents a monetary amount with currency information
5
9
  *
6
- * Pattern breakdown:
7
- * - ^-? : Optional minus sign at the start (for negative amounts)
8
- * - (?:\d{1,3}(?:,\d{3})*|\d+) : Either:
9
- * - \d{1,3}(?:,\d{3})* : 1-3 digits followed by groups of comma + 3 digits (e.g., "1,234,567")
10
- * - |\d+ : OR just any sequence of digits (e.g., "1234567")
11
- * - (?:\.\d+)? : Optional decimal part with any number of digits
12
- * - $ : End of string
10
+ * Enhanced with currency-amount parsing support:
11
+ * - Supports formats like "TZS1000", "1000TZS", "TZS1,000.00", "1,000.00TZS"
12
+ * - Case-insensitive currency codes
13
+ * - US/UK format (comma for thousands, period for decimal)
14
+ * - Automatic whitespace removal
15
+ * - Clear error messages for invalid formats
13
16
  *
14
- * Valid examples: "123", "-456", "1,234", "-1,234.56", "0", "-0.50"
15
- * Invalid examples: "12.34.56", "1,23", "-", "abc", "1,,234"
17
+ * @example
18
+ * ```typescript
19
+ * // Currency-amount combinations
20
+ * Amount.from('TZS10000'); // TSh 10,000.00
21
+ * Amount.from('TZS 10,000'); // TSh 10,000.00 (spaces removed)
22
+ * Amount.from('10000TZS'); // TSh 10,000.00
23
+ * Amount.from('TZS1,234.56'); // TSh 1,234.56
24
+ * Amount.from('-TZS1000'); // -TSh 1,000.00
25
+ * Amount.from('TZS-1000'); // -TSh 1,000.00
26
+ *
27
+ * // Traditional usage still works
28
+ * Amount.from(1000, 'TZS'); // TSh 1,000.00
29
+ * Amount.from('1000', 'USD'); // $1,000.00
30
+ * ```
16
31
  */
17
- declare const AMOUNT_REGEX: RegExp;
18
32
  /**
19
33
  * JSON representation interface for Amount serialization
20
34
  */
@@ -101,27 +115,42 @@ declare class Amount {
101
115
  */
102
116
  private constructor();
103
117
  /**
104
- * Creates an Amount instance from a string or number input
118
+ * Creates an Amount instance from string or number input
105
119
  *
106
- * Supports various input formats:
107
- * - Numbers: 1000, -500, 0, 123.45, -67.89
108
- * - Strings: "1000", "-500", "1,234", "-1,234.56"
109
- * - Currency symbols are automatically removed from strings
120
+ * Supports currency-amount combinations like:
121
+ * - "TZS10000", "10000TZS" (no space)
122
+ * - "TZS 10,000", "10,000 TZS" (with spaces - automatically cleaned)
123
+ * - "-TZS1000", "TZS-1000", "-1000TZS" (negative amounts)
124
+ * - Case-insensitive: "tzs10000", "TZS10000", "Tzs10000"
110
125
  *
111
- * @param input - The input value (string or number, positive or negative)
112
- * @param currencyCode - The currency code (defaults to TZS)
113
- * @returns Amount instance or undefined if validation fails
114
- *
115
- * @example
116
- * ```typescript
117
- * Amount.from(1000); // Valid: TSh 1,000.00
118
- * Amount.from(-500, 'USD'); // Valid: -$500.00
119
- * Amount.from('-1,234.56'); // Valid: -TSh 1,234.56
120
- * Amount.from('abc'); // Invalid: returns undefined
121
- * Amount.from(''); // Invalid: returns undefined
122
- * ```
126
+ * @param input - String with currency-amount or number
127
+ * @param currencyCode - Currency code (optional if detected from string)
128
+ * @returns Amount instance or undefined if invalid
129
+ * @throws Error with clear message if format is invalid
123
130
  */
124
131
  static from(input: string | number, currencyCode?: CurrencyCode | null): Amount | undefined;
132
+ /**
133
+ * Parse currency-amount combination from cleaned string
134
+ * @param cleaned - String with whitespace already removed
135
+ * @returns Object with currency code and amount string
136
+ */
137
+ private static parseCurrencyAmount;
138
+ /**
139
+ * Validate and normalize currency code
140
+ * @param code - Currency code string (e.g., "TZS", "tzs")
141
+ * @returns Normalized currency code or undefined if invalid
142
+ */
143
+ private static validateCurrencyCode;
144
+ /**
145
+ * Create Amount from numeric value
146
+ */
147
+ private static fromNumericValue;
148
+ /**
149
+ * Create Amount from parsed currency and amount values
150
+ * @param amountStr - Numeric amount string (may include commas, decimal, minus)
151
+ * @param currencyCode - Validated currency code
152
+ */
153
+ private static fromParsedValues;
125
154
  /**
126
155
  * Returns the currency code
127
156
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@temboplus/frontend-core",
3
- "version": "0.2.14",
3
+ "version": "0.2.16",
4
4
  "description": "A JavaScript/TypeScript package providing common utilities and logic shared across front-end TemboPlus projects.",
5
5
  "author": "Okello Gerald",
6
6
  "license": "MIT",