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

infra(unicorn): no-array-reduce #2479

Merged
merged 19 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ module.exports = defineConfig({
'unicorn/filename-case': 'off',
'unicorn/import-style': 'off',
'unicorn/no-array-callback-reference': 'off',
'unicorn/no-array-reduce': 'off',
'unicorn/no-await-expression-member': 'off',
'unicorn/no-negated-condition': 'off',
'unicorn/no-object-as-default-parameter': 'off',
Expand Down
23 changes: 10 additions & 13 deletions scripts/apidoc/apiDocsWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,17 @@ export async function writeApiDocsModule(
text: moduleName,
link: `/api/${lowerModuleName}.html`,
methods,
diff: methods.reduce(
(data, method) => ({
...data,
[method.name]: methodDiffHash(method),
diff: {
moduleHash: diffHash({
name: moduleName,
field: lowerModuleName,
deprecated,
comment,
}),
{
moduleHash: diffHash({
name: moduleName,
field: lowerModuleName,
deprecated,
comment,
}),
}
),
...Object.fromEntries(
methods.map((method) => [method.name, methodDiffHash(method)])
),
},
};
}

Expand Down
25 changes: 25 additions & 0 deletions src/internal/group-by.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Groups the values by the key function.
*
* @internal
*
* @param values The values to group.
* @param keyFunction The function to get the key from the value.
*/
export function groupBy<TValue>(
values: ReadonlyArray<TValue>,
keyFunction: (value: TValue) => string | number
): Record<string, TValue[]> {
const result: Record<string, TValue[]> = {};

for (const value of values) {
const key = keyFunction(value);
if (result[key] === undefined) {
result[key] = [];
}

result[key].push(value);
}

return result;
}
10 changes: 2 additions & 8 deletions src/modules/word/filterWordListByLength.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FakerError } from '../../errors/faker-error';
import { groupBy } from '../../internal/group-by';

/**
* The error handling strategies for the `filterWordListByLength` function.
Expand All @@ -13,14 +14,7 @@ const STRATEGIES = {
wordList: ReadonlyArray<string>,
length: { min: number; max: number }
): string[] => {
const wordsByLength = wordList.reduce(
(data, word) => {
(data[word.length] = data[word.length] ?? []).push(word);
return data;
},
{} as Record<number, string[]>
);

const wordsByLength = groupBy(wordList, (word) => word.length);
const lengths = Object.keys(wordsByLength).map(Number);
const min = Math.min(...lengths);
const max = Math.max(...lengths);
Expand Down
59 changes: 32 additions & 27 deletions test/all_functional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,36 @@ const IGNORED_MODULES = new Set([
'_defaultRefDate',
]);

function isTestableModule(mod: string) {
return !IGNORED_MODULES.has(mod);
function getMethodNamesByModules(faker: Faker): { [module: string]: string[] } {
return Object.fromEntries(
Object.keys(faker)
.filter(isTestableModule)
.sort()
.map<[string, string[]]>((moduleName) => [
moduleName,
getMethodNamesOf(faker[moduleName]),
])
.filter(([module, methods]) => {
if (methods.length === 0) {
console.log(`Skipping ${module} - No testable methods`);
return false;
}

return true;
})
);
}

function isTestableModule(moduleName: string): moduleName is keyof Faker {
return !IGNORED_MODULES.has(moduleName);
}

function getMethodNamesOf(module: Faker[keyof Faker]): string[] {
return Object.keys(module).filter(isMethodOf(module));
}

function isMethodOf(mod: string) {
return (meth: string) => typeof fakerEN[mod][meth] === 'function';
function isMethodOf(module: Faker[keyof Faker]): (method: string) => boolean {
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
return (method: string) => typeof module[method] === 'function';
}

type SkipConfig<TModule> = Partial<
Expand Down Expand Up @@ -53,36 +77,17 @@ const BROKEN_LOCALE_METHODS = {
};

function isWorkingLocaleForMethod(
mod: string,
meth: string,
module: string,
method: string,
locale: string
): boolean {
const broken = BROKEN_LOCALE_METHODS[mod]?.[meth] ?? [];
const broken = BROKEN_LOCALE_METHODS[module]?.[method] ?? [];
return broken !== '*' && !broken.includes(locale);
}

// Basic smoke tests to make sure each method is at least implemented and returns a value.

function modulesList(): { [module: string]: string[] } {
const modules = Object.keys(fakerEN)
.sort()
.filter(isTestableModule)
.reduce((result, mod) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const methods = Object.keys(fakerEN[mod]).filter(isMethodOf(mod));
if (methods.length > 0) {
result[mod] = methods;
} else {
console.log(`Skipping ${mod} - No testable methods`);
}

return result;
}, {});

return modules;
}

const modules = modulesList();
const modules = getMethodNamesByModules(fakerEN);

describe('BROKEN_LOCALE_METHODS test', () => {
it('should not contain obsolete configuration (modules)', () => {
Expand Down
18 changes: 7 additions & 11 deletions test/scripts/apidoc/verify-jsdoc-tags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,24 @@ describe('verify JSDoc tags', () => {
}

const allowedReferences = new Set(
Object.values(modules).reduce((acc, [module, methods]) => {
Object.values(modules).flatMap(([module, methods]) => {
const moduleFieldName = extractModuleFieldName(module);
return [
...acc,
...Object.keys(methods).map(
(methodName) => `faker.${moduleFieldName}.${methodName}`
),
];
}, [] as string[])
return Object.keys(methods).map(
(methodName) => `faker.${moduleFieldName}.${methodName}`
);
})
);
const allowedLinks = new Set(
Object.values(modules).reduce((acc, [module, methods]) => {
Object.values(modules).flatMap(([module, methods]) => {
const moduleFieldName = extractModuleFieldName(module);
return [
...acc,
`/api/${moduleFieldName}.html`,
...Object.keys(methods).map(
(methodName) =>
`/api/${moduleFieldName}.html#${methodName.toLowerCase()}`
),
];
}, [] as string[])
})
);

function assertDescription(description: string, isHtml: boolean): void {
Expand Down