diff --git a/.eslintrc.js b/.eslintrc.js index 5f83641a77c..b678af7419c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -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', diff --git a/src/internal/merge.ts b/src/internal/merge.ts index 88204e29cea..2bb4c035191 100644 --- a/src/internal/merge.ts +++ b/src/internal/merge.ts @@ -8,5 +8,5 @@ * @param args The arrays to merge. */ export function mergeArrays(...args: T[][]): T[] { - return Array.from(new Set(args.flat())).sort(); + return [...new Set(args.flat())].sort(); } diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 790a6925e1a..5855cd862e3 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -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'); diff --git a/src/modules/company/index.ts b/src/modules/company/index.ts index c24ea7f3b6b..361449618c0 100644 --- a/src/modules/company/index.ts +++ b/src/modules/company/index.ts @@ -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]; } /** diff --git a/src/modules/finance/index.ts b/src/modules/finance/index.ts index 43cd16a1c5e..ce4b2889edf 100644 --- a/src/modules/finance/index.ts +++ b/src/modules/finance/index.ts @@ -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; } diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index f4ceaaf4abe..ab0ec5b8b86 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -704,7 +704,7 @@ export class SimpleHelpersModule { uniqueArray(source: ReadonlyArray | (() => T), length: number): T[] { if (Array.isArray(source)) { const set = new Set(source); - const array = Array.from(set); + const array = [...set]; return this.shuffle(array).splice(0, length); } @@ -722,7 +722,7 @@ export class SimpleHelpersModule { // Ignore } - return Array.from(set); + return [...set]; } /** @@ -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; diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 6e8f822f22f..947edfbaab0 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -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) @@ -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]) { diff --git a/src/modules/string/index.ts b/src/modules/string/index.ts index 8167ebeafb7..a370c386c11 100644 --- a/src/modules/string/index.ts +++ b/src/modules/string/index.ts @@ -5,13 +5,9 @@ import type { LiteralUnion } from '../../utils/types'; export type Casing = 'upper' | 'lower' | 'mixed'; -const UPPER_CHARS: ReadonlyArray = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split( - '' -); -const LOWER_CHARS: ReadonlyArray = 'abcdefghijklmnopqrstuvwxyz'.split( - '' -); -const DIGIT_CHARS: ReadonlyArray = '0123456789'.split(''); +const UPPER_CHARS: ReadonlyArray = [...'ABCDEFGHIJKLMNOPQRSTUVWXYZ']; +const LOWER_CHARS: ReadonlyArray = [...'abcdefghijklmnopqrstuvwxyz']; +const DIGIT_CHARS: ReadonlyArray = [...'0123456789']; export type LowerAlphaChar = | 'a' @@ -145,7 +141,7 @@ export class StringModule { } if (typeof characters === 'string') { - characters = characters.split(''); + characters = [...characters]; } if (characters.length === 0) { @@ -229,7 +225,7 @@ export class StringModule { let { exclude = [] } = options; if (typeof exclude === 'string') { - exclude = exclude.split(''); + exclude = [...exclude]; } let charsArray: string[]; @@ -319,7 +315,7 @@ export class StringModule { let { exclude = [] } = options; if (typeof exclude === 'string') { - exclude = exclude.split(''); + exclude = [...exclude]; } let charsArray = [...DIGIT_CHARS]; @@ -596,7 +592,7 @@ export class StringModule { let { exclude = [] } = options; if (typeof exclude === 'string') { - exclude = exclude.split(''); + exclude = [...exclude]; } const allowedDigits = DIGIT_CHARS.filter( diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 89fd23f1e82..8a21a39b567 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -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]); } /** @@ -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]); } /** diff --git a/test/modules/datatype.spec.ts b/test/modules/datatype.spec.ts index e3e052ef994..964636bc995 100644 --- a/test/modules/datatype.spec.ts +++ b/test/modules/datatype.spec.ts @@ -164,8 +164,8 @@ 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, @@ -173,16 +173,16 @@ describe('datatype', () => { 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, @@ -190,8 +190,8 @@ describe('datatype', () => { precision: 0.4, }) ) - ) - ).sort(); + ), + ].sort(); expect(results).toEqual([0, 0.4, 0.8, 1.2, 1.6]); }); @@ -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]); }); diff --git a/test/modules/helpers.spec.ts b/test/modules/helpers.spec.ts index be023b152ce..0564a794b08 100644 --- a/test/modules/helpers.spec.ts +++ b/test/modules/helpers.spec.ts @@ -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) => { @@ -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) => { diff --git a/test/modules/number.spec.ts b/test/modules/number.spec.ts index 19bd8f393d2..79b14261e31 100644 --- a/test/modules/number.spec.ts +++ b/test/modules/number.spec.ts @@ -227,8 +227,8 @@ 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, @@ -236,15 +236,15 @@ describe('number', () => { 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, @@ -252,8 +252,8 @@ describe('number', () => { precision: 0.4, }) ) - ) - ).sort(); + ), + ].sort(); expect(results).toEqual([0, 0.4, 0.8, 1.2, 1.6]); }); diff --git a/test/modules/random.spec.ts b/test/modules/random.spec.ts index 21200c3a8f1..80fa10aa703 100644 --- a/test/modules/random.spec.ts +++ b/test/modules/random.spec.ts @@ -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, @@ -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, }); @@ -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, }); @@ -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, @@ -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'); @@ -415,7 +416,7 @@ describe('random', () => { expect(() => faker.random.numeric(4, { allowLeadingZeros: false, - bannedDigits: '123456789'.split(''), + bannedDigits: [...'123456789'], }) ).toThrow( new FakerError( @@ -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); diff --git a/test/modules/string.spec.ts b/test/modules/string.spec.ts index 4b885a3ab5d..9d2debba257 100644 --- a/test/modules/string.spec.ts +++ b/test/modules/string.spec.ts @@ -9,14 +9,14 @@ describe('string', () => { seededTests(faker, 'string', (t) => { t.describe('fromCharacters', (t) => { t.it('with string characters', 'foobar') - .it('with string[] characters', 'foobar'.split('')) + .it('with string[] characters', [...'foobar']) .it('with string characters and length', 'foobar', 5) - .it('with string[] characters and length', 'foobar'.split(''), 5) + .it('with string[] characters and length', [...'foobar'], 5) .it('with string characters and length range', 'foobar', { min: 10, max: 20, }) - .it('with string[] characters and length range', 'foobar'.split(''), { + .it('with string[] characters and length range', [...'foobar'], { min: 10, max: 20, }); @@ -283,7 +283,7 @@ describe('string', () => { }); it('should throw if all possible characters being excluded (string[])', () => { - const exclude = 'abcdefghijklmnopqrstuvwxyz'.split(''); + const exclude = [...'abcdefghijklmnopqrstuvwxyz']; expect(() => faker.string.alpha({ length: 5, @@ -357,7 +357,7 @@ describe('string', () => { }); it('should be able to ban all alphabetic characters', () => { - const exclude = 'abcdefghijklmnopqrstuvwxyz'.split(''); + const exclude = [...'abcdefghijklmnopqrstuvwxyz']; const alphaText = faker.string.alphanumeric({ length: 5, casing: 'lower', @@ -385,7 +385,7 @@ describe('string', () => { }); it('should be able to ban all numeric characters', () => { - const exclude = '0123456789'.split(''); + const exclude = [...'0123456789']; const alphaText = faker.string.alphanumeric({ length: 5, exclude, @@ -437,7 +437,7 @@ describe('string', () => { }); it('should throw if all possible characters being excluded (string[])', () => { - const exclude = 'abcdefghijklmnopqrstuvwxyz0123456789'.split(''); + const exclude = [...'abcdefghijklmnopqrstuvwxyz0123456789']; expect(() => faker.string.alphanumeric({ length: 5, @@ -645,7 +645,7 @@ describe('string', () => { const actual = faker.string.numeric({ length: 4, allowLeadingZeros: true, - exclude: '123456789'.split(''), + exclude: [...'123456789'], }); expect(actual).toBe('0000'); @@ -666,7 +666,7 @@ describe('string', () => { faker.string.numeric({ length: 4, allowLeadingZeros: false, - exclude: '123456789'.split(''), + exclude: [...'123456789'], }) ).toThrow( new FakerError( @@ -692,7 +692,7 @@ describe('string', () => { it('should ban all digits passed via exclude', () => { const actual = faker.string.numeric({ length: 1000, - exclude: 'c84U1'.split(''), + exclude: [...'c84U1'], }); expect(actual).toHaveLength(1000); diff --git a/test/modules/system.spec.ts b/test/modules/system.spec.ts index 1ce31c3c8dc..b325468d6f8 100644 --- a/test/modules/system.spec.ts +++ b/test/modules/system.spec.ts @@ -405,13 +405,11 @@ describe('system', () => { ); it('should be able to return non-standard cron expressions', () => { - const validResults = [...'0123456789'.split(''), '*', '@']; + const validResults = new Set('0123456789*@'); expect( faker.system.cron({ includeNonStandard: true })[0], 'generated cron, string should contain standard or non-standard cron labels' - ).toSatisfy( - (value) => !!validResults.find((result) => value === result) - ); + ).toSatisfy((value: string) => validResults.has(value)); }); }); } diff --git a/test/support/seededRuns.ts b/test/support/seededRuns.ts index eaf4520acab..4b014cb6d03 100644 --- a/test/support/seededRuns.ts +++ b/test/support/seededRuns.ts @@ -289,7 +289,7 @@ class TestGenerator< * This method will be called automatically at the end of each run. */ expectAllMethodsToBeTested(): void { - const actual = Array.from(this.tested).sort(); + const actual = [...this.tested].sort(); const expected = Object.entries(this.module) .filter(([, value]) => typeof value === 'function') .map(([key]) => key)