Skip to content

Commit

Permalink
feat: add autocomplete support for locales (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Jan 23, 2022
1 parent 6ba72a4 commit c1f2b09
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
25 changes: 14 additions & 11 deletions src/index.ts
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
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 =
| '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

0 comments on commit c1f2b09

Please sign in to comment.