Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / strip.rb
1 #
2 #  strip.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:strip, :type => :rvalue, :doc => <<-DOC
6     @summary
7       This function removes leading and trailing whitespace from a string or from
8       every string inside an array.
9
10     @return
11       String or Array converted
12
13     @example **Usage**
14
15       strip("    aaa   ")
16       Would result in: "aaa"
17
18     > *Note:*: from Puppet 6.0.0, the compatible function with the same name in Puppet core
19     will be used instead of this function.
20     DOC
21              ) do |arguments|
22
23     raise(Puppet::ParseError, "strip(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
24
25     value = arguments[0]
26
27     unless value.is_a?(Array) || value.is_a?(String)
28       raise(Puppet::ParseError, 'strip(): Requires either array or string to work with')
29     end
30
31     result = if value.is_a?(Array)
32                value.map { |i| i.is_a?(String) ? i.strip : i }
33              else
34                value.strip
35              end
36
37     return result
38   end
39 end
40
41 # vim: set ts=2 sw=2 et :