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