Skip to content

Commit c4affd6

Browse files
authored
Merge branch 'next' into infra/unicorn/no-instanceof-array
2 parents 9a81b3c + 980f230 commit c4affd6

File tree

17 files changed

+87
-95
lines changed

17 files changed

+87
-95
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ module.exports = defineConfig({
6969
'unicorn/prefer-negative-index': 'off',
7070
'unicorn/prefer-number-properties': 'off',
7171
'unicorn/prefer-optional-catch-binding': 'off',
72-
'unicorn/prefer-spread': 'off',
7372
'unicorn/prefer-string-slice': 'off',
7473
'unicorn/prefer-ternary': 'off',
7574
'unicorn/prefer-top-level-await': 'off',
@@ -112,6 +111,7 @@ module.exports = defineConfig({
112111
'error',
113112
{ blankLine: 'always', prev: 'block-like', next: '*' },
114113
],
114+
'@typescript-eslint/prefer-regexp-exec': 'error',
115115
'@typescript-eslint/restrict-template-expressions': [
116116
'error',
117117
{ allowNumber: true, allowBoolean: true },

src/internal/merge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
* @param args The arrays to merge.
99
*/
1010
export function mergeArrays<T>(...args: T[][]): T[] {
11-
return Array.from(new Set(args.flat())).sort();
11+
return [...new Set(args.flat())].sort();
1212
}

src/modules/color/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function toBinary(values: number[]): string {
8686
const buffer = new ArrayBuffer(4);
8787
new DataView(buffer).setFloat32(0, value);
8888
const bytes = new Uint8Array(buffer);
89-
return toBinary(Array.from(bytes)).split(' ').join('');
89+
return toBinary([...bytes]).replace(/ /g, '');
9090
}
9191

9292
return (value >>> 0).toString(2).padStart(8, '0');

src/modules/company/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class CompanyModule {
4242
});
4343
// Don't want the source array exposed to modification, so return a copy
4444
// eslint-disable-next-line deprecation/deprecation
45-
return this.faker.definitions.company.suffix.slice(0);
45+
return [...this.faker.definitions.company.suffix];
4646
}
4747

4848
/**

src/modules/finance/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -722,9 +722,9 @@ export class FinanceModule {
722722
let address = this.faker.helpers.arrayElement(['L', 'M', '3']);
723723

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

729729
return address;
730730
}

src/modules/helpers/index.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ export class SimpleHelpersModule {
407407
if (pattern instanceof RegExp) {
408408
isCaseInsensitive = pattern.flags.includes('i');
409409
pattern = pattern.toString();
410-
pattern = pattern.match(/\/(.+?)\//)?.[1] ?? ''; // Remove frontslash from front and back of RegExp
410+
pattern = /\/(.+?)\//.exec(pattern)?.[1] ?? ''; // Remove frontslash from front and back of RegExp
411411
}
412412

413413
let min: number;
@@ -417,7 +417,7 @@ export class SimpleHelpersModule {
417417
// Deal with single wildcards
418418
const SINGLE_CHAR_REG =
419419
/([.A-Za-z0-9])(?:\{(\d+)(?:,(\d+)|)\}|(\?|\*|\+))(?![^[]*]|[^{]*})/;
420-
let token = pattern.match(SINGLE_CHAR_REG);
420+
let token = SINGLE_CHAR_REG.exec(pattern);
421421
while (token != null) {
422422
const quantifierMin: string = token[2];
423423
const quantifierMax: string = token[3];
@@ -434,14 +434,14 @@ export class SimpleHelpersModule {
434434
pattern.slice(0, token.index) +
435435
token[1].repeat(repetitions) +
436436
pattern.slice(token.index + token[0].length);
437-
token = pattern.match(SINGLE_CHAR_REG);
437+
token = SINGLE_CHAR_REG.exec(pattern);
438438
}
439439

440440
const SINGLE_RANGE_REG = /(\d-\d|\w-\w|\d|\w|[-!@#$&()`.+,/"])/;
441441
const RANGE_ALPHANUMEMRIC_REG =
442442
/\[(\^|)(-|)(.+?)\](?:\{(\d+)(?:,(\d+)|)\}|(\?|\*|\+)|)/;
443443
// Deal with character classes with quantifiers `[a-z0-9]{min[, max]}`
444-
token = pattern.match(RANGE_ALPHANUMEMRIC_REG);
444+
token = RANGE_ALPHANUMEMRIC_REG.exec(pattern);
445445
while (token != null) {
446446
const isNegated = token[1] === '^';
447447
const includesDash: boolean = token[2] === '-';
@@ -452,7 +452,7 @@ export class SimpleHelpersModule {
452452
const rangeCodes: number[] = [];
453453

454454
let ranges = token[3];
455-
let range = ranges.match(SINGLE_RANGE_REG);
455+
let range = SINGLE_RANGE_REG.exec(ranges);
456456

457457
if (includesDash) {
458458
// 45 is the ascii code for '-'
@@ -494,7 +494,7 @@ export class SimpleHelpersModule {
494494
}
495495

496496
ranges = ranges.substring(range[0].length);
497-
range = ranges.match(SINGLE_RANGE_REG);
497+
range = SINGLE_RANGE_REG.exec(ranges);
498498
}
499499

500500
repetitions = getRepetitionsBasedOnQuantifierParameters(
@@ -549,12 +549,12 @@ export class SimpleHelpersModule {
549549
pattern.slice(0, token.index) +
550550
generatedString +
551551
pattern.slice(token.index + token[0].length);
552-
token = pattern.match(RANGE_ALPHANUMEMRIC_REG);
552+
token = RANGE_ALPHANUMEMRIC_REG.exec(pattern);
553553
}
554554

555555
const RANGE_REP_REG = /(.)\{(\d+),(\d+)\}/;
556556
// Deal with quantifier ranges `{min,max}`
557-
token = pattern.match(RANGE_REP_REG);
557+
token = RANGE_REP_REG.exec(pattern);
558558
while (token != null) {
559559
min = parseInt(token[2]);
560560
max = parseInt(token[3]);
@@ -568,19 +568,19 @@ export class SimpleHelpersModule {
568568
pattern.slice(0, token.index) +
569569
token[1].repeat(repetitions) +
570570
pattern.slice(token.index + token[0].length);
571-
token = pattern.match(RANGE_REP_REG);
571+
token = RANGE_REP_REG.exec(pattern);
572572
}
573573

574574
const REP_REG = /(.)\{(\d+)\}/;
575575
// Deal with repeat `{num}`
576-
token = pattern.match(REP_REG);
576+
token = REP_REG.exec(pattern);
577577
while (token != null) {
578578
repetitions = parseInt(token[2]);
579579
pattern =
580580
pattern.slice(0, token.index) +
581581
token[1].repeat(repetitions) +
582582
pattern.slice(token.index + token[0].length);
583-
token = pattern.match(REP_REG);
583+
token = REP_REG.exec(pattern);
584584
}
585585

586586
return pattern;
@@ -704,7 +704,7 @@ export class SimpleHelpersModule {
704704
uniqueArray<T>(source: ReadonlyArray<T> | (() => T), length: number): T[] {
705705
if (Array.isArray(source)) {
706706
const set = new Set<T>(source);
707-
const array = Array.from(set);
707+
const array = [...set];
708708
return this.shuffle(array).splice(0, length);
709709
}
710710

@@ -722,7 +722,7 @@ export class SimpleHelpersModule {
722722
// Ignore
723723
}
724724

725-
return Array.from(set);
725+
return [...set];
726726
}
727727

728728
/**
@@ -1003,7 +1003,7 @@ export class SimpleHelpersModule {
10031003
return [];
10041004
}
10051005

1006-
const arrayCopy = array.slice(0);
1006+
const arrayCopy = [...array];
10071007
let i = array.length;
10081008
const min = i - numElements;
10091009
let temp: T;

src/modules/internet/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ export class InternetModule {
271271
// We limit to 50 chars to be more realistic
272272
localPart = localPart.substring(0, 50);
273273
if (allowSpecialCharacters) {
274-
const usernameChars: string[] = '._-'.split('');
275-
const specialChars: string[] = ".!#$%&'*+-/=?^_`{|}~".split('');
274+
const usernameChars: string[] = [...'._-'];
275+
const specialChars: string[] = [...".!#$%&'*+-/=?^_`{|}~"];
276276
localPart = localPart.replace(
277277
this.faker.helpers.arrayElement(usernameChars),
278278
this.faker.helpers.arrayElement(specialChars)
@@ -638,8 +638,7 @@ export class InternetModule {
638638
.normalize('NFKD') //for example è decomposes to as e + ̀
639639
.replace(/[\u0300-\u036f]/g, ''); // removes combining marks
640640

641-
result = result
642-
.split('')
641+
result = [...result]
643642
.map((char) => {
644643
// If we have a mapping for this character, (for Cyrillic, Greek etc) use it
645644
if (charMapping[char]) {

src/modules/string/index.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ import type { LiteralUnion } from '../../utils/types';
55

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

8-
const UPPER_CHARS: ReadonlyArray<string> = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(
9-
''
10-
);
11-
const LOWER_CHARS: ReadonlyArray<string> = 'abcdefghijklmnopqrstuvwxyz'.split(
12-
''
13-
);
14-
const DIGIT_CHARS: ReadonlyArray<string> = '0123456789'.split('');
8+
const UPPER_CHARS: ReadonlyArray<string> = [...'ABCDEFGHIJKLMNOPQRSTUVWXYZ'];
9+
const LOWER_CHARS: ReadonlyArray<string> = [...'abcdefghijklmnopqrstuvwxyz'];
10+
const DIGIT_CHARS: ReadonlyArray<string> = [...'0123456789'];
1511

1612
export type LowerAlphaChar =
1713
| 'a'
@@ -145,7 +141,7 @@ export class StringModule {
145141
}
146142

147143
if (typeof characters === 'string') {
148-
characters = characters.split('');
144+
characters = [...characters];
149145
}
150146

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

231227
if (typeof exclude === 'string') {
232-
exclude = exclude.split('');
228+
exclude = [...exclude];
233229
}
234230

235231
let charsArray: string[];
@@ -319,7 +315,7 @@ export class StringModule {
319315
let { exclude = [] } = options;
320316

321317
if (typeof exclude === 'string') {
322-
exclude = exclude.split('');
318+
exclude = [...exclude];
323319
}
324320

325321
let charsArray = [...DIGIT_CHARS];
@@ -596,7 +592,7 @@ export class StringModule {
596592
let { exclude = [] } = options;
597593

598594
if (typeof exclude === 'string') {
599-
exclude = exclude.split('');
595+
exclude = [...exclude];
600596
}
601597

602598
const allowedDigits = DIGIT_CHARS.filter(

src/modules/system/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ export class SystemModule {
159159
const typeSet = new Set(
160160
Object.keys(mimeTypes).map((key) => key.split('/')[0])
161161
);
162-
const types = Array.from(typeSet);
163-
return this.faker.helpers.arrayElement(types);
162+
return this.faker.helpers.arrayElement([...typeSet]);
164163
}
165164

166165
/**
@@ -184,8 +183,7 @@ export class SystemModule {
184183
const extensionSet = new Set(
185184
Object.values(mimeTypes).flatMap(({ extensions }) => extensions)
186185
);
187-
const extensions = Array.from(extensionSet);
188-
return this.faker.helpers.arrayElement(extensions);
186+
return this.faker.helpers.arrayElement([...extensionSet]);
189187
}
190188

191189
/**

test/modules/airline.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ describe('airline', () => {
9595
const seatRegex = /^(\d{1,2})([A-K])$/;
9696
it('should return a random narrowbody seat when not passing an argument', () => {
9797
const seat = faker.airline.seat();
98-
const matchResult = seat.match(seatRegex);
98+
const matchResult = seatRegex.exec(seat);
9999
expect(matchResult).not.toBeNull();
100100
const row = matchResult[1];
101101
const seatLetter = matchResult[2];
@@ -106,7 +106,7 @@ describe('airline', () => {
106106
const seat = faker.airline.seat({
107107
aircraftType: Aircraft.Narrowbody,
108108
});
109-
const matchResult = seat.match(seatRegex);
109+
const matchResult = seatRegex.exec(seat);
110110
expect(matchResult).not.toBeNull();
111111
const row = matchResult[1];
112112
const seatLetter = matchResult[2];
@@ -115,7 +115,7 @@ describe('airline', () => {
115115
});
116116
it('should return a random regional seat', () => {
117117
const seat = faker.airline.seat({ aircraftType: Aircraft.Regional });
118-
const matchResult = seat.match(seatRegex);
118+
const matchResult = seatRegex.exec(seat);
119119
expect(matchResult).not.toBeNull();
120120
const row = matchResult[1];
121121
const seatLetter = matchResult[2];
@@ -124,7 +124,7 @@ describe('airline', () => {
124124
});
125125
it('should return a random widebody seat', () => {
126126
const seat = faker.airline.seat({ aircraftType: Aircraft.Widebody });
127-
const matchResult = seat.match(seatRegex);
127+
const matchResult = seatRegex.exec(seat);
128128
expect(matchResult).not.toBeNull();
129129
const row = matchResult[1];
130130
const seatLetter = matchResult[2];

test/modules/datatype.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,34 +164,34 @@ describe('datatype', () => {
164164
});
165165

166166
it('provides numbers with a given precision of 0.5 steps', () => {
167-
const results = Array.from(
168-
new Set(
167+
const results = [
168+
...new Set(
169169
Array.from({ length: 50 }, () =>
170170
faker.datatype.float({
171171
min: 0,
172172
max: 1.5,
173173
precision: 0.5,
174174
})
175175
)
176-
)
177-
).sort();
176+
),
177+
].sort();
178178

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

182182
// TODO @Shinigami92 2022-11-24: https://github.com/faker-js/faker/issues/1595
183183
it.todo('provides numbers with a given precision of 0.4 steps', () => {
184-
const results = Array.from(
185-
new Set(
184+
const results = [
185+
...new Set(
186186
Array.from({ length: 50 }, () =>
187187
faker.datatype.float({
188188
min: 0,
189189
max: 1.9,
190190
precision: 0.4,
191191
})
192192
)
193-
)
194-
).sort();
193+
),
194+
].sort();
195195

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

280280
it('provides numbers with a given precision', () => {
281281
const options = { min: 0, max: 1.5, precision: 0.5 };
282-
const results = Array.from(
283-
new Set(
282+
const results = [
283+
...new Set(
284284
Array.from({ length: 50 }, () => faker.datatype.float(options))
285-
)
286-
).sort();
285+
),
286+
].sort();
287287

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

0 commit comments

Comments
 (0)