From a133ab0c649c574bf8555244c444f27bcb3f3fc9 Mon Sep 17 00:00:00 2001 From: PKief Date: Sun, 15 Mar 2020 19:08:51 +0100 Subject: [PATCH] Refactor code --- src/helpers/objects.ts | 2 +- src/i18n/index.ts | 7 +++---- src/icons/generator/jsonGenerator.ts | 24 ++++++++--------------- src/scripts/helpers/similarity.ts | 29 ++++++++++++++++------------ src/scripts/icons/generateJson.ts | 2 +- 5 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/helpers/objects.ts b/src/helpers/objects.ts index 63e87c4161..98e9a076b5 100644 --- a/src/helpers/objects.ts +++ b/src/helpers/objects.ts @@ -1,5 +1,5 @@ /** - * Get the nested properties of an object + * Get the nested properties of an object. * This solution is lighter than the lodash get-version. * Source: http://stackoverflow.com/a/6491621/6942210 */ diff --git a/src/i18n/index.ts b/src/i18n/index.ts index f187b56c76..39c460e91b 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -41,10 +41,9 @@ const getTranslationObject = async (language: string) => { * and the fallback (required for testing purposes). * */ export const getTranslationValue = (key: string, translations = currentTranslation, fallback = fallbackTranslation) => { - return getObjectPropertyValue(translations, key) ? - getObjectPropertyValue(translations, key) : - getObjectPropertyValue(fallback, key) ? - getObjectPropertyValue(fallback, key) : undefined; + return getObjectPropertyValue(translations, key) + || getObjectPropertyValue(fallback, key) + || undefined; }; /** diff --git a/src/icons/generator/jsonGenerator.ts b/src/icons/generator/jsonGenerator.ts index ab7f96bb2c..12ebe57079 100644 --- a/src/icons/generator/jsonGenerator.ts +++ b/src/icons/generator/jsonGenerator.ts @@ -31,23 +31,15 @@ export const createIconFile = (updatedConfigs?: IconJsonOptions, updatedJSONConf const options: IconJsonOptions = merge({}, getDefaultIconOptions(), updatedJSONConfig); const json = generateIconConfigurationObject(options); - // make sure that the opacity and saturation values must be entered correctly to trigger a reload. - if (updatedConfigs) { - if (updatedConfigs.opacity !== undefined && !validateOpacityValue(updatedConfigs.opacity)) { - throw Error('Material Icons: Invalid opacity value!'); - } - if (updatedConfigs.saturation !== undefined && !validateSaturationValue(updatedConfigs.saturation)) { - throw Error('Material Icons: Invalid saturation value!'); - } + // make sure that the folder color, opacity and saturation values are entered correctly + if (updatedConfigs?.opacity && !validateOpacityValue(updatedConfigs?.opacity)) { + throw Error('Material Icons: Invalid opacity value!'); } - - // make sure that the value of the folder color is entered correctly to trigger a reload. - if (updatedConfigs && updatedConfigs.folders) { - if (typeof updatedConfigs.folders.color !== 'undefined') { - if (!validateHEXColorCode(updatedConfigs.folders.color)) { - throw Error('Material Icons: Invalid folder color value!'); - } - } + if (updatedConfigs?.saturation && !validateSaturationValue(updatedConfigs?.saturation)) { + throw Error('Material Icons: Invalid saturation value!'); + } + if (updatedConfigs?.folders?.color && !validateHEXColorCode(updatedConfigs?.folders?.color)) { + throw Error('Material Icons: Invalid folder color value!'); } try { diff --git a/src/scripts/helpers/similarity.ts b/src/scripts/helpers/similarity.ts index 6486365607..85ceab8eb7 100644 --- a/src/scripts/helpers/similarity.ts +++ b/src/scripts/helpers/similarity.ts @@ -1,6 +1,10 @@ -// from here https://stackoverflow.com/a/36566052/6942210 - -export const similarity = (s1, s2) => { +/** + * Compares two strings and returns the Levenshtein distance + * @see https://stackoverflow.com/a/36566052/6942210 + * @param s1 Text string + * @param s2 text string + */ +export const similarity = (s1: string, s2: string) => { let longer = s1; let shorter = s2; if (s1.length < s2.length) { @@ -11,32 +15,33 @@ export const similarity = (s1, s2) => { if (longerLength === 0) { return 1.0; } - return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength); + return (longerLength - editDistance(longer, shorter)) / longerLength; }; -const editDistance = (s1, s2) => { +const editDistance = (s1: string, s2: string) => { s1 = s1.toLowerCase(); s2 = s2.toLowerCase(); - const costs = new Array(); + const costs = new Array(); for (let i = 0; i <= s1.length; i++) { let lastValue = i; for (let j = 0; j <= s2.length; j++) { - if (i === 0) + if (i === 0) { costs[j] = j; - else { + } else { if (j > 0) { let newValue = costs[j - 1]; - if (s1.charAt(i - 1) !== s2.charAt(j - 1)) - newValue = Math.min(Math.min(newValue, lastValue), - costs[j]) + 1; + if (s1.charAt(i - 1) !== s2.charAt(j - 1)) { + newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1; + } costs[j - 1] = lastValue; lastValue = newValue; } } } - if (i > 0) + if (i > 0) { costs[s2.length] = lastValue; + } } return costs[s2.length]; }; diff --git a/src/scripts/icons/generateJson.ts b/src/scripts/icons/generateJson.ts index d8e3d66fc1..f3b5883027 100644 --- a/src/scripts/icons/generateJson.ts +++ b/src/scripts/icons/generateJson.ts @@ -1,5 +1,5 @@ /** - * This file is only a script file that should only be executed by the npm scripts. + * This file is meant to be executed exclusively by npm scripts. */ import { createIconFile } from './../../icons/index';