Skip to content

Commit b9cf139

Browse files
committed
Add ReturnType type and minor refactor of currency function
1 parent 1b77d68 commit b9cf139

File tree

2 files changed

+73
-62
lines changed

2 files changed

+73
-62
lines changed

src/index.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -588,19 +588,22 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
588588

589589
if (maskType === undefined && mask === undefined) return onChangeText(val);
590590

591+
let newValue: string | undefined;
592+
591593
if (maskType !== 'currency' && mask !== undefined) {
592-
const newValue = getValueFromNonCurrencyMask({ value: val, mask });
593-
return onChangeText(newValue);
594+
newValue = getValueFromNonCurrencyMask({ value: val, mask });
594595
}
595596

596597
if (maskType === 'currency') {
597-
const newValue = getValueFromCurrencyMask({
598-
value: val,
598+
newValue = getValueFromCurrencyMask({
599+
value,
600+
newValue: val,
599601
currencyDivider,
600602
maxDecimalPlaces,
601603
});
602-
return onChangeText(newValue);
603604
}
605+
606+
if (newValue !== undefined) return onChangeText(newValue);
604607
}
605608

606609
function onLayout(event: LayoutChangeEvent) {

src/utils.tsx

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ type NonCurrencyMaskTypeArgs = {
1414
mask: Mask;
1515
};
1616

17+
type ResultType = string | undefined;
18+
1719
export function getValueFromNonCurrencyMask({
1820
value,
1921
mask,
20-
}: NonCurrencyMaskTypeArgs): string {
22+
}: NonCurrencyMaskTypeArgs): ResultType {
2123
let unmasked = value.replace(/[^0-9A-Za-z]/g, '');
2224

2325
// mark positions of special characters
@@ -42,81 +44,87 @@ export function getValueFromNonCurrencyMask({
4244
return newValue;
4345
}
4446

47+
function getCurrencyDividerAndDecimal(divider: CurrencyDivider | undefined) {
48+
if (divider === ',') {
49+
return {
50+
divider: ',' as const,
51+
decimal: '.' as const,
52+
};
53+
} else
54+
return {
55+
divider: '.' as const,
56+
decimal: ',' as const,
57+
};
58+
}
59+
4560
export function getValueFromCurrencyMask({
4661
value,
4762
newValue,
4863
currencyDivider,
4964
maxDecimalPlaces,
50-
}: CurrencyMaskTypeArgs): string {
51-
let divider = '';
52-
let decimal = '';
53-
if (currencyDivider === ',') {
54-
divider = ',';
55-
decimal = '.';
56-
} else {
57-
divider = '.';
58-
decimal = ',';
59-
}
60-
if (value !== undefined && value.length < newValue.length) {
61-
if (newValue.includes(decimal)) {
62-
let intVal = newValue.split(decimal)[0].replace(/[,.]/g, '');
63-
let decimalValue = newValue.split(decimal)[1];
64-
if (intVal.length > 3) {
65-
let arr: string[] = [];
66-
for (let i = 0; i < intVal.length; i += 3) {
67-
arr.push(
68-
intVal
69-
.split('')
70-
.splice(intVal.length - i, 3)
71-
.join(''),
72-
);
73-
}
74-
75-
arr = arr.reverse();
76-
arr.pop();
77-
let initial = arr.join('');
78-
if (intVal.includes(initial)) {
79-
intVal = intVal.replace(initial, '');
80-
}
81-
intVal = intVal + divider + arr.join(divider);
82-
}
83-
84-
newValue = intVal + decimal + decimalValue;
65+
}: CurrencyMaskTypeArgs): ResultType {
66+
const { divider, decimal } = getCurrencyDividerAndDecimal(currencyDivider);
8567

86-
let decimalPlaces: number =
87-
maxDecimalPlaces !== undefined ? maxDecimalPlaces : 2;
68+
if (value.length >= newValue.length) return undefined;
8869

89-
if (
90-
newValue.split(decimal)[1] !== undefined &&
91-
value.split(decimal)[1] !== undefined &&
92-
newValue.split(decimal)[1].length > value.split(decimal)[1].length &&
93-
value.split(decimal)[1].length === decimalPlaces
94-
) {
95-
return '';
96-
}
97-
if (newValue.split(decimal)[1].length > decimalPlaces) {
98-
newValue = newValue.slice(0, newValue.length - 1);
99-
}
100-
} else if (newValue.length > 3) {
70+
if (newValue.includes(decimal)) {
71+
let intVal = newValue.split(decimal)[0].replace(/[,.]/g, '');
72+
let decimalValue = newValue.split(decimal)[1];
73+
if (intVal.length > 3) {
10174
let arr: string[] = [];
102-
let unmasked = newValue.replace(/[,.]/g, '');
103-
for (let i = 0; i < unmasked.length; i += 3) {
75+
for (let i = 0; i < intVal.length; i += 3) {
10476
arr.push(
105-
unmasked
77+
intVal
10678
.split('')
107-
.splice(unmasked.length - i, 3)
79+
.splice(intVal.length - i, 3)
10880
.join(''),
10981
);
11082
}
11183

11284
arr = arr.reverse();
11385
arr.pop();
11486
let initial = arr.join('');
115-
if (unmasked.includes(initial)) {
116-
unmasked = unmasked.replace(initial, '');
87+
if (intVal.includes(initial)) {
88+
intVal = intVal.replace(initial, '');
11789
}
118-
newValue = unmasked + divider + arr.join(divider);
90+
intVal = intVal + divider + arr.join(divider);
91+
}
92+
93+
newValue = intVal + decimal + decimalValue;
94+
95+
let decimalPlaces: number =
96+
maxDecimalPlaces !== undefined ? maxDecimalPlaces : 2;
97+
98+
if (
99+
newValue.split(decimal)[1] !== undefined &&
100+
value.split(decimal)[1] !== undefined &&
101+
newValue.split(decimal)[1].length > value.split(decimal)[1].length &&
102+
value.split(decimal)[1].length === decimalPlaces
103+
) {
104+
return '';
105+
}
106+
if (newValue.split(decimal)[1].length > decimalPlaces) {
107+
newValue = newValue.slice(0, newValue.length - 1);
108+
}
109+
} else if (newValue.length > 3) {
110+
let arr: string[] = [];
111+
let unmasked = newValue.replace(/[,.]/g, '');
112+
for (let i = 0; i < unmasked.length; i += 3) {
113+
arr.push(
114+
unmasked
115+
.split('')
116+
.splice(unmasked.length - i, 3)
117+
.join(''),
118+
);
119+
}
120+
121+
arr = arr.reverse();
122+
arr.pop();
123+
let initial = arr.join('');
124+
if (unmasked.includes(initial)) {
125+
unmasked = unmasked.replace(initial, '');
119126
}
127+
newValue = unmasked + divider + arr.join(divider);
120128
}
121129
return newValue;
122130
}

0 commit comments

Comments
 (0)