Skip to content

Commit

Permalink
create phone class
Browse files Browse the repository at this point in the history
  • Loading branch information
BolajiOlajide committed Sep 4, 2024
1 parent e4d692a commit c7bea57
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 2 additions & 2 deletions lib/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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);
Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions lib/phone.spec.ts
Original file line number Diff line number Diff line change
@@ -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+$/);
});
});
});
28 changes: 28 additions & 0 deletions lib/phone.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};

0 comments on commit c7bea57

Please sign in to comment.