Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / grep.rb
1 #
2 # grep.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:grep, :type => :rvalue, :doc => <<-DOC
6     @summary
7       This function searches through an array and returns any elements that match
8       the provided regular expression.
9
10     @return
11       array of elements that match the provided regular expression.
12     @example Example Usage:
13       grep(['aaa','bbb','ccc','aaaddd'], 'aaa') # Returns ['aaa','aaaddd']
14
15     > **Note:** that since Puppet 4.0.0, the built-in
16     [`filter`](https://puppet.com/docs/puppet/latest/function.html#filter) function does
17     the "same" - as any logic can be used to filter, as opposed to just regular expressions:
18     ```['aaa', 'bbb', 'ccc', 'aaaddd']. filter |$x| { $x =~ 'aaa' }```
19     DOC
20              ) do |arguments|
21
22     if arguments.size != 2
23       raise(Puppet::ParseError, "grep(): Wrong number of arguments given #{arguments.size} for 2")
24     end
25
26     a = arguments[0]
27     pattern = Regexp.new(arguments[1])
28
29     a.grep(pattern)
30   end
31 end
32
33 # vim: set ts=2 sw=2 et :