2020-12-03 10:55:06 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// Modules from file
|
2021-02-13 15:21:28 +00:00
|
|
|
import shared = require("../shared.js");
|
2020-12-03 10:55:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert prefixes and platform tags from string to ID and vice versa.
|
|
|
|
*/
|
2021-02-13 15:21:28 +00:00
|
|
|
export class PrefixParser {
|
2020-12-03 10:55:06 +00:00
|
|
|
//#region Private methods
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* 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-13 15:21:28 +00:00
|
|
|
_getKeyByValue(object: { [x: string]: unknown; }, value: unknown): 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
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Makes an array of strings uppercase.
|
|
|
|
*/
|
2021-02-13 15:21:28 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* Check if `dict` contains `value` as a value.
|
|
|
|
*/
|
2021-02-13 15:21:28 +00:00
|
|
|
_valueInDict(dict: { [s: number]: string; }, value: string): boolean {
|
2020-12-03 11:35:50 +00:00
|
|
|
const array = Object.values(dict);
|
|
|
|
const upperArr = this._toUpperCaseArray(array);
|
|
|
|
const element = value.toUpperCase();
|
|
|
|
return upperArr.includes(element);
|
|
|
|
}
|
2020-12-03 10:55:06 +00:00
|
|
|
//#endregion Private methods
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* Convert a list of prefixes to their respective IDs.
|
|
|
|
*/
|
2021-02-13 15:21:28 +00:00
|
|
|
prefixesToIDs(prefixes: string[]) : number[] {
|
|
|
|
const ids: number[] = [];
|
2020-12-03 10:55:06 +00:00
|
|
|
for(const p of prefixes) {
|
|
|
|
// Check what dict contains the value
|
|
|
|
let dict = null;
|
2020-12-03 11:35:50 +00:00
|
|
|
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;
|
2020-12-03 10:55:06 +00:00
|
|
|
else continue;
|
|
|
|
|
|
|
|
// Extract the key from the dict
|
|
|
|
const key = this._getKeyByValue(dict, p);
|
|
|
|
if(key) ids.push(parseInt(key));
|
|
|
|
}
|
2020-12-03 10:59:27 +00:00
|
|
|
return ids;
|
2020-12-03 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* It converts a list of IDs into their respective prefixes.
|
|
|
|
*/
|
2021-02-13 15:21:28 +00:00
|
|
|
idsToPrefixes(ids: number[]): string[] {
|
|
|
|
const prefixes:string[] = [];
|
2020-12-03 10:55:06 +00:00
|
|
|
for(const id of ids) {
|
|
|
|
// Check what dict contains the key
|
|
|
|
let dict = null;
|
2020-12-03 11:35:50 +00:00
|
|
|
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;
|
2020-12-03 10:55:06 +00:00
|
|
|
else continue;
|
|
|
|
|
|
|
|
// Check if the key exists in the dict
|
|
|
|
if (id in dict) prefixes.push(dict[id]);
|
|
|
|
}
|
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
|
|
|
}
|