Skip to content

Commit

Permalink
Merge pull request #32 from daemon3000/random-seed
Browse files Browse the repository at this point in the history
Seed support for random generator
  • Loading branch information
drager authored Jan 10, 2021
2 parents 63ed4c3 + ae09db9 commit 91b1f0e
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 58 deletions.
5 changes: 4 additions & 1 deletion example/faker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ library faker.example;
import 'package:faker/faker.dart';

void main() {
// final random = RandomGenerator(seed: 63833423);
// final faker = Faker.withGenerator(random);

// Generate some different types of data.
print(faker.address.country());
print(faker.company.name());
Expand All @@ -28,7 +31,7 @@ void main() {
print(faker.internet.userAgent(osName: 'iOS'));

// Generate localized Farsi lorem text
var fakerFa = Faker(FakerDataProviderFa());
final fakerFa = Faker(provider: FakerDataProviderFa());
print(fakerFa.lorem.word());
print(fakerFa.lorem.sentence());
print(fakerFa.lorem.words(10).join(' '));
Expand Down
22 changes: 13 additions & 9 deletions lib/src/address.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import 'data/address/countries.dart';
import 'data/address/country_codes.dart';
import 'data/address/neighborhoods.dart';
import 'data/address/street_suffixes.dart';
import 'faker.dart';
import 'person.dart';
import 'random_generator.dart';

class Address {
static const compassDirections = ['North', 'East', 'West', 'South'];
static const cityPrefixes = ['New', 'Lake', 'Port'];

const Address();
const Address(this.person);

final Person person;

RandomGenerator get random => person.random;

/// Generates a zip code.
///
Expand All @@ -30,15 +34,15 @@ class Address {
String city() {
switch (random.integer(4)) {
case 0:
return '${cityPrefix()} ${faker.person.firstName()}${citySuffix()}';
return '${cityPrefix()} ${person.firstName()}${citySuffix()}';
case 1:
return '${cityPrefix()} ${faker.person.firstName()}';
return '${cityPrefix()} ${person.firstName()}';
case 2:
return '${faker.person.firstName()}${citySuffix()}';
return '${person.firstName()}${citySuffix()}';
case 3:
return '${faker.person.lastName()}${citySuffix()}';
return '${person.lastName()}${citySuffix()}';
default:
return '${faker.person.lastName()}${citySuffix()}';
return '${person.lastName()}${citySuffix()}';
}
}

Expand Down Expand Up @@ -68,8 +72,8 @@ class Address {
/// faker.address.streetName();
/// ```
String streetName() => random.integer(2) == 0
? '${Faker().person.lastName()} ${streetSuffix()}'
: '${Faker().person.firstName()} ${streetSuffix()}';
? '${person.lastName()} ${streetSuffix()}'
: '${person.firstName()} ${streetSuffix()}';

/// Generates a street address.
///
Expand Down
18 changes: 11 additions & 7 deletions lib/src/company.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'faker.dart';
import 'person.dart';
import 'random_generator.dart';

class Company {
Expand Down Expand Up @@ -30,7 +30,11 @@ class Company {
'Division'
];

const Company();
const Company(this.person);

final Person person;

RandomGenerator get random => person.random;

/// Generates a company name.
///
Expand All @@ -41,14 +45,14 @@ class Company {
String name() {
switch (random.integer(3)) {
case 0:
return '${faker.person.lastName()} ${suffix()}';
return '${person.lastName()} ${suffix()}';
case 1:
return '${faker.person.lastName()}-${faker.person.lastName()}';
return '${person.lastName()}-${person.lastName()}';
case 2:
return '${faker.person.lastName()}, ${faker.person.lastName()} '
'and ${faker.person.lastName()}';
return '${person.lastName()}, ${person.lastName()} '
'and ${person.lastName()}';
default:
return '${faker.person.lastName()} ${suffix()}';
return '${person.lastName()} ${suffix()}';
}
}

Expand Down
4 changes: 3 additions & 1 deletion lib/src/conference.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'data/conference/conference_names.dart';
import 'random_generator.dart';

class Conference {
const Conference();
const Conference(this.random);

final RandomGenerator random;

/// Generates a conference name.
///
Expand Down
4 changes: 3 additions & 1 deletion lib/src/currency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'data/currency/currency_names.dart';
import 'random_generator.dart';

class Currency {
const Currency();
const Currency(this.random);

final RandomGenerator random;

/// Generates a currency code.
///
Expand Down
4 changes: 3 additions & 1 deletion lib/src/date.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'data/date_time/time_zones.dart';
import 'random_generator.dart';

class Date {
const Date();
const Date(this.random);

final RandomGenerator random;

static final _yearSuffixes = [
'BC',
Expand Down
37 changes: 19 additions & 18 deletions lib/src/faker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import 'providers/base_providers.dart';
import 'random_generator.dart';
import 'sport.dart';

final Faker faker = Faker();
final Faker faker = Faker.withGenerator(random);

class Faker {
final Address address;
Expand All @@ -37,23 +37,24 @@ class Faker {
final Date date;
final RandomGenerator randomGenerator;

Faker([FakerDataProvider provider])
: address = const Address(),
conference = const Conference(),
company = const Company(),
currency = const Currency(),
food = const Food(),
guid = const Guid(),
image = const Image(),
internet = const Internet(),
job = const Job(),
lorem =
Lorem(provider?.loremDataProvider ?? DefaultLoremDataProvider()),
person = const Person(),
phoneNumber = const PhoneNumber(),
sport = const Sport(),
date = const Date(),
randomGenerator = const RandomGenerator();
Faker.withGenerator(RandomGenerator random, {FakerDataProvider provider})
: address = Address(Person(random)),
conference = Conference(random),
company = Company(Person(random)),
currency = Currency(random),
food = Food(random),
guid = Guid(random),
image = Image(),
internet = Internet(random),
job = Job(random),
lorem = Lorem(random, provider?.loremDataProvider ?? DefaultLoremDataProvider()),
person = Person(random),
sport = Sport(random),
date = Date(random),
phoneNumber = PhoneNumber(random),
randomGenerator = random;

factory Faker({int seed, FakerDataProvider provider}) => Faker.withGenerator(RandomGenerator(seed: seed), provider: provider);
}

class FakerDataProvider {
Expand Down
4 changes: 3 additions & 1 deletion lib/src/food.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import 'data/food/restaurants.dart';
import 'random_generator.dart';

class Food {
const Food();
const Food(this.random);

final RandomGenerator random;

/// Generates a restaurant.
///
Expand Down
4 changes: 3 additions & 1 deletion lib/src/guid.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'random_generator.dart';

class Guid {
const Guid();
const Guid(this.random);

final RandomGenerator random;

String guid() =>
random.fromPatternToHex(['########-####-####-####-############']);
Expand Down
5 changes: 3 additions & 2 deletions lib/src/internet.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import '../faker.dart';
import 'data/person/firstnames.dart';
import 'data/user_agent/user_agent.dart';
import 'data/user_agent/user_agent_data.dart';
Expand All @@ -24,7 +23,9 @@ class Internet {
'safetymail.info'
];

const Internet();
const Internet(this.random);

final RandomGenerator random;

/// Generates an email from the [userName] and [domainName] methods.
///
Expand Down
4 changes: 3 additions & 1 deletion lib/src/job.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import 'data/job/job_prefix.dart';
import 'random_generator.dart';

class Job {
const Job();
const Job(this.random);

final RandomGenerator random;

/// Generates a job title.
///
Expand Down
5 changes: 3 additions & 2 deletions lib/src/lorem.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import 'package:faker/src/providers/base_providers.dart';
import 'random_generator.dart';

class Lorem {
final LoremDataProvider _provider;
const Lorem(this.random, this._provider);

const Lorem(this._provider);
final RandomGenerator random;
final LoremDataProvider _provider;

/// Generates a word.
///
Expand Down
4 changes: 3 additions & 1 deletion lib/src/person.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class Person {
'DVM'
];

const Person();
const Person(this.random);

final RandomGenerator random;

/// Generates a name.
///
Expand Down
6 changes: 4 additions & 2 deletions lib/src/phone_number.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:faker/faker.dart';
import 'random_generator.dart';

class PhoneNumber {
static const usPhoneNumberPatterns = [
Expand Down Expand Up @@ -37,7 +37,9 @@ class PhoneNumber {
'001-###-###-####x#####',
];

const PhoneNumber();
const PhoneNumber(this.random);

final RandomGenerator random;

String us() => random.fromPattern(usPhoneNumberPatterns);
}
19 changes: 10 additions & 9 deletions lib/src/random_generator.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import 'dart:math';

var _rng = Random();

const random = RandomGenerator();
final random = RandomGenerator();

class RandomGenerator {
const RandomGenerator();
RandomGenerator({int seed}):
_random = Random(seed);

final Random _random;

/// Plucks a random element from the given [list].
T element<T>(List<T> list) {
return list[_rng.nextInt(list.length)];
return list[_random.nextInt(list.length)];
}

/// Generates a random number from the [max] value
Expand All @@ -20,7 +21,7 @@ class RandomGenerator {
var number = <int>[];

for (var i = 0; i < times; i++) {
number.add(_rng.nextInt(max));
number.add(_random.nextInt(max));
}
return number;
}
Expand All @@ -35,15 +36,15 @@ class RandomGenerator {
/// random.integer(10, min: 5);
/// ```
int integer(int max, {int min = 0}) =>
max == min ? max : _rng.nextInt(max - min) + min;
max == min ? max : _random.nextInt(max - min) + min;

/// Generates a random boolean.
bool boolean() => _rng.nextBool();
bool boolean() => _random.nextBool();

/// Generates a random decimal.
/// Accepts a [scale] and a [min] value.
double decimal({num scale = 1, num min = 0}) =>
_rng.nextDouble() * scale + min;
_random.nextDouble() * scale + min;

/// Generates a random string with the given [max] value
/// and to the lowest [min] value if provided.
Expand Down
4 changes: 3 additions & 1 deletion lib/src/sport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import 'data/sport/sport_names.dart';
import 'random_generator.dart';

class Sport {
const Sport();
const Sport(this.random);

final RandomGenerator random;

/// Generates a sport name.
///
Expand Down
2 changes: 2 additions & 0 deletions test/runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'specs/lorem.dart' as lorem;
import 'specs/person.dart' as person;
import 'specs/phone_number.dart' as phoneNumber;
import 'specs/random_generator.dart' as random;
import 'specs/seed.dart' as seed;
import 'specs/sport.dart' as sport;

void main() {
Expand All @@ -30,4 +31,5 @@ void main() {
sport.main();
date.main();
random.main();
seed.main();
}
Loading

0 comments on commit 91b1f0e

Please sign in to comment.