Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / downcase.rb
1 #
2 #  downcase.rb
3 #  Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
4 #
5
6 module Puppet::Parser::Functions
7   newfunction(:downcase, :type => :rvalue, :doc => <<-EOS
8 Converts the case of a string or all strings in an array to lower case.
9     EOS
10   ) do |arguments|
11
12     raise(Puppet::ParseError, "downcase(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size < 1
13
14     value = arguments[0]
15
16     unless value.is_a?(Array) || value.is_a?(String)
17       raise(Puppet::ParseError, 'downcase(): Requires either array or string to work with')
18     end
19
20     if value.is_a?(Array)
21       # Numbers in Puppet are often string-encoded which is troublesome ...
22       result = value.collect { |i| i.is_a?(String) ? i.downcase : i }
23     else
24       result = value.downcase
25     end
26
27     return result
28   end
29 end
30
31 # vim: set ts=2 sw=2 et :