Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / regexpescape.rb
1 #
2 #  regexpescape.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:regexpescape, :type => :rvalue, :doc => <<-DOC
6     Regexp escape a string or array of strings.
7     Requires either a single string or an array as an input.
8     DOC
9   ) do |arguments| # rubocop:disable Layout/ClosingParenthesisIndentation
10     raise(Puppet::ParseError, "regexpescape(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
11
12     value = arguments[0]
13
14     unless value.is_a?(Array) || value.is_a?(String)
15       raise(Puppet::ParseError, 'regexpescape(): Requires either array or string to work with')
16     end
17
18     result = if value.is_a?(Array)
19                # Numbers in Puppet are often string-encoded which is troublesome ...
20                value.map { |i| i.is_a?(String) ? Regexp.escape(i) : i }
21              else
22                Regexp.escape(value)
23              end
24
25     return result
26   end
27 end
28
29 # vim: set ts=2 sw=2 et :