UNPKG

mobile-operator-lookup

Version:

A lightweight NPM package that identifies the telecom operator of a given mobile number, including company name, mobile money service, and country code, based on predefined prefixes.

1 lines 7.01 kB
{"version":3,"sources":["../src/index.ts","../src/data.ts","../src/types.ts"],"sourcesContent":["import { parsePhoneNumber } from 'libphonenumber-js';\nimport { telecomProviders } from './data.js';\nimport type { MobileOperatorResult, LookupSuccess, LookupError } from './types.js';\n\n/**\n * Looks up mobile operator information for a given phone number\n * \n * @param phoneNumber - The phone number to look up (with or without country code)\n * @returns Object containing operator information or error details\n * \n * @example\n * ```typescript\n * import { lookupMobileOperator } from 'mobile-operator-lookup';\n * \n * const result = lookupMobileOperator(\"+23277123456\");\n * if ('error' in result) {\n * console.log('Error:', result.error);\n * } else {\n * console.log('Operator:', result.company);\n * console.log('Mobile Money:', result.mobile_money);\n * }\n * ```\n */\nexport function lookupMobileOperator(phoneNumber: string): MobileOperatorResult {\n try {\n const parsedNumber = parsePhoneNumber(phoneNumber);\n \n if (!parsedNumber || !parsedNumber.isValid()) {\n const errorResult: LookupError = { error: \"Invalid phone number\" };\n return errorResult;\n }\n\n const countryCode = `+${parsedNumber.countryCallingCode}`;\n const nationalNumber = parsedNumber.nationalNumber;\n \n const countryData = telecomProviders.find(country => country.country_code === countryCode);\n\n if (!countryData) {\n const errorResult: LookupError = { error: \"Country not supported\" };\n return errorResult;\n }\n\n const operator = countryData.operators.find(op => \n op.prefix.some(prefix => nationalNumber.startsWith(prefix))\n );\n\n if (!operator) {\n const errorResult: LookupError = { error: \"Operator not found\" };\n return errorResult;\n }\n\n const successResult: LookupSuccess = {\n company: operator.company,\n mobile_money: operator.m_money,\n slug: operator.slug,\n country_code: countryCode,\n monime_code: operator.monime_code\n };\n \n return successResult;\n } catch (error) {\n const errorResult: LookupError = { \n error: error instanceof Error ? error.message : \"Unknown error occurred\" \n };\n return errorResult;\n }\n}\n\n// Export types for consumers\nexport type { \n MobileOperatorResult, \n LookupSuccess, \n LookupError, \n Operator, \n Country \n} from './types.js';\n\nexport { isLookupSuccess, isLookupError } from './types.js';\nexport { telecomProviders } from './data.js';\n\n// Default export for backward compatibility\nexport default lookupMobileOperator;","import type { Country } from './types.js';\n\n/**\n * Telecom providers data organized by country\n * Contains operator information including prefixes, company names, and mobile money services\n */\nexport const telecomProviders: readonly Country[] = [\n {\n country: \"Sierra Leone\",\n country_code: \"+232\",\n operators: [\n { \n prefix: [\"74\", \"75\", \"76\", \"78\", \"79\"], \n company: \"Orange\", \n m_money: \"Orange Money\", \n slug: \"orange-money\", \n monime_code: \"m17\" \n },\n { \n prefix: [\"88\", \"77\", \"90\", \"99\", \"30\", \"33\"], \n company: \"Africell\", \n m_money: \"Afrimoney\", \n slug: \"afrimoney\", \n monime_code: \"m18\" \n },\n { \n prefix: [\"31\", \"34\"], \n company: \"Qcell\", \n m_money: \"Qcell Money\", \n slug: \"qcell-money\", \n monime_code: \"m13\" \n }\n ]\n },\n // Add more countries and operators as needed\n] as const;\n\nexport default telecomProviders;","/**\n * Represents a telecom operator with associated mobile money service\n */\nexport interface Operator {\n /** Array of mobile number prefixes for this operator */\n prefix: string[];\n /** Name of the telecom company */\n company: string;\n /** Name of the mobile money service */\n m_money: string;\n /** URL-friendly slug for the mobile money service */\n slug: string;\n /** Unique identifier for Monime integration */\n monime_code: string;\n}\n\n/**\n * Represents a country with its operators and metadata\n */\nexport interface Country {\n /** Human-readable country name */\n country: string;\n /** International dialing code (e.g., \"+232\") */\n country_code: string;\n /** Array of telecom operators in this country */\n operators: Operator[];\n}\n\n/**\n * Successful lookup result containing operator information\n */\nexport interface LookupSuccess {\n /** Name of the telecom company */\n company: string;\n /** Name of the mobile money service */\n mobile_money: string;\n /** URL-friendly slug for the mobile money service */\n slug: string;\n /** International dialing code */\n country_code: string;\n /** Unique identifier for Monime integration */\n monime_code: string;\n}\n\n/**\n * Error result when lookup fails\n */\nexport interface LookupError {\n /** Error message describing why the lookup failed */\n error: string;\n}\n\n/**\n * Union type representing all possible return values from mobile operator lookup\n */\nexport type MobileOperatorResult = LookupSuccess | LookupError;\n\n/**\n * Type guard to check if result is a successful lookup\n */\nexport function isLookupSuccess(result: MobileOperatorResult): result is LookupSuccess {\n return !('error' in result);\n}\n\n/**\n * Type guard to check if result is an error\n */\nexport function isLookupError(result: MobileOperatorResult): result is LookupError {\n return 'error' in result;\n}"],"mappings":";AAAA,SAAS,wBAAwB;;;ACM1B,IAAM,mBAAuC;AAAA,EAClD;AAAA,IACE,SAAS;AAAA,IACT,cAAc;AAAA,IACd,WAAW;AAAA,MACT;AAAA,QACE,QAAQ,CAAC,MAAM,MAAM,MAAM,MAAM,IAAI;AAAA,QACrC,SAAS;AAAA,QACT,SAAS;AAAA,QACT,MAAM;AAAA,QACN,aAAa;AAAA,MACf;AAAA,MACA;AAAA,QACE,QAAQ,CAAC,MAAM,MAAM,MAAM,MAAM,MAAM,IAAI;AAAA,QAC3C,SAAS;AAAA,QACT,SAAS;AAAA,QACT,MAAM;AAAA,QACN,aAAa;AAAA,MACf;AAAA,MACA;AAAA,QACE,QAAQ,CAAC,MAAM,IAAI;AAAA,QACnB,SAAS;AAAA,QACT,SAAS;AAAA,QACT,MAAM;AAAA,QACN,aAAa;AAAA,MACf;AAAA,IACF;AAAA,EACF;AAAA;AAEF;;;ACyBO,SAAS,gBAAgB,QAAuD;AACrF,SAAO,EAAE,WAAW;AACtB;AAKO,SAAS,cAAc,QAAqD;AACjF,SAAO,WAAW;AACpB;;;AF9CO,SAAS,qBAAqB,aAA2C;AAC9E,MAAI;AACF,UAAM,eAAe,iBAAiB,WAAW;AAEjD,QAAI,CAAC,gBAAgB,CAAC,aAAa,QAAQ,GAAG;AAC5C,YAAM,cAA2B,EAAE,OAAO,uBAAuB;AACjE,aAAO;AAAA,IACT;AAEA,UAAM,cAAc,IAAI,aAAa,kBAAkB;AACvD,UAAM,iBAAiB,aAAa;AAEpC,UAAM,cAAc,iBAAiB,KAAK,aAAW,QAAQ,iBAAiB,WAAW;AAEzF,QAAI,CAAC,aAAa;AAChB,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