Skip to content

Commit

Permalink
Add Any#as_number_or_zero (#86)
Browse files Browse the repository at this point in the history
* Add Any#as_number_or_zero
* Improve error message for Any#as_number
  • Loading branch information
HCLarsen authored Jan 13, 2025
1 parent 0803388 commit e65f59e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
24 changes: 24 additions & 0 deletions spec/any_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "./spec_helper"

describe Liquid::Any do
it "determines if underlying value is a number or not" do
any = Any.new(5)
any.as_number?.should eq(5)

any = Any.new(5.0)
any.as_number?.should eq(5.0)

any = Any.new("5.0")
any.as_number?.should eq(5.0)

any = Any.new("Hello, world!")
any.as_number?.should be_nil
end

it "raises if underlying value is not a number" do
expect_raises(TypeCastError, "Cast to Number+ failed") do
any = Any.new("Hello, world!")
any.as_number
end
end
end
14 changes: 10 additions & 4 deletions src/liquid/any.cr
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,13 @@ module Liquid
Any.new(-raw)
end

# Checks that the underlying value is a numerical value, and returns its value.
# Returns 0 otherwise.
# Checks that the underlying value is a `Number`, and returns its value.
# Raises otherwise.
def as_number : Number
as_number? || 0
as_number? || raise TypeCastError.new("Cast to Number+ failed")
end

# Checks that the underlying value is a numerical value, and returns its value.
# Checks that the underlying value is a `Number`, and returns its value.
# Returns `nil` otherwise.
def as_number?
raw = @raw
Expand All @@ -289,6 +289,12 @@ module Liquid
end
end

# Checks that the underlying value is a `Number`, and returns its value.
# Returns 0 otherwise.
def as_number_or_zero : Number
as_number? || 0
end

# Returns a string representation of the underlying value.
def to_s(io : IO) : Nil
@raw.to_s(io)
Expand Down
4 changes: 2 additions & 2 deletions src/liquid/filters/at_least.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module Liquid::Filters
def self.filter(data : Any, args : Array(Any), options : Hash(String, Any)) : Any
raise FilterArgumentException.new("at_least filter expects one argument.") if args.size != 1

min_value = args.first.as_number
result = data.as_number
min_value = args.first.as_number_or_zero
result = data.as_number_or_zero
result = min_value if min_value > result

Any.new(result)
Expand Down
4 changes: 2 additions & 2 deletions src/liquid/filters/at_most.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ module Liquid::Filters
def self.filter(data : Any, args : Array(Any), options : Hash(String, Any)) : Any
raise FilterArgumentException.new("at_most filter expects one argument.") if args.size != 1

max_value = args.first.as_number
result = data.as_number
max_value = args.first.as_number_or_zero
result = data.as_number_or_zero
result = max_value if result > max_value

Any.new(result)
Expand Down
4 changes: 2 additions & 2 deletions src/liquid/filters/divided_by.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module Liquid::Filters
def self.filter(data : Any, args : Array(Any), options : Hash(String, Any)) : Any
raise FilterArgumentException.new("divided_by filter expects one argument.") if args.size != 1

lvalue = data.as_number
rvalue = args.first.as_number
lvalue = data.as_number_or_zero
rvalue = args.first.as_number_or_zero

raise FilterArgumentException.new("divided_by filter cannot divide by 0 or 0.0.") if rvalue.zero?
return Any.new(lvalue // rvalue) if rvalue.is_a?(Int) && lvalue.is_a?(Int)
Expand Down

0 comments on commit e65f59e

Please sign in to comment.