Skip to content

Commit

Permalink
0.3.0 - add currency argument to numberformat function (some docs and…
Browse files Browse the repository at this point in the history
… tests updates)
  • Loading branch information
elisherer committed Sep 15, 2024
1 parent 258903d commit b1546ad
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion docs/docs/functions/numberformat.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,7 +87,7 @@ Formats a number
123456789.87654321
```
```transformers
"$$numberformat(CURRENCY,en-GB):$"
"$$numberformat(CURRENCY,en-GB,GBP):$"
```
```json
"£123,456,789.88"
Expand Down
2 changes: 1 addition & 1 deletion java/json-transform/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group 'co.nlighten'
version = '0.2.5'
version = '0.3.0'

ext {
gsonVersion = "2.10.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import co.nlighten.jsontransform.functions.annotations.*;

import java.text.NumberFormat;
import java.util.Currency;
import java.util.Locale;

/*
Expand All @@ -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<JE, JA extends Iterable<JE>, JO extends JE> extends TransformerFunction<JE, JA, JO> {
public TransformerFunctionNumberFormat(JsonAdapter<JE, JA, JO> adapter) {
Expand All @@ -59,7 +62,10 @@ public Object apply(FunctionContext<JE, JA, JO> 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(
Expand All @@ -70,4 +76,12 @@ public Object apply(FunctionContext<JE, JA, JO> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<JE, JA extends Iterable<JE>, JO extends JE> extends TransformerFunction<JE, JA, JO> {
public TransformerFunctionObject(JsonAdapter<JE, JA, JO> adapter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b1546ad

Please sign in to comment.