nhb-toolbox 4.12.35 → 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.36] - 2025-06-13
10
+
11
+ - **Added** new `convertSync()` method in `Currency` class to convert currency without network request.
12
+
9
13
  ## [4.12.34-35] - 2025-06-12
10
14
 
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.
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
 
@@ -89,6 +89,37 @@ class Currency {
89
89
  }
90
90
  }
91
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
+ }
92
123
  /**
93
124
  * @private Attempts to fetch rate from frankfurter.app
94
125
  * @param to - Target currency code
@@ -57,5 +57,24 @@ export declare class Currency {
57
57
  * await new Currency(100, 'USD').convert('EUR');
58
58
  */
59
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;
60
79
  }
61
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;;;;;;;;;;;;;;;;;;;OAmBG;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"}
@@ -86,6 +86,37 @@ export class Currency {
86
86
  }
87
87
  }
88
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
+ }
89
120
  /**
90
121
  * @private Attempts to fetch rate from frankfurter.app
91
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.35",
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",