Skip to content

Commit

Permalink
Merge branch 'next' into docs/module-guides
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Mar 29, 2023
2 parents 6777928 + 88e561a commit 9d902b6
Show file tree
Hide file tree
Showing 106 changed files with 418 additions and 139 deletions.
1 change: 0 additions & 1 deletion .editorconfig
Expand Up @@ -9,5 +9,4 @@ trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
max_line_length = off
trim_trailing_whitespace = false
13 changes: 11 additions & 2 deletions docs/guide/localization.md
Expand Up @@ -44,7 +44,7 @@ If our built-in faker instances don't satisfy your needs, you can build your own

```ts
import type { LocaleDefinition } from '@faker-js/faker';
import { Faker, de_CH, de, en } from '@faker-js/faker';
import { Faker, de_CH, de, en, base } from '@faker-js/faker';

const customLocale: LocaleDefinition = {
title: 'My custom locale',
Expand All @@ -54,10 +54,18 @@ const customLocale: LocaleDefinition = {
};

export const customFaker = new Faker({
locale: [customLocale, de_CH, de, en, global],
locale: [customLocale, de_CH, de, en, base],
});
```

In this example there are 5 locales. Each of these is checked in order, and the first locale which contains the requested data will be used:

- `customLocale` is your custom locale definition which will override all other fallback definitions.
- `de_CH` is a specific locale definition that overrides some German definitions with `CH` (Switzerland) data.
- `de` is a generic `de` (German) locale definition.
- `en` is a generic `en` (English) locale definition. This is our most complete locale, so we add it to fill some gaps. Depending on your needs, you might want or not want to have it as a fallback.
- `base` is the base locale definition which contains definitions that can be used in every language (e.g. emojis).

## Available locales

<!-- LOCALES-AUTO-GENERATED-START -->
Expand All @@ -69,6 +77,7 @@ export const customFaker = new Faker({
| `af_ZA` | Afrikaans | `fakerAF_ZA` |
| `ar` | Arabic | `fakerAR` |
| `az` | Azerbaijani | `fakerAZ` |
| `base` | Base | `fakerBASE` |
| `cz` | Czech | `fakerCZ` |
| `de` | German | `fakerDE` |
| `de_AT` | German (Austria) | `fakerDE_AT` |
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/upgrading.md
Expand Up @@ -60,12 +60,12 @@ const b = customFaker.internet.emoji();
**New**

```ts
import { Faker, de_CH, de, en, global } from '@faker-js/faker';
import { Faker, de_CH, de, en, base } from '@faker-js/faker';

// same as fakerDE_CH
export const customFaker = new Faker({
// Now multiple fallbacks are supported
locale: [de_CH, de, en, global],
locale: [de_CH, de, en, base],
});
const a = customFaker.internet.email();
const b = customFaker.internet.emoji();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -112,7 +112,7 @@
"glob": "~9.3.2",
"npm-run-all": "~4.1.5",
"picocolors": "~1.0.0",
"prettier": "2.8.4",
"prettier": "2.8.7",
"prettier-plugin-organize-imports": "~3.2.2",
"react": "~18.2.0",
"react-dom": "~18.2.0",
Expand Down
20 changes: 10 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion scripts/apidoc/signature.ts
Expand Up @@ -308,7 +308,17 @@ function typeToText(type_?: Type, short = false): string {

case 'reference':
if (!type.typeArguments || !type.typeArguments.length) {
return type.name;
const reflection = type.reflection as DeclarationReflection | undefined;
const reflectionType = reflection?.type;
if (
(reflectionType?.type === 'literal' ||
reflectionType?.type === 'union') &&
!type.name.match(/Char$/)
) {
return typeToText(reflectionType, short);
} else {
return type.name;
}
} else if (type.name === 'LiteralUnion') {
return [
typeToText(type.typeArguments[0], short),
Expand Down
20 changes: 14 additions & 6 deletions scripts/generateLocales.ts
Expand Up @@ -125,10 +125,15 @@ function generateLocaleFile(locale: string): void {
}
}

if (locales[locales.length - 1] !== 'en') {
// TODO christopher 2023-03-07: Remove 'en' fallback in a separate PR
if (locales[locales.length - 1] !== 'en' && locale !== 'base') {
locales.push('en');
}

if (locales[locales.length - 1] !== 'base') {
locales.push('base');
}

let content = `
${autoGeneratedCommentHeader}
Expand All @@ -148,8 +153,10 @@ function generateLocaleFile(locale: string): void {
writeFileSync(resolve(pathLocale, `${locale}.ts`), content);
}

function tryLoadLocalesMainIndexFile(pathModules: string): LocaleDefinition {
let localeDef: LocaleDefinition;
function tryLoadLocalesMainIndexFile(
pathModules: string
): LocaleDefinition | undefined {
let localeDef: LocaleDefinition | undefined;
// This call might fail, if the module setup is broken.
// Unfortunately, we try to fix it with this script
// Thats why have a fallback logic here, we only need the title anyway
Expand All @@ -165,9 +172,10 @@ function tryLoadLocalesMainIndexFile(pathModules: string): LocaleDefinition {
resolve(pathModules, 'index.ts'),
'utf-8'
);
localeDef = {
title: localeIndex.match(/title: '(.*)',/)[1],
};
const title = localeIndex.match(/title: '(.*)',/)?.[1];
if (title) {
localeDef = { title };
}
} catch {
console.error(`Failed to load ${pathModules} or manually parse it.`, e);
}
Expand Down
6 changes: 3 additions & 3 deletions src/definitions/location.ts
Expand Up @@ -45,17 +45,17 @@ export type LocationDefinitions = LocaleEntry<{
country_code: Array<{ alpha2: string; alpha3: string }>;

/**
* The names of this country's states.
* The names of this country's states, or other first-level administrative areas.
*/
state: string[];

/**
* The abbreviated names of this country's states.
* The abbreviated names of this country's states, or other first-level administrative areas.
*/
state_abbr: string[];

/**
* The names of counties inside the country or state.
* The names of counties, or other second-level administrative areas, inside the country's states.
*/
county: string[];

Expand Down
3 changes: 2 additions & 1 deletion src/locale/af_ZA.ts
Expand Up @@ -5,8 +5,9 @@

import { Faker } from '../faker';
import af_ZA from '../locales/af_ZA';
import base from '../locales/base';
import en from '../locales/en';

export const faker = new Faker({
locale: [af_ZA, en],
locale: [af_ZA, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/ar.ts
Expand Up @@ -5,8 +5,9 @@

import { Faker } from '../faker';
import ar from '../locales/ar';
import base from '../locales/base';
import en from '../locales/en';

export const faker = new Faker({
locale: [ar, en],
locale: [ar, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/az.ts
Expand Up @@ -5,8 +5,9 @@

import { Faker } from '../faker';
import az from '../locales/az';
import base from '../locales/base';
import en from '../locales/en';

export const faker = new Faker({
locale: [az, en],
locale: [az, en, base],
});
11 changes: 11 additions & 0 deletions src/locale/base.ts
@@ -0,0 +1,11 @@
/*
* This file is automatically generated.
* Run 'pnpm run generate:locales' to update.
*/

import { Faker } from '../faker';
import base from '../locales/base';

export const faker = new Faker({
locale: base,
});
3 changes: 2 additions & 1 deletion src/locale/cz.ts
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import cz from '../locales/cz';
import en from '../locales/en';

export const faker = new Faker({
locale: [cz, en],
locale: [cz, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/de.ts
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import de from '../locales/de';
import en from '../locales/en';

export const faker = new Faker({
locale: [de, en],
locale: [de, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/de_AT.ts
Expand Up @@ -4,10 +4,11 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import de from '../locales/de';
import de_AT from '../locales/de_AT';
import en from '../locales/en';

export const faker = new Faker({
locale: [de_AT, de, en],
locale: [de_AT, de, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/de_CH.ts
Expand Up @@ -4,10 +4,11 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import de from '../locales/de';
import de_CH from '../locales/de_CH';
import en from '../locales/en';

export const faker = new Faker({
locale: [de_CH, de, en],
locale: [de_CH, de, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/dv.ts
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import dv from '../locales/dv';
import en from '../locales/en';

export const faker = new Faker({
locale: [dv, en],
locale: [dv, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/el.ts
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import el from '../locales/el';
import en from '../locales/en';

export const faker = new Faker({
locale: [el, en],
locale: [el, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en.ts
Expand Up @@ -4,8 +4,9 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';

export const faker = new Faker({
locale: en,
locale: [en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_AU.ts
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_AU from '../locales/en_AU';

export const faker = new Faker({
locale: [en_AU, en],
locale: [en_AU, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_AU_ocker.ts
Expand Up @@ -4,10 +4,11 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_AU from '../locales/en_AU';
import en_AU_ocker from '../locales/en_AU_ocker';

export const faker = new Faker({
locale: [en_AU_ocker, en_AU, en],
locale: [en_AU_ocker, en_AU, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_BORK.ts
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_BORK from '../locales/en_BORK';

export const faker = new Faker({
locale: [en_BORK, en],
locale: [en_BORK, en, base],
});
3 changes: 2 additions & 1 deletion src/locale/en_CA.ts
Expand Up @@ -4,9 +4,10 @@
*/

import { Faker } from '../faker';
import base from '../locales/base';
import en from '../locales/en';
import en_CA from '../locales/en_CA';

export const faker = new Faker({
locale: [en_CA, en],
locale: [en_CA, en, base],
});

0 comments on commit 9d902b6

Please sign in to comment.