Skip to content

Commit

Permalink
Merge pull request #499 from p4535992/master
Browse files Browse the repository at this point in the history
Add utility method getStringFromCurrencies
  • Loading branch information
Haxxer authored Dec 31, 2023
2 parents 3a0b784 + 234870d commit 064d835
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
33 changes: 33 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
* [removeCurrencies](#removeCurrencies)
* [transferCurrencies](#transferCurrencies)
* [transferAllCurrencies](#transferAllCurrencies)
* [getCurrenciesAbbreviations](#getCurrenciesAbbreviations)
* [getStringFromCurrencies](#getStringFromcurrencies)
* [getCurrenciesFromString](#getCurrenciesFromString)
* [calculateCurrencies](#calculateCurrencies)
* [getPaymentData](#getPaymentData)
Expand Down Expand Up @@ -928,6 +930,37 @@ Transfers all currencies between the source and the target.

---

### getCurrenciesAbbreviations

`game.itempiles.API.getCurrenciesAbbreviations()``Array.<string>`

Turns an array containing the data and quantities for each currency into a string of currencies

**NOTE:** This is just a utility method for module intercompatibility to use the other currencies api methods based on a string input.

**Returns**: `Array.<string>` - An array of string containing the abbreviation for each currency registered (eg, ["GP","SP"])

---

### getStringFromCurrencies

`game.itempiles.API.getStringFromCurrencies(currencies)``string`

Turns an array containing the data and quantities for each currency into a string of currencies

**NOTE:** This is just a utility method for module intercompatibility to use the other currencies api methods based on a string input.

**Returns**: `string` - A string of currencies to add (eg, "5gp 25sp")

| Param | Type | Default | Description |
|---------------------------|----------|---------|------------------------------------------------|
| currencies | `Array` | | An array of object containing the data and quantity for each currency |
| currencies[].cost | `number` | | The quantity of the currency |
| currencies[].abbreviation | `string` | | The abbreviation of the currency, which are usually {#}GP, which when {#} is replaced with the number it becomes 5GP. |
| currencies[].percent | `boolean`| | The cost of the currency is in percentage (NOTE: for work the 'abbreviation' property must includes '%' substring) |

---

### getCurrenciesFromString

`game.itempiles.API.getCurrenciesFromString(currencies)``object`
Expand Down
48 changes: 48 additions & 0 deletions src/API/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,54 @@ class API {

}

/**
* Return all th registered currencies abbreviations
* @returns {Array<string>} An array of string containing the abbreviation for each currency registered
*/
static getCurrenciesAbbreviations() {
return PileUtilities.getCurrenciesAbbreviations();
}

/**
* Turns an array containing the data and quantities for each currency into a string of currencies
*
* @param {Array<object>} currencies An array of object containing the data and quantity for each currency
| * @param {number} currencies[].cost The cost of the currency
| * @param {string} currencies[].denom The abbreviation of the currency, which are usually {#}GP, which when {#} is replaced with the number it becomes 5GP.
* @param {string} currencies[].percent The cost of the currency is in percentage (for work the 'abbreviation' property must includes '%' substring)
* @returns {string} An array of object containing the data and quantity for each currency
*/
static getStringFromCurrencies(currencies) {
if (!currencies) {
throw Helpers.custom_error(`getStringFromCurrencies | currencies must be defined`, true);
}
if (typeof currencies === "string") {
throw Helpers.custom_error(`getStringFromCurrencies | currencies can't be of type string`, true);
}
if (!Array.isArray(currencies)) {
throw Helpers.custom_error(`getStringFromCurrencies | currencies must be of type array`, true);
}
if (currencies.length === 0) {
throw Helpers.custom_error(`getStringFromCurrencies | currencies must be a not empty array`, true);
}
currencies.forEach(currency => {
if (typeof currency !== "object") {
throw Helpers.custom_error("setCurrencies | each entry in inCurrencies must be of type object");
}
if (typeof currency.cost !== "number") {
throw Helpers.custom_error("getStringFromCurrencies | currency.cost must be of type number");
}
// Is optional
// if (typeof currency.percent !== "boolean") {
// throw Helpers.custom_error("getStringFromCurrencies | currency.percent must be of type boolean");
// }
if (typeof currency.abbreviation !== "string") {
throw Helpers.custom_error("getStringFromCurrencies | currency.abbreviation must be of type string");
}
});
return PileUtilities.getStringFromCurrencies(currencies);
}

/**
* Turns a string of currencies into an array containing the data and quantities for each currency
*
Expand Down
41 changes: 41 additions & 0 deletions src/helpers/pile-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,47 @@ export function getPriceArray(totalCost, currencies) {
});
}

export function getCurrenciesAbbreviations() {
// Retrieve all the primary abbreviations for the check
let primaryAbbreviationsArray = game.itempiles.API.CURRENCIES
.filter(currency => currency.abbreviation)
.map(currency => {
return currency.abbreviation?.replace("{#}", "");
});
let secondaryAbbreviationsArray = game.itempiles.API.SECONDARY_CURRENCIES
.filter(currency => currency.abbreviation)
.map(currency => {
return currency.abbreviation?.replace("{#}", "");
});
let allAbbreviationsArray = primaryAbbreviationsArray.concat(secondaryAbbreviationsArray);
return allAbbreviationsArray;
}

export function getStringFromCurrencies(currencies) {
let allAbbreviationsArray = getCurrenciesAbbreviations();

let priceString = currencies
.filter(price => price.cost)
.map(price => {
let cost = price.cost;
let abbreviation = price.abbreviation;
if(!Helpers.isRealNumber(cost) || !abbreviation) {
Helpers.custom_error(`getStringFromCurrencies | The currency element is not valid with cost '${cost}' and abbreviation '${abbreviation}'`, false);
return "";
}
if(!allAbbreviationsArray.includes(abbreviation?.replace("{#}", ""))) {
Helpers.custom_error(`getStringFromCurrencies | The currency abbreviation '${abbreviation?.replace("{#}", "")}' is not registered`, false);
return "";
}
if (price.percent && abbreviation.includes("%")) {
abbreviation = abbreviation.replaceAll("%", "")
}
return abbreviation.replace("{#}", price.cost)
}).join(" ");

return priceString ? priceString.trim() : "";
}

export function getPriceFromString(str, currencyList = false) {

if (!currencyList) {
Expand Down

0 comments on commit 064d835

Please sign in to comment.