Skip to content

Commit

Permalink
Merge branch 'next' into infra/unicorn/escape-case
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Oct 17, 2023
2 parents 54884e8 + b8f31f6 commit c6ff3c9
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 96 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ module.exports = defineConfig({
'unicorn/no-array-reduce': 'off',
'unicorn/no-await-expression-member': 'off',
'unicorn/no-for-loop': 'off',
'unicorn/no-instanceof-array': 'off',
'unicorn/no-negated-condition': 'off',
'unicorn/no-object-as-default-parameter': 'off',
'unicorn/no-useless-switch-case': 'off',
Expand All @@ -69,7 +68,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 Expand Up @@ -112,6 +110,7 @@ module.exports = defineConfig({
'error',
{ blankLine: 'always', prev: 'block-like', next: '*' },
],
'@typescript-eslint/prefer-regexp-exec': 'error',
'@typescript-eslint/restrict-template-expressions': [
'error',
{ allowNumber: true, allowBoolean: true },
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
28 changes: 14 additions & 14 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ export class SimpleHelpersModule {
if (pattern instanceof RegExp) {
isCaseInsensitive = pattern.flags.includes('i');
pattern = pattern.toString();
pattern = pattern.match(/\/(.+?)\//)?.[1] ?? ''; // Remove frontslash from front and back of RegExp
pattern = /\/(.+?)\//.exec(pattern)?.[1] ?? ''; // Remove frontslash from front and back of RegExp
}

let min: number;
Expand All @@ -417,7 +417,7 @@ export class SimpleHelpersModule {
// Deal with single wildcards
const SINGLE_CHAR_REG =
/([.A-Za-z0-9])(?:\{(\d+)(?:,(\d+)|)\}|(\?|\*|\+))(?![^[]*]|[^{]*})/;
let token = pattern.match(SINGLE_CHAR_REG);
let token = SINGLE_CHAR_REG.exec(pattern);
while (token != null) {
const quantifierMin: string = token[2];
const quantifierMax: string = token[3];
Expand All @@ -434,14 +434,14 @@ export class SimpleHelpersModule {
pattern.slice(0, token.index) +
token[1].repeat(repetitions) +
pattern.slice(token.index + token[0].length);
token = pattern.match(SINGLE_CHAR_REG);
token = SINGLE_CHAR_REG.exec(pattern);
}

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

let ranges = token[3];
let range = ranges.match(SINGLE_RANGE_REG);
let range = SINGLE_RANGE_REG.exec(ranges);

if (includesDash) {
// 45 is the ascii code for '-'
Expand Down Expand Up @@ -494,7 +494,7 @@ export class SimpleHelpersModule {
}

ranges = ranges.substring(range[0].length);
range = ranges.match(SINGLE_RANGE_REG);
range = SINGLE_RANGE_REG.exec(ranges);
}

repetitions = getRepetitionsBasedOnQuantifierParameters(
Expand Down Expand Up @@ -549,12 +549,12 @@ export class SimpleHelpersModule {
pattern.slice(0, token.index) +
generatedString +
pattern.slice(token.index + token[0].length);
token = pattern.match(RANGE_ALPHANUMEMRIC_REG);
token = RANGE_ALPHANUMEMRIC_REG.exec(pattern);
}

const RANGE_REP_REG = /(.)\{(\d+),(\d+)\}/;
// Deal with quantifier ranges `{min,max}`
token = pattern.match(RANGE_REP_REG);
token = RANGE_REP_REG.exec(pattern);
while (token != null) {
min = parseInt(token[2]);
max = parseInt(token[3]);
Expand All @@ -568,19 +568,19 @@ export class SimpleHelpersModule {
pattern.slice(0, token.index) +
token[1].repeat(repetitions) +
pattern.slice(token.index + token[0].length);
token = pattern.match(RANGE_REP_REG);
token = RANGE_REP_REG.exec(pattern);
}

const REP_REG = /(.)\{(\d+)\}/;
// Deal with repeat `{num}`
token = pattern.match(REP_REG);
token = REP_REG.exec(pattern);
while (token != null) {
repetitions = parseInt(token[2]);
pattern =
pattern.slice(0, token.index) +
token[1].repeat(repetitions) +
pattern.slice(token.index + token[0].length);
token = pattern.match(REP_REG);
token = REP_REG.exec(pattern);
}

return pattern;
Expand Down 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
8 changes: 4 additions & 4 deletions test/modules/airline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('airline', () => {
const seatRegex = /^(\d{1,2})([A-K])$/;
it('should return a random narrowbody seat when not passing an argument', () => {
const seat = faker.airline.seat();
const matchResult = seat.match(seatRegex);
const matchResult = seatRegex.exec(seat);
expect(matchResult).not.toBeNull();
const row = matchResult[1];
const seatLetter = matchResult[2];
Expand All @@ -106,7 +106,7 @@ describe('airline', () => {
const seat = faker.airline.seat({
aircraftType: Aircraft.Narrowbody,
});
const matchResult = seat.match(seatRegex);
const matchResult = seatRegex.exec(seat);
expect(matchResult).not.toBeNull();
const row = matchResult[1];
const seatLetter = matchResult[2];
Expand All @@ -115,7 +115,7 @@ describe('airline', () => {
});
it('should return a random regional seat', () => {
const seat = faker.airline.seat({ aircraftType: Aircraft.Regional });
const matchResult = seat.match(seatRegex);
const matchResult = seatRegex.exec(seat);
expect(matchResult).not.toBeNull();
const row = matchResult[1];
const seatLetter = matchResult[2];
Expand All @@ -124,7 +124,7 @@ describe('airline', () => {
});
it('should return a random widebody seat', () => {
const seat = faker.airline.seat({ aircraftType: Aircraft.Widebody });
const matchResult = seat.match(seatRegex);
const matchResult = seatRegex.exec(seat);
expect(matchResult).not.toBeNull();
const row = matchResult[1];
const seatLetter = matchResult[2];
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
Loading

0 comments on commit c6ff3c9

Please sign in to comment.