Skip to content

Commit

Permalink
feat: phone IMEI (#829)
Browse files Browse the repository at this point in the history
  • Loading branch information
import-brain authored Apr 10, 2022
1 parent 301ad54 commit c25ecd0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/phone.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Faker } from '.';

/**
* Module to generate phone numbers.
* Module to generate phone-related data.
*/
export class Phone {
constructor(private readonly faker: Faker) {
Expand Down Expand Up @@ -60,4 +60,17 @@ export class Phone {
this.faker.definitions.phone_number.formats
);
}

/**
* Generates IMEI number.
*
* @example
* faker.phone.imei() // '13-850175-913761-7'
*/
imei(): string {
return this.faker.helpers.replaceCreditCardSymbols(
'##-######-######-L',
'#'
);
}
}
34 changes: 33 additions & 1 deletion test/phone.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { faker } from '../src';
import { luhnCheck } from './support/luhnCheck';

const seededRuns = [
{
Expand All @@ -15,6 +16,9 @@ const seededRuns = [
phoneFormats: {
noArgs: '!##.!##.####',
},
imei: {
noArgs: '37-917755-141004-5',
},
},
},
{
Expand All @@ -30,6 +34,9 @@ const seededRuns = [
phoneFormats: {
noArgs: '(!##) !##-####',
},
imei: {
noArgs: '25-122540-325523-6',
},
},
},
{
Expand All @@ -45,11 +52,19 @@ const seededRuns = [
phoneFormats: {
noArgs: '1-!##-!##-#### x#####',
},
imei: {
noArgs: '94-872190-616274-6',
},
},
},
];

const functionNames = ['phoneNumber', 'phoneNumberFormat', 'phoneFormats'];
const functionNames = [
'phoneNumber',
'phoneNumberFormat',
'phoneFormats',
'imei',
];

const NON_SEEDED_BASED_RUN = 25;

Expand Down Expand Up @@ -125,6 +140,23 @@ describe('phone', () => {
expect(faker.definitions.phone_number.formats).contain(phoneFormat);
});
});

describe('imei()', () => {
it('should return a string', () => {
const imei = faker.phone.imei();
expect(imei).toBeTypeOf('string');
});

it('should have a length of 18', () => {
const imei = faker.phone.imei();
expect(imei).toHaveLength(18);
});

it('should be Luhn-valid', () => {
const imei = faker.phone.imei();
expect(imei).satisfy(luhnCheck);
});
});
}
});
});

0 comments on commit c25ecd0

Please sign in to comment.