From c7bea579fafbdffcd510b0f293c170c1d59ea54e Mon Sep 17 00:00:00 2001 From: Bolaji Olajide <25608335+BolajiOlajide@users.noreply.github.com> Date: Wed, 4 Sep 2024 02:29:32 +0100 Subject: [PATCH] create phone class --- lib/account.spec.ts | 4 +++- lib/account.ts | 4 ++-- lib/index.ts | 7 ++++++- lib/phone.spec.ts | 40 ++++++++++++++++++++++++++++++++++++++++ lib/phone.ts | 28 ++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 lib/phone.spec.ts create mode 100644 lib/phone.ts diff --git a/lib/account.spec.ts b/lib/account.spec.ts index fb3f58c..0921966 100644 --- a/lib/account.spec.ts +++ b/lib/account.spec.ts @@ -2,12 +2,14 @@ import { describe, test, expect, beforeAll } from 'vitest'; import { Account } from './account'; import { banks } from './common/banks'; +import { Random } from './random'; describe('Account', () => { let account: Account; beforeAll(() => { - account = new Account(); + const random = new Random(); + account = new Account(random); }); describe('banks', () => { diff --git a/lib/account.ts b/lib/account.ts index 49cefcc..53f9ebb 100644 --- a/lib/account.ts +++ b/lib/account.ts @@ -4,8 +4,8 @@ import { Random } from './random' export class Account { private random: Random; - constructor() { - this.random = new Random(); + constructor(random: Random) { + this.random = random; } banks(): string | undefined { diff --git a/lib/index.ts b/lib/index.ts index 8922637..8b9dc59 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -3,6 +3,8 @@ import { ZodError } from 'zod'; import { Locale } from './types'; import { ngFakerOptsSchema, type NgFakerOpts } from './schema'; import { Random } from './random'; +import { Account } from './account'; +import { Phone } from './phone'; const definitions = { name: ['male_first_name', 'last_name', 'female_first_name', 'prefix'], @@ -14,7 +16,8 @@ class NgFaker { private locale: Locale; public random: Random; - + public account: Account; + public phone: Phone; constructor(opts: NgFakerOpts) { try { ngFakerOptsSchema.parse(opts); @@ -24,6 +27,8 @@ class NgFaker { this.locale = opts.locale; this.random = new Random(); + this.account = new Account(this.random); + this.phone = new Phone(this.random); } catch (err: unknown) { if (err instanceof ZodError) { // We want to display Zod errors one at a time, so we stick diff --git a/lib/phone.spec.ts b/lib/phone.spec.ts new file mode 100644 index 0000000..077adc9 --- /dev/null +++ b/lib/phone.spec.ts @@ -0,0 +1,40 @@ +import { describe, test, expect, beforeEach } from 'vitest'; + +import { Phone } from './phone'; +import { serviceProviders } from './common/phone'; +import { Random } from './random'; + +describe('Phone', () => { + let phone: Phone; + + beforeEach(() => { + const random = new Random(); + phone = new Phone(random); + }); + + describe('serviceProvider', () => { + test('should return a service provider from the serviceProviders array', () => { + const result = phone.serviceProvider(); + + expect(serviceProviders).toContain(result); + }); + }); + + describe('number', () => { + test('should generate a local phone number starting with 0', () => { + const result = phone.number(); + + expect(result).toHaveLength(11); + expect(result.startsWith('0')).toEqual(true); + expect(result).toMatch(/^0\d+$/); + }); + + test('should generate an international phone number starting with +234', () => { + const result = phone.number(true); + + expect(result).toHaveLength(14); + expect(result.startsWith('+234')).toEqual(true); + expect(result).toMatch(/^\+234\d+$/); + }); + }); +}); diff --git a/lib/phone.ts b/lib/phone.ts new file mode 100644 index 0000000..0af275f --- /dev/null +++ b/lib/phone.ts @@ -0,0 +1,28 @@ +import { serviceProviders } from './common/phone'; +import type { Random } from './random'; + +export class Phone { + private random: Random; + + constructor(random: Random) { + this.random = random; + } + + serviceProvider(): string { + return this.random.arrayElement(serviceProviders)!; + } + + number(intl: boolean = false): string { + let phoneNumber: string; + if (intl) { + phoneNumber = '+234'; + } else { + phoneNumber = '0'; + } + + for (var i = 0; i < 10; i++) { + phoneNumber += this.random.number(0, 9); + } + return phoneNumber; + } +};