nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.
9 lines (8 loc) • 440 B
JavaScript
import { isNonEmptyString, isNumber, isString } from '../guards/primitives.js';
import { COUNTRIES } from '../object/countries.js';
export function getCountryByPhone(phone) {
if (!isNonEmptyString(phone) && !isNumber(phone))
return [];
const normalized = (isString(phone) ? phone : String(phone)).replace(/\D/g, '');
return COUNTRIES.filter((country) => normalized.startsWith(country.country_code.replace(/-/g, '')));
}