phone-number-lookup
Version:
phone-number-lookup is TS written library that provides information about a phone number, such as: Country, Prefix, Type (Mobile/Fixed/VoIP), Carrier etc. Every data is processed locally, so no internet connection is needed. Made primarily for personal-us
39 lines (31 loc) • 1.27 kB
text/typescript
import { generateHashMapData } from '.';
import { ICountry } from '../interface/country.interface';
import { IData } from '../interface/data.interface';
import { IPhoneInformation } from '../interface/phone-information';
export function getInformation(number: string, data: Map<ICountry, Array<IData>>): IPhoneInformation {
let phoneInformation: IPhoneInformation = {
name: '',
dialCode: '',
isoCode: '',
flag: '',
type: '',
};
let prefixLength = 2;
data.forEach((value, key) => {
if (number.startsWith(key.dialCode)) {
const splittedInfromation = value[0].Destination.split(' - ');
phoneInformation = { ...key, type: splittedInfromation[1], carrier: splittedInfromation[2] };
for (let index = 0; index < value.length; index++) {
if (number.startsWith('+' + value[index].Prefix.toString())) {
if (value[index].Prefix.toString().length > prefixLength) {
prefixLength = value[index].Prefix.toString().length;
const splittedInfromation = value[index].Destination.split(' - ');
phoneInformation.type = splittedInfromation[1];
phoneInformation.carrier = splittedInfromation[2];
}
}
}
}
});
return phoneInformation;
}