salix-front/src/filters/parsePhone.js

17 lines
508 B
JavaScript
Raw Normal View History

2024-11-27 11:54:11 +00:00
import axios from 'axios';
export default async function parsePhone(phone, country) {
if (!phone) return;
2024-11-27 11:54:11 +00:00
if (phone.startsWith('+')) return `${phone.slice(1)}`;
if (phone.startsWith('00')) return `${phone.slice(2)}`;
try {
const prefix = (
await axios.get(`Prefixes/${country.toLowerCase()}`)
).data?.prefix.replace(/^0+/, '');
if (phone.startsWith(prefix)) return phone;
return `${prefix}${phone}`;
} catch (e) {
return null;
}
}