aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEric Jeker <[email protected]>2022-01-11 19:50:25 +0800
committerDamien Retzinger <[email protected]>2022-01-14 18:37:49 -0500
commitcfdfad5d77b9b52a07c02d2fb3ca4588acdb7d5d (patch)
treed7a6dd995c37d5759aba149fa19a68f89f2b996f /src
parentb8bfb4542b847004213ddb2294e982bf666cc1a4 (diff)
downloadfaker-cfdfad5d77b9b52a07c02d2fb3ca4588acdb7d5d.tar.xz
faker-cfdfad5d77b9b52a07c02d2fb3ca4588acdb7d5d.zip
feat: add definitions (#84)
Co-authored-by: Shinigami <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/index.ts183
1 files changed, 183 insertions, 0 deletions
diff --git a/src/index.ts b/src/index.ts
index ea724d04..103746d2 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -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;
+ readonly definitions = {};
+ private readonly definitionTypes: DefinitionTypes = {
+ 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();
@@ -23,6 +157,46 @@ 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
+ */
+ private loadDefinitions(types: DefinitionTypes): void {
+ 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) {
@@ -30,6 +204,15 @@ export class Faker {
this.random = new Random(this, this.seedValue);
this.datatype = new Datatype(this, this.seedValue);
}
+
+ /**
+ * Set Faker's locale
+ *
+ * @param locale
+ */
+ setLocale(locale: string): void {
+ this.locale = locale;
+ }
}
export default Faker;