Skip to content

Commit

Permalink
infra(unicorn): no-array-for-each (#2461)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Oct 11, 2023
1 parent 6cb5aa2 commit 201d6e3
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 76 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ module.exports = defineConfig({
'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 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
29 changes: 9 additions & 20 deletions src/modules/system/index.ts
Original file line number Diff line number Diff line change
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
10 changes: 6 additions & 4 deletions test/all_functional.spec.ts
Original file line number Diff line number Diff line change
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);
}
});
}
});
});
});
52 changes: 26 additions & 26 deletions test/modules/color.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ describe('color', () => {
it('should return a random rgb color in decimal format', () => {
const color = faker.color.rgb({ format: 'decimal' });
expect(color).length(3);
color.forEach((value: number) => {
for (const value of color) {
expect(value).toBeGreaterThanOrEqual(0);
expect(value).toBeLessThanOrEqual(255);
});
}
});
});

Expand Down Expand Up @@ -124,10 +124,10 @@ describe('color', () => {
});
expect(color[color.length - 1]).toBeGreaterThanOrEqual(0);
expect(color[color.length - 1]).toBeLessThanOrEqual(1);
color.slice(0, 4).forEach((value: number) => {
for (const value of color.slice(0, 4)) {
expect(value).toBeGreaterThanOrEqual(0);
expect(value).toBeLessThanOrEqual(255);
});
}
});
});

Expand All @@ -154,21 +154,21 @@ describe('color', () => {
it('should return a random cmyk color', () => {
const color = faker.color.cmyk();
expect(color).length(4);
color.forEach((value: number) => {
for (const value of color) {
expect(value).toBeGreaterThanOrEqual(0);
expect(value).toBeLessThanOrEqual(1);
});
}
});
});

describe(`cmyk({ format: 'decimal' })`, () => {
it('should return a random cmyk color in decimal format', () => {
const color = faker.color.cmyk({ format: 'decimal' });
expect(color).length(4);
color.forEach((value: number) => {
for (const value of color) {
expect(value).toBeGreaterThanOrEqual(0);
expect(value).toBeLessThanOrEqual(1);
});
}
});
});

Expand Down Expand Up @@ -196,10 +196,10 @@ describe('color', () => {
expect(color).length(3);
expect(color[0]).toBeGreaterThanOrEqual(0);
expect(color[0]).toBeLessThanOrEqual(360);
color.slice(1).forEach((value: number) => {
for (const value of color.slice(1)) {
expect(value).toBeGreaterThanOrEqual(0);
expect(value).toBeLessThanOrEqual(1);
});
}
});
});

Expand Down Expand Up @@ -246,10 +246,10 @@ describe('color', () => {
expect(color).length(3);
expect(color[0]).toBeGreaterThanOrEqual(0);
expect(color[0]).toBeLessThanOrEqual(360);
color.slice(1).forEach((value: number) => {
for (const value of color.slice(1)) {
expect(value).toBeGreaterThanOrEqual(0);
expect(value).toBeLessThanOrEqual(1);
});
}
});
});

Expand All @@ -259,10 +259,10 @@ describe('color', () => {
expect(color).length(3);
expect(color[0]).toBeGreaterThanOrEqual(0);
expect(color[0]).toBeLessThanOrEqual(360);
color.slice(1).forEach((value: number) => {
for (const value of color.slice(1)) {
expect(value).toBeGreaterThanOrEqual(0);
expect(value).toBeLessThanOrEqual(1);
});
}
});
});

Expand All @@ -286,10 +286,10 @@ describe('color', () => {
expect(color).length(3);
expect(color[0]).toBeGreaterThanOrEqual(0);
expect(color[0]).toBeLessThanOrEqual(1);
color.forEach((value: number) => {
for (const value of color) {
expect(value).toBeGreaterThanOrEqual(-100);
expect(value).toBeLessThanOrEqual(100);
});
}
});
});

Expand All @@ -299,10 +299,10 @@ describe('color', () => {
expect(color).length(3);
expect(color[0]).toBeGreaterThanOrEqual(0);
expect(color[0]).toBeLessThanOrEqual(1);
color.forEach((value: number) => {
for (const value of color) {
expect(value).toBeGreaterThanOrEqual(-100);
expect(value).toBeLessThanOrEqual(100);
});
}
});
});

Expand All @@ -328,10 +328,10 @@ describe('color', () => {
expect(color).length(3);
expect(color[0]).toBeGreaterThanOrEqual(0);
expect(color[0]).toBeLessThanOrEqual(1);
color.forEach((value: number) => {
for (const value of color) {
expect(value).toBeGreaterThanOrEqual(0);
expect(value).toBeLessThanOrEqual(230);
});
}
});
});

Expand All @@ -341,10 +341,10 @@ describe('color', () => {
expect(color).length(3);
expect(color[0]).toBeGreaterThanOrEqual(0);
expect(color[0]).toBeLessThanOrEqual(1);
color.forEach((value: number) => {
for (const value of color) {
expect(value).toBeGreaterThanOrEqual(0);
expect(value).toBeLessThanOrEqual(230);
});
}
});
});

Expand All @@ -368,21 +368,21 @@ describe('color', () => {
it('should return a random color for a CSS color space in decimal format', () => {
const color = faker.color.colorByCSSColorSpace();
expect(color).length(3);
color.forEach((value: number) => {
for (const value of color) {
expect(value).toBeGreaterThanOrEqual(0);
expect(value).toBeLessThanOrEqual(1);
});
}
});
});

describe(`colorByCSSColorSpace({ format: 'decimal' })`, () => {
it('should return a random color for a CSS color space in decimal format', () => {
const color = faker.color.colorByCSSColorSpace({ format: 'decimal' });
expect(color).length(3);
color.forEach((value: number) => {
for (const value of color) {
expect(value).toBeGreaterThanOrEqual(0);
expect(value).toBeLessThanOrEqual(1);
});
}
});
});

Expand Down
16 changes: 8 additions & 8 deletions test/modules/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,9 @@ describe('helpers', () => {
expect(subset.length).toBeLessThanOrEqual(testArray.length);

// Check elements
subset.forEach((element) => {
for (const element of subset) {
expect(testArray).toContain(element);
});
}

// Check uniqueness
expect(subset).not.toContainDuplicates();
Expand All @@ -391,9 +391,9 @@ describe('helpers', () => {
expect(subset).toHaveLength(3);

// Check elements
subset.forEach((element) => {
for (const element of subset) {
expect(testArray).toContain(element);
});
}

// Check uniqueness
expect(subset).toHaveLength(new Set(subset).size);
Expand All @@ -411,9 +411,9 @@ describe('helpers', () => {
expect(subset.length).toBeLessThanOrEqual(4);

// Check elements
subset.forEach((element) => {
for (const element of subset) {
expect(testArray).toContain(element);
});
}

// Check uniqueness
expect(subset).not.toContainDuplicates();
Expand All @@ -427,9 +427,9 @@ describe('helpers', () => {
expect(subset.length).toBe(5);

// Check elements
subset.forEach((element) => {
for (const element of subset) {
expect(testArray).toContain(element);
});
}
});

it('should return an empty array when array length > 0 and count = 0', () => {
Expand Down
8 changes: 4 additions & 4 deletions test/modules/system.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,12 @@ describe('system', () => {
(options, count: number) => {
const cron = faker.system.cron(options).split(' ');
expect(cron).toHaveLength(count);
cron.forEach((cronElement, i) =>
for (const [index, cronElement] of cron.entries()) {
expect(
cronElement,
`generated cron, ${cronElement} should match regex ${regexElements[i]}`
).toMatch(new RegExp(regexElements[i]))
);
`generated cron, ${cronElement} should match regex ${regexElements[index]}`
).toMatch(new RegExp(regexElements[index]));
}
}
);

Expand Down
4 changes: 2 additions & 2 deletions test/scripts/apidoc/signature.debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const methods = loadExampleMethods();

initMarkdownRenderer()
.then(() => {
Object.entries(methods).forEach(([name, method]) => {
for (const [name, method] of Object.entries(methods)) {
console.log('Analyzing:', name);
const result = analyzeSignature(method, '', method.name);
console.log('Result:', result);
});
}
})
.catch(console.error);
Loading

0 comments on commit 201d6e3

Please sign in to comment.