npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tf2-item-parser

v1.2.8

Published

Utility module to deal with anything related to tf2 item formats.

Downloads

351

Readme

TF2 Item Parser

Simple-to-use parser for TF2 items. Fully typed and lightning fast.

Provides a unified interface for items, which allows parsing from and to largely any format you might come across:

  • Item names
  • SKU
  • EconItem (steam api, node-steam-user and node-steamcommunity)
  • TF2 (node-tf2, GC and backpack.tf api)
  • Backpack Documents (old and new backpack.tf api)
  • Backpack.tf URLs
  • Attribute objects (tf2-item-format)

No longer must we suffer from too many item formats

Installation

npm install tf2-item-parser

Basic Usage

import Item from "tf2-item-parser";

const item = Item.fromName("Strange Professional Killstreak Australium Rocket Launcher");

console.log(item.toSKU()); // 18;11;australium;kt-3
console.log(item.toString()); // Strange Professional Killstreak Australium Rocket Launcher

Initialization

This module relies on preprocessed item definitions to efficiently parse items. These come already bundled with the module, but can be updated by calling the init method.

It is not strictly required to call this method since most methods will function without it, but it is needed to parse new items following tf2 updates. Generally, I recommend calling this method once on startup.

If you do not call this method, the following will not be available: Item.fromTF2 and global_info.schema.

You can also use an instance of tf2-schema for initialization. When providing an api key, init will automatically call itself every few hours, so you will not need to call it again.

import Item from "tf2-item-parser";

await Item.init(""); // Steam API Key

Documentation

Item

The main Item class.

Create with Item.fromX. Compare with .equal(item). Convert with .toX.

Properties

  • name: The item's base name.
  • def_index: The item's definition index.
  • quality: number. The item's quality as a number (see EItemQuality).
  • id?: The item's id.
  • craftable: Whether the item is craftable.
  • killstreak: The item's killstreak tier (see EKillstreakTier).
  • killstreak_sheen: The item's killstreak sheen (see EKillstreakSheen).
  • killstreaker: The item's killstreaker (see EKillstreaker).
  • australium: Whether the item is australium.
  • festivized: Whether the item is festivized.
  • unusual: The item's unusual effect (see EUnusualEffects).
  • texture: The item's texture (see ETexture). This can be 0 (Red Rock Roscoe).
  • wear: The item's wear (see EWear).
  • strange: Whether the item is strange.
  • tradable: Whether the item is tradable.
  • paint: The item's paint (see EPaints).
  • spells: List of the item's spells (see ESpells).
  • strange_parts: List of the item's strange parts (see EStrangeParts).
  • usable: Whether the item is usable.
  • max_uses?: The item's max uses. Specified for usable items.
  • remaining_uses?: The item's remaining uses. Specified for usable items.
  • item_number?: The item's item number. Meaning depends on the item's type, one of: craft number, crate number, chemistry set series, medal number
  • target_def_index?: def_index of item that this can be used on (for killstreak kits, strangifiers, etc)
  • input_items?: List of items (as strings) that are required to craft this item.
  • output_item?: Item received when crafting this item. Specifies .def_index and .quality for Strangifiers. Specifies .item for Chemistry Sets, Kit Fabricators and Wrapped Gifts.
    • def_index?: The item's definition index.
    • quality?: number. The item's quality as a number (see EItemQuality).
    • item?: A Item instance indicating the item received when using this item.
  • type: The item's type (see ItemType).
  • needs_the: Whether the item needs "The" in front of its name.
  • never_tradable: Whether the item is never tradable (as opposed to not currently tradable).

Static Methods

new Item(item: ItemTraits)

Create a new item directly from known item traits.

init(init_value?: string | Schema)

Initialize the item parser with a Steam API key or a tf2-schema instance.

Will throw when initialized with an API key, the schema cannot be fetched and the module cannot find a backup schema (module hasn't been initialized before).

fromName(name: string): Item | undefined

Parses an item from a string. Ignores case and special characters. Will return undefined if the string is not a valid item.

fromSKU(sku: string): Item | undefined

Parses an item from a SKU.

fromEconItem(item: EconItem): Item | undefined

Parses an item from the steam api, node-steam-user and node-steamcommunity.

fromTF2(item: TF2Item): Item | undefined

Parses an item from the node-tf2 module and older backpack.tf api endpoints (including snapshots).

fromBPDocument(item: BPDocumentType): Item | undefined

Parses an item from newer backpack.tf api endpoints (websocket, /v2).

fromBPUrl(url: string): Item | undefined

Parses an item from a URL to a backpack.tf "stats" page (https://backpack.tf/stats/...).

fromItemFormat(item: AllFormatAttributes): Item | undefined

Parses an item from a tf2-item-format attribute object.

fromJSON(json: ItemTraits): Item | undefined

Creates an Item from an ItemTraits object. Useful for converting an Item back after using .toJSON.

normalizeDefIndex(def_index?: number): number | undefined

Some Items have several possible def_index values depending on quality, style or obtain method. This method returns the lowest possible def_index for the item.

Methods

equal(item: Item, ignore_traits: ETraits[] = []): boolean

Compares two Items.

Only compares traits that meaningfully differentiate items (i.e. ignores paint, killstreak sheens, killstreakers, strange parts and spells). Use .equalExact to compare all traits.

toString(): string

Converts the Item to its fully qualified name. Includes name, quality, killstreak, etc.

toSKU(): string

Converts the Item to a SKU. Equivalent to the widely used Marketplace.tf SKU.

Note that the backpack.tf api usually expects a name (use toString) when using the sku parameter.

toJSON(): ItemTraits

Converts the Item to JSON. Useful for saving items to a database or file.

Convert them back to items using fromJSON.

toBPURL(): string

Obtains the URL to the item on backpack.tf.

toBPDocument(): BPDocumentType

Converts the item to the new backpack.tf item format. Used across the new v2 api endpoints.

toBPItemV1(): BPItemV1

Converts the item to an item for the old backpack.tf api endpoints.

toTradeOfferManagerItem(): TradeOfferManagerItem

Creates a TradeOfferManager item from the Item. Used for sending trade offers using steam-tradeoffer-manager.

isCurrency(): boolean

Checks if the Item is a key, ref, rec or scrap.

duplicate(): Item

Duplicate the Item. Useful for creating a copy of an item that can be modified without affecting the original.

Other exports of note

global_info

Holds various internal objects used by the module, including a reference to the tf2-schema instance used for parsing.

Various Enums

  • EItemQuality
  • EItemKillstreak
  • EKillstreakSheen
  • EKillstreaker
  • EUnusualEffects
  • ETexture
  • EWear
  • EPaints
  • ESpells
  • EStrangeParts
  • ETraits (for ignore_traits parameter of .equal)

Various types

  • Various types

License

MIT