Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
PKief committed Mar 15, 2020
1 parent e4920f5 commit a133ab0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/helpers/objects.ts
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down
7 changes: 3 additions & 4 deletions src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down
24 changes: 8 additions & 16 deletions src/icons/generator/jsonGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 17 additions & 12 deletions src/scripts/helpers/similarity.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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<number>();
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];
};
2 changes: 1 addition & 1 deletion src/scripts/icons/generateJson.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down

0 comments on commit a133ab0

Please sign in to comment.