Skip to content

Commit

Permalink
Merge pull request #34 from wim07101993/phone-number
Browse files Browse the repository at this point in the history
feat(phone-number): implemented phone number support
  • Loading branch information
drager authored Dec 18, 2020
2 parents af7f834 + 443e416 commit fd8ee51
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 5 deletions.
7 changes: 5 additions & 2 deletions lib/src/faker.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:faker/src/date.dart';
import 'package:faker/src/lorem.dart';
import 'package:faker/src/phone_number.dart';
import 'package:faker/src/providers/default_providers.dart';
import 'package:faker/src/providers/fa_providers.dart';

Expand Down Expand Up @@ -31,6 +32,7 @@ class Faker {
final Job job;
final Lorem lorem;
final Person person;
final PhoneNumber phoneNumber;
final Sport sport;
final Date date;
final RandomGenerator randomGenerator;
Expand All @@ -45,8 +47,10 @@ class Faker {
image = const Image(),
internet = const Internet(),
job = const Job(),
lorem = Lorem(provider?.loremDataProvider ?? DefaultLoremDataProvider()),
lorem =
Lorem(provider?.loremDataProvider ?? DefaultLoremDataProvider()),
person = const Person(),
phoneNumber = const PhoneNumber(),
sport = const Sport(),
date = const Date(),
randomGenerator = const RandomGenerator();
Expand All @@ -61,4 +65,3 @@ class FakerDataProvider {
class FakerDataProviderFa extends FakerDataProvider {
FakerDataProviderFa() : super(loremDataProvider: LoremDataProviderFa());
}

43 changes: 43 additions & 0 deletions lib/src/phone_number.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:faker/faker.dart';

class PhoneNumber {
static const usPhoneNumberPatterns = [
// Standard 10-digit phone number formats
'##########',
'##########',
'###-###-####',
'###-###-####',
// Optional 10-digit local phone number format
'(###)###-####',
'(###)###-####',
// Non-standard 10-digit phone number format
'###.###.####',
'###.###.####',
// Standard 10-digit phone number format with extensions
'###-###-####x###',
'###-###-####x####',
'###-###-####x#####',
// Optional 10-digit local phone number format with extensions
'(###)###-####x###',
'(###)###-####x####',
'(###)###-####x#####',
// Non-standard 10-digit phone number format with extensions
'###.###.####x###',
'###.###.####x####',
'###.###.####x#####',
// Standard 11-digit phone number format
'+1-###-###-####',
'001-###-###-####',
// Standard 11-digit phone number format with extensions
'+1-###-###-####x###',
'+1-###-###-####x####',
'+1-###-###-####x#####',
'001-###-###-####x###',
'001-###-###-####x####',
'001-###-###-####x#####',
];

const PhoneNumber();

String us() => random.fromPattern(usPhoneNumberPatterns);
}
8 changes: 5 additions & 3 deletions test/runner.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import 'specs/address.dart' as address;
import 'specs/conference.dart' as conference;
import 'specs/company.dart' as company;
import 'specs/conference.dart' as conference;
import 'specs/currency.dart' as currency;
import 'specs/date.dart' as date;
import 'specs/food.dart' as food;
import 'specs/guid.dart' as guid;
import 'specs/image.dart' as image;
import 'specs/internet.dart' as internet;
import 'specs/job.dart' as job;
import 'specs/lorem.dart' as lorem;
import 'specs/person.dart' as person;
import 'specs/sport.dart' as sport;
import 'specs/date.dart' as date;
import 'specs/phone_number.dart' as phoneNumber;
import 'specs/random_generator.dart' as random;
import 'specs/sport.dart' as sport;

void main() {
address.main();
Expand All @@ -25,6 +26,7 @@ void main() {
job.main();
lorem.main();
person.main();
phoneNumber.main();
sport.main();
date.main();
random.main();
Expand Down
49 changes: 49 additions & 0 deletions test/specs/phone_number.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:faker/faker.dart';
import 'package:test/test.dart';

void main() {
var faker = Faker();

group('us', () {
test('should be able to generate us phone number', () {
for (var i = 0; i < 20; i++) {
expect(
faker.phoneNumber.us(),
anyOf(
[
// Standard 10-digit phone number formats
matches(r'\d{10}'),
matches(r'\d{3}-\d{3}-\d{3}'),
// Optional 10-digit local phone number format
matches(r'\(\d{3}\)\d{3}-\d{3}'),
// Non-standard 10-digit phone number format
matches(r'\d{3}.\d{3}.\d{4}'),
// Standard 10-digit phone number format with extensions
matches(r'\d{3}-\d{3}-\d{4}x\d{3}'),
matches(r'\d{3}-\d{3}-\d{4}x\d{4}'),
matches(r'\d{3}-\d{3}-\d{4}x\d{5}'),
// Optional 10-digit local phone number format with extensions
matches(r'\(\d{3}\)\d{3}-\d{4}x\d{3}'),
matches(r'\(\d{3}\)\d{3}-\d{4}x\d{4}'),
matches(r'\(\d{3}\)\d{3}-\d{4}x\d{5}'),
// Non-standard 10-digit phone number format with extensions
matches(r'\d{3}.\d{3}.\d{4}x\d{3}'),
matches(r'\d{3}.\d{3}.\d{4}x\d{4}'),
matches(r'\d{3}.\d{3}.\d{4}x\d{5}'),
// Standard 11-digit phone number format
matches(r'\+1-\d{3}-\d{3}-\d{4}'),
matches(r'001-\d{3}-\d{3}-\d{4}'),
// Standard 11-digit phone number format with extensions
matches(r'\+1-\d{3}-\d{3}-\d{4}x\d{3}'),
matches(r'\+1-\d{3}-\d{3}-\d{4}x\d{4}'),
matches(r'\+1-\d{3}-\d{3}-\d{4}x\d{5}'),
matches(r'001-\d{3}-\d{3}-\d{4}x\d{3}'),
matches(r'001-\d{3}-\d{3}-\d{4}x\d{4}'),
matches(r'001-\d{3}-\d{3}-\d{4}x\d{5}'),
],
),
);
}
});
});
}

0 comments on commit fd8ee51

Please sign in to comment.