k4-utils
v1.1.35
Published
This node package contains a collection of useful functions for working with the SPT framework. Whether you're a seasoned developer or just starting out, these tools will help you get the job done faster and more efficiently. With a focus on simplicity an
Downloads
11
Readme
K4-Utils is a package that packs some useful tools to be used in the Singleplayer Esacpe From Tarkov framework
Avaiable languages string IDs: "ch", "cz", "en", "es-mx", "es", "fr", "ge", "hu", "it", "jp", "pl", "po", "ru", "sk", "tu"
get_name(ID: string, mode?: number, Language?: string): string:
ID
: a string representing an identifier.Mode
: a number representing the mode of operation. Default value 0 searching by the locale's of the game, 1 taking the name from the item's proprieties.Default Value is 0
Language
: a string representing the language to be used. The default value isen
.The function returns the name associated with the given ID, either from a global locale database or from a database of items, depending on the value of mode. If the specified language is not found in the list of available languages, or if the ID is not found in the relevant database, the function throws an error. The error message will include suggestions for similar IDs that may be used instead.
get_short_name(ID: string, Language?: string): string:
ID
: a string representing an identifier.Language
: a string representing the language to be used. The default value is en.This function returns the short name associated with the given ID from a global locale database. If the specified language is not found in the list of available languages, or if the ID is not found in the database, the function throws an error. The error message will include suggestions for similar IDs that may be used instead.
get_description(ID: string, Language?: string): string:
ID
: a string representing an identifier.Language
: a string representing the language to be used. The default value is en.This function returns the description associated with the given ID from a global locale database. If the specified language is not found in the list of available languages, or if the ID is not found in the database, the function throws an error. The error message will include suggestions for similar IDs that may be used instead.
get_all_ids(): string[]
This function returns an array of strings representing all of the IDs in the database of items. The IDs are retrieved from the items property of the templates property of the DB object, which is initialized as database.tableData.
find_most_similar(mainString: string, stringArray: string[]): string
- This function takes in a mainString and an array of strings called stringArray and returns the string in stringArray that is the most similar to mainString, as determined by the Levenshtein distance. The Levenshtein distance is a measure of the difference between two strings, calculated as the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into the other.
- The function first initializes the variables mostSimilar and lowestDistance to the first string in stringArray and the Levenshtein distance between mainString and the first string in stringArray, respectively. It then iterates through the rest of the strings in stringArray, calculates the Levenshtein distance between mainString and each string, and updates mostSimilar and lowestDistance if a string is found that is more similar to mainString (i.e. has a lower Levenshtein distance). Finally, the function returns mostSimilar.
Example input:
mainString: "dog"
stringArray: ["cat", "rats", "bats", "dogs"]
Example Output: "dogs"
LevenshteinDistance(a: string, b: string): number
- This function calculates the ``Levenshtein distance`` between two strings, ``a`` and ``b``. The Levenshtein distance is a measure of the difference between two strings, calculated as the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into the other.
- The function initializes a two-dimensional array called distanceMatrix with dimensions ``b.length + 1`` by a.length + 1, and fills it with null values. It then populates the first row and column of distanceMatrix with values from 0 to a.length and 0 to b.length, respectively.
get_handbook_price(id: string): number
ID
: a string representing an identifier.This function returns the price of an item with the given id as it appears in a handbook database. The function first checks if the item with the given id exists in the database of items. If it does not exist, the function throws an error. If the item does exist, the function searches the handbook database for the price of the item and returns it. If the item is not found in the handbook database, the function does not return a value.
get_flea_price(id: string): number
ID
: a string representing an identifier.This function returns the price of an item with the given id as it appears in a flea market database. The function first checks if the item with the given id exists in the database of items. If it does not exist, the function throws an error. If the item does exist, the function retrieves the price of the item from the flea market database and returns it. If the item is not found in the flea market database, the function does not return a value.
get_parent(id: string): string
ID
: a string representing an identifier.This function returns the parent of an item with the given id as it appears in the database of items. The function first checks if the item with the given id exists in the database. If it does not exist, the function throws an error.
If the item does exist, the function retrieves the parent of the item from the database and returns it. If the item does not have a parent, the function returns the string "No parent".
arrayID_to_name(array: string[]): string[]
Example input:
let array = ["5ae30e795acfc408fb139a0b", "5447a9cd4bdc2dbd208b4567"]; arrayID_to_name(array);
Example output:
let array = ["M4A1 front sight", "Colt M4A1 5.56x45 assault rifle"]; arrayID_to_name(array);
This function takes in an array of strings representing item IDs and returns an array of strings representing the names of the items. The function first checks if the input array is defined and not empty. If it is not defined or empty, the function throws an error. The function then iterates through the input array and retrieves the name of each item from the database of items. If an item with a given ID is not found in the database, the function throws an error with a suggestion for a similar ID that may be used instead. Finally, the function returns the array of names.
get_items_with_parent(parent: string): string[]
parent
: a string representing an parent identifier.This function returns an array of strings representing the IDs of items that have the given parent. The function iterates through the database of items and checks the _parent property of each item. If the _parent property is equal to the input parent, the ID of the item is added to the array of items with the given parent. Finally, the function returns the array of IDs.
has_props(ID: string): boolean
ID
: a string representing an identifier.This function returns a boolean value indicating whether an item with the given ID has properties in the database of items. The function first checks if the item with the given ID exists in the database. If it does not exist, the function throws an error with a suggestion for a similar ID that may be used instead. If the item does exist, the function checks the _props property of the item. If the _props property is defined, not an empty string, and has more than five entries, the function returns true. Otherwise, it returns false.
get_date_from_id(id: string): Date
ID
: a string representing an identifier.This function returns a Date object representing the date and time encoded in an item id. The function first extracts the first eight characters from the id string, which represent a timestamp in hexadecimal format. The timestamp is then converted to a decimal number and used to create a Date object. The function returns the Date object.
get_all_ids_names(): string[]
This function returns an array of strings representing the names of all the items in the database. The function iterates through the database of items and retrieves the _name property of each item. The names are then added to the array and the function returns the array.
crash(): void
This function intentionally crashes the node.js process by calling the process.exit() function with an exit code of 0. The function logs a message to the console before calling process.exit(). This function is useful for when you need to debug something but do not want to wait for the server to fully load...
It is generally not recommended to use this function in production, as it can cause unexpected termination of the process. It may be useful for debugging purposes or as a way to gracefully shutdown the process in certain situations.
isRequired(ID: string, SlotID: string): boolean
ID
: a string representing an identifier.SlotID
: a string representing an identifier for the SlotID where the item specified by the first ID must go in.This function returns a boolean value indicating whether a given SlotID is required for an item with the given ID. The function retrieves the Slots property of the item from the database of items and checks the _required property of each slot. If a slot with the given SlotID is found and its _required property is true, the function returns true. Otherwise, it returns false.
get_item(ID: string): object
ID
: a string representing an identifier.This function returns an object representing an item with the given ID from the database of items. If the item does not exist in the database, the function returns undefined.
get_all_ids_names_from_locale(Language?: string): string[]
Language
: a string representing the language to be used. The default value is en.This function returns an array of strings representing the names of all the items in the database in a specific language. The function takes an optional Language parameter, which defaults to "en" if not provided. The function iterates through the database of items and retrieves the Name property of each item from the specified language's locale. The names are then added to the array and the function returns the array.
get_id_from_locale_name(name: string, Language?: string): string | undefined
name
: a string representing the specific name you're searching for.Language
: a string representing the language to be used. The default value is en.This function returns the ID of an item in the database whose name is equal to the name parameter in a specific language. The function takes a required name parameter, which is a string representing the name of the item, and an optional Language parameter, which defaults to "en" if not provided. The function iterates through the database of items and retrieves the Name property of each item from the specified language's locale. If the Name property matches the name parameter, the function returns the ID of the item. If no item with a matching name is found, the function returns undefined.
can_trader_buy(ID : string,TraderID : string): boolean
ID
: a string representing an identifier.TraderID
: a string representing an identifier of the trader.This function returns true/false representing if the specified trader can buy the specified item
get_trader_by_name(Name) : ID
Name
: a string representing the name of the trader "Prapor; Therapist; Fence; Skier; Peacekeeper; Mechanic; Ragman; Jaeger".This function takes a string input as the name of the trader and returns the ID if it find's the specfified trader, it is not lowercase sensitive
get_handbook_parent(ID : string): string
ID
: a string representing an identifier.This function will get the Item's parentID from the handbook Items database.