Reduced cyclomatic complexity

pull/73/head
MillenniumEarl 2021-02-16 16:06:32 +01:00
parent 6604d3b5c3
commit 3a3ee4173a
1 changed files with 42 additions and 29 deletions

View File

@ -1,29 +1,28 @@
"use strict"; "use strict";
// Modules from file // Modules from file
import shared from "../shared"; import shared, { DictType } from "../shared.js";
/** /**
* Convert prefixes and platform tags from string to ID and vice versa. * Convert prefixes and platform tags from string to ID and vice versa.
*/ */
export default class PrefixParser { export default class PrefixParser {
//#region Private methods //#region Private methods
/** /**
* @private
* Gets the key associated with a given value from a dictionary. * Gets the key associated with a given value from a dictionary.
* @param {Object} object Dictionary to search * @param {Object} object Dictionary to search
* @param {Any} value Value associated with the key * @param {Any} value Value associated with the key
* @returns {String|undefined} Key found or undefined * @returns {String|undefined} Key found or undefined
*/ */
_getKeyByValue(object: { [x: string]: unknown; }, value: unknown): string | undefined { getKeyByValue(object: DictType, value: string): string | undefined {
return Object.keys(object).find(key => object[key] === value); return Object.keys(object).find(key => object[key] === value);
} }
/** /**
* @private
* Makes an array of strings uppercase. * Makes an array of strings uppercase.
*/ */
_toUpperCaseArray(a: string[]): string[] { toUpperCaseArray(a: string[]): string[] {
/** /**
* Makes a string uppercase. * Makes a string uppercase.
*/ */
@ -34,56 +33,70 @@ export default class PrefixParser {
} }
/** /**
* @private
* Check if `dict` contains `value` as a value. * Check if `dict` contains `value` as a value.
*/ */
_valueInDict(dict: { [s: number]: string; }, value: string): boolean { valueInDict(dict: DictType, value: string): boolean {
const array = Object.values(dict); const array = Object.values(dict);
const upperArr = this._toUpperCaseArray(array); const upperArr = this.toUpperCaseArray(array);
const element = value.toUpperCase(); const element = value.toUpperCase();
return upperArr.includes(element); return upperArr.includes(element);
} }
searchElementInPrefixes(element: string | number): DictType | null {
// Local variables
let dictName = null;
// Iterate the key/value pairs in order to find the element
for (const [key, subdict] of Object.entries(shared.prefixes)) {
// Check if the element is a value in the sub-dict
const valueInDict = typeof element === "string" && this.valueInDict(subdict, element as string);
// Check if the element is a key in the subdict
const keyInDict = typeof element === "number" && Object.keys(subdict).includes(element.toString());
if (valueInDict || keyInDict) {
dictName = key;
break;
}
}
return shared.prefixes[dictName] ?? null;
}
//#endregion Private methods //#endregion Private methods
/** /**
* @public
* Convert a list of prefixes to their respective IDs. * Convert a list of prefixes to their respective IDs.
*/ */
prefixesToIDs(prefixes: string[]) : number[] { public prefixesToIDs(prefixes: string[]) : number[] {
const ids: number[] = []; const ids: number[] = [];
for(const p of prefixes) { for(const p of prefixes) {
// Check what dict contains the value // Check what dict contains the value
let dict = null; const dict = this.searchElementInPrefixes(p);
if (this._valueInDict(shared.statuses, p)) dict = shared.statuses;
else if (this._valueInDict(shared.engines, p)) dict = shared.engines;
else if (this._valueInDict(shared.tags, p)) dict = shared.tags;
else if (this._valueInDict(shared.others, p)) dict = shared.others;
else continue;
if (dict) {
// Extract the key from the dict // Extract the key from the dict
const key = this._getKeyByValue(dict, p); const key = this.getKeyByValue(dict, p);
if(key) ids.push(parseInt(key)); ids.push(parseInt(key));
}
} }
return ids; return ids;
} }
/** /**
* @public
* It converts a list of IDs into their respective prefixes. * It converts a list of IDs into their respective prefixes.
*/ */
idsToPrefixes(ids: number[]): string[] { public idsToPrefixes(ids: number[]): string[] {
const prefixes:string[] = []; const prefixes:string[] = [];
for(const id of ids) { for(const id of ids) {
// Check what dict contains the key // Check what dict contains the key
let dict = null; const dict = this.searchElementInPrefixes(id);
if (Object.keys(shared.statuses).includes(id.toString())) dict = shared.statuses;
else if (Object.keys(shared.engines).includes(id.toString())) dict = shared.engines;
else if (Object.keys(shared.tags).includes(id.toString())) dict = shared.tags;
else if (Object.keys(shared.others).includes(id.toString())) dict = shared.others;
else continue;
// Check if the key exists in the dict // Add the key to the list
if (id in dict) prefixes.push(dict[id]); if (dict) {
prefixes.push(dict[id]);
}
} }
return prefixes; return prefixes;
} }