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): prefer-spread #2421

Merged
merged 9 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -70,7 +70,6 @@ module.exports = defineConfig({
'unicorn/prefer-negative-index': 'off',
'unicorn/prefer-number-properties': 'off',
'unicorn/prefer-optional-catch-binding': 'off',
'unicorn/prefer-spread': 'off',
'unicorn/prefer-string-slice': 'off',
'unicorn/prefer-ternary': 'off',
'unicorn/prefer-top-level-await': 'off',
Expand Down
2 changes: 1 addition & 1 deletion src/internal/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
* @param args The arrays to merge.
*/
export function mergeArrays<T>(...args: T[][]): T[] {
return Array.from(new Set(args.flat())).sort();
return [...new Set(args.flat())].sort();
}
2 changes: 1 addition & 1 deletion src/modules/color/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function toBinary(values: number[]): string {
const buffer = new ArrayBuffer(4);
new DataView(buffer).setFloat32(0, value);
const bytes = new Uint8Array(buffer);
return toBinary(Array.from(bytes)).split(' ').join('');
return toBinary([...bytes]).replace(/ /g, '');
}

return (value >>> 0).toString(2).padStart(8, '0');
Expand Down
2 changes: 1 addition & 1 deletion src/modules/company/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class CompanyModule {
});
// Don't want the source array exposed to modification, so return a copy
// eslint-disable-next-line deprecation/deprecation
return this.faker.definitions.company.suffix.slice(0);
return [...this.faker.definitions.company.suffix];
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/modules/finance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,9 +722,9 @@ export class FinanceModule {
let address = this.faker.helpers.arrayElement(['L', 'M', '3']);

for (let i = 0; i < addressLength - 1; i++)
address += this.faker.helpers.arrayElement(
'123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'.split('')
);
address += this.faker.helpers.arrayElement([
...'123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ',
]);

return address;
}
Expand Down
6 changes: 3 additions & 3 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ export class SimpleHelpersModule {
uniqueArray<T>(source: ReadonlyArray<T> | (() => T), length: number): T[] {
if (Array.isArray(source)) {
const set = new Set<T>(source);
const array = Array.from(set);
const array = [...set];
return this.shuffle(array).splice(0, length);
}

Expand All @@ -722,7 +722,7 @@ export class SimpleHelpersModule {
// Ignore
}

return Array.from(set);
return [...set];
}

/**
Expand Down Expand Up @@ -1003,7 +1003,7 @@ export class SimpleHelpersModule {
return [];
}

const arrayCopy = array.slice(0);
const arrayCopy = [...array];
let i = array.length;
const min = i - numElements;
let temp: T;
Expand Down
7 changes: 3 additions & 4 deletions src/modules/internet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,8 @@ export class InternetModule {
// We limit to 50 chars to be more realistic
localPart = localPart.substring(0, 50);
if (allowSpecialCharacters) {
const usernameChars: string[] = '._-'.split('');
const specialChars: string[] = ".!#$%&'*+-/=?^_`{|}~".split('');
const usernameChars: string[] = [...'._-'];
const specialChars: string[] = [...".!#$%&'*+-/=?^_`{|}~"];
localPart = localPart.replace(
this.faker.helpers.arrayElement(usernameChars),
this.faker.helpers.arrayElement(specialChars)
Expand Down Expand Up @@ -638,8 +638,7 @@ export class InternetModule {
.normalize('NFKD') //for example è decomposes to as e + ̀
.replace(/[\u0300-\u036f]/g, ''); // removes combining marks

result = result
.split('')
result = [...result]
.map((char) => {
// If we have a mapping for this character, (for Cyrillic, Greek etc) use it
if (charMapping[char]) {
Expand Down
18 changes: 7 additions & 11 deletions src/modules/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import type { LiteralUnion } from '../../utils/types';

export type Casing = 'upper' | 'lower' | 'mixed';

const UPPER_CHARS: ReadonlyArray<string> = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(
''
);
const LOWER_CHARS: ReadonlyArray<string> = 'abcdefghijklmnopqrstuvwxyz'.split(
''
);
const DIGIT_CHARS: ReadonlyArray<string> = '0123456789'.split('');
const UPPER_CHARS: ReadonlyArray<string> = [...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'];
const LOWER_CHARS: ReadonlyArray<string> = [...'abcdefghijklmnopqrstuvwxyz'];
const DIGIT_CHARS: ReadonlyArray<string> = [...'0123456789'];

export type LowerAlphaChar =
| 'a'
Expand Down Expand Up @@ -145,7 +141,7 @@ export class StringModule {
}

if (typeof characters === 'string') {
characters = characters.split('');
characters = [...characters];
}

if (characters.length === 0) {
Expand Down Expand Up @@ -229,7 +225,7 @@ export class StringModule {
let { exclude = [] } = options;

if (typeof exclude === 'string') {
exclude = exclude.split('');
exclude = [...exclude];
}

let charsArray: string[];
Expand Down Expand Up @@ -319,7 +315,7 @@ export class StringModule {
let { exclude = [] } = options;

if (typeof exclude === 'string') {
exclude = exclude.split('');
exclude = [...exclude];
}

let charsArray = [...DIGIT_CHARS];
Expand Down Expand Up @@ -596,7 +592,7 @@ export class StringModule {
let { exclude = [] } = options;

if (typeof exclude === 'string') {
exclude = exclude.split('');
exclude = [...exclude];
}

const allowedDigits = DIGIT_CHARS.filter(
Expand Down
6 changes: 2 additions & 4 deletions src/modules/system/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ export class SystemModule {
const typeSet = new Set(
Object.keys(mimeTypes).map((key) => key.split('/')[0])
);
const types = Array.from(typeSet);
return this.faker.helpers.arrayElement(types);
return this.faker.helpers.arrayElement([...typeSet]);
}

/**
Expand All @@ -184,8 +183,7 @@ export class SystemModule {
const extensionSet = new Set(
Object.values(mimeTypes).flatMap(({ extensions }) => extensions)
);
const extensions = Array.from(extensionSet);
return this.faker.helpers.arrayElement(extensions);
return this.faker.helpers.arrayElement([...extensionSet]);
}

/**
Expand Down
24 changes: 12 additions & 12 deletions test/modules/datatype.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,34 +164,34 @@ describe('datatype', () => {
});

it('provides numbers with a given precision of 0.5 steps', () => {
const results = Array.from(
new Set(
const results = [
...new Set(
Array.from({ length: 50 }, () =>
faker.datatype.float({
min: 0,
max: 1.5,
precision: 0.5,
})
)
)
).sort();
),
].sort();

expect(results).toEqual([0, 0.5, 1, 1.5]);
});

// TODO @Shinigami92 2022-11-24: https://github.com/faker-js/faker/issues/1595
it.todo('provides numbers with a given precision of 0.4 steps', () => {
const results = Array.from(
new Set(
const results = [
...new Set(
Array.from({ length: 50 }, () =>
faker.datatype.float({
min: 0,
max: 1.9,
precision: 0.4,
})
)
)
).sort();
),
].sort();

expect(results).toEqual([0, 0.4, 0.8, 1.2, 1.6]);
});
Expand Down Expand Up @@ -279,11 +279,11 @@ describe('datatype', () => {

it('provides numbers with a given precision', () => {
const options = { min: 0, max: 1.5, precision: 0.5 };
const results = Array.from(
new Set(
const results = [
...new Set(
Array.from({ length: 50 }, () => faker.datatype.float(options))
)
).sort();
),
].sort();

expect(results).toEqual([0, 0.5, 1, 1.5]);
});
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 @@ -71,7 +71,7 @@ describe('helpers', () => {
});

t.describe('arrayElement', (t) => {
t.it('with array', 'Hello World!'.split(''));
t.it('with array', [...'Hello World!']);
});

t.describe('enumValue', (t) => {
Expand Down Expand Up @@ -120,26 +120,26 @@ describe('helpers', () => {
});

t.describe('arrayElements', (t) => {
t.it('with array', 'Hello World!'.split(''))
.it('with array and count', 'Hello World!'.split(''), 3)
.it('with array and count range', 'Hello World!'.split(''), {
t.it('with array', [...'Hello World!'])
.it('with array and count', [...'Hello World!'], 3)
.it('with array and count range', [...'Hello World!'], {
min: 1,
max: 5,
});
});

t.describe('shuffle', (t) => {
t.it('with array', 'Hello World!'.split(''))
.it('with array and inplace true', 'Hello World!'.split(''), {
t.it('with array', [...'Hello World!'])
.it('with array and inplace true', [...'Hello World!'], {
inplace: true,
})
.it('with array and inplace false', 'Hello World!'.split(''), {
.it('with array and inplace false', [...'Hello World!'], {
inplace: false,
});
});

t.describe('uniqueArray', (t) => {
t.it('with array', 'Hello World!'.split(''), 3);
t.it('with array', [...'Hello World!'], 3);
});

t.describe('maybe', (t) => {
Expand Down
16 changes: 8 additions & 8 deletions test/modules/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,33 +227,33 @@ describe('number', () => {
});

it('provides numbers with a given precision of 0.5 steps', () => {
const results = Array.from(
new Set(
const results = [
...new Set(
Array.from({ length: 50 }, () =>
faker.number.float({
min: 0,
max: 1.5,
precision: 0.5,
})
)
)
).sort();
),
].sort();

expect(results).toEqual([0, 0.5, 1, 1.5]);
});

it('provides numbers with a given precision of 0.4 steps', () => {
const results = Array.from(
new Set(
const results = [
...new Set(
Array.from({ length: 50 }, () =>
faker.number.float({
min: 0,
max: 1.9,
precision: 0.4,
})
)
)
).sort();
),
].sort();

expect(results).toEqual([0, 0.4, 0.8, 1.2, 1.6]);
});
Expand Down
17 changes: 9 additions & 8 deletions test/modules/random.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,9 @@ describe('random', () => {
});

it('should throw if all possible characters being banned', () => {
const bannedChars =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
const bannedChars = [
...'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
];
expect(() =>
faker.random.alpha({
count: 5,
Expand Down Expand Up @@ -256,7 +257,7 @@ describe('random', () => {
);

it('should be able to ban all alphabetic characters', () => {
const bannedChars = 'abcdefghijklmnopqrstuvwxyz'.split('');
const bannedChars = [...'abcdefghijklmnopqrstuvwxyz'];
const alphaText = faker.random.alphaNumeric(5, {
bannedChars,
});
Expand All @@ -280,7 +281,7 @@ describe('random', () => {
});

it('should be able to ban all numeric characters', () => {
const bannedChars = '0123456789'.split('');
const bannedChars = [...'0123456789'];
const alphaText = faker.random.alphaNumeric(5, {
bannedChars,
});
Expand Down Expand Up @@ -314,7 +315,7 @@ describe('random', () => {
});

it('should throw if all possible characters being banned', () => {
const bannedChars = 'abcdefghijklmnopqrstuvwxyz0123456789'.split('');
const bannedChars = [...'abcdefghijklmnopqrstuvwxyz0123456789'];
expect(() =>
faker.random.alphaNumeric(5, {
bannedChars,
Expand Down Expand Up @@ -396,7 +397,7 @@ describe('random', () => {
it('should allow leading zeros via option and all other digits banned', () => {
const actual = faker.random.numeric(4, {
allowLeadingZeros: true,
bannedDigits: '123456789'.split(''),
bannedDigits: [...'123456789'],
});

expect(actual).toBe('0000');
Expand All @@ -415,7 +416,7 @@ describe('random', () => {
expect(() =>
faker.random.numeric(4, {
allowLeadingZeros: false,
bannedDigits: '123456789'.split(''),
bannedDigits: [...'123456789'],
})
).toThrow(
new FakerError(
Expand All @@ -439,7 +440,7 @@ describe('random', () => {

it('should ban all digits passed via bannedDigits', () => {
const actual = faker.random.numeric(1000, {
bannedDigits: 'c84U1'.split(''),
bannedDigits: [...'c84U1'],
});

expect(actual).toHaveLength(1000);
Expand Down
Loading