From b1546ad28cab9e0f626394978f5b2889cd51ee54 Mon Sep 17 00:00:00 2001 From: Eli Sherer Date: Sun, 15 Sep 2024 17:56:51 +0300 Subject: [PATCH] 0.3.0 - add currency argument to numberformat function (some docs and tests updates) --- docs/docs/functions/numberformat.md | 3 ++- java/json-transform/build.gradle | 2 +- .../TransformerFunctionNumberFormat.java | 16 +++++++++++++++- .../functions/TransformerFunctionObject.java | 2 +- .../TransformerFunctionNumberFormatTest.java | 4 ++-- .../TransformerFunctionNumberParseTest.java | 1 + 6 files changed, 22 insertions(+), 6 deletions(-) diff --git a/docs/docs/functions/numberformat.md b/docs/docs/functions/numberformat.md index 0ac97a2..f85a162 100644 --- a/docs/docs/functions/numberformat.md +++ b/docs/docs/functions/numberformat.md @@ -23,6 +23,7 @@ Formats a number | `grouping` | `String` | Single character string | `,` | A custom character to be used for grouping | | `decimal` | `String` | Single character string | `.` | A custom character to be used for decimal point | | `radix` | `Integer` | | `10` | Radix to be used for formatting input | +| `currency` | `String` | [ISO-4217 currency code](https://www.iso.org/iso-4217-currency-codes.html) | Yes (when `type == CURRENCY`) | Currency to use in format | ## Examples @@ -86,7 +87,7 @@ Formats a number 123456789.87654321 ``` ```transformers -"$$numberformat(CURRENCY,en-GB):$" +"$$numberformat(CURRENCY,en-GB,GBP):$" ``` ```json "£123,456,789.88" diff --git a/java/json-transform/build.gradle b/java/json-transform/build.gradle index 85270ce..d7a9eaf 100644 --- a/java/json-transform/build.gradle +++ b/java/json-transform/build.gradle @@ -9,7 +9,7 @@ plugins { } group 'co.nlighten' -version = '0.2.5' +version = '0.3.0' ext { gsonVersion = "2.10.1" diff --git a/java/json-transform/src/main/java/co/nlighten/jsontransform/functions/TransformerFunctionNumberFormat.java b/java/json-transform/src/main/java/co/nlighten/jsontransform/functions/TransformerFunctionNumberFormat.java index 0db421d..1cc78b9 100644 --- a/java/json-transform/src/main/java/co/nlighten/jsontransform/functions/TransformerFunctionNumberFormat.java +++ b/java/json-transform/src/main/java/co/nlighten/jsontransform/functions/TransformerFunctionNumberFormat.java @@ -8,6 +8,7 @@ import co.nlighten.jsontransform.functions.annotations.*; import java.text.NumberFormat; +import java.util.Currency; import java.util.Locale; /* @@ -33,6 +34,8 @@ description = "(DECIMAL) A custom character to be used for decimal point (default is .)") @ArgumentType(value = "radix", type = ArgType.Integer, position = 1, defaultInteger = 10, description = "(BASE) Radix to be used for formatting input") +@ArgumentType(value = "currency", type = ArgType.String, position = 2, defaultIsNull = true, + description = "(CURRENCY) Currency to use in format") @OutputType(ArgType.String) public class TransformerFunctionNumberFormat, JO extends JE> extends TransformerFunction { public TransformerFunctionNumberFormat(JsonAdapter adapter) { @@ -59,7 +62,10 @@ public Object apply(FunctionContext context) { context.getString("grouping"), context.getString("decimal") ); - case "CURRENCY" -> NumberFormat.getCurrencyInstance(resolvedLocale); + case "CURRENCY" -> getCurrencyFormatter( + resolvedLocale, + context.getString("currency") + ); case "PERCENT" -> NumberFormat.getPercentInstance(resolvedLocale); case "INTEGER" -> NumberFormat.getIntegerInstance(resolvedLocale); case "COMPACT" -> NumberFormat.getCompactNumberInstance( @@ -70,4 +76,12 @@ public Object apply(FunctionContext context) { }; return formatter.format(input); } + + private NumberFormat getCurrencyFormatter(Locale resolvedLocale, String currency) { + var nf = NumberFormat.getCurrencyInstance(resolvedLocale); + if (!FunctionHelpers.isNullOrEmpty(currency)) { + nf.setCurrency(Currency.getInstance(currency)); + } + return nf; + } } diff --git a/java/json-transform/src/main/java/co/nlighten/jsontransform/functions/TransformerFunctionObject.java b/java/json-transform/src/main/java/co/nlighten/jsontransform/functions/TransformerFunctionObject.java index 77275bd..e379f0d 100644 --- a/java/json-transform/src/main/java/co/nlighten/jsontransform/functions/TransformerFunctionObject.java +++ b/java/json-transform/src/main/java/co/nlighten/jsontransform/functions/TransformerFunctionObject.java @@ -16,7 +16,7 @@ @Aliases("object") @Documentation(value = "Reduces an array of entries into an object", notes = "- Entry is in the form of [ key, value ]") -@InputType(ArgType.Array) +@InputType(ArgType.ArrayOfArray) @OutputType(ArgType.Object) public class TransformerFunctionObject, JO extends JE> extends TransformerFunction { public TransformerFunctionObject(JsonAdapter adapter) { diff --git a/java/json-transform/src/test/java/co/nlighten/jsontransform/functions/TransformerFunctionNumberFormatTest.java b/java/json-transform/src/test/java/co/nlighten/jsontransform/functions/TransformerFunctionNumberFormatTest.java index 128c49c..1828835 100644 --- a/java/json-transform/src/test/java/co/nlighten/jsontransform/functions/TransformerFunctionNumberFormatTest.java +++ b/java/json-transform/src/test/java/co/nlighten/jsontransform/functions/TransformerFunctionNumberFormatTest.java @@ -28,14 +28,14 @@ void apply() { // assertTransformation(val, "$$numberformat(CURRENCY):$", NumberFormat.getCurrencyInstance(FunctionHelpers.DEFAULT_LOCALE).format(val)); - assertTransformation(val, "$$numberformat(CURRENCY,en-GB):$", NumberFormat.getCurrencyInstance(Locale.UK).format(val)); + assertTransformation(val, "$$numberformat(CURRENCY,en-GB,GBP):$", NumberFormat.getCurrencyInstance(Locale.UK).format(val)); assertTransformation(val,"$$numberformat(PERCENT):$", NumberFormat.getPercentInstance(FunctionHelpers.DEFAULT_LOCALE).format(val)); assertTransformation(val,"$$numberformat(INTEGER):$", NumberFormat.getIntegerInstance(FunctionHelpers.DEFAULT_LOCALE).format(val)); assertTransformation(val,"$$numberformat(COMPACT):$", NumberFormat.getCompactNumberInstance(FunctionHelpers.DEFAULT_LOCALE, NumberFormat.Style.SHORT).format(val)); assertTransformation(val,"$$numberformat(COMPACT,en-US,LONG):$", NumberFormat.getCompactNumberInstance(Locale.forLanguageTag("en-US"), NumberFormat.Style.LONG).format(val)); // literal assertTransformation(val, "$$numberformat(CURRENCY):$", "$123,456,789.88"); - assertTransformation(val, "$$numberformat(CURRENCY,en-GB):$", "£123,456,789.88"); + assertTransformation(val, "$$numberformat(CURRENCY,en-GB,GBP):$", "£123,456,789.88"); assertTransformation(val,"$$numberformat(PERCENT):$", "12,345,678,988%"); assertTransformation(val,"$$numberformat(INTEGER):$", "123,456,790"); assertTransformation(val,"$$numberformat(COMPACT):$", "123M"); diff --git a/java/json-transform/src/test/java/co/nlighten/jsontransform/functions/TransformerFunctionNumberParseTest.java b/java/json-transform/src/test/java/co/nlighten/jsontransform/functions/TransformerFunctionNumberParseTest.java index d48cae9..e77c918 100644 --- a/java/json-transform/src/test/java/co/nlighten/jsontransform/functions/TransformerFunctionNumberParseTest.java +++ b/java/json-transform/src/test/java/co/nlighten/jsontransform/functions/TransformerFunctionNumberParseTest.java @@ -20,6 +20,7 @@ void apply() { assertTransformation(FunctionHelpers.getDecimalFormatter(Locale.forLanguageTag("en-US"), "#,##0.00", ".", ",").format(val),"$$numberparse('#,##0.00',en-US,'.',','):$", val); // literal assertTransformation("123,456,789.88","$$numberparse('#,##0.00'):$", val); + assertTransformation("123.456.789,88","$$numberparse('#,##0.00',de):$", val); assertTransformation("123.456.789,88","$$numberparse('#,##0.00',en-US,'.',','):$", val); // HEX