F95API/src/scripts/classes/prefix-parser.ts

108 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-12-03 10:55:06 +00:00
"use strict";
// Modules from file
2021-02-21 13:45:21 +00:00
import shared, { TPrefixDict } from "../shared.js";
2020-12-03 10:55:06 +00:00
/**
* Convert prefixes and platform tags from string to ID and vice versa.
*/
2021-02-14 11:27:51 +00:00
export default class PrefixParser {
2021-02-16 15:06:32 +00:00
2020-12-03 10:55:06 +00:00
//#region Private methods
/**
* Gets the key associated with a given value from a dictionary.
* @param {Object} object Dictionary to search
* @param {Any} value Value associated with the key
* @returns {String|undefined} Key found or undefined
*/
2021-02-21 13:45:21 +00:00
getKeyByValue(object: TPrefixDict, value: string): string | undefined {
2020-12-03 10:55:06 +00:00
return Object.keys(object).find(key => object[key] === value);
}
2020-12-03 11:35:50 +00:00
/**
* Makes an array of strings uppercase.
*/
2021-02-16 15:06:32 +00:00
toUpperCaseArray(a: string[]): string[] {
2020-12-03 11:35:50 +00:00
/**
* Makes a string uppercase.
*/
2021-02-13 15:21:28 +00:00
function toUpper(s: string): string {
2020-12-03 11:35:50 +00:00
return s.toUpperCase();
}
return a.map(toUpper);
}
/**
* Check if `dict` contains `value` as a value.
*/
2021-02-21 13:45:21 +00:00
valueInDict(dict: TPrefixDict, value: string): boolean {
2020-12-03 11:35:50 +00:00
const array = Object.values(dict);
2021-02-16 15:06:32 +00:00
const upperArr = this.toUpperCaseArray(array);
2020-12-03 11:35:50 +00:00
const element = value.toUpperCase();
return upperArr.includes(element);
}
2021-02-16 15:06:32 +00:00
2021-02-17 08:04:56 +00:00
/**
* Search within the platform prefixes for the
* desired element and return the dictionary that contains it.
* @param element Element to search in the prefixes as a key or as a value
*/
2021-02-21 13:45:21 +00:00
searchElementInPrefixes(element: string | number): TPrefixDict | null {
2021-02-16 15:06:32 +00:00
// 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;
}
2020-12-03 10:55:06 +00:00
//#endregion Private methods
/**
* Convert a list of prefixes to their respective IDs.
*/
2021-02-16 15:06:32 +00:00
public prefixesToIDs(prefixes: string[]) : number[] {
2021-02-13 15:21:28 +00:00
const ids: number[] = [];
2021-02-16 15:06:32 +00:00
2020-12-03 10:55:06 +00:00
for(const p of prefixes) {
// Check what dict contains the value
2021-02-16 15:06:32 +00:00
const dict = this.searchElementInPrefixes(p);
2020-12-03 10:55:06 +00:00
2021-02-16 15:06:32 +00:00
if (dict) {
// Extract the key from the dict
const key = this.getKeyByValue(dict, p);
ids.push(parseInt(key));
}
2020-12-03 10:55:06 +00:00
}
2020-12-03 10:59:27 +00:00
return ids;
2020-12-03 10:55:06 +00:00
}
/**
* It converts a list of IDs into their respective prefixes.
*/
2021-02-16 15:06:32 +00:00
public idsToPrefixes(ids: number[]): string[] {
2021-02-13 15:21:28 +00:00
const prefixes:string[] = [];
2021-02-16 15:06:32 +00:00
2020-12-03 10:55:06 +00:00
for(const id of ids) {
// Check what dict contains the key
2021-02-16 15:06:32 +00:00
const dict = this.searchElementInPrefixes(id);
2020-12-03 10:55:06 +00:00
2021-02-16 15:06:32 +00:00
// Add the key to the list
if (dict) {
prefixes.push(dict[id]);
}
2020-12-03 10:55:06 +00:00
}
2020-12-03 10:59:27 +00:00
return prefixes;
2020-12-03 10:55:06 +00:00
}
2021-02-13 15:21:28 +00:00
}