Skip to content

Commit

Permalink
Merge pull request #981 from ferranpm/add-delimiter_pattern-support
Browse files Browse the repository at this point in the history
Allow specifying a delimiter pattern
  • Loading branch information
semmons99 authored Mar 11, 2021
2 parents 61c8de9 + c5ade17 commit 4ba42d1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Edwin Vlieg
Eloy
Evan Alter
Exoth
Ferran Pelayo Monfort
Filipe Goncalves
Francisco Trindade
François Beausoleil
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 6.15.0

- Add :delimiter_pattern option to the Formatter

## 6.14.1

- Fix CHF format regression introduced in v6.14.0
Expand Down
19 changes: 9 additions & 10 deletions lib/money/money/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@ class Formatter
# Money.new(89000, :btc).format(drop_trailing_zeros: true) #=> B⃦0.00089
# Money.new(110, :usd).format(drop_trailing_zeros: true) #=> $1.1
#
# @option rules [Boolean] :delimiter_pattern (/(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/) Regular expression to set the placement
# for the thousands delimiter
#
# @example
# Money.new(89000, :btc).format(delimiter_pattern: /(\d)(?=\d)/) #=> B⃦8,9,0.00
#
# @option rules [String] :format (nil) Provide a template for formatting. `%u` will be replaced
# with the symbol (if present) and `%n` will be replaced with the number.
#
Expand Down Expand Up @@ -330,7 +336,9 @@ def free_text

def format_whole_part(value)
# Apply thousands_separator
value.gsub regexp_format, "\\1#{thousands_separator}"
value.gsub(rules[:delimiter_pattern]) do |digit_to_delimit|
"#{digit_to_delimit}#{thousands_separator}"
end
end

def extract_whole_and_decimal_parts
Expand Down Expand Up @@ -366,15 +374,6 @@ def lookup(key)
(Money.locale_backend && Money.locale_backend.lookup(key, currency)) || DEFAULTS[key]
end

def regexp_format
if rules[:south_asian_number_formatting]
# from http://blog.revathskumar.com/2014/11/regex-comma-seperated-indian-currency-format.html
/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/
else
/(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/
end
end

def symbol_value_from(rules)
if rules.has_key?(:symbol)
if rules[:symbol] === true
Expand Down
10 changes: 10 additions & 0 deletions lib/money/money/formatting_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def initialize(currency, *raw_rules)
@rules = localize_formatting_rules(@rules)
@rules = translate_formatting_rules(@rules) if @rules[:translate]
@rules[:format] ||= determine_format_from_formatting_rules(@rules)
@rules[:delimiter_pattern] ||= delimiter_pattern_rule(@rules)

warn_about_deprecated_rules(@rules)
end
Expand Down Expand Up @@ -90,6 +91,15 @@ def determine_format_from_formatting_rules(rules)
end
end

def delimiter_pattern_rule(rules)
if rules[:south_asian_number_formatting]
# from http://blog.revathskumar.com/2014/11/regex-comma-seperated-indian-currency-format.html
/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/
else
I18n.t('number.currency.format.delimiter_pattern', default: nil) || /(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/
end
end

def symbol_position_from(rules)
if rules.has_key?(:symbol_position)
if [:before, :after].include?(rules[:symbol_position])
Expand Down
2 changes: 1 addition & 1 deletion lib/money/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Money
VERSION = '6.14.1'
VERSION = '6.15.0'
end
6 changes: 5 additions & 1 deletion spec/money/formatting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
I18n.locale = :de
I18n.backend.store_translations(
:de,
number: { currency: { format: { delimiter: ".", separator: "," } } }
number: { currency: { format: { delimiter: ".", separator: ",", delimiter_pattern: /(\d)(?=\d)/ } } }
)
end

Expand All @@ -105,6 +105,10 @@
it "should use ',' as the decimal mark" do
expect(money.decimal_mark).to eq ','
end

it "should use delimiter pattern" do
expect(Money.new(1_456_00, "EUR").format).to eq "€1.4.5.6,00"
end
end

context "with number.currency.symbol.*" do
Expand Down

0 comments on commit 4ba42d1

Please sign in to comment.