use of com.squarespace.cldrengine.api.Decimal in project template-compiler by Squarespace.
the class DecimalFormatter method apply.
@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
Variable var = variables.first();
Decimal number = GeneralUtils.nodeToDecimal(var.node());
if (number == null) {
var.setMissing();
return;
}
CLDR cldr = ctx.cldr();
DecimalFormatOptions opts = (DecimalFormatOptions) args.getOpaque();
String result = cldr.Numbers.formatDecimal(number, opts);
var.set(result);
}
use of com.squarespace.cldrengine.api.Decimal in project template-compiler by Squarespace.
the class MessageFormats method decimal.
/**
* Number / decimal message formatter.
*/
private String decimal(List<Object> args, List<String> options) {
if (args.isEmpty()) {
return "";
}
JsonNode node = (JsonNode) args.get(0);
if (node == null) {
return "";
}
Decimal value = this.converter.asDecimal(node);
DecimalFormatOptions opts = OptionParsers.decimal(options);
return cldr.Numbers.formatDecimal(value, opts);
}
use of com.squarespace.cldrengine.api.Decimal in project template-compiler by Squarespace.
the class CommerceUtils method getLowestPriceAmongVariants.
public static JsonNode getLowestPriceAmongVariants(JsonNode item) {
ProductType type = getProductType(item);
JsonNode structuredContent = item.path("structuredContent");
switch(type) {
case PHYSICAL:
case SERVICE:
case GIFT_CARD:
JsonNode variants = structuredContent.path("variants");
if (variants.size() == 0) {
return DEFAULT_MONEY_NODE;
}
JsonNode first = variants.get(0);
JsonNode moneyNode = isTruthy(first.path("onSale")) ? first.path("salePriceMoney") : first.path("priceMoney");
Decimal price = getAmountFromMoneyNode(moneyNode);
for (int i = 1; i < variants.size(); i++) {
JsonNode var = variants.get(i);
JsonNode currentMoneyNode = isTruthy(var.path("onSale")) ? var.path("salePriceMoney") : var.path("priceMoney");
Decimal current = getAmountFromMoneyNode(currentMoneyNode);
if (current.compare(price) < 0) {
price = current;
moneyNode = currentMoneyNode;
}
}
return moneyNode;
case DIGITAL:
JsonNode digitalMoneyNode = structuredContent.path("priceMoney");
return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;
default:
return DEFAULT_MONEY_NODE;
}
}
use of com.squarespace.cldrengine.api.Decimal in project template-compiler by Squarespace.
the class CommerceUtils method getSalePriceMoneyNode.
public static JsonNode getSalePriceMoneyNode(JsonNode item) {
ProductType type = getProductType(item);
JsonNode structuredContent = item.path("structuredContent");
switch(type) {
case PHYSICAL:
case SERVICE:
JsonNode variants = structuredContent.path("variants");
if (variants.size() == 0) {
return DEFAULT_MONEY_NODE;
}
Decimal salePrice = null;
JsonNode salePriceNode = null;
for (int i = 0; i < variants.size(); i++) {
JsonNode variant = variants.path(i);
JsonNode priceMoney = variant.path("salePriceMoney");
Decimal price = getAmountFromMoneyNode(priceMoney);
if (isTruthy(variant.path("onSale")) && (salePriceNode == null || price.compare(salePrice) < 0)) {
salePrice = price;
salePriceNode = priceMoney;
}
}
return (salePriceNode == null) ? DEFAULT_MONEY_NODE : salePriceNode;
case DIGITAL:
JsonNode digitalMoneyNode = structuredContent.path("salePriceMoney");
return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;
case GIFT_CARD:
default:
return DEFAULT_MONEY_NODE;
}
}
use of com.squarespace.cldrengine.api.Decimal in project template-compiler by Squarespace.
the class CommerceUtils method getHighestPriceAmongVariants.
public static JsonNode getHighestPriceAmongVariants(JsonNode item) {
ProductType type = getProductType(item);
JsonNode structuredContent = item.path("structuredContent");
switch(type) {
case PHYSICAL:
case SERVICE:
case GIFT_CARD:
JsonNode variants = structuredContent.path("variants");
if (variants.size() == 0) {
return DEFAULT_MONEY_NODE;
}
JsonNode moneyNode = variants.get(0).path("priceMoney");
Decimal price = getAmountFromMoneyNode(moneyNode);
for (int i = 1; i < variants.size(); i++) {
JsonNode currMoneyNode = variants.get(i).path("priceMoney");
Decimal curr = getAmountFromMoneyNode(currMoneyNode);
if (curr.compare(price) > 0) {
price = curr;
moneyNode = currMoneyNode;
}
}
return moneyNode;
case DIGITAL:
JsonNode digitalMoneyNode = structuredContent.path("priceMoney");
return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;
default:
return DEFAULT_MONEY_NODE;
}
}
Aggregations