Skip to content

Commit

Permalink
Merge branch 'next' into infra/unicorn/prefer-module
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Nov 14, 2023
2 parents 96dbc1f + 7e3c92e commit 72ea9ef
Show file tree
Hide file tree
Showing 31 changed files with 300 additions and 131 deletions.
2 changes: 1 addition & 1 deletion cypress/e2e/api.cy.ts
Expand Up @@ -34,7 +34,7 @@ describe('API Test', () => {
cy.get('.api-group li').each(($el) => {
const anchor = $el.find('a');
const text = anchor.text();
const link = anchor.attr('href').split('#')[0];
const link = anchor.attr('href')?.split('#')[0] ?? 'MISSING';
if (checked.has(link)) {
return;
}
Expand Down
13 changes: 8 additions & 5 deletions docs/.vitepress/config.ts
Expand Up @@ -121,11 +121,14 @@ const config = defineConfig({
{ icon: 'github', link: 'https://github.com/faker-js/faker' },
],

algolia: {
apiKey: process.env.API_KEY,
appId: process.env.APP_ID,
indexName: 'fakerjs',
},
algolia:
process.env.API_KEY == null || process.env.APP_ID == null
? undefined
: {
apiKey: process.env.API_KEY,
appId: process.env.APP_ID,
indexName: 'fakerjs',
},

footer: {
message: 'Released under the MIT License.',
Expand Down
21 changes: 13 additions & 8 deletions docs/.vitepress/versions.ts
Expand Up @@ -14,18 +14,23 @@ function readOtherLatestReleaseTagNames(): string[] {
.toString('utf8')
.split('\n')
.filter((tag) => semver.valid(tag))
.reduce<Record<number, string[]>>((acc, tag) => {
const majorVersion = semver.major(tag);
.filter((tag) => {
// Only consider tags for our deployed website versions,
// excluding the current major version.
if (majorVersion >= 6 && majorVersion !== currentMajorVersion) {
(acc[majorVersion] = acc[majorVersion] ?? []).push(tag);
const majorVersion = semver.major(tag);
return majorVersion >= 6 && majorVersion !== currentMajorVersion;
})
.reduce<Record<number, string>>((latestTagByMajor, tag) => {
const majorVersion = semver.major(tag);

const latestTag = latestTagByMajor[majorVersion];
if (latestTag == null || semver.lt(latestTag, tag)) {
latestTagByMajor[majorVersion] = tag;
}
return acc;

return latestTagByMajor;
}, {});
return Object.entries(latestReleaseTagNames)
.map(([major, tags]) => semver.maxSatisfying(tags, `^${major}`))
.sort(semver.rcompare);
return Object.values(latestReleaseTagNames).sort(semver.rcompare);
}

// Set by netlify
Expand Down
6 changes: 5 additions & 1 deletion scripts/apidoc/faker-class.ts
Expand Up @@ -28,6 +28,10 @@ export async function processFakerRandomizer(
.getChildrenByKind(ReflectionKind.Interface)
.find((clazz) => clazz.name === 'Randomizer');

if (randomizerClass == null) {
throw new Error('Randomizer class not found');
}

return processClass(randomizerClass);
}

Expand Down Expand Up @@ -63,7 +67,7 @@ async function processClass(
function hasConstructor(clazz: DeclarationReflection): boolean {
return clazz
.getChildrenByKind(ReflectionKind.Constructor)
.some((constructor) => constructor.signatures.length > 0);
.some((constructor) => (constructor.signatures?.length ?? 0) > 0);
}

async function processConstructor(
Expand Down
2 changes: 1 addition & 1 deletion scripts/apidoc/signature.ts
Expand Up @@ -305,7 +305,7 @@ async function signatureTypeToText(
await Promise.all(
signature.parameters?.map(
async (p) => `${p.name}: ${await typeToText(p.type)}`
)
) ?? []
)
).join(', ')}) => ${await typeToText(signature.type)}`;
}
Expand Down
3 changes: 2 additions & 1 deletion src/definitions/date.ts
Expand Up @@ -26,8 +26,9 @@ export interface DateEntryDefinition {

/**
* The short name/abbreviation of the entry.
* If null, the locale does not support a short name/abbreviation for the entry.
*/
abbr: string[];
abbr: string[] | null;

/**
* The wide name of the entry when used in context. If absent wide will be used instead.
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/definitions.ts
Expand Up @@ -50,4 +50,4 @@ export type LocaleDefinition = {
system?: SystemDefinition;
vehicle?: VehicleDefinition;
word?: WordDefinition;
} & Record<string, Record<string, unknown> | undefined>;
} & Record<string, Record<string, unknown>>;
40 changes: 27 additions & 13 deletions src/locale-proxy.ts
Expand Up @@ -54,6 +54,30 @@ export function createLocaleProxy(locale: LocaleDefinition): LocaleProxy {
}) as LocaleProxy;
}

/**
* Checks that the value is not null or undefined and throws an error if it is.
*
* @param value The value to check.
* @param path The path to the locale data.
*/
export function assertLocaleData<T>(
value: T,
...path: string[]
): asserts value is NonNullable<T> {
if (value === null) {
throw new FakerError(
`The locale data for '${path.join('.')}' aren't applicable to this locale.
If you think this is a bug, please report it at: https://github.com/faker-js/faker`
);
} else if (value === undefined) {
throw new FakerError(
`The locale data for '${path.join('.')}' are missing in this locale.
Please contribute the missing data to the project or use a locale/Faker instance that has these data.
For more information see https://fakerjs.dev/guide/localization.html`
);
}
}

/**
* Creates a proxy for a category that throws an error when accessing an undefined property.
*
Expand All @@ -79,20 +103,10 @@ function createCategoryProxy<
const value = target[entryName];
if (typeof entryName === 'symbol' || entryName === 'nodeType') {
return value;
} else if (value === null) {
throw new FakerError(
`The locale data for '${categoryName}.${entryName.toString()}' aren't applicable to this locale.
If you think this is a bug, please report it at: https://github.com/faker-js/faker`
);
} else if (value === undefined) {
throw new FakerError(
`The locale data for '${categoryName}.${entryName.toString()}' are missing in this locale.
Please contribute the missing data to the project or use a locale/Faker instance that has these data.
For more information see https://fakerjs.dev/guide/localization.html`
);
} else {
return value;
}

assertLocaleData(value, categoryName, entryName.toString());
return value;
},

set: throwReadOnlyError,
Expand Down
10 changes: 10 additions & 0 deletions src/locales/fr_SN/location/index.ts
Expand Up @@ -8,15 +8,25 @@ import city_name from './city_name';
import city_pattern from './city_pattern';
import default_country from './default_country';
import postcode from './postcode';
import secondary_address from './secondary_address';
import state from './state';
import street_address from './street_address';
import street_pattern from './street_pattern';
import street_prefix from './street_prefix';
import street_suffix from './street_suffix';

const location: LocationDefinition = {
building_number,
city_name,
city_pattern,
default_country,
postcode,
secondary_address,
state,
street_address,
street_pattern,
street_prefix,
street_suffix,
};

export default location;
1 change: 1 addition & 0 deletions src/locales/fr_SN/location/secondary_address.ts
@@ -0,0 +1 @@
export default ['Apt. ###', '# étage'];
4 changes: 4 additions & 0 deletions src/locales/fr_SN/location/street_address.ts
@@ -0,0 +1,4 @@
export default {
normal: '{{location.buildingNumber}} {{location.street}}',
full: '{{location.buildingNumber}} {{location.street}} {{location.secondaryAddress}}',
};
1 change: 1 addition & 0 deletions src/locales/fr_SN/location/street_pattern.ts
@@ -0,0 +1 @@
export default ['{{location.street_prefix}} {{location.street_suffix}}'];
12 changes: 12 additions & 0 deletions src/locales/fr_SN/location/street_prefix.ts
@@ -0,0 +1,12 @@
export default [
'Allée',
'Voie',
'Rue',
'Avenue',
'Boulevard',
'Quai',
'Place',
'Échangeur',
'Résidence',
'Immeuble',
];
33 changes: 33 additions & 0 deletions src/locales/fr_SN/location/street_suffix.ts
@@ -0,0 +1,33 @@
export default [
"de l'alternance",
"de l'émergence",
'Abdou Aziz Sy Dabakh',
'Amadou Assane Ndoye',
'Birago Diop',
'Blaise Diagne',
'Cheikh Amadou Bamba',
'Cheikh Anta Diop',
'Cheikh Seydi El Hadji Malick Sy',
'Dial Diop',
'Faidherbe',
'Galandou Diouf',
'Houphouët Boigny',
'Lamine Gueye',
'Lat Dior',
'Léopold Sedar Senghor',
'Neslon Mandela',
'Saint Michel',
'St Joseph',
'Sérigne Fallou Mbacké',
'Victor Hugo',
'Wagane Diouf',
'William Ponty',
'Yacine Boubou',
'de Bordeaux',
'de Contournement Nord',
'de Nguinth',
'des Diambars',
'des Jasmins',
'du Baol',
'du Tirailleur',
];
2 changes: 1 addition & 1 deletion src/modules/airline/index.ts
Expand Up @@ -155,7 +155,7 @@ export class AirlineModule extends ModuleBase {
): string {
const { allowNumerics = false, allowVisuallySimilarCharacters = false } =
options;
const excludedChars = [];
const excludedChars: string[] = [];
if (!allowNumerics) {
excludedChars.push(...numerics);
}
Expand Down
37 changes: 20 additions & 17 deletions src/modules/color/index.ts
Expand Up @@ -47,17 +47,19 @@ export type Casing = 'lower' | 'upper' | 'mixed';
*
* @param hexColor Hex color string to be formatted.
* @param options Options object.
* @param options.prefix Prefix of the generated hex color. Defaults to `'0x'`.
* @param options.casing Letter type case of the generated hex color. Defaults to `'mixed'`.
* @param options.prefix Prefix of the generated hex color.
* @param options.casing Letter type case of the generated hex color.
*/
function formatHexColor(
hexColor: string,
options?: {
prefix?: string;
casing?: Casing;
options: {
prefix: string;
casing: Casing;
}
): string {
switch (options?.casing) {
const { prefix, casing } = options;

switch (casing) {
case 'upper':
hexColor = hexColor.toUpperCase();
break;
Expand All @@ -68,8 +70,8 @@ function formatHexColor(
// Do nothing
}

if (options?.prefix) {
hexColor = options.prefix + hexColor;
if (prefix) {
hexColor = prefix + hexColor;
}

return hexColor;
Expand Down Expand Up @@ -360,27 +362,28 @@ export class ColorModule extends ModuleBase {
*/
includeAlpha?: boolean;
}): string | number[];
rgb(options?: {
prefix?: string;
casing?: Casing;
format?: 'hex' | ColorFormat;
includeAlpha?: boolean;
}): string | number[] {
rgb(
options: {
prefix?: string;
casing?: Casing;
format?: 'hex' | ColorFormat;
includeAlpha?: boolean;
} = {}
): string | number[] {
const {
format = 'hex',
includeAlpha = false,
prefix = '#',
casing = 'lower',
} = options || {};
options = { format, includeAlpha, prefix, casing };
} = options;
let color: string | number[];
let cssFunction: CssFunctionType = 'rgb';
if (format === 'hex') {
color = this.faker.string.hexadecimal({
length: includeAlpha ? 8 : 6,
prefix: '',
});
color = formatHexColor(color, options);
color = formatHexColor(color, { prefix, casing });
return color;
}

Expand Down
10 changes: 9 additions & 1 deletion src/modules/commerce/index.ts
@@ -1,3 +1,4 @@
import { FakerError } from '../../errors/faker-error';
import { deprecated } from '../../internal/deprecated';
import { ModuleBase } from '../../internal/module-base';

Expand Down Expand Up @@ -401,7 +402,14 @@ export class CommerceModule extends ModuleBase {

const registrantLength = groupRules.find(
([rangeMaximum]) => elementValue <= rangeMaximum
)[1];
)?.[1];

if (!registrantLength) {
// This can only happen if the ISBN_LENGTH_RULES are corrupted
throw new FakerError(
`Unable to find a registrant length for the group ${group}`
);
}

const registrant = element.slice(0, registrantLength);
const publication = element.slice(registrantLength);
Expand Down

0 comments on commit 72ea9ef

Please sign in to comment.