Skip to content

chore: add types for definitions #355

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

Merged
merged 3 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
export interface Definitions {
address: {
city_name;
city_prefix;
city_suffix;
country_code_alpha_3;
country_code;
country;
county;
direction_abbr;
direction;
postcode_by_state;
postcode: string | string[];
state_abbr;
state;
street_prefix;
street_suffix;
time_zone;
};
animal: {
bear;
bird;
cat;
cetacean;
cow;
crocodilia;
dog;
fish;
horse;
insect;
lion;
rabbit;
snake;
type;
};
commerce: {
color;
department;
product_description;
product_name;
};
company: {
adjective;
bs_adjective;
bs_noun;
bs_verb;
descriptor;
noun;
suffix;
};
database: {
collation;
column;
engine;
type;
};
date: {
month;
weekday;
};
finance: {
account_type;
credit_card;
currency: Record<string, { code: string; symbol: string }>;
transaction_type;
};
hacker: {
abbreviation;
adjective;
ingverb;
noun;
phrase;
verb;
};
internet: {
domain_suffix;
example_email;
free_email;
};
lorem: {
words;
};
music: {
genre;
};
name: {
binary_gender;
female_first_name;
female_last_name;
female_middle_name;
female_prefix;
first_name;
gender;
last_name;
male_first_name;
male_last_name;
male_middle_name;
male_prefix;
middle_name;
prefix;
suffix;
title: {
descriptor;
level;
job;
};
};
phone_number: {
formats;
};
system: {
directoryPaths;
mimeTypes;
};
vehicle: {
bicycle_type;
fuel;
manufacturer;
model;
type;
};
word: {
adjective: string[];
adverb: string[];
conjunction: string[];
interjection: string[];
noun: string[];
preposition: string[];
verb: string[];
};
}
14 changes: 4 additions & 10 deletions src/finance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ export class Finance {
* @method faker.finance.currencyCode
*/
currencyCode(): string {
// TODO @Shinigami92 2022-01-14: missing second parameter
// @ts-expect-error
return this.faker.random.objectElement(
this.faker.definitions.finance.currency
)['code'];
Expand All @@ -179,15 +177,11 @@ export class Finance {
* @method faker.finance.currencySymbol
*/
currencySymbol(): string {
let symbol;

let symbol: string;
while (!symbol) {
symbol =
// TODO @Shinigami92 2022-01-14: missing second parameter
// @ts-expect-error
this.faker.random.objectElement(
this.faker.definitions.finance.currency
)['symbol'];
symbol = this.faker.random.objectElement(
this.faker.definitions.finance.currency
)['symbol'];
}
return symbol;
}
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Company } from './company';
import { Database } from './database';
import { Datatype } from './datatype';
import { _Date } from './date';
import type { Definitions } from './definitions';
import { Fake } from './fake';
import { Finance } from './finance';
import { Git } from './git';
Expand Down Expand Up @@ -193,6 +194,8 @@ export interface LocaleDefinition {
export type UsableLocale = LiteralUnion<KnownLocale>;
export type UsedLocales = Partial<Record<UsableLocale, LocaleDefinition>>;

export type { Definitions };

export interface FakerOptions {
locales?: UsedLocales;
locale?: UsableLocale;
Expand Down Expand Up @@ -225,9 +228,8 @@ export class Faker {
locale: UsableLocale;
localeFallback: UsableLocale;

// TODO @Shinigami92 2022-01-11: For now we loose types here
// @ts-expect-error: will be lazy filled by constructor
readonly definitions: Record<keyof DefinitionTypes, any> = {};
readonly definitions: Definitions = {};
private readonly definitionTypes: DefinitionTypes = {
name: [
'first_name',
Expand Down
7 changes: 2 additions & 5 deletions src/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,15 @@ export class Random {
return arrayCopy.slice(min);
}

// TODO @Shinigami92 2022-01-28: This function needs types
/**
* Takes an object and returns a random key or value.
*
* @method faker.random.objectElement
* @param object
* @param field
*/
// TODO @Shinigami92 2022-01-11: Not sure if these generic types are correct
objectElement<T extends any, Key extends keyof T>(
object: T = { foo: 'bar', too: 'car' } as unknown as T,
field: Key
): T[Key] {
objectElement(object: any = { foo: 'bar', too: 'car' }, field?: string) {
const array = Object.keys(object);
const key = this.faker.random.arrayElement(array);

Expand Down