Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / chomp.rb
1 #
2 #  chomp.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:chomp, :type => :rvalue, :doc => <<-DOC
6     Removes the record separator from the end of a string or an array of
7     strings, for example `hello\n` becomes `hello`.
8     Requires a single string or array as an input.
9
10     Note: from Puppet 6.0.0, the compatible function with the same name in Puppet core
11     will be used instead of this function.
12     DOC
13              ) do |arguments|
14
15     raise(Puppet::ParseError, "chomp(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
16
17     value = arguments[0]
18
19     unless value.is_a?(Array) || value.is_a?(String)
20       raise(Puppet::ParseError, 'chomp(): Requires either array or string to work with')
21     end
22
23     result = if value.is_a?(Array)
24                # Numbers in Puppet are often string-encoded which is troublesome ...
25                value.map { |i| i.is_a?(String) ? i.chomp : i }
26              else
27                value.chomp
28              end
29
30     return result
31   end
32 end
33
34 # vim: set ts=2 sw=2 et :