From 917ae7a6ab7fca5a320de845eb7f9a0b1930e655 Mon Sep 17 00:00:00 2001 From: Mike Angell Date: Mon, 16 Sep 2019 12:36:37 +1000 Subject: [PATCH] Switch to named inputs --- lib/liquid/standardfilters.rb | 15 ++++++----- test/integration/standard_filter_test.rb | 33 ++++++++++++------------ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index c5335ba8..bd497fab 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -392,13 +392,14 @@ module Liquid end # Defaults are passed as nil so systems can easily override - def format(input, n = nil, thousands = nil, decimal = nil) - n = 2 if n.nil? - thousands = " ".freeze if thousands.nil? - decimal = ".".freeze if decimal.nil? - return input if (precision = Utils.to_number(n).to_i) < 0 - whole_part, decimal_part = Kernel.format("%.#{precision}f", Utils.to_number(input)).split('.') - [whole_part.gsub(/(\d)(?=\d{3}+$)/, "\\1#{thousands}"), decimal_part].compact.join(decimal.to_s) + def format_number(input, options = {}) + options = {} unless options.is_a?(Hash) + precision = options['precision'] || 2 + delimiter = options['delimiter'] || " ".freeze + separator = options['separator'] || ".".freeze + return input if (prec = Utils.to_number(precision).to_i) < 0 + whole_part, decimal_part = Kernel.format("%.#{prec}f", Utils.to_number(input)).split('.') + [whole_part.gsub(/(\d)(?=\d{3}+$)/, "\\1#{delimiter}"), decimal_part].compact.join(separator.to_s) end def ceil(input) diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index 7856fede..6bb741f2 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -610,22 +610,23 @@ class StandardFiltersTest < Minitest::Test assert_template_result "4", "{{ price | round }}", 'price' => NumberLikeThing.new(4.3) end - def test_format - assert_template_result "4.60", "{{ input | format }}", 'input' => 4.6 - assert_template_result "4.30", "{{ '4.3' | format }}" - assert_template_result "4.56", "{{ input | format: 2 }}", 'input' => 4.5612 - assert_template_result "5", "{{ price | format: 0 }}", 'price' => NumberLikeThing.new(4.6) - assert_template_result "4", "{{ price | format: 0 }}", 'price' => NumberLikeThing.new(4.3) - assert_template_result "4.30", "{{ price | format: 2 }}", 'price' => NumberLikeThing.new(4.3) - assert_template_result "5.0000000", "{{ price | format: 7 }}", 'price' => 5 - assert_template_result "50", "{{ price | format: -1 }}", 'price' => 50 - assert_template_result "50.00", "{{ price | format: A }}", 'price' => 50 - assert_template_result "50.00", "{{ price | format: '2e' }}", 'price' => 50 - assert_template_result "50 000 000", "{{ price | format: 0 }}", 'price' => 50000000 - assert_template_result "50 000 000.00", "{{ price | format }}", 'price' => 50000000 - assert_template_result "50000000.00", "{{ price | format: 2, '', '.'}}", 'price' => 50000000 - assert_template_result "50$000$000#00", "{{ price | format: 2, '$', '#'}}", 'price' => 50000000 - assert_template_result "-50$000$000#00", "{{ price | format: 2, '$', '#'}}", 'price' => -50000000 + def test_format_number + assert_template_result "4.60", "{{ input | format_number }}", 'input' => 4.6 + assert_template_result "4.30", "{{ '4.3' | format_number }}" + assert_template_result "4.56", "{{ input | format_number: precision: 2 }}", 'input' => 4.5612 + assert_template_result "5", "{{ price | format_number: precision: 0 }}", 'price' => NumberLikeThing.new(4.6) + assert_template_result "4", "{{ price | format_number: precision: 0 }}", 'price' => NumberLikeThing.new(4.3) + assert_template_result "4.30", "{{ price | format_number: precision: 2 }}", 'price' => NumberLikeThing.new(4.3) + assert_template_result "5.0000000", "{{ price | format_number: precision: 7 }}", 'price' => 5 + assert_template_result "50", "{{ price | format_number: precision: -1 }}", 'price' => 50 + assert_template_result "50.00", "{{ price | format_number: precision: A }}", 'price' => 50 + assert_template_result "50.00", "{{ price | format_number: precision: '2e' }}", 'price' => 50 + assert_template_result "50 000 000", "{{ price | format_number: precision: 0 }}", 'price' => 50000000 + assert_template_result "50 000 000.00", "{{ price | format_number }}", 'price' => 50000000 + assert_template_result "50000000.00", "{{ price | format_number: precision: 2, delimiter: '', separator: '.'}}", 'price' => 50000000 + assert_template_result "50$000$000#00", "{{ price | format_number: precision: 2, delimiter: '$', separator:'#'}}", 'price' => 50000000 + assert_template_result "-50$000$000#00", "{{ price | format_number: precision: 2, delimiter: '$',separator: '#'}}", 'price' => -50000000 + assert_template_result "-50 000 000.00", "{{ price | format_number: precision: A, delimiter: A,separator: A}}", 'price' => -50000000 end def test_ceil