Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / swapcase.rb
1 #
2 #  swapcase.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(:swapcase, :type => :rvalue, :doc => <<-DOC
7     This function will swap the existing case of a string.
8
9     *Examples:*
10
11         swapcase("aBcD")
12
13     Would result in: "AbCd"
14     DOC
15              ) do |arguments|
16
17     raise(Puppet::ParseError, "swapcase(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
18
19     value = arguments[0]
20
21     unless value.is_a?(Array) || value.is_a?(String)
22       raise(Puppet::ParseError, 'swapcase(): Requires either array or string to work with')
23     end
24
25     result = if value.is_a?(Array)
26                # Numbers in Puppet are often string-encoded which is troublesome ...
27                value.map { |i| i.is_a?(String) ? i.swapcase : i }
28              else
29                value.swapcase
30              end
31
32     return result
33   end
34 end
35
36 # vim: set ts=2 sw=2 et :