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-number-properties #2452

Merged
merged 11 commits into from
Oct 21, 2023
Merged
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ module.exports = defineConfig({
'unicorn/prefer-module': 'off',
'unicorn/prefer-native-coercion-functions': 'off',
'unicorn/prefer-negative-index': 'off',
'unicorn/prefer-number-properties': 'off',
'unicorn/prefer-optional-catch-binding': 'off',
'unicorn/prefer-string-slice': 'off',
'unicorn/prefer-ternary': 'off',
Expand Down
4 changes: 2 additions & 2 deletions src/modules/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export class CommerceModule {
const [group, groupRules] =
this.faker.helpers.objectEntry(ISBN_LENGTH_RULES);
const element = this.faker.string.numeric(8);
const elementValue = parseInt(element.slice(0, -1));
const elementValue = Number.parseInt(element.slice(0, -1));

const registrantLength = groupRules.find(
([rangeMaximum]) => elementValue <= rangeMaximum
Expand All @@ -401,7 +401,7 @@ export class CommerceModule {
let checksum = 0;
for (let i = 0; i < variant - 1; i++) {
const weight = variant === 10 ? i + 1 : i % 2 ? 3 : 1;
checksum += weight * parseInt(isbn[i]);
checksum += weight * Number.parseInt(isbn[i]);
}

checksum = variant === 10 ? checksum % 11 : (10 - (checksum % 10)) % 10;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function toDate(
fallback: () => Date
): Date {
date = new Date(date);
if (isNaN(date.valueOf())) {
if (Number.isNaN(date.valueOf())) {
date = fallback();
}

Expand Down
31 changes: 17 additions & 14 deletions src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ function getRepetitionsBasedOnQuantifierParameters(
}
} else if (quantifierMin != null && quantifierMax != null) {
repetitions = faker.number.int({
min: parseInt(quantifierMin),
max: parseInt(quantifierMax),
min: Number.parseInt(quantifierMin),
max: Number.parseInt(quantifierMax),
});
} else if (quantifierMin != null && quantifierMax == null) {
repetitions = parseInt(quantifierMin);
repetitions = Number.parseInt(quantifierMin);
}

return repetitions;
Expand Down Expand Up @@ -108,8 +108,8 @@ function legacyRegexpStringParse(
let repetitions: number;
let token = RANGE_REP_REG.exec(string);
while (token != null) {
min = parseInt(token[2]);
max = parseInt(token[3]);
min = Number.parseInt(token[2]);
max = Number.parseInt(token[3]);
// switch min and max
if (min > max) {
tmp = max;
Expand All @@ -128,7 +128,7 @@ function legacyRegexpStringParse(
// Deal with repeat `{num}`
token = REP_REG.exec(string);
while (token != null) {
repetitions = parseInt(token[2]);
repetitions = Number.parseInt(token[2]);
string =
string.slice(0, token.index) +
token[1].repeat(repetitions) +
Expand All @@ -139,8 +139,8 @@ function legacyRegexpStringParse(

token = RANGE_REG.exec(string);
while (token != null) {
min = parseInt(token[1]); // This time we are not capturing the char before `[]`
max = parseInt(token[2]);
min = Number.parseInt(token[1]); // This time we are not capturing the char before `[]`
max = Number.parseInt(token[2]);
// switch min and max
if (min > max) {
tmp = max;
Expand Down Expand Up @@ -462,7 +462,7 @@ export class SimpleHelpersModule {
while (range != null) {
if (!range[0].includes('-')) {
// handle non-ranges
if (isCaseInsensitive && isNaN(Number(range[0]))) {
if (isCaseInsensitive && Number.isNaN(Number(range[0]))) {
rangeCodes.push(
range[0].toUpperCase().charCodeAt(0),
range[0].toLowerCase().charCodeAt(0)
Expand All @@ -481,7 +481,10 @@ export class SimpleHelpersModule {
}

for (let i = min; i <= max; i++) {
if (isCaseInsensitive && isNaN(Number(String.fromCharCode(i)))) {
if (
isCaseInsensitive &&
Number.isNaN(Number(String.fromCharCode(i)))
) {
const ch = String.fromCharCode(i);
rangeCodes.push(
ch.toUpperCase().charCodeAt(0),
Expand Down Expand Up @@ -556,8 +559,8 @@ export class SimpleHelpersModule {
// Deal with quantifier ranges `{min,max}`
token = RANGE_REP_REG.exec(pattern);
while (token != null) {
min = parseInt(token[2]);
max = parseInt(token[3]);
min = Number.parseInt(token[2]);
max = Number.parseInt(token[3]);
// throw error if min larger than max
if (min > max) {
throw new FakerError('Numbers out of order in {} quantifier.');
Expand All @@ -575,7 +578,7 @@ export class SimpleHelpersModule {
// Deal with repeat `{num}`
token = REP_REG.exec(pattern);
while (token != null) {
repetitions = parseInt(token[2]);
repetitions = Number.parseInt(token[2]);
pattern =
pattern.slice(0, token.index) +
token[1].repeat(repetitions) +
Expand Down Expand Up @@ -1046,7 +1049,7 @@ export class SimpleHelpersModule {
): T[keyof T] {
// ignore numeric keys added by TypeScript
const keys: Array<keyof T> = Object.keys(enumObject).filter((key) =>
isNaN(Number(key))
Number.isNaN(Number(key))
);
const randomKey = this.arrayElement(keys);
return enumObject[randomKey];
Expand Down
2 changes: 1 addition & 1 deletion src/modules/helpers/luhn-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function luhnChecksum(str: string): number {
let sum = 0;
let alternate = false;
for (let i = str.length - 1; i >= 0; i--) {
let n = parseInt(str.substring(i, i + 1));
let n = Number.parseInt(str.substring(i, i + 1));
if (alternate) {
n *= 2;
if (n > 9) {
Expand Down
10 changes: 5 additions & 5 deletions test/modules/number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ describe('number', () => {
expect(binary).toBeTypeOf('string');
expect(binary).toSatisfy(isBinary);

const binaryNum = parseInt(binary, 2);
const binaryNum = Number.parseInt(binary, 2);
expect(binaryNum).toBeLessThanOrEqual(5);
});

Expand All @@ -329,7 +329,7 @@ describe('number', () => {
expect(binary).toBeTypeOf('string');
expect(binary).toSatisfy(isBinary);

const binaryNum = parseInt(binary, 2);
const binaryNum = Number.parseInt(binary, 2);
expect(binaryNum).toBeLessThanOrEqual(255);
expect(binaryNum).greaterThanOrEqual(15);
});
Expand Down Expand Up @@ -362,7 +362,7 @@ describe('number', () => {
expect(octal).toBeTypeOf('string');
expect(octal).toSatisfy(validator.isOctal);

const octalNum = parseInt(octal, 8);
const octalNum = Number.parseInt(octal, 8);
expect(octalNum).toBeLessThanOrEqual(5);
});

Expand All @@ -372,7 +372,7 @@ describe('number', () => {
expect(octal).toBeTypeOf('string');
expect(octal).toSatisfy(validator.isOctal);

const octalNum = parseInt(octal, 8);
const octalNum = Number.parseInt(octal, 8);
expect(octalNum).toBeLessThanOrEqual(255);
expect(octalNum).greaterThanOrEqual(15);
});
Expand Down Expand Up @@ -412,7 +412,7 @@ describe('number', () => {
expect(hex).toBeTypeOf('string');
expect(hex).toSatisfy(validator.isHexadecimal);

const hexNum = parseInt(hex, 16);
const hexNum = Number.parseInt(hex, 16);
expect(hexNum).toBeLessThanOrEqual(255);
expect(hexNum).greaterThanOrEqual(15);
});
Expand Down