UNPKG

pmgo-pokedex

Version:

Provide Pokemon GO pokedex data and useful translation.

94 lines (93 loc) 5.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Pokedex = void 0; // Node modules. var node_fs_1 = require("node:fs"); var node_path_1 = require("node:path"); var lodash_1 = __importDefault(require("lodash")); var string_similarity_1 = require("string-similarity"); var readJson = function (filename) { var filepath = (0, node_path_1.join)(__dirname, '../data', filename); var json = JSON.parse((0, node_fs_1.readFileSync)(filepath, { encoding: 'utf-8' })); return json; }; var Pokedex = /** @class */ (function () { function Pokedex(targetLocale) { if (targetLocale === void 0) { targetLocale = 'zh-TW'; } var _this = this; /** Get pokemon info object by pokemon's fuzzy name. */ this.getPokemonByFuzzyName = function (pokemonName, targetLocale) { var _a; if (targetLocale === void 0) { targetLocale = _this.defaultTargetLocale; } var bestNameIndex = (0, string_similarity_1.findBestMatch)(pokemonName, _this.pokemonNameList.map(function (pokemonName) { return pokemonName['en-US']; })).bestMatchIndex; var bestIndex = (0, string_similarity_1.findBestMatch)(pokemonName, _this.pokemonList.map(function (pokemon) { return "".concat(pokemon.name, "-").concat(pokemon.form); })).bestMatchIndex; var matchedPokemonName = _this.pokemonNameList[bestNameIndex]; var matchedPokemon = _this.pokemonList[bestIndex]; var predictedForm = null; // Regional. _this.regionList.forEach(function (region) { region.patterns.forEach(function (pattern) { if (new RegExp(pattern, 'i').test(pokemonName)) { predictedForm = region[targetLocale]; } }); }); // Forms. var formCandidates = _this.formList.filter(function (form) { var isSameName = form.name === matchedPokemon.name; var isMatchedForm = new RegExp(lodash_1.default.escapeRegExp(form['en-US']), 'i').test(String(matchedPokemon.form)) || new RegExp(lodash_1.default.escapeRegExp(String(matchedPokemon.form)), 'i').test(String(form['en-US'])); return isSameName && isMatchedForm; }); if (formCandidates.length > 0) { var formText = pokemonName.replace(String((_a = lodash_1.default.first(formCandidates)) === null || _a === void 0 ? void 0 : _a.name), ''); var bestFormIndex = (0, string_similarity_1.findBestMatch)(formText, formCandidates.map(function (form) { return String(form[targetLocale]); })).bestMatchIndex; predictedForm = String(formCandidates[bestFormIndex][targetLocale]); } // Mega evolutions. if (/mega\s/i.test(pokemonName)) { predictedForm = pokemonName.replace(matchedPokemonName['en-US'], matchedPokemonName[targetLocale]); } var pokemon = { no: matchedPokemonName.no, name: matchedPokemonName[targetLocale], form: predictedForm, types: matchedPokemon.types.map(function (typeText) { return _this.transType(typeText, targetLocale); }), }; return pokemon; }; this.defaultTargetLocale = targetLocale; this.pokemonNameList = readJson('pokemon-name-list.json'); this.pokemonList = readJson('pokemon-list.json'); this.formList = readJson('form-list.json'); this.regionList = readJson('region-list.json'); this.typeList = readJson('type-list.json'); } /** Get pokemon's name by pokemon's number. */ Pokedex.prototype.getPokemonNameByNo = function (pokemonNo, locale) { if (locale === void 0) { locale = this.defaultTargetLocale; } var foundPokemon = this.pokemonNameList.find(function (pokemon) { return pokemon.no === pokemonNo; }); return foundPokemon ? foundPokemon[locale] : null; }; ; /** Translate pokemon's display name by pokemon's fuzzy name. */ Pokedex.prototype.transPokemonName = function (pokemonName, targetLocale) { if (targetLocale === void 0) { targetLocale = this.defaultTargetLocale; } var pokemon = this.getPokemonByFuzzyName(pokemonName, targetLocale); var translatedName = pokemon.form ? "".concat(pokemon.name, " (").concat(pokemon.form, ")") : pokemon.name; return translatedName; }; /** Translate type by text of pokemon type. */ Pokedex.prototype.transType = function (typeText, targetLocale) { if (targetLocale === void 0) { targetLocale = this.defaultTargetLocale; } var targetType = typeText.toLowerCase(); var foundType = this.typeList.find(function (type) { return type.patterns.includes(targetType); }); return foundType ? foundType[targetLocale] : null; }; return Pokedex; }()); exports.Pokedex = Pokedex;