Skip to content

Commit 1fcd056

Browse files
committed
Implement a raw-to-pretty formatter utility
1 parent 95b9daf commit 1fcd056

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

development/src/phone-hooks/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const getRawValue = (value: PhoneNumber | string) => {
2727
}
2828

2929
export const displayFormat = (value: string) => {
30+
/** Returns the formatted value that can be displayed as an actual input value */
3031
return value.replace(/[.\s\D]+$/, "").replace(/(\(\d+)$/, "$1)");
3132
}
3233

@@ -35,6 +36,11 @@ export const cleanInput = (input: any, pattern: string) => {
3536
return Array.from(pattern, c => input[0] === c || slots.has(c) ? input.shift() || c : c);
3637
}
3738

39+
export const getFormattedNumber = (rawValue: any, pattern: string) => {
40+
/** Returns the reformatted input value based on the given pattern */
41+
return displayFormat(cleanInput(rawValue, pattern.replaceAll(/\d/g, ".")).join(""));
42+
}
43+
3844
export const checkValidity = (metadata: PhoneNumber, strict: boolean = false) => {
3945
/** Checks if both the area code and phone number match the validation pattern */
4046
const pattern = (validations as any)[metadata.isoCode as keyof typeof validations][Number(strict)];
@@ -134,11 +140,11 @@ export const usePhone = ({
134140
i = clean(target.value.slice(0, i)).findIndex(c => slots.has(c));
135141
return i < 0 ? prev[prev.length - 1] : backRef.current ? prev[i - 1] || first : i;
136142
});
137-
target.value = displayFormat(clean(target.value).join(""));
143+
target.value = getFormattedNumber(target.value, pattern);
138144
target.setSelectionRange(i, j);
139145
backRef.current = false;
140146
setValue(target.value);
141-
}, [clean, first, prev])
147+
}, [clean, first, pattern, prev])
142148

143149
return {
144150
clean,

src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const getRawValue = (value: PhoneNumber | string) => {
2727
}
2828

2929
export const displayFormat = (value: string) => {
30+
/** Returns the formatted value that can be displayed as an actual input value */
3031
return value.replace(/[.\s\D]+$/, "").replace(/(\(\d+)$/, "$1)");
3132
}
3233

@@ -35,6 +36,11 @@ export const cleanInput = (input: any, pattern: string) => {
3536
return Array.from(pattern, c => input[0] === c || slots.has(c) ? input.shift() || c : c);
3637
}
3738

39+
export const getFormattedNumber = (rawValue: any, pattern: string) => {
40+
/** Returns the reformatted input value based on the given pattern */
41+
return displayFormat(cleanInput(rawValue, pattern.replaceAll(/\d/g, ".")).join(""));
42+
}
43+
3844
export const checkValidity = (metadata: PhoneNumber, strict: boolean = false) => {
3945
/** Checks if both the area code and phone number match the validation pattern */
4046
const pattern = (validations as any)[metadata.isoCode as keyof typeof validations][Number(strict)];
@@ -134,11 +140,11 @@ export const usePhone = ({
134140
i = clean(target.value.slice(0, i)).findIndex(c => slots.has(c));
135141
return i < 0 ? prev[prev.length - 1] : backRef.current ? prev[i - 1] || first : i;
136142
});
137-
target.value = displayFormat(clean(target.value).join(""));
143+
target.value = getFormattedNumber(target.value, pattern);
138144
target.setSelectionRange(i, j);
139145
backRef.current = false;
140146
setValue(target.value);
141-
}, [clean, first, prev])
147+
}, [clean, first, pattern, prev])
142148

143149
return {
144150
clean,

0 commit comments

Comments
 (0)