Skip to content

Commit

Permalink
Merge pull request #520 from p4535992/master
Browse files Browse the repository at this point in the history
Add a check for negative number on getStringFromCurrencies
  • Loading branch information
Haxxer authored Mar 3, 2024
2 parents 9698a9d + 4b2f9fd commit 7250001
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
11 changes: 10 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,9 @@ game.itempiles.API.updateCurrencies(tokenOrActor, "10gp");
// Set the value on the GP currency with the quantity 10 and on the SP currency with the quantity 5 to the target
game.itempiles.API.updateCurrencies(tokenOrActor, "10gp 5sp");

// Set the value on the GP currency with the quantity 1d4 and on the SP currency with the quantity 1d3 to the target
game.itempiles.API.updateCurrencies(actor, "1d4gp 1d3sp")

```

---
Expand Down Expand Up @@ -918,7 +921,10 @@ const tokenOrActor = game.actors.getName("Bharash");
game.itempiles.API.addCurrencies(tokenOrActor, "10gp");

// Add 10 GP and 5 SP to the target
game.itempiles.API.removeCurrencies(tokenOrActor, "10gp 5sp");
game.itempiles.API.addCurrencies(tokenOrActor, "10gp 5sp");

// Add 1d4 GP to the target
game.itempiles.API.addCurrencies(tokenOrActor, "1d4gp");

```

Expand Down Expand Up @@ -952,6 +958,9 @@ game.itempiles.API.removeCurrencies(tokenOrActor, "10gp");
// Remove 10 GP and 5 SP from the target
game.itempiles.API.removeCurrencies(tokenOrActor, "10gp 5sp");

// Remove 1d4 GP from the target
game.itempiles.API.removeCurrencies(tokenOrActor, "1d4gp");

```

---
Expand Down
3 changes: 3 additions & 0 deletions src/API/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,9 @@ class API {
if (typeof currency.cost !== "number") {
throw Helpers.custom_error("getStringFromCurrencies | currency.cost must be of type number");
}
if (currency.cost < 0) {
throw Helpers.custom_error("getStringFromCurrencies | currency.cost cannot be a negative number");
}
// Is optional
// if (typeof currency.percent !== "boolean") {
// throw Helpers.custom_error("getStringFromCurrencies | currency.percent must be of type boolean");
Expand Down
22 changes: 17 additions & 5 deletions src/helpers/pile-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,12 +791,20 @@ export function getCurrenciesAbbreviations() {
let primaryAbbreviationsArray = game.itempiles.API.CURRENCIES
.filter(currency => currency.abbreviation)
.map(currency => {
return currency.abbreviation?.replace("{#}", "");
if(currency.abbreviation?.includes("{#}")) {
return currency.abbreviation?.replace("{#}", "");
} else {
return currency.abbreviation || "";
}
});
let secondaryAbbreviationsArray = game.itempiles.API.SECONDARY_CURRENCIES
.filter(currency => currency.abbreviation)
.map(currency => {
return currency.abbreviation?.replace("{#}", "");
if(currency.abbreviation?.includes("{#}")) {
return currency.abbreviation?.replace("{#}", "");
} else {
return currency.abbreviation || "";
}
});
let allAbbreviationsArray = primaryAbbreviationsArray.concat(secondaryAbbreviationsArray);
return allAbbreviationsArray;
Expand All @@ -811,21 +819,25 @@ export function getStringFromCurrencies(currencies) {
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);
Helpers.custom_error(`getStringFromCurrencies | The currency element is not valid with cost '${cost}' and abbreviation '${abbreviation}'`, true);
return "";
}
// Check abbreviation by case unsensitive...
const indexAbbreviation = allAbbreviationsArray.findIndex(a => {
return a?.replace("{#}", "")?.toLowerCase() === abbreviation?.replace("{#}", "")?.toLowerCase();
});
if (indexAbbreviation === -1) {
Helpers.custom_error(`getStringFromCurrencies | The currency abbreviation '${abbreviation?.replace("{#}", "")}' is not registered`, false);
Helpers.custom_error(`getStringFromCurrencies | The currency abbreviation '${abbreviation?.replace("{#}", "")}' is not registered`, true);
return "";
}
if (price.percent && abbreviation.includes("%")) {
abbreviation = abbreviation.replaceAll("%", "")
}
return abbreviation.replace("{#}", price.cost)
if(abbreviation.includes("{#}")) {
return abbreviation.replace("{#}", price.cost)
} else {
return price.cost+abbreviation;
}
}).join(" ");

return priceString ? priceString.trim() : "";
Expand Down

0 comments on commit 7250001

Please sign in to comment.