Skip to content

Commit

Permalink
number_to_currency now returns a String instead of writing to the view (
Browse files Browse the repository at this point in the history
  • Loading branch information
jwoertink authored and paulcsmith committed Jun 16, 2019
1 parent cf8e7e6 commit 9ddb4dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
20 changes: 10 additions & 10 deletions spec/lucky/number_to_currency_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,44 @@ end
describe Lucky::NumberToCurrency do
describe "number_to_currency" do
it "accepts Float" do
view.number_to_currency(29.92).to_s.should eq(HTML.escape("$29.92"))
view.number_to_currency(29.92).should eq(HTML.escape("$29.92"))
end

it "accepts String" do
view.number_to_currency("92.29").to_s.should eq(HTML.escape("$92.29"))
view.number_to_currency("92.29").should eq(HTML.escape("$92.29"))
end

it "accepts Integer" do
view.number_to_currency(92).to_s.should eq(HTML.escape("$92.00"))
view.number_to_currency(92).should eq(HTML.escape("$92.00"))
end

describe "options" do
it "accepts precision" do
view.number_to_currency(29.929_123, precision: 3).to_s.should eq(HTML.escape("$29.929"))
view.number_to_currency(29.929_123, precision: 3).should eq(HTML.escape("$29.929"))
end

it "accepts unit" do
view.number_to_currency(29.92, unit: "CAD").to_s.should eq "CAD29.92"
view.number_to_currency(29.92, unit: "CAD").should eq "CAD29.92"
end

it "accepts separator" do
view.number_to_currency(29.92, separator: "-").to_s.should eq(HTML.escape("$29-92"))
view.number_to_currency(29.92, separator: "-").should eq(HTML.escape("$29-92"))
end

it "accepts delimiter" do
view.number_to_currency(1_234_567_890.29, delimiter: "-").to_s.should eq(HTML.escape("$1-234-567-890.29"))
view.number_to_currency(1_234_567_890.29, delimiter: "-").should eq(HTML.escape("$1-234-567-890.29"))
end

it "accepts delimiter pattern" do
view.number_to_currency(1_230_000, delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/).to_s.should eq(HTML.escape("$12,30,000.00"))
view.number_to_currency(1_230_000, delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/).should eq(HTML.escape("$12,30,000.00"))
end

it "accepts format" do
view.number_to_currency("1234567890.50", format: "%n ** %u").to_s.should eq(HTML.escape("1,234,567,890.50 ** $"))
view.number_to_currency("1234567890.50", format: "%n ** %u").should eq(HTML.escape("1,234,567,890.50 ** $"))
end

it "accepts negative format" do
view.number_to_currency("-1234567890.50", negative_format: "%n ** - %u").to_s.should eq(HTML.escape("1,234,567,890.50 ** - $"))
view.number_to_currency("-1234567890.50", negative_format: "%n ** - %u").should eq(HTML.escape("1,234,567,890.50 ** - $"))
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/lucky/page_helpers/number_to_currency.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Lucky::NumberToCurrency
delimiter : String = DEFAULT_DELIMITER,
delimiter_pattern : Regex = DEFAULT_DELIMITER_REGEX,
format : String = DEFAULT_FORMAT,
negative_format : String = DEFAULT_FORMAT)
negative_format : String = DEFAULT_FORMAT) : String
value = value.to_s

if value.to_f.sign == -1
Expand All @@ -30,6 +30,6 @@ module Lucky::NumberToCurrency

number = "#{left}#{separator}#{right}"

text format.gsub("%n", number).gsub("%u", unit)
format.gsub("%n", number).gsub("%u", unit)
end
end

0 comments on commit 9ddb4dd

Please sign in to comment.