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

115 lines
3.1 KiB
TypeScript
Raw Normal View History

2021-03-04 12:19:49 +00:00
// Copyright (c) 2021 MillenniumEarl
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
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-03-04 11:26:45 +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-03-05 10:26:53 +00:00
private getKeyByValue(object: TPrefixDict, value: string): string | undefined {
2021-03-04 11:26:45 +00:00
return Object.keys(object).find((key) => object[key] === value);
}
2021-02-16 15:06:32 +00:00
2021-03-04 11:26:45 +00:00
/**
* Makes an array of strings uppercase.
*/
private toUpperCaseArray(a: string[]): string[] {
2020-12-03 10:55:06 +00:00
/**
2021-03-04 11:26:45 +00:00
* Makes a string uppercase.
2020-12-03 10:55:06 +00:00
*/
2021-03-04 11:26:45 +00:00
function toUpper(s: string): string {
return s.toUpperCase();
2020-12-03 10:55:06 +00:00
}
2021-03-04 11:26:45 +00:00
return a.map(toUpper);
}
2020-12-03 11:35:50 +00:00
2021-03-04 11:26:45 +00:00
/**
* Check if `dict` contains `value` as a value.
*/
private valueInDict(dict: TPrefixDict, value: string): boolean {
const array = Object.values(dict);
const upperArr = this.toUpperCaseArray(array);
const element = value.toUpperCase();
return upperArr.includes(element);
}
2021-02-16 15:06:32 +00:00
2021-03-04 11:26:45 +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-03-05 10:26:53 +00:00
private searchElementInPrefixes(element: string | number): TPrefixDict | null {
2021-03-04 11:26:45 +00:00
// Local variables
let dictName = null;
2021-02-16 15:06:32 +00:00
2021-03-04 11:26:45 +00:00
// 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 =
2021-03-05 10:26:53 +00:00
typeof element === "string" && this.valueInDict(subdict, element as string);
2021-02-16 15:06:32 +00:00
2021-03-04 11:26:45 +00:00
// Check if the element is a key in the subdict
const keyInDict =
2021-03-05 10:26:53 +00:00
typeof element === "number" && Object.keys(subdict).includes(element.toString());
2021-02-16 15:06:32 +00:00
2021-03-04 11:26:45 +00:00
if (valueInDict || keyInDict) {
dictName = key;
break;
}
2021-02-16 15:06:32 +00:00
}
2020-12-03 10:55:06 +00:00
2021-03-04 11:26:45 +00:00
return shared.prefixes[dictName] ?? null;
}
//#endregion Private methods
2021-02-16 15:06:32 +00:00
2021-03-04 11:26:45 +00:00
/**
* Convert a list of prefixes to their respective IDs.
*/
public prefixesToIDs(prefixes: string[]): number[] {
const ids: number[] = [];
2020-12-03 10:55:06 +00:00
2021-03-04 11:26:45 +00:00
for (const p of prefixes) {
// Check what dict contains the value
const dict = this.searchElementInPrefixes(p);
if (dict) {
// Extract the key from the dict
const key = this.getKeyByValue(dict, p);
ids.push(parseInt(key, 10));
}
2020-12-03 10:55:06 +00:00
}
2021-03-04 11:26:45 +00:00
return ids;
}
2020-12-03 10:55:06 +00:00
2021-03-04 11:26:45 +00:00
/**
* It converts a list of IDs into their respective prefixes.
*/
public idsToPrefixes(ids: number[]): string[] {
const prefixes: string[] = [];
2021-02-16 15:06:32 +00:00
2021-03-04 11:26:45 +00:00
for (const id of ids) {
// Check what dict contains the key
const dict = this.searchElementInPrefixes(id);
2020-12-03 10:55:06 +00:00
2021-03-04 11:26:45 +00:00
// Add the key to the list
if (dict) {
prefixes.push(dict[id]);
}
2020-12-03 10:55:06 +00:00
}
2021-03-04 11:26:45 +00:00
return prefixes;
}
}