Skip to content

Commit a133ab0

Browse files
committed
Refactor code
1 parent e4920f5 commit a133ab0

5 files changed

Lines changed: 30 additions & 34 deletions

File tree

src/helpers/objects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Get the nested properties of an object
2+
* Get the nested properties of an object.
33
* This solution is lighter than the lodash get-version.
44
* Source: http://stackoverflow.com/a/6491621/6942210
55
*/

src/i18n/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ const getTranslationObject = async (language: string) => {
4141
* and the fallback (required for testing purposes).
4242
* */
4343
export const getTranslationValue = (key: string, translations = currentTranslation, fallback = fallbackTranslation) => {
44-
return getObjectPropertyValue(translations, key) ?
45-
getObjectPropertyValue(translations, key) :
46-
getObjectPropertyValue(fallback, key) ?
47-
getObjectPropertyValue(fallback, key) : undefined;
44+
return getObjectPropertyValue(translations, key)
45+
|| getObjectPropertyValue(fallback, key)
46+
|| undefined;
4847
};
4948

5049
/**

src/icons/generator/jsonGenerator.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,15 @@ export const createIconFile = (updatedConfigs?: IconJsonOptions, updatedJSONConf
3131
const options: IconJsonOptions = merge({}, getDefaultIconOptions(), updatedJSONConfig);
3232
const json = generateIconConfigurationObject(options);
3333

34-
// make sure that the opacity and saturation values must be entered correctly to trigger a reload.
35-
if (updatedConfigs) {
36-
if (updatedConfigs.opacity !== undefined && !validateOpacityValue(updatedConfigs.opacity)) {
37-
throw Error('Material Icons: Invalid opacity value!');
38-
}
39-
if (updatedConfigs.saturation !== undefined && !validateSaturationValue(updatedConfigs.saturation)) {
40-
throw Error('Material Icons: Invalid saturation value!');
41-
}
34+
// make sure that the folder color, opacity and saturation values are entered correctly
35+
if (updatedConfigs?.opacity && !validateOpacityValue(updatedConfigs?.opacity)) {
36+
throw Error('Material Icons: Invalid opacity value!');
4237
}
43-
44-
// make sure that the value of the folder color is entered correctly to trigger a reload.
45-
if (updatedConfigs && updatedConfigs.folders) {
46-
if (typeof updatedConfigs.folders.color !== 'undefined') {
47-
if (!validateHEXColorCode(updatedConfigs.folders.color)) {
48-
throw Error('Material Icons: Invalid folder color value!');
49-
}
50-
}
38+
if (updatedConfigs?.saturation && !validateSaturationValue(updatedConfigs?.saturation)) {
39+
throw Error('Material Icons: Invalid saturation value!');
40+
}
41+
if (updatedConfigs?.folders?.color && !validateHEXColorCode(updatedConfigs?.folders?.color)) {
42+
throw Error('Material Icons: Invalid folder color value!');
5143
}
5244

5345
try {

src/scripts/helpers/similarity.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
// from here https://stackoverflow.com/a/36566052/6942210
2-
3-
export const similarity = (s1, s2) => {
1+
/**
2+
* Compares two strings and returns the Levenshtein distance
3+
* @see https://stackoverflow.com/a/36566052/6942210
4+
* @param s1 Text string
5+
* @param s2 text string
6+
*/
7+
export const similarity = (s1: string, s2: string) => {
48
let longer = s1;
59
let shorter = s2;
610
if (s1.length < s2.length) {
@@ -11,32 +15,33 @@ export const similarity = (s1, s2) => {
1115
if (longerLength === 0) {
1216
return 1.0;
1317
}
14-
return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength);
18+
return (longerLength - editDistance(longer, shorter)) / longerLength;
1519
};
1620

17-
const editDistance = (s1, s2) => {
21+
const editDistance = (s1: string, s2: string) => {
1822
s1 = s1.toLowerCase();
1923
s2 = s2.toLowerCase();
2024

21-
const costs = new Array();
25+
const costs = new Array<number>();
2226
for (let i = 0; i <= s1.length; i++) {
2327
let lastValue = i;
2428
for (let j = 0; j <= s2.length; j++) {
25-
if (i === 0)
29+
if (i === 0) {
2630
costs[j] = j;
27-
else {
31+
} else {
2832
if (j > 0) {
2933
let newValue = costs[j - 1];
30-
if (s1.charAt(i - 1) !== s2.charAt(j - 1))
31-
newValue = Math.min(Math.min(newValue, lastValue),
32-
costs[j]) + 1;
34+
if (s1.charAt(i - 1) !== s2.charAt(j - 1)) {
35+
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
36+
}
3337
costs[j - 1] = lastValue;
3438
lastValue = newValue;
3539
}
3640
}
3741
}
38-
if (i > 0)
42+
if (i > 0) {
3943
costs[s2.length] = lastValue;
44+
}
4045
}
4146
return costs[s2.length];
4247
};

src/scripts/icons/generateJson.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* This file is only a script file that should only be executed by the npm scripts.
2+
* This file is meant to be executed exclusively by npm scripts.
33
*/
44
import { createIconFile } from './../../icons/index';
55

0 commit comments

Comments
 (0)