PromucFlow_constructor/app/client/src/widgets/CurrencyInputWidget/component/utilities.ts
Bhavin K e8219284ea
feat:allow local decimal sep. curr. & input widget (#16380)
* feat:allow local decimal sep. curr. & input widget

* test: fix the test for input widget

* refactor: code refinement

* refactor: rename the suggestion key

* fix: changed the regex to replaceAll function

* fix: address review callouts

* refactor: new default and min max validation

* fix: directly converting default value to current locale

* fix: converting value to locale directly inside onStep

* fix: add fraction during formatting

* revert: input widget locale changes

* fix: restrict getting of current locale for currency widget

* fix: introduced shouldUseLocale prop

Co-authored-by: balajisoundar <balaji@appsmith.com>
Co-authored-by: keyurparalkar <keyur@appsmith.com>
Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
2022-11-02 16:02:45 +05:30

73 lines
2.2 KiB
TypeScript

import { getLocale } from "utils/helpers";
import {
getLocaleDecimalSeperator,
getLocaleThousandSeparator,
} from "widgets/WidgetUtils";
export const countryToFlag = (isoCode: string) => {
return typeof String.fromCodePoint !== "undefined"
? isoCode
.toUpperCase()
.replace(/./g, (char) =>
String.fromCodePoint(char.charCodeAt(0) + 127397),
)
: isoCode;
};
/*
Returns formatted value with maximum number of decimals based on decimalsInCurrency value
and add commas based on user's locale
for eg:
a) (2, 1235.456) will return 1,235.45
b) (1, 1234.456) will return 1,234.4
*/
export const formatCurrencyNumber = (decimalsInCurrency = 0, value: string) => {
const fractionDigits = decimalsInCurrency || 0;
const hasDecimal = value.includes(getLocaleDecimalSeperator());
const locale = getLocale();
const formatter = new Intl.NumberFormat(locale, {
style: "decimal",
minimumFractionDigits: hasDecimal ? fractionDigits : 0,
maximumFractionDigits: hasDecimal ? fractionDigits : 0,
});
const parsedValue = parseLocaleFormattedStringToNumber(value);
return formatter.format(isNaN(parsedValue) ? 0 : parsedValue);
};
/*
Returns value in string format with maximum number of decimals based on decimalsInCurrency value
for eg:
a) (2, 1235.456) will return 1235.45
b) (1, 1234.456) will return 1234.4
*/
export const limitDecimalValue = (decimals = 0, value = "") => {
const decimalSeperator = getLocaleDecimalSeperator();
value = value.split(getLocaleThousandSeparator()).join("");
switch (decimals) {
case 0:
return value.split(decimalSeperator).shift() || "";
case 1:
case 2:
const decimalValueArray = value.split(decimalSeperator);
return (
decimalValueArray[0] +
decimalSeperator +
decimalValueArray[1].slice(0, decimals)
);
default:
return value;
}
};
/*
* Parses the locale formatted currency string to number
*/
export function parseLocaleFormattedStringToNumber(currencyString = "") {
return parseFloat(
currencyString
.replace(new RegExp("\\" + getLocaleThousandSeparator(), "g"), "")
.replace(new RegExp("\\" + getLocaleDecimalSeperator()), "."),
);
}