Update stdlib and concat to 6.1.0 both
[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     @summary
8       This function will swap the existing case of a string.
9
10     @return
11       string with uppercase alphabetic characters converted to lowercase and lowercase characters converted to uppercase
12
13     @example **Usage**
14
15       swapcase("aBcD")
16       Would result in: "AbCd"
17     DOC
18              ) do |arguments|
19
20     raise(Puppet::ParseError, "swapcase(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
21
22     value = arguments[0]
23
24     unless value.is_a?(Array) || value.is_a?(String)
25       raise(Puppet::ParseError, 'swapcase(): Requires either array or string to work with')
26     end
27
28     result = if value.is_a?(Array)
29                # Numbers in Puppet are often string-encoded which is troublesome ...
30                value.map { |i| i.is_a?(String) ? i.swapcase : i }
31              else
32                value.swapcase
33              end
34
35     return result
36   end
37 end
38
39 # vim: set ts=2 sw=2 et :