nhb-toolbox 4.12.34 → 4.12.36

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 CHANGED
@@ -6,9 +6,15 @@ All notable changes to the package will be documented here.
6
6
 
7
7
  ---
8
8
 
9
- ## [4.12.34] - 2025-06-12
9
+ ## [4.12.36] - 2025-06-13
10
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.
11
+ - **Added** new `convertSync()` method in `Currency` class to convert currency without network request.
12
+
13
+ ## [4.12.34-35] - 2025-06-12
14
+
15
+ - **Updated** `format()` and `convert()` methods in `Currency` class:
16
+ - `format()` method now accepts `CurrencyCode` as optional second parameter
17
+ - `convert()` method now returns a new `Currency` instance.
12
18
 
13
19
  ## [4.12.33] - 2025-06-11
14
20
 
@@ -62,8 +62,11 @@ class Currency {
62
62
  * @param options - Optional settings:
63
63
  * - `fallbackRate`: A manual exchange rate to use if the API call fails or currency is not supported.
64
64
  * - `forceRefresh`: If true, ignores cached rates and fetches fresh data.
65
- * @returns The converted amount as a number.
65
+ * @returns A new `Currency` instance with the converted amount in the target currency.
66
66
  * @throws Will throw error if the API call fails and no `fallbackRate` is provided.
67
+ *
68
+ * @example
69
+ * await new Currency(100, 'USD').convert('EUR');
67
70
  */
68
71
  async convert(to, options) {
69
72
  const key = `${this.#code}->${to}`;
@@ -86,6 +89,37 @@ class Currency {
86
89
  }
87
90
  }
88
91
  }
92
+ /**
93
+ * @instance Converts the current currency amount to a target currency using either a cached rate or a manual exchange rate.
94
+ *
95
+ * - This method is **synchronous** and does **not perform any network requests**.
96
+ * - If a cached rate exists for the currency pair, it is used.
97
+ * - If no cached rate is found, `rate` is used as a manual exchange rate.
98
+ * - If neither are available, the original instance is returned unchanged.
99
+ *
100
+ * @param to - The target currency code to convert to.
101
+ * @param rate - A manual exchange rate to use if no cached rate is available.
102
+ * @returns A new `Currency` instance with the converted amount, or the original instance if no rate is available.
103
+ *
104
+ * @example
105
+ * const usd = new Currency(100, 'USD');
106
+ * const eur = usd.convertSync('EUR', 0.92);
107
+ *
108
+ * console.log(eur.currency); // €92.00
109
+ */
110
+ convertSync(to, rate) {
111
+ const key = `${this.#code}->${to}`;
112
+ const cachedRate = Currency.#rateCache.get(key);
113
+ if (cachedRate) {
114
+ return new Currency(this.#amount * cachedRate, to);
115
+ }
116
+ else if (rate) {
117
+ return new Currency(this.#amount * rate, to);
118
+ }
119
+ else {
120
+ return this;
121
+ }
122
+ }
89
123
  /**
90
124
  * @private Attempts to fetch rate from frankfurter.app
91
125
  * @param to - Target currency code
@@ -50,9 +50,31 @@ export declare class Currency {
50
50
  * @param options - Optional settings:
51
51
  * - `fallbackRate`: A manual exchange rate to use if the API call fails or currency is not supported.
52
52
  * - `forceRefresh`: If true, ignores cached rates and fetches fresh data.
53
- * @returns The converted amount as a number.
53
+ * @returns A new `Currency` instance with the converted amount in the target currency.
54
54
  * @throws Will throw error if the API call fails and no `fallbackRate` is provided.
55
+ *
56
+ * @example
57
+ * await new Currency(100, 'USD').convert('EUR');
55
58
  */
56
59
  convert(to: SupportedCurrency | CurrencyCode, options?: ConvertOptions): Promise<Currency>;
60
+ /**
61
+ * @instance Converts the current currency amount to a target currency using either a cached rate or a manual exchange rate.
62
+ *
63
+ * - This method is **synchronous** and does **not perform any network requests**.
64
+ * - If a cached rate exists for the currency pair, it is used.
65
+ * - If no cached rate is found, `rate` is used as a manual exchange rate.
66
+ * - If neither are available, the original instance is returned unchanged.
67
+ *
68
+ * @param to - The target currency code to convert to.
69
+ * @param rate - A manual exchange rate to use if no cached rate is available.
70
+ * @returns A new `Currency` instance with the converted amount, or the original instance if no rate is available.
71
+ *
72
+ * @example
73
+ * const usd = new Currency(100, 'USD');
74
+ * const eur = usd.convertSync('EUR', 0.92);
75
+ *
76
+ * console.log(eur.currency); // €92.00
77
+ */
78
+ convertSync(to: CurrencyCode, rate: number): Currency;
57
79
  }
58
80
  //# 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;;;;;;;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"}
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;;;;;;;;;;;;;;;;;;;OAmBG;IACG,OAAO,CACZ,EAAE,EAAE,iBAAiB,GAAG,YAAY,EACpC,OAAO,CAAC,EAAE,cAAc,GACtB,OAAO,CAAC,QAAQ,CAAC;IA6BpB;;;;;;;;;;;;;;;;;OAiBG;IACH,WAAW,CAAC,EAAE,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,GAAG,QAAQ;CA6CrD"}
@@ -59,8 +59,11 @@ export class Currency {
59
59
  * @param options - Optional settings:
60
60
  * - `fallbackRate`: A manual exchange rate to use if the API call fails or currency is not supported.
61
61
  * - `forceRefresh`: If true, ignores cached rates and fetches fresh data.
62
- * @returns The converted amount as a number.
62
+ * @returns A new `Currency` instance with the converted amount in the target currency.
63
63
  * @throws Will throw error if the API call fails and no `fallbackRate` is provided.
64
+ *
65
+ * @example
66
+ * await new Currency(100, 'USD').convert('EUR');
64
67
  */
65
68
  async convert(to, options) {
66
69
  const key = `${this.#code}->${to}`;
@@ -83,6 +86,37 @@ export class Currency {
83
86
  }
84
87
  }
85
88
  }
89
+ /**
90
+ * @instance Converts the current currency amount to a target currency using either a cached rate or a manual exchange rate.
91
+ *
92
+ * - This method is **synchronous** and does **not perform any network requests**.
93
+ * - If a cached rate exists for the currency pair, it is used.
94
+ * - If no cached rate is found, `rate` is used as a manual exchange rate.
95
+ * - If neither are available, the original instance is returned unchanged.
96
+ *
97
+ * @param to - The target currency code to convert to.
98
+ * @param rate - A manual exchange rate to use if no cached rate is available.
99
+ * @returns A new `Currency` instance with the converted amount, or the original instance if no rate is available.
100
+ *
101
+ * @example
102
+ * const usd = new Currency(100, 'USD');
103
+ * const eur = usd.convertSync('EUR', 0.92);
104
+ *
105
+ * console.log(eur.currency); // €92.00
106
+ */
107
+ convertSync(to, rate) {
108
+ const key = `${this.#code}->${to}`;
109
+ const cachedRate = Currency.#rateCache.get(key);
110
+ if (cachedRate) {
111
+ return new Currency(this.#amount * cachedRate, to);
112
+ }
113
+ else if (rate) {
114
+ return new Currency(this.#amount * rate, to);
115
+ }
116
+ else {
117
+ return this;
118
+ }
119
+ }
86
120
  /**
87
121
  * @private Attempts to fetch rate from frankfurter.app
88
122
  * @param to - Target currency code
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "4.12.34",
3
+ "version": "4.12.36",
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",