From 3c9b4f8a93579c2690f0445997a613afbacdc3c9 Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Mon, 9 Oct 2023 12:24:05 +0200 Subject: [PATCH] infra(unicorn): prefer-number-properties --- .eslintrc.js | 1 - src/modules/commerce/index.ts | 4 ++-- src/modules/date/index.ts | 2 +- src/modules/helpers/index.ts | 31 +++++++++++++++++-------------- src/modules/helpers/luhn-check.ts | 2 +- test/modules/number.spec.ts | 10 +++++----- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index bb135397802..fc64685a5c2 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -77,7 +77,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-spread': 'off', 'unicorn/prefer-string-slice': 'off', diff --git a/src/modules/commerce/index.ts b/src/modules/commerce/index.ts index 2464af03020..18147dcb2e8 100644 --- a/src/modules/commerce/index.ts +++ b/src/modules/commerce/index.ts @@ -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 @@ -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; diff --git a/src/modules/date/index.ts b/src/modules/date/index.ts index 8f7efaaeed4..e93be54d90f 100644 --- a/src/modules/date/index.ts +++ b/src/modules/date/index.ts @@ -16,7 +16,7 @@ function toDate( fallback: () => Date ): Date { date = new Date(date); - if (isNaN(date.valueOf())) { + if (Number.isNaN(date.valueOf())) { date = fallback(); } diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 3cc9e85590e..e50f21ba4ad 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -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; @@ -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; @@ -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) + @@ -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; @@ -462,7 +462,7 @@ export class SimpleHelpersModule { while (range != null) { if (range[0].indexOf('-') === -1) { // handle non-ranges - if (isCaseInsensitive && isNaN(Number(range[0]))) { + if (isCaseInsensitive && Number.isNaN(Number(range[0]))) { rangeCodes.push(range[0].toUpperCase().charCodeAt(0)); rangeCodes.push(range[0].toLowerCase().charCodeAt(0)); } else { @@ -479,7 +479,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)); rangeCodes.push(ch.toLowerCase().charCodeAt(0)); @@ -552,8 +555,8 @@ export class SimpleHelpersModule { // Deal with quantifier ranges `{min,max}` token = pattern.match(RANGE_REP_REG); 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.'); @@ -571,7 +574,7 @@ export class SimpleHelpersModule { // Deal with repeat `{num}` token = pattern.match(REP_REG); while (token != null) { - repetitions = parseInt(token[2]); + repetitions = Number.parseInt(token[2]); pattern = pattern.slice(0, token.index) + token[1].repeat(repetitions) + @@ -1042,7 +1045,7 @@ export class SimpleHelpersModule { ): T[keyof T] { // ignore numeric keys added by TypeScript const keys: Array = Object.keys(enumObject).filter((key) => - isNaN(Number(key)) + Number.isNaN(Number(key)) ); const randomKey = this.arrayElement(keys); return enumObject[randomKey]; diff --git a/src/modules/helpers/luhn-check.ts b/src/modules/helpers/luhn-check.ts index 7d841878261..ba68b41d470 100644 --- a/src/modules/helpers/luhn-check.ts +++ b/src/modules/helpers/luhn-check.ts @@ -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) { diff --git a/test/modules/number.spec.ts b/test/modules/number.spec.ts index 19bd8f393d2..cc343952e44 100644 --- a/test/modules/number.spec.ts +++ b/test/modules/number.spec.ts @@ -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); }); @@ -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); }); @@ -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); }); @@ -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); }); @@ -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); });