@wrcb/cb-common 1.0.492 → 1.0.494
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/build/index.d.ts
CHANGED
package/build/index.js
CHANGED
|
@@ -46,3 +46,4 @@ __exportStar(require("./types/giftType"), exports);
|
|
|
46
46
|
__exportStar(require("./types/paymentProviderType"), exports);
|
|
47
47
|
__exportStar(require("./events/subjects"), exports);
|
|
48
48
|
__exportStar(require("./services/TenantDataService"), exports);
|
|
49
|
+
__exportStar(require("./services/CurrencyService"), exports);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Country } from '@wrcb/cb-common';
|
|
2
|
+
export declare class CurrencyService {
|
|
3
|
+
private static readonly CURRENCY_CONFIG;
|
|
4
|
+
private static getCurrencyFromCountry;
|
|
5
|
+
private static getCurrencyConfig;
|
|
6
|
+
static formatToCurrency(valueInCents: number, country?: Country): string;
|
|
7
|
+
static parseFromCurrency(formattedValue: string, country?: Country): number;
|
|
8
|
+
static formatToInput(valueInCents: number, country?: Country): string;
|
|
9
|
+
static isValidCents(value: number): boolean;
|
|
10
|
+
static toCents(unitsValue: number): number;
|
|
11
|
+
static getCurrencySymbol(country?: Country): string;
|
|
12
|
+
static getCurrencyCode(country?: Country): string;
|
|
13
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CurrencyService = void 0;
|
|
4
|
+
const cb_common_1 = require("@wrcb/cb-common");
|
|
5
|
+
class CurrencyService {
|
|
6
|
+
static getCurrencyFromCountry(country) {
|
|
7
|
+
var _a;
|
|
8
|
+
if (!country)
|
|
9
|
+
return cb_common_1.Currency.BRL;
|
|
10
|
+
return ((_a = cb_common_1.countryInfoMap[country]) === null || _a === void 0 ? void 0 : _a.currency) || cb_common_1.Currency.BRL;
|
|
11
|
+
}
|
|
12
|
+
static getCurrencyConfig(country) {
|
|
13
|
+
const currency = this.getCurrencyFromCountry(country);
|
|
14
|
+
return this.CURRENCY_CONFIG[currency];
|
|
15
|
+
}
|
|
16
|
+
static formatToCurrency(valueInCents, country) {
|
|
17
|
+
const config = this.getCurrencyConfig(country);
|
|
18
|
+
const valueInUnits = valueInCents / 100;
|
|
19
|
+
return valueInUnits.toLocaleString(config.locale, {
|
|
20
|
+
style: 'currency',
|
|
21
|
+
currency: config.currency,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
static parseFromCurrency(formattedValue, country) {
|
|
25
|
+
const config = this.getCurrencyConfig(country);
|
|
26
|
+
let cleanValue = formattedValue
|
|
27
|
+
.replace(new RegExp(`[${config.symbol}\\s.]`, 'g'), '')
|
|
28
|
+
.replace(',', '.');
|
|
29
|
+
return Math.round(parseFloat(cleanValue) * 100);
|
|
30
|
+
}
|
|
31
|
+
static formatToInput(valueInCents, country) {
|
|
32
|
+
const config = this.getCurrencyConfig(country);
|
|
33
|
+
const valueInUnits = valueInCents / 100;
|
|
34
|
+
if (config.currency === cb_common_1.Currency.BRL) {
|
|
35
|
+
return valueInUnits.toFixed(2).replace('.', ',');
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
// Para Argentina, usar ponto decimal
|
|
39
|
+
return valueInUnits.toFixed(2);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
static isValidCents(value) {
|
|
43
|
+
return Number.isInteger(value) && value >= 0;
|
|
44
|
+
}
|
|
45
|
+
static toCents(unitsValue) {
|
|
46
|
+
return Math.round(unitsValue * 100);
|
|
47
|
+
}
|
|
48
|
+
static getCurrencySymbol(country) {
|
|
49
|
+
const config = this.getCurrencyConfig(country);
|
|
50
|
+
return config.symbol;
|
|
51
|
+
}
|
|
52
|
+
static getCurrencyCode(country) {
|
|
53
|
+
const config = this.getCurrencyConfig(country);
|
|
54
|
+
return config.currency;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.CurrencyService = CurrencyService;
|
|
58
|
+
CurrencyService.CURRENCY_CONFIG = {
|
|
59
|
+
[cb_common_1.Currency.BRL]: {
|
|
60
|
+
locale: 'pt-BR',
|
|
61
|
+
currency: 'BRL',
|
|
62
|
+
symbol: 'R$',
|
|
63
|
+
},
|
|
64
|
+
[cb_common_1.Currency.ARS]: {
|
|
65
|
+
locale: 'es-AR',
|
|
66
|
+
currency: 'ARS',
|
|
67
|
+
symbol: '$',
|
|
68
|
+
},
|
|
69
|
+
};
|