UNPKG

tf2-item-format

Version:
161 lines 5.89 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; const shouldSetNumber_1 = __importDefault(require("./stringify/shouldSetNumber")); const shouldSetQuality_1 = __importDefault(require("./stringify/shouldSetQuality")); const addTargetToName_1 = __importDefault(require("./stringify/addTargetToName")); const getOutput_1 = __importDefault(require("./shared/getOutput")); const guards_1 = require("./shared/guards"); const guards_2 = require("./toSKU/guards"); const guards_3 = require("./types/guards"); const DEFAULT_OPTIONS = { determineUniqueHat: false, }; /** * Stringifies item object into item name */ function default_1(schema, attributes, options = {}) { options = Object.assign(Object.assign({}, DEFAULT_OPTIONS), options); const { craftable, australium, festivized, killstreak, elevated, quality, wear, texture, effect, outputQuality, itemNumber, } = attributes; let name; let target; let output; if ((0, guards_3.nameTypeGuard)(attributes)) { name = attributes.name; target = getTarget(schema, attributes); output = getOutputItem(schema, attributes); } else if ((0, guards_3.skuTypeGuard)(attributes)) { name = getName(attributes.defindex, schema); target = (0, guards_2.hasTargetDefindex)(attributes) ? schema.getName(attributes.targetDefindex) : ''; output = (0, guards_2.hasOutputDefindex)(attributes) ? schema.getName(attributes.outputDefindex) : ''; } else { throw new Error('Defindex or Name is missing.'); } let shouldSetUniqueHat = true; let itemName = ''; if (!craftable) { itemName += 'Non-Craftable '; shouldSetUniqueHat = false; } if (elevated) { itemName += 'Strange '; shouldSetUniqueHat = false; } if ((0, shouldSetQuality_1.default)(quality, elevated, effect)) { itemName += `${schema.getQualityName(quality)} `; shouldSetUniqueHat = false; } if (effect) { itemName += `${schema.getEffectName(effect)} `; shouldSetUniqueHat = false; } if (festivized) { itemName += 'Festivized '; shouldSetUniqueHat = false; } if (killstreak && canAddKillstreak(killstreak, target)) { itemName += `${schema.getKillstreakName(killstreak)} `; shouldSetUniqueHat = false; } if (isAustralium(australium)) { itemName += 'Australium '; shouldSetUniqueHat = false; } if ((0, guards_1.hasDefindex)(texture)) { itemName += `${schema.getTextureName(texture)} `; shouldSetUniqueHat = false; } if (target && isKillstreakKitOrFabricator(name, target)) { // eslint-disable-next-line no-param-reassign name = (0, addTargetToName_1.default)(name, schema.getName(target)); shouldSetUniqueHat = false; } else if (target || (output && outputQuality)) { // There can be both target and output, target is prefered thus the check. // getOutput constructs full output name if quality present. // target has no quality if (target && output) { const outputName = (0, getOutput_1.default)(schema.getName(output), schema.getQualityName(outputQuality)); itemName += `${target} ${outputName} `; } else { itemName += `${output && !target ? (0, getOutput_1.default)(schema.getName(output), schema.getQualityName(outputQuality)) : schema.getName(target)} `; } shouldSetUniqueHat = false; } if (wear) { shouldSetUniqueHat = false; } if (shouldSetUniqueHat && isUniqueHat(schema, attributes, options)) { itemName += 'The '; } // Either we have name or defindex. itemName += name; if (wear) { itemName += ` (${schema.getWearName(wear)})`; } if (itemNumber && (0, shouldSetNumber_1.default)(itemNumber)) { if (itemNumber.type === 'series') itemName += ` Series #${itemNumber.value}`; else itemName += ` #${itemNumber.value}`; } return itemName; } function getName(defindex, schema) { const name = schema.getName(defindex); if (name.includes(' Fabricator')) { return name.replace(' Fabricator', ' Kit Fabricator'); } return name; } function isAustralium(australium) { return !!(australium && australium !== -1); } /** * Checks if we can add killstreak to the name, * killstreak stays present on target items such as kits and fabricators. * @param {*} killstreak * @param {string} target * @return {boolean} */ function canAddKillstreak(killstreak, target) { return !!(killstreak && !target); } function isKillstreakKitOrFabricator(name, target) { return !!(target && (name.includes(' Kit') || name.includes(' Fabricator'))); // This checks for fabricator too. } function isUniqueHat(schema, attributes, options) { if (typeof attributes.isUniqueHat === 'boolean') { return attributes.isUniqueHat; } if (!options.determineUniqueHat) { return false; } return schema.isUniqueHat((0, guards_3.skuTypeGuard)(attributes) ? attributes.defindex : attributes.name); } function getTarget(schema, attributes) { return (attributes.target || ((0, guards_2.hasTargetDefindex)(attributes) ? schema.getName(attributes.targetDefindex) : '')); } function getOutputItem(schema, attributes) { return (attributes.output || ((0, guards_2.hasOutputDefindex)(attributes) ? schema.getName(attributes.outputDefindex) : '')); } //# sourceMappingURL=stringify.js.map