Skip to content

Commit

Permalink
Merge branch 'next' into test/jsdocs/default-assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Oct 11, 2023
2 parents 9eebdc7 + 28d3bad commit dd544be
Show file tree
Hide file tree
Showing 14 changed files with 94 additions and 106 deletions.
4 changes: 0 additions & 4 deletions .eslintrc.js
Expand Up @@ -49,14 +49,11 @@ module.exports = defineConfig({
// Each rule should be checked whether it should be enabled/configured and the problems fixed, or stay disabled permanently.
'unicorn/better-regex': 'off',
'unicorn/catch-error-name': 'off',
'unicorn/consistent-destructuring': 'off',
'unicorn/consistent-function-scoping': 'off',
'unicorn/escape-case': 'off',
'unicorn/explicit-length-check': 'off',
'unicorn/filename-case': 'off',
'unicorn/import-style': 'off',
'unicorn/no-array-callback-reference': 'off',
'unicorn/no-array-for-each': 'off',
'unicorn/no-array-reduce': 'off',
'unicorn/no-await-expression-member': 'off',
'unicorn/no-for-loop': 'off',
Expand All @@ -69,7 +66,6 @@ module.exports = defineConfig({
'unicorn/prefer-array-some': 'off',
'unicorn/prefer-code-point': 'off',
'unicorn/prefer-export-from': 'off',
'unicorn/prefer-includes': 'off',
'unicorn/prefer-module': 'off',
'unicorn/prefer-native-coercion-functions': 'off',
'unicorn/prefer-negative-index': 'off',
Expand Down
7 changes: 3 additions & 4 deletions scripts/apidoc/parameterDefaults.ts
Expand Up @@ -130,8 +130,7 @@ function patchSignatureParameterDefaults(
throw new Error('Unexpected parameter length mismatch');
}

signatureParameters.forEach(
(param, index) =>
(param.defaultValue = parameterDefaults[index] || param.defaultValue)
);
for (const [index, param] of signatureParameters.entries()) {
param.defaultValue = parameterDefaults[index] || param.defaultValue;
}
}
4 changes: 2 additions & 2 deletions scripts/apidoc/signature.ts
Expand Up @@ -64,7 +64,7 @@ export async function analyzeSignature(
// Generate usage section

let signatureTypeParametersString = '';
if (signatureTypeParameters.length !== 0) {
if (signatureTypeParameters.length > 0) {
signatureTypeParametersString = `<${signatureTypeParameters.join(', ')}>`;
}

Expand Down Expand Up @@ -207,7 +207,7 @@ async function typeToText(type_?: Type, short = false): Promise<string> {
.join(' | ');

case 'reference':
if (!type.typeArguments || !type.typeArguments.length) {
if (!type.typeArguments || type.typeArguments.length === 0) {
const reflection = type.reflection as DeclarationReflection | undefined;
const reflectionType = reflection?.type;
if (
Expand Down
4 changes: 2 additions & 2 deletions src/modules/datatype/index.ts
Expand Up @@ -407,11 +407,11 @@ export class DatatypeModule {
const properties = ['foo', 'bar', 'bike', 'a', 'b', 'name', 'prop'];
const returnObject: Record<string, string | number> = {};

properties.forEach((prop) => {
for (const prop of properties) {
returnObject[prop] = this.boolean()
? this.faker.string.sample()
: this.faker.number.int();
});
}

return JSON.stringify(returnObject);
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/helpers/index.ts
Expand Up @@ -460,7 +460,7 @@ export class SimpleHelpersModule {
}

while (range != null) {
if (range[0].indexOf('-') === -1) {
if (!range[0].includes('-')) {
// handle non-ranges
if (isCaseInsensitive && isNaN(Number(range[0]))) {
rangeCodes.push(
Expand Down
20 changes: 9 additions & 11 deletions src/modules/helpers/unique.ts
Expand Up @@ -95,53 +95,51 @@ export function exec<
startTime = Date.now(),
maxTime = 50,
maxRetries = 50,
currentIterations = 0,
compare = defaultCompare,
store,
} = options;
let { exclude } = options;
options.currentIterations = options.currentIterations ?? 0;
options.currentIterations = currentIterations;

// Support single exclude argument as string
if (!Array.isArray(exclude)) {
exclude = [exclude];
}

// if (options.currentIterations > 0) {
// console.log('iterating', options.currentIterations)
// }

// console.log(now - startTime)
// If out of time -> throw error.
if (now - startTime >= maxTime) {
return errorMessage(
startTime,
now,
`Exceeded maxTime: ${maxTime}`,
store,
options.currentIterations
currentIterations
);
}

if (options.currentIterations >= maxRetries) {
// If out of retries -> throw error.
if (currentIterations >= maxRetries) {
return errorMessage(
startTime,
now,
`Exceeded maxRetries: ${maxRetries}`,
store,
options.currentIterations
currentIterations
);
}

// Execute the provided method to find a potential satisfied value.
const result: ReturnType<TMethod> = method(...args) as ReturnType<TMethod>;

// If the result has not been previously found, add it to the found array and return the value as it's unique.
if (compare(store, result) === -1 && exclude.indexOf(result) === -1) {
if (compare(store, result) === -1 && !exclude.includes(result)) {
store[result] = result;
options.currentIterations = 0;
return result;
}

// console.log('conflict', result);
// Conflict, try again.
options.currentIterations++;
return exec(method, args, {
...options,
Expand Down
29 changes: 9 additions & 20 deletions src/modules/system/index.ts
Expand Up @@ -154,15 +154,11 @@ export class SystemModule {
* @since 3.1.0
*/
fileType(): string {
const typeSet = new Set<string>();
const mimeTypes = this.faker.definitions.system.mimeTypes;

Object.keys(mimeTypes).forEach((m) => {
const type = m.split('/')[0];

typeSet.add(type);
});

const typeSet = new Set(
Object.keys(mimeTypes).map((key) => key.split('/')[0])
);
const types = Array.from(typeSet);
return this.faker.helpers.arrayElement(types);
}
Expand All @@ -179,22 +175,15 @@ export class SystemModule {
* @since 3.1.0
*/
fileExt(mimeType?: string): string {
if (typeof mimeType === 'string') {
const mimes = this.faker.definitions.system.mimeTypes;
return this.faker.helpers.arrayElement(mimes[mimeType].extensions);
}

const mimeTypes = this.faker.definitions.system.mimeTypes;
const extensionSet = new Set<string>();

Object.keys(mimeTypes).forEach((m) => {
if (mimeTypes[m].extensions instanceof Array) {
mimeTypes[m].extensions.forEach((ext) => {
extensionSet.add(ext);
});
}
});
if (typeof mimeType === 'string') {
return this.faker.helpers.arrayElement(mimeTypes[mimeType].extensions);
}

const extensionSet = new Set(
Object.values(mimeTypes).flatMap(({ extensions }) => extensions)
);
const extensions = Array.from(extensionSet);
return this.faker.helpers.arrayElement(extensions);
}
Expand Down
18 changes: 10 additions & 8 deletions test/all_functional.spec.ts
Expand Up @@ -2,16 +2,16 @@ import { describe, expect, it } from 'vitest';
import type { allLocales, Faker, RandomModule } from '../src';
import { allFakers, fakerEN } from '../src';

const IGNORED_MODULES = [
const IGNORED_MODULES = new Set([
'rawDefinitions',
'definitions',
'helpers',
'_randomizer',
'_defaultRefDate',
];
]);

function isTestableModule(mod: string) {
return IGNORED_MODULES.indexOf(mod) === -1;
return !IGNORED_MODULES.has(mod);
}

function isMethodOf(mod: string) {
Expand Down Expand Up @@ -70,7 +70,7 @@ function modulesList(): { [module: string]: string[] } {
.reduce((result, mod) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const methods = Object.keys(fakerEN[mod]).filter(isMethodOf(mod));
if (methods.length) {
if (methods.length > 0) {
result[mod] = methods;
} else {
console.log(`Skipping ${mod} - No testable methods`);
Expand Down Expand Up @@ -119,7 +119,8 @@ describe('functional tests', () => {
}

describe.each(Object.entries(modules))('%s', (module, methods) => {
methods.forEach((meth) => {
// eslint-disable-next-line vitest/prefer-each -- need to dynamically succeed/fail
for (const meth of methods) {
const testAssertion = () => {
// TODO @ST-DDT 2022-03-28: Use random seed once there are no more failures
faker.seed(1);
Expand All @@ -140,7 +141,7 @@ describe('functional tests', () => {
// We expect a failure here to ensure we remove the exclusions when fixed
it.fails(`${meth}()`, testAssertion);
}
});
}
});
});
});
Expand All @@ -153,7 +154,8 @@ describe('faker.helpers.fake functional tests', () => {
}

describe.each(Object.entries(modules))('%s', (module, methods) => {
methods.forEach((meth) => {
// eslint-disable-next-line vitest/prefer-each -- need to dynamically succeed/fail
for (const meth of methods) {
const testAssertion = () => {
// TODO @ST-DDT 2022-03-28: Use random seed once there are no more failures
faker.seed(1);
Expand All @@ -172,7 +174,7 @@ describe('faker.helpers.fake functional tests', () => {
// We expect a failure here to ensure we remove the exclusions when fixed
it.fails(`${meth}()`, testAssertion);
}
});
}
});
});
});

0 comments on commit dd544be

Please sign in to comment.