Update stdlib and concat to 6.1.0 both
[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     @summary
7       Regexp escape a string or array of strings.
8       Requires either a single string or an array as an input.
9     @return [String]
10       A string of characters with metacharacters converted to their escaped form.
11     DOC
12   ) do |arguments| # rubocop:disable Layout/ClosingParenthesisIndentation
13     raise(Puppet::ParseError, "regexpescape(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
14
15     value = arguments[0]
16
17     unless value.is_a?(Array) || value.is_a?(String)
18       raise(Puppet::ParseError, 'regexpescape(): Requires either array or string to work with')
19     end
20
21     result = if value.is_a?(Array)
22                # Numbers in Puppet are often string-encoded which is troublesome ...
23                value.map { |i| i.is_a?(String) ? Regexp.escape(i) : i }
24              else
25                Regexp.escape(value)
26              end
27
28     return result
29   end
30 end
31
32 # vim: set ts=2 sw=2 et :