Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / capitalize.rb
1 #
2 #  capitalize.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 module Puppet::Parser::Functions
6   newfunction(:capitalize, :type => :rvalue, :doc => <<-DOC
7     @summary
8       **Deprecated** Capitalizes the first letter of a string or array of strings.
9
10     Requires either a single string or an array as an input.
11
12     > *Note:*
13       **Deprecated** from Puppet 6.0.0, yhis function has been replaced with a
14       built-in [`capitalize`](https://puppet.com/docs/puppet/latest/function.html#capitalize)
15       function.
16
17     @return [String] The converted String, if it was a String that was given
18     @return [Array[String]] The converted Array, if it was a Array that was given
19     DOC
20              ) do |arguments|
21
22     raise(Puppet::ParseError, "capitalize(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
23
24     value = arguments[0]
25
26     unless value.is_a?(Array) || value.is_a?(String)
27       raise(Puppet::ParseError, 'capitalize(): Requires either array or string to work with')
28     end
29
30     result = if value.is_a?(Array)
31                # Numbers in Puppet are often string-encoded which is troublesome ...
32                value.map { |i| i.is_a?(String) ? i.capitalize : i }
33              else
34                value.capitalize
35              end
36
37     return result
38   end
39 end