From 95df3522a15631abc51a4295cae0ea401a8d4e1e Mon Sep 17 00:00:00 2001 From: Markus Hatvan Date: Tue, 14 Sep 2021 19:58:52 +0200 Subject: feat: add eslint-plugin-unicorn (#1936) --- src/helpers/i18n-helpers.ts | 12 +++++------- src/helpers/password-helpers.ts | 14 +++++++------- src/helpers/recipe-helpers.ts | 14 ++++++-------- src/helpers/schedule-helpers.ts | 8 ++++---- src/helpers/url-helpers.ts | 2 +- src/helpers/userAgent-helpers.ts | 2 +- src/helpers/validation-helpers.ts | 19 +++++++++---------- 7 files changed, 33 insertions(+), 38 deletions(-) (limited to 'src/helpers') diff --git a/src/helpers/i18n-helpers.ts b/src/helpers/i18n-helpers.ts index c1f18f446..ec7dc8e98 100644 --- a/src/helpers/i18n-helpers.ts +++ b/src/helpers/i18n-helpers.ts @@ -4,13 +4,11 @@ export function getLocale({ let localeStr = locale; if (locales[locale] === undefined) { let localeFuzzy: string | undefined; - Object.keys(locales).forEach((localStr) => { - if (locales && Object.hasOwnProperty.call(locales, localStr)) { - if (locale.substring(0, 2) === localStr.substring(0, 2)) { + for (const localStr of Object.keys(locales)) { + if (locales && Object.hasOwnProperty.call(locales, localStr) && locale.slice(0, 2) === localStr.slice(0, 2)) { localeFuzzy = localStr; } - } - }); + } if (localeFuzzy !== undefined) { localeStr = localeFuzzy; @@ -61,12 +59,12 @@ export function getSelectOptions({ if (sort) { keys = keys.sort(Intl.Collator().compare); } - keys.forEach((key) => { + for (const key of keys) { options.push({ value: key, label: locales[key], }); - }); + } return options; } diff --git a/src/helpers/password-helpers.ts b/src/helpers/password-helpers.ts index 89c75c752..e5d9a4a25 100644 --- a/src/helpers/password-helpers.ts +++ b/src/helpers/password-helpers.ts @@ -12,9 +12,9 @@ export function scorePassword(password: string) { // award every unique letter until 5 repetitions const letters = {}; - for (let i = 0; i < password.length; i += 1) { - letters[password[i]] = (letters[password[i]] || 0) + 1; - score += 5.0 / letters[password[i]]; + for (const letter of password) { + letters[letter] = (letters[letter] || 0) + 1; + score += 5 / letters[letter]; } // bonus points for mixing it up @@ -26,11 +26,11 @@ export function scorePassword(password: string) { }; let variationCount = 0; - Object.keys(variations).forEach((key) => { - variationCount += (variations[key] === true) ? 1 : 0; - }); + for (const key of Object.keys(variations)) { + variationCount += variations[key] === true ? 1 : 0; + } score += (variationCount - 1) * 10; - return parseInt(score.toString(), 10); + return Number.parseInt(score.toString(), 10); } diff --git a/src/helpers/recipe-helpers.ts b/src/helpers/recipe-helpers.ts index 965429210..65ef04088 100644 --- a/src/helpers/recipe-helpers.ts +++ b/src/helpers/recipe-helpers.ts @@ -1,3 +1,4 @@ +/* eslint-disable global-require */ import { parse } from 'path'; import { userDataRecipesPath } from '../environment'; @@ -15,20 +16,17 @@ export function loadRecipeConfig(recipeId: string) { // Delete module from cache delete require.cache[require.resolve(configPath)]; - // eslint-disable-next-line - let config = require(configPath); + // eslint-disable-next-line import/no-dynamic-require + const config = require(configPath); const moduleConfigPath = require.resolve(configPath); config.path = parse(moduleConfigPath).dir; return config; - } catch (e) { - console.error(e); + } catch (error) { + console.error(error); return null; } } -module.paths.unshift( - getDevRecipeDirectory(), - getRecipeDirectory(), -); +module.paths.unshift(getDevRecipeDirectory(), getRecipeDirectory()); diff --git a/src/helpers/schedule-helpers.ts b/src/helpers/schedule-helpers.ts index 754fd5556..55b7c1e6f 100644 --- a/src/helpers/schedule-helpers.ts +++ b/src/helpers/schedule-helpers.ts @@ -5,15 +5,15 @@ export function isInTimeframe(start: string, end: string) { startHourStr, startMinuteStr, ] = start.split(':'); - const startHour = parseInt(startHourStr, 10); - const startMinute = parseInt(startMinuteStr, 10); + const startHour = Number.parseInt(startHourStr, 10); + const startMinute = Number.parseInt(startMinuteStr, 10); const [ endHourStr, endMinuteStr, ] = end.split(':'); - const endHour = parseInt(endHourStr, 10); - const endMinute = parseInt(endMinuteStr, 10); + const endHour = Number.parseInt(endHourStr, 10); + const endMinute = Number.parseInt(endMinuteStr, 10); const currentHour = new Date().getHours(); const currentMinute = new Date().getMinutes(); diff --git a/src/helpers/url-helpers.ts b/src/helpers/url-helpers.ts index 3657ae693..1e87ecabb 100644 --- a/src/helpers/url-helpers.ts +++ b/src/helpers/url-helpers.ts @@ -12,7 +12,7 @@ export function isValidExternalURL(url: string | URL) { let parsedUrl: URL; try { parsedUrl = new URL(url.toString()); - } catch (_) { + } catch { return false; } diff --git a/src/helpers/userAgent-helpers.ts b/src/helpers/userAgent-helpers.ts index 73c8bfd03..091a76400 100644 --- a/src/helpers/userAgent-helpers.ts +++ b/src/helpers/userAgent-helpers.ts @@ -8,7 +8,7 @@ import { function macOS() { const version = macosVersion() || ''; let cpuName = os.cpus()[0].model.split(' ')[0]; - if (cpuName && cpuName.match(/\(/)) { + if (cpuName && /\(/.test(cpuName)) { cpuName = cpuName.split('(')[0]; } return `Macintosh; ${cpuName} Mac OS X ${version.replace(/\./g, '_')}`; diff --git a/src/helpers/validation-helpers.ts b/src/helpers/validation-helpers.ts index 3a9622309..23c297443 100644 --- a/src/helpers/validation-helpers.ts +++ b/src/helpers/validation-helpers.ts @@ -49,16 +49,15 @@ export function url({ field }) { const value = field.value.trim(); let isValid = false; - if (value !== '') { - // eslint-disable-next-line - isValid = Boolean( - value.match( - /(^|[\s.:;?\-\]<(])(https?:\/\/[-\w;/?:@&=+$|_.!~*|'()[\]%#,☺]+[\w/#](\(\))?)(?=$|[\s',|().:;?\-[\]>)])/i, - ), - ); - } else { - isValid = true; - } + isValid = + value !== '' + ? Boolean( + // eslint-disable-next-line unicorn/better-regex + /(^|[\s.:;?\-\]<(])(https?:\/\/[-\w;/?:@&=+$|_.!~*|'()[\]%#,☺]+[\w/#](\(\))?)(?=$|[\s',|().:;?\-[\]>)])/i.test( + value, + ), + ) + : true; return [ isValid, -- cgit v1.2.3-54-g00ecf