Line data Source code
1 : import 'package:money2/money2.dart';
2 :
3 : /// Extension on [Money] to add improved serialization and formatting methods.
4 : extension MoneyFixer on Money {
5 : /// Creates a Money from a [Fixed] [amount].
6 : ///
7 : /// The [amount] is scaled to match the currency selected via
8 : /// [currency].
9 2 : static Money parseWithCurrencyImproved(
10 : String amount,
11 : Currency currency, {
12 : int? scale,
13 : }) =>
14 2 : Money.fromFixedWithCurrency(
15 4 : Fixed.parse(amount, scale: scale ?? currency.decimalDigits),
16 : currency,
17 : );
18 :
19 : /// Formats a [Money] value into a String according to the
20 : /// passed [pattern].
21 : ///
22 : /// If [invertSeparator] is true then the role of the '.' and ',' are
23 : /// rroflsed. By default the '.' is used as the decimal separator
24 : /// whilst the ',' is used as the grouping separator.
25 : ///
26 : /// S outputs the currencies symbol e.g. $.
27 : /// 0 A single digit
28 : /// # A single digit, omitted if the value is zero (works only for integer
29 : /// part and as last fractional symbol as flag for trimming zeros)
30 : /// . or , Decimal separator dependant on [invertSeparator]
31 : /// - Minus sign
32 : /// , or . Grouping separator dependant on [invertSeparator]
33 : /// space Space character.
34 1 : String formatImproved({String? pattern, bool invertSeparator = false}) {
35 2 : final p = pattern ?? currency.pattern;
36 : final decimalSeparator = invertSeparator ? ',' : '.';
37 3 : return p.replaceAllMapped(RegExp(r'([0#.\-,]+)'), (m) {
38 3 : final result = amount.format(m[0]!, invertSeparator: invertSeparator);
39 3 : final trimZerosRight = RegExp(r'#$').hasMatch(m[0]!);
40 : return trimZerosRight
41 : ? result
42 2 : .replaceFirst(RegExp(r'0*$'), '')
43 3 : .replaceFirst(RegExp('\\$decimalSeparator\$'), '')
44 : : result;
45 5 : }).replaceAllMapped(RegExp('S'), (m) => currency.symbol);
46 : }
47 :
48 : /// Serializes a [Money] value into a Map<String, dynamic>.
49 1 : Map<String, dynamic> toJsonImproved() {
50 1 : return {
51 2 : 'amount': amount.toJsonImproved(),
52 2 : 'currency': currency.toJsonImproved(),
53 : };
54 : }
55 :
56 : /// Deserializes a [Money] value from a Map<String, dynamic>.
57 1 : static Money fromJsonImproved(Map<String, dynamic> json) {
58 1 : return Money.fromFixedWithCurrency(
59 2 : FixedFixer.fromJsonImproved(json['amount'] as Map<String, dynamic>),
60 2 : CurrencyFixer.fromJsonImproved(json['currency'] as Map<String, dynamic>),
61 : );
62 : }
63 : }
64 :
65 : /// Extension on [Currency] to add improved serialization and formatting methods
66 : extension CurrencyFixer on Currency {
67 : /// Serializes a [Currency] value into a Map<String, dynamic>.
68 1 : Map<String, dynamic> toJsonImproved() {
69 1 : return {
70 1 : 'isoCode': isoCode,
71 1 : 'decimalDigits': decimalDigits,
72 1 : 'symbol': symbol,
73 1 : 'pattern': pattern,
74 1 : 'groupSeparator': groupSeparator,
75 1 : 'decimalSeparator': decimalSeparator,
76 1 : 'country': country,
77 1 : 'unit': unit,
78 1 : 'name': name,
79 : };
80 : }
81 :
82 : /// Deserializes a [Currency] value from a Map<String, dynamic>.
83 1 : static Currency fromJsonImproved(Map<String, dynamic> json) {
84 1 : return Currency.create(
85 1 : json['isoCode'] as String,
86 1 : json['decimalDigits'] as int,
87 1 : symbol: (json['symbol'] ?? r'$') as String,
88 1 : pattern: (json['pattern'] ?? Currency.defaultPattern) as String,
89 1 : groupSeparator: (json['groupSeparator'] ?? ',') as String,
90 1 : decimalSeparator: (json['decimalSeparator'] ?? '.') as String,
91 1 : country: (json['country'] ?? '') as String,
92 1 : unit: (json['unit'] ?? '') as String,
93 1 : name: (json['name'] ?? '') as String,
94 : );
95 : }
96 : }
97 :
98 : /// Extension on [Fixed] to add improved serialization and formatting methods.
99 : extension FixedFixer on Fixed {
100 : /// Serializes a [Fixed] value into a Map<String, dynamic>.
101 1 : Map<String, dynamic> toJsonImproved() {
102 1 : return {
103 2 : 'minorUnits': minorUnits.toString(),
104 1 : 'scale': scale,
105 : };
106 : }
107 :
108 : /// Deserializes a [Fixed] value from a Map<String, dynamic>.
109 1 : static Fixed fromJsonImproved(Map<String, dynamic> json) {
110 1 : return Fixed.fromBigInt(
111 2 : BigInt.parse(json['minorUnits'] as String),
112 1 : scale: (json['scale'] ?? 2) as int,
113 : );
114 : }
115 : }
|