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 autocomplete support for locales #248

Merged
merged 2 commits into from
Jan 23, 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
25 changes: 14 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Hacker } from './hacker';
import { Helpers } from './helpers';
import { Image } from './image';
import { Internet } from './internet';
import type { KnownLocale } from './locales';
import allLocales from './locales';
import { Lorem } from './lorem';
import { Mersenne } from './mersenne';
Expand All @@ -25,6 +26,9 @@ import { Unique } from './unique';
import { Vehicle } from './vehicle';
import { Word } from './word';

// https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609
type LiteralUnion<T extends U, U = string> = T | (U & { zz_IGNORE_ME?: never });

export interface LocaleDefinition {
title: string;
separator?: string;
Expand Down Expand Up @@ -186,12 +190,13 @@ export interface LocaleDefinition {
[group: string]: any;
}

export type UsableLocale = LiteralUnion<KnownLocale>;
export type UsedLocales = Partial<Record<UsableLocale, LocaleDefinition>>;

export interface FakerOptions {
locales?: {
[locale: string]: LocaleDefinition;
};
locale?: string;
localeFallback?: string;
locales?: UsedLocales;
locale?: UsableLocale;
localeFallback?: UsableLocale;
}

export interface DefinitionTypes {
Expand All @@ -216,11 +221,9 @@ export interface DefinitionTypes {
}

export class Faker {
locales: {
[locale: string]: LocaleDefinition;
};
locale: string;
localeFallback: string;
locales: UsedLocales;
locale: UsableLocale;
localeFallback: UsableLocale;

// TODO @Shinigami92 2022-01-11: For now we loose types here
// @ts-expect-error: will be lazy filled by constructor
Expand Down Expand Up @@ -432,7 +435,7 @@ export class Faker {
*
* @param locale
*/
setLocale(locale: string): void {
setLocale(locale: UsableLocale): void {
this.locale = locale;
}
}
Expand Down
62 changes: 61 additions & 1 deletion src/locales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,67 @@ import zh_CN from './zh_CN';
import zh_TW from './zh_TW';
import zu_ZA from './zu_ZA';

const locales: { [lang: string]: LocaleDefinition } = {
export type KnownLocale =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you use keyof typeof locales, so we don't have to repeat the same thing again?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wont this result in a circular type?
One of them needs to be defined first, or not?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, first create the locales, and then below it, create the KnownLocale type. There is no circular reference if they stay in the same file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this I had in mind:

const locales = {
  af_ZA,
  ar,
  ...
};

export type KnownLocale = keyof typeof locales

export default locales;

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please create an issue (or PR) for that?
I currently work on something different and don't want to forget about it.

| 'af_ZA'
| 'ar'
| 'az'
| 'cz'
| 'de'
| 'de_AT'
| 'de_CH'
| 'el'
| 'en'
| 'en_AU'
| 'en_AU_ocker'
| 'en_BORK'
| 'en_CA'
| 'en_GB'
| 'en_GH'
| 'en_IE'
| 'en_IND'
| 'en_NG'
| 'en_US'
| 'en_ZA'
| 'es'
| 'es_MX'
| 'fa'
| 'fi'
| 'fr'
| 'fr_BE'
| 'fr_CA'
| 'fr_CH'
| 'ge'
| 'he'
| 'hr'
| 'hy'
| 'id_ID'
| 'it'
| 'ja'
| 'ko'
| 'lv'
| 'mk'
| 'nb_NO'
| 'ne'
| 'nl'
| 'nl_BE'
| 'pl'
| 'pt_BR'
| 'pt_PT'
| 'ro'
| 'ru'
| 'sk'
| 'sv'
| 'tr'
| 'uk'
| 'ur'
| 'vi'
| 'zh_CN'
| 'zh_TW'
| 'zu_ZA';

export type KnownLocales = Record<KnownLocale, LocaleDefinition>;

const locales: KnownLocales = {
af_ZA,
ar,
az,
Expand Down