Skip to content

Commit

Permalink
Merge branch 'next' into unicorn/prefer-spread
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Oct 11, 2023
2 parents 7a48f89 + 28d3bad commit 7674d33
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 97 deletions.
3 changes: 0 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +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/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 @@ -68,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
Original file line number Diff line number Diff line change
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 src/modules/datatype/index.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
35 changes: 11 additions & 24 deletions src/modules/system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,12 @@ 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 types = [...typeSet];
return this.faker.helpers.arrayElement(types);
const typeSet = new Set(
Object.keys(mimeTypes).map((key) => key.split('/')[0])
);
return this.faker.helpers.arrayElement([...typeSet]);
}

/**
Expand All @@ -179,24 +174,16 @@ export class SystemModule {
* @since 3.1.0
*/
fileExt(mimeType?: string): string {
const mimeTypes = this.faker.definitions.system.mimeTypes;

if (typeof mimeType === 'string') {
const mimes = this.faker.definitions.system.mimeTypes;
return this.faker.helpers.arrayElement(mimes[mimeType].extensions);
return this.faker.helpers.arrayElement(mimeTypes[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);
});
}
});

const extensions = [...extensionSet];
return this.faker.helpers.arrayElement(extensions);
const extensionSet = new Set(
Object.values(mimeTypes).flatMap(({ extensions }) => extensions)
);
return this.faker.helpers.arrayElement([...extensionSet]);
}

/**
Expand Down
16 changes: 9 additions & 7 deletions test/all_functional.spec.ts
Original file line number Diff line number Diff line change
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 @@ -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);
}
});
}
});
});
});
Loading

0 comments on commit 7674d33

Please sign in to comment.