nhb-toolbox 4.12.33 → 4.12.34
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/CHANGELOG.md +4 -0
- package/dist/cjs/number/Currency.js +17 -12
- package/dist/dts/number/Currency.d.ts +11 -8
- package/dist/dts/number/Currency.d.ts.map +1 -1
- package/dist/dts/number/types.d.ts +1 -0
- package/dist/dts/number/types.d.ts.map +1 -1
- package/dist/esm/number/Currency.js +17 -12
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,10 @@ All notable changes to the package will be documented here.
|
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
+
## [4.12.34] - 2025-06-12
|
|
10
|
+
|
|
11
|
+
- **Updated** `format()` and `convert()` methods in `Currency` class. `format()` now accepts `CurrencyCode` as optional second parameter and `convert()` method now returns a new `Currency` instance.
|
|
12
|
+
|
|
9
13
|
## [4.12.33] - 2025-06-11
|
|
10
14
|
|
|
11
15
|
- **Trim** input string for `numberToWordsOrdinal` utility.
|
|
@@ -5,7 +5,7 @@ const utilities_1 = require("./utilities");
|
|
|
5
5
|
/**
|
|
6
6
|
* * A utility class for handling currency operations like formatting and conversion.
|
|
7
7
|
*
|
|
8
|
-
* - Supports formatting based on locale.
|
|
8
|
+
* - Supports formatting based on locale and currency code.
|
|
9
9
|
* - Converts between **fiat currencies supported by `api.frankfurter.app`**.
|
|
10
10
|
* - Automatically caches conversion rates to reduce redundant API calls.
|
|
11
11
|
* - Intended for use with numeric inputs (number or numeric string).
|
|
@@ -18,7 +18,7 @@ class Currency {
|
|
|
18
18
|
*
|
|
19
19
|
* - Generated using the `en-US` locale during construction.
|
|
20
20
|
* - This is a display-friendly version of the currency value.
|
|
21
|
-
* - For formatting with other locales, use the `format(
|
|
21
|
+
* - For formatting with other locales, use the `format()` method.
|
|
22
22
|
*/
|
|
23
23
|
currency;
|
|
24
24
|
/**
|
|
@@ -38,12 +38,15 @@ class Currency {
|
|
|
38
38
|
Currency.#rateCache.clear();
|
|
39
39
|
}
|
|
40
40
|
/**
|
|
41
|
-
* @instance Formats the
|
|
42
|
-
*
|
|
43
|
-
* @
|
|
41
|
+
* @instance Formats the stored amount as a localized currency string.
|
|
42
|
+
*
|
|
43
|
+
* @param locale - Optional. A BCP 47 locale string (e.g., `'de-DE'`, `'en-US'`). Defaults to `'en-US'` if not provided.
|
|
44
|
+
* @param code - Optional. An ISO 4217 currency code (e.g., `'USD'`, `'EUR'`) used solely for formatting purposes.
|
|
45
|
+
* _This does not alter the internal currency code set during instantiation._
|
|
46
|
+
* @returns A string representing the formatted currency value according to the specified locale and currency code.
|
|
44
47
|
*/
|
|
45
|
-
format(locale) {
|
|
46
|
-
return (0, utilities_1.formatCurrency)(this.#amount, this.#code, locale);
|
|
48
|
+
format(locale, code) {
|
|
49
|
+
return (0, utilities_1.formatCurrency)(this.#amount, code ?? this.#code, locale);
|
|
47
50
|
}
|
|
48
51
|
/**
|
|
49
52
|
* @instance Converts the current currency amount to a target currency using real-time exchange rates.
|
|
@@ -60,25 +63,27 @@ class Currency {
|
|
|
60
63
|
* - `fallbackRate`: A manual exchange rate to use if the API call fails or currency is not supported.
|
|
61
64
|
* - `forceRefresh`: If true, ignores cached rates and fetches fresh data.
|
|
62
65
|
* @returns The converted amount as a number.
|
|
63
|
-
* @throws Will throw if the API call fails and no `fallbackRate` is provided.
|
|
66
|
+
* @throws Will throw error if the API call fails and no `fallbackRate` is provided.
|
|
64
67
|
*/
|
|
65
68
|
async convert(to, options) {
|
|
66
69
|
const key = `${this.#code}->${to}`;
|
|
67
70
|
if (!options?.forceRefresh && Currency.#rateCache.has(key)) {
|
|
68
71
|
const cachedRate = Currency.#rateCache.get(key);
|
|
69
|
-
return this.#amount * cachedRate;
|
|
72
|
+
return new Currency(this.#amount * cachedRate, to);
|
|
70
73
|
}
|
|
71
74
|
try {
|
|
72
75
|
const rate = await this.#fetchFromFrankfurter(to);
|
|
73
76
|
Currency.#rateCache.set(key, rate);
|
|
74
|
-
return this.#amount * rate;
|
|
77
|
+
return new Currency(this.#amount * rate, to);
|
|
75
78
|
}
|
|
76
79
|
catch (error) {
|
|
77
80
|
if (options?.fallbackRate != null) {
|
|
78
81
|
console.warn(`Currency conversion failed (${this.#code} → ${to}): ${error.message}. Using fallback rate...`);
|
|
79
|
-
return this.#amount * options.fallbackRate;
|
|
82
|
+
return new Currency(this.#amount * options.fallbackRate, to);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
throw new Error(`Currency conversion failed (${this.#code} → ${to}): ${error.message}`);
|
|
80
86
|
}
|
|
81
|
-
throw new Error(`Currency conversion failed (${this.#code} → ${to}): ${error.message}`);
|
|
82
87
|
}
|
|
83
88
|
}
|
|
84
89
|
/**
|
|
@@ -3,7 +3,7 @@ import type { ConvertOptions, CurrencyCode, LocaleCode, SupportedCurrency } from
|
|
|
3
3
|
/**
|
|
4
4
|
* * A utility class for handling currency operations like formatting and conversion.
|
|
5
5
|
*
|
|
6
|
-
* - Supports formatting based on locale.
|
|
6
|
+
* - Supports formatting based on locale and currency code.
|
|
7
7
|
* - Converts between **fiat currencies supported by `api.frankfurter.app`**.
|
|
8
8
|
* - Automatically caches conversion rates to reduce redundant API calls.
|
|
9
9
|
* - Intended for use with numeric inputs (number or numeric string).
|
|
@@ -15,7 +15,7 @@ export declare class Currency {
|
|
|
15
15
|
*
|
|
16
16
|
* - Generated using the `en-US` locale during construction.
|
|
17
17
|
* - This is a display-friendly version of the currency value.
|
|
18
|
-
* - For formatting with other locales, use the `format(
|
|
18
|
+
* - For formatting with other locales, use the `format()` method.
|
|
19
19
|
*/
|
|
20
20
|
readonly currency: string;
|
|
21
21
|
/**
|
|
@@ -28,11 +28,14 @@ export declare class Currency {
|
|
|
28
28
|
/** * Clears cached rates that were fetched previously. */
|
|
29
29
|
static clearRateCache(): void;
|
|
30
30
|
/**
|
|
31
|
-
* @instance Formats the
|
|
32
|
-
*
|
|
33
|
-
* @
|
|
31
|
+
* @instance Formats the stored amount as a localized currency string.
|
|
32
|
+
*
|
|
33
|
+
* @param locale - Optional. A BCP 47 locale string (e.g., `'de-DE'`, `'en-US'`). Defaults to `'en-US'` if not provided.
|
|
34
|
+
* @param code - Optional. An ISO 4217 currency code (e.g., `'USD'`, `'EUR'`) used solely for formatting purposes.
|
|
35
|
+
* _This does not alter the internal currency code set during instantiation._
|
|
36
|
+
* @returns A string representing the formatted currency value according to the specified locale and currency code.
|
|
34
37
|
*/
|
|
35
|
-
format(locale?: LocaleCode): string;
|
|
38
|
+
format(locale?: LocaleCode, code?: CurrencyCode): string;
|
|
36
39
|
/**
|
|
37
40
|
* @instance Converts the current currency amount to a target currency using real-time exchange rates.
|
|
38
41
|
*
|
|
@@ -48,8 +51,8 @@ export declare class Currency {
|
|
|
48
51
|
* - `fallbackRate`: A manual exchange rate to use if the API call fails or currency is not supported.
|
|
49
52
|
* - `forceRefresh`: If true, ignores cached rates and fetches fresh data.
|
|
50
53
|
* @returns The converted amount as a number.
|
|
51
|
-
* @throws Will throw if the API call fails and no `fallbackRate` is provided.
|
|
54
|
+
* @throws Will throw error if the API call fails and no `fallbackRate` is provided.
|
|
52
55
|
*/
|
|
53
|
-
convert(to: SupportedCurrency | CurrencyCode, options?: ConvertOptions): Promise<
|
|
56
|
+
convert(to: SupportedCurrency | CurrencyCode, options?: ConvertOptions): Promise<Currency>;
|
|
54
57
|
}
|
|
55
58
|
//# sourceMappingURL=Currency.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Currency.d.ts","sourceRoot":"","sources":["../../../src/number/Currency.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,KAAK,EACX,cAAc,EACd,YAAY,EAEZ,UAAU,EACV,iBAAiB,EACjB,MAAM,SAAS,CAAC;AAGjB;;;;;;;GAOG;AACH,qBAAa,QAAQ;;IAGpB;;;;;;OAMG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B;;;;;OAKG;gBACS,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,YAAY;IAQ/C,0DAA0D;IAC1D,MAAM,CAAC,cAAc,IAAI,IAAI;IAI7B
|
|
1
|
+
{"version":3,"file":"Currency.d.ts","sourceRoot":"","sources":["../../../src/number/Currency.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,KAAK,EACX,cAAc,EACd,YAAY,EAEZ,UAAU,EACV,iBAAiB,EACjB,MAAM,SAAS,CAAC;AAGjB;;;;;;;GAOG;AACH,qBAAa,QAAQ;;IAGpB;;;;;;OAMG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B;;;;;OAKG;gBACS,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,YAAY;IAQ/C,0DAA0D;IAC1D,MAAM,CAAC,cAAc,IAAI,IAAI;IAI7B;;;;;;;OAOG;IACH,MAAM,CAAC,MAAM,CAAC,EAAE,UAAU,EAAE,IAAI,CAAC,EAAE,YAAY,GAAG,MAAM;IAIxD;;;;;;;;;;;;;;;;OAgBG;IACG,OAAO,CACZ,EAAE,EAAE,iBAAiB,GAAG,YAAY,EACpC,OAAO,CAAC,EAAE,cAAc,GACtB,OAAO,CAAC,QAAQ,CAAC;CA6DpB"}
|
|
@@ -78,6 +78,7 @@ export interface ConvertOptions {
|
|
|
78
78
|
/** If true, bypasses the cache and fetches fresh rate. */
|
|
79
79
|
forceRefresh?: boolean;
|
|
80
80
|
}
|
|
81
|
+
/** Type Interface for API Response from `api.frankfurter.app` */
|
|
81
82
|
export interface FrankFurter {
|
|
82
83
|
amount: number;
|
|
83
84
|
base: CurrencyCode;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/number/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX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7 +2,7 @@ import { formatCurrency } from './utilities.js';
|
|
|
2
2
|
/**
|
|
3
3
|
* * A utility class for handling currency operations like formatting and conversion.
|
|
4
4
|
*
|
|
5
|
-
* - Supports formatting based on locale.
|
|
5
|
+
* - Supports formatting based on locale and currency code.
|
|
6
6
|
* - Converts between **fiat currencies supported by `api.frankfurter.app`**.
|
|
7
7
|
* - Automatically caches conversion rates to reduce redundant API calls.
|
|
8
8
|
* - Intended for use with numeric inputs (number or numeric string).
|
|
@@ -15,7 +15,7 @@ export class Currency {
|
|
|
15
15
|
*
|
|
16
16
|
* - Generated using the `en-US` locale during construction.
|
|
17
17
|
* - This is a display-friendly version of the currency value.
|
|
18
|
-
* - For formatting with other locales, use the `format(
|
|
18
|
+
* - For formatting with other locales, use the `format()` method.
|
|
19
19
|
*/
|
|
20
20
|
currency;
|
|
21
21
|
/**
|
|
@@ -35,12 +35,15 @@ export class Currency {
|
|
|
35
35
|
Currency.#rateCache.clear();
|
|
36
36
|
}
|
|
37
37
|
/**
|
|
38
|
-
* @instance Formats the
|
|
39
|
-
*
|
|
40
|
-
* @
|
|
38
|
+
* @instance Formats the stored amount as a localized currency string.
|
|
39
|
+
*
|
|
40
|
+
* @param locale - Optional. A BCP 47 locale string (e.g., `'de-DE'`, `'en-US'`). Defaults to `'en-US'` if not provided.
|
|
41
|
+
* @param code - Optional. An ISO 4217 currency code (e.g., `'USD'`, `'EUR'`) used solely for formatting purposes.
|
|
42
|
+
* _This does not alter the internal currency code set during instantiation._
|
|
43
|
+
* @returns A string representing the formatted currency value according to the specified locale and currency code.
|
|
41
44
|
*/
|
|
42
|
-
format(locale) {
|
|
43
|
-
return formatCurrency(this.#amount, this.#code, locale);
|
|
45
|
+
format(locale, code) {
|
|
46
|
+
return formatCurrency(this.#amount, code ?? this.#code, locale);
|
|
44
47
|
}
|
|
45
48
|
/**
|
|
46
49
|
* @instance Converts the current currency amount to a target currency using real-time exchange rates.
|
|
@@ -57,25 +60,27 @@ export class Currency {
|
|
|
57
60
|
* - `fallbackRate`: A manual exchange rate to use if the API call fails or currency is not supported.
|
|
58
61
|
* - `forceRefresh`: If true, ignores cached rates and fetches fresh data.
|
|
59
62
|
* @returns The converted amount as a number.
|
|
60
|
-
* @throws Will throw if the API call fails and no `fallbackRate` is provided.
|
|
63
|
+
* @throws Will throw error if the API call fails and no `fallbackRate` is provided.
|
|
61
64
|
*/
|
|
62
65
|
async convert(to, options) {
|
|
63
66
|
const key = `${this.#code}->${to}`;
|
|
64
67
|
if (!options?.forceRefresh && Currency.#rateCache.has(key)) {
|
|
65
68
|
const cachedRate = Currency.#rateCache.get(key);
|
|
66
|
-
return this.#amount * cachedRate;
|
|
69
|
+
return new Currency(this.#amount * cachedRate, to);
|
|
67
70
|
}
|
|
68
71
|
try {
|
|
69
72
|
const rate = await this.#fetchFromFrankfurter(to);
|
|
70
73
|
Currency.#rateCache.set(key, rate);
|
|
71
|
-
return this.#amount * rate;
|
|
74
|
+
return new Currency(this.#amount * rate, to);
|
|
72
75
|
}
|
|
73
76
|
catch (error) {
|
|
74
77
|
if (options?.fallbackRate != null) {
|
|
75
78
|
console.warn(`Currency conversion failed (${this.#code} → ${to}): ${error.message}. Using fallback rate...`);
|
|
76
|
-
return this.#amount * options.fallbackRate;
|
|
79
|
+
return new Currency(this.#amount * options.fallbackRate, to);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
throw new Error(`Currency conversion failed (${this.#code} → ${to}): ${error.message}`);
|
|
77
83
|
}
|
|
78
|
-
throw new Error(`Currency conversion failed (${this.#code} → ${to}): ${error.message}`);
|
|
79
84
|
}
|
|
80
85
|
}
|
|
81
86
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nhb-toolbox",
|
|
3
|
-
"version": "4.12.
|
|
3
|
+
"version": "4.12.34",
|
|
4
4
|
"description": "A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|