Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add definitions #84

Merged
merged 5 commits into from
Jan 11, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
183 changes: 183 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,145 @@ export interface FakerOptions {
localeFallback?: string;
}

export interface DefinitionTypes {
name: string[];
address: string[];
animal: string[];
company: string[];
lorem: string[];
hacker: string[];
phone_number: string[];
finance: string[];
internet: string[];
commerce: string[];
database: string[];
system: string[];
date: string[];
vehicle: string[];
music: string[];
word: string[];
title: string | string[];
separator: string | string[];
}

export class Faker {
locales: string[] | {};
locale: string;
localeFallback: string;

definitions = {};
definitionTypes: DefinitionTypes = {
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
name: [
'first_name',
'last_name',
'prefix',
'suffix',
'binary_gender',
'gender',
'title',
'male_prefix',
'female_prefix',
'male_first_name',
'female_first_name',
'male_middle_name',
'female_middle_name',
'male_last_name',
'female_last_name',
],
address: [
'city_name',
'city_prefix',
'city_suffix',
'street_suffix',
'county',
'country',
'country_code',
'country_code_alpha_3',
'state',
'state_abbr',
'street_prefix',
'postcode',
'postcode_by_state',
'direction',
'direction_abbr',
'time_zone',
],
animal: [
'dog',
'cat',
'snake',
'bear',
'lion',
'cetacean',
'insect',
'crocodilia',
'cow',
'bird',
'fish',
'rabbit',
'horse',
'type',
],
company: [
'adjective',
'noun',
'descriptor',
'bs_adjective',
'bs_noun',
'bs_verb',
'suffix',
],
lorem: ['words'],
hacker: ['abbreviation', 'adjective', 'noun', 'verb', 'ingverb', 'phrase'],
phone_number: ['formats'],
finance: [
'account_type',
'transaction_type',
'currency',
'iban',
'credit_card',
],
internet: [
'avatar_uri',
'domain_suffix',
'free_email',
'example_email',
'password',
],
commerce: [
'color',
'department',
'product_name',
'price',
'categories',
'product_description',
],
database: ['collation', 'column', 'engine', 'type'],
system: ['mimeTypes', 'directoryPaths'],
date: ['month', 'weekday'],
vehicle: [
'vehicle',
'manufacturer',
'model',
'type',
'fuel',
'vin',
'color',
],
music: ['genre'],
word: [
'adjective',
'adverb',
'conjunction',
'interjection',
'noun',
'preposition',
'verb',
],
title: '',
separator: '',
};

seedValue?: any[] | any;

readonly mersenne: Mersenne = new Mersenne();
Expand All @@ -23,13 +157,62 @@ export class Faker {
this.locales = this.locales || opts.locales || {};
this.locale = this.locale || opts.locale || 'en';
this.localeFallback = this.localeFallback || opts.localeFallback || 'en';

this.loadDefinitions(this.definitionTypes);
}

/**
* Load the definitions contained in the locales file for the given types
*
* @param types
*/
loadDefinitions(types: DefinitionTypes) {
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
Object.keys(types).forEach((t: string) => {
if (typeof this.definitions[t] === 'undefined') {
this.definitions[t] = {};
}

if (typeof types[t] === 'string') {
this.definitions[t] = types[t];
return;
}

types[t].forEach((p) => {
Object.defineProperty(this.definitions[t], p, {
get: () => {
if (
typeof this.locales[this.locale][t] === 'undefined' ||
typeof this.locales[this.locale][t][p] === 'undefined'
) {
// certain localization sets contain less data then others.
// in the case of a missing definition, use the default localeFallback
// to substitute the missing set data
// throw new Error('unknown property ' + d + p)
return this.locales[this.localeFallback][t][p];
} else {
// return localized data
return this.locales[this.locale][t][p];
}
},
});
});
});
}

seed(value?: any[] | any) {
this.seedValue = value;
this.random = new Random(this, this.seedValue);
this.datatype = new Datatype(this, this.seedValue);
}

/**
* Set Faker's locale
*
* @param locale
*/
setLocale(locale: string) {
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
this.locale = locale;
}
}

export default Faker;
Expand Down