Set of tools that manage operations with tf2 currency. Inspired by tf2-currencies made by Nicklason. Made as a drop in replacement for your plain currency object.
- Fully typed, written in typescript
- Throughly tested, over 100 tests
- More convenience methods for comparing/operating with currency
npm install tf2-currency or via yarn yarn add tf2-currency
Following classes, types and functions are exported:
export {
Currency, // Currency class that manages operations
CurrencyError, // Error thrown by Currency class
ICurrency, // Interface for currency
c, // Create Currency object faster
fixCurrency, // Fix partial currency objects
}Currency is stored like this:
{
keys: number,
metalInWeapons: number,
}uses weapons to avoid weird floating point issues. This way it's "immune" against conversion changes.
If you want to use weapons / scrap value as storage, use toWeapons and toScrap methods to convert it to weapons or scrap.
currencyrepresents plain currency object such as{ keys: 12, metal: 52.11 }metalInWeaponsoptional parameter that represents how much metal is in weapons, defaults to 0- Prioritized over
currency.metal
- Prioritized over
Creates a Currency class from weapons value
weaponsvalue you are creating currency fromconversionrate to be used to convert into keys (in metal)
Creates a Currency class from scrap value
scrapvalue you are creating currency fromconversionrate to be used to convert into keys (in metal)
Creates a Currency class from key value
keysfloating point number to create currency fromconversionrate to be used to convert into metal (in metal)- Required if keys is not an integer
Converts currency to weapons
conversionto convert keys to weapons (in metal)- Required if keys are present
Converts currency to scrap
conversionto convert keys to scrap (in metal)- Required if keys are present
Converts currency to keys
conversionto convert metal to keys (in metal)- Required if metal is present
Builds a currency string readable for humans
Returns plain currency object, with keys & metal, for json serialization
Returns plain currency object, with keys & weapons, for json serialization
Adds weapon value to the currency
weaponsvalue you are adding to the currencyconversionrate to be used to convert exceeding metal value into keys (in metal)- Required if weapons is not an integer
Adds scrap value to the currency
scrapvalue you are adding to the currencyconversionrate to be used to convert exceeding metal value into keys (in metal)
Adds metal value to the currency
metalvalue you are adding to the currencyconversionrate to be used to convert exceeding metal value into keys (in metal)
Adds key value to the currency
keysvalue you are adding to the currencyconversionrate to be used to convert exceeding metal value into keys (in metal)- Required if keys is not an integer
Adds currency value to the currency
currencyvalue you are adding to the currencyconversionrate to be used to convert exceeding metal value into keys
Removes weapon value from the currency
weaponsvalue you are removing from the currencyconversionrate to be used to convert key into weapons (in metal)- Required when we go to negative values with weapons
Removes scrap value from the currency
scrapvalue you are removing from the currencyconversionrate to be used to convert key into scrap- Required when we go to negative values with scrap
Removes metal value from the currency
metalvalue you are removing from the currencyconversionrate to be used to convert key into metal- Required when we go to negative values with scrap
Removes key value from the currency
keysvalue you are removing from the currencyconversionrate to be used to convert key decimal to metal- Required when we go to negative values with scrap or when keys is not an integer
Removes currency value from the currency
currencyvalue you are removing from the currencyconversionrate to be used to convert key into metal- Required when we go to negative values with scrap
Compares current currency object with supplied one
currencywe are comparing to
Compares current currency object with supplied one
currencywe are comparing to
Compares current currency object with supplied one
currencywe are comparing to
Compares current currency object with supplied one
currencywe are comparing to
Compares current currency object with supplied one
currencywe are comparing to
Compares current currency object with supplied one
valuewe are comparing to- Returns:
0if equal1if current is bigger-1if current is smaller
Compares current currency object with supplied weaponized currency (keys and metalInWeapons instead of metal)
currencywe are comparing to
Compares current currency object with supplied weaponized currency
currencywe are comparing to
Compares current currency object with supplied weaponized currency
currencywe are comparing to
Compares current currency object with supplied weaponized currency
currencywe are comparing to
Compares current currency object with supplied weaponized currency
currencywe are comparing to
Compares current currency object with supplied weaponized currency
valuewe are comparing to- Returns:
0if equal1if current is bigger-1if current is smaller
Creates Currency object
currencyfrom which we are creating the object
Fixes partial currency object
currencywe are fixing