diff --git a/src/phone.ts b/src/phone.ts index 88b2a038ad0..c922e341e14 100644 --- a/src/phone.ts +++ b/src/phone.ts @@ -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) { @@ -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', + '#' + ); + } } diff --git a/test/phone.spec.ts b/test/phone.spec.ts index 0f80f038dfa..162de0311c6 100644 --- a/test/phone.spec.ts +++ b/test/phone.spec.ts @@ -1,5 +1,6 @@ import { beforeEach, describe, expect, it } from 'vitest'; import { faker } from '../src'; +import { luhnCheck } from './support/luhnCheck'; const seededRuns = [ { @@ -15,6 +16,9 @@ const seededRuns = [ phoneFormats: { noArgs: '!##.!##.####', }, + imei: { + noArgs: '37-917755-141004-5', + }, }, }, { @@ -30,6 +34,9 @@ const seededRuns = [ phoneFormats: { noArgs: '(!##) !##-####', }, + imei: { + noArgs: '25-122540-325523-6', + }, }, }, { @@ -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; @@ -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); + }); + }); } }); });