UNPKG

nigerian-mobile-validator

Version:

The most rigorous, up-to-date library for validating Nigerian mobile numbers. Fully NCC-compliant, and security-focused, with enterprise-grade features to prevent the business risks of validation failures in regulated industries.

115 lines (114 loc) 5.1 kB
"use strict"; // src/__tests__/synthetic-data/test-data-generator-invalid-numbers.ts var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TestDataGeneratorInvalidNumbers = void 0; // src/test-data-generator-invalid-numbers.ts const test_data_generator_base_1 = require("./test-data-generator-base"); const network_access_code_1 = require("../../numbering-plan/network-access-code"); const chance_1 = __importDefault(require("chance")); // Initialize Chance with a seed for reproducibility if needed const chance = new chance_1.default(); /** * Generator for invalid Nigerian mobile numbers */ class TestDataGeneratorInvalidNumbers extends test_data_generator_base_1.TestDataGeneratorBase { /** * Generate a valid withdrawn number in the 702 range */ static generateWithdrawn702Number() { if (chance.bool()) { return `0${network_access_code_1.NetworkAccessCode.n702}${this.random702SubscriberNumber('Withdrawn1')}`; } else { return `0${network_access_code_1.NetworkAccessCode.n702}${this.random702SubscriberNumber('Withdrawn2')}`; } } /** * Generate a valid returned number in the 702 range */ static generateReturned702Number() { return `0${network_access_code_1.NetworkAccessCode.n702}${this.random702SubscriberNumber('Returned')}`; } /** * Generate a number with invalid length (too long or too short) */ static generateInvalidLengthNumber(networkCode, tooLong = true) { if (tooLong) { // Add 1-3 extra random digits const extraDigits = chance.string({ length: chance.integer({ min: 1, max: 3 }), pool: '0123456789' }); return `0${networkCode}${this.randomSubscriberNumber(networkCode)}${extraDigits}`; } else { // Remove 1-3 digits from subscriber number const truncatedLength = chance.integer({ min: 4, max: 6 }); return `0${networkCode}${chance.string({ length: truncatedLength, pool: '0123456789' })}`; } } /** * Generate a number with non-numeric characters at random positions */ static generateNonNumericNumber(networkCode) { const subscriberNumber = this.randomSubscriberNumber(networkCode); const number = `0${networkCode}${subscriberNumber}`; // Replace 1-3 digits with non-numeric characters let result = number.split(''); const positions = chance.unique(chance.integer, chance.integer({ min: 1, max: 3 }), { min: 1, max: number.length - 1 }); for (const pos of positions) { // Alpha 'O', 'o' and '(', ')' are not included because those are replaced by numberic 0 when we sanitize. // '('and ')' are not included because those are removed when we sanitize. result[pos] = chance.character({ pool: 'ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz!@#$%^&*' }); } return result.join(''); } /** * Generate a number with invalid network code */ static generateInvalidNetworkCodeNumber() { const invalidCode = this.randomInvalidNetworkCode(); return `0${invalidCode}${this.randomSubscriberNumber()}`; } // Helper methods for the randomPhoneNumber generator static addRandomSpaces(mobileNumber) { let result = ''; const positions = chance.unique(chance.integer, chance.integer({ min: 1, max: 3 }), { min: 1, max: mobileNumber.length - 1 }); let lastPos = 0; for (const pos of positions.sort((a, b) => a - b)) { result += mobileNumber.substring(lastPos, pos) + ' '; lastPos = pos; } result += mobileNumber.substring(lastPos); return result; } static replaceZerosWithO(number) { let result = ''; for (const element of number) { if (element === '0' && chance.bool({ likelihood: 30 })) { result += chance.bool() ? 'o' : 'O'; } else { result += element; } } return result; } static addNonNumericChars(number) { let result = number.split(''); const positions = chance.unique(chance.integer, chance.integer({ min: 1, max: 2 }), { min: 1, max: number.length - 1 }); for (const pos of positions) { result[pos] = chance.character({ pool: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()' }); } return result.join(''); } static makeTooLong(number) { const extraDigits = chance.string({ length: chance.integer({ min: 1, max: 3 }), pool: '0123456789' }); return `${number}${extraDigits}`; } static makeTooShort(number) { const charsToRemove = chance.integer({ min: 1, max: 3 }); return number.substring(0, number.length - charsToRemove); } } exports.TestDataGeneratorInvalidNumbers = TestDataGeneratorInvalidNumbers;