class JMESPath::Nodes::MinFunction

Public Instance Methods

call(args) click to toggle source
# File lib/jmespath/nodes/function.rb, line 255
def call(args)
  if args.count == 1
    values = args.first
  else
    return maybe_raise Errors::InvalidArityError, "function min() expects one argument"
  end
  if Array === values
    return nil if values.empty?
    first = values.first
    first_type = get_type(first)
    unless first_type == NUMBER_TYPE || first_type == STRING_TYPE
      msg = "function min() expects numeric or string values"
      return maybe_raise Errors::InvalidTypeError, msg
    end
    values.inject([first, first_type]) do |(min, min_type), v|
      v_type = get_type(v)
      if min_type == v_type
        v < min ? [v, v_type] : [min, min_type]
      else
        msg = "function min() encountered a type mismatch in sequence: "
        msg << "#{min_type}, #{v_type}"
        return maybe_raise Errors::InvalidTypeError, msg
      end
    end.first
  else
    return maybe_raise Errors::InvalidTypeError, "function min() expects an array"
  end
end