6be5962b26f823e289b369c5fb55564bd8211222
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / flatten.rb
1 #
2 # flatten.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:flatten, :type => :rvalue, :doc => <<-DOC
6     This function flattens any deeply nested arrays and returns a single flat array
7     as a result.
8
9     *Examples:*
10
11         flatten(['a', ['b', ['c']]])
12
13     Would return: ['a','b','c']
14
15     Note: from Puppet 5.5.0, the compatible function with the same name in Puppet core
16     will be used instead of this function.
17   DOC
18              ) do |arguments|
19
20     raise(Puppet::ParseError, "flatten(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.size != 1
21
22     array = arguments[0]
23
24     unless array.is_a?(Array)
25       raise(Puppet::ParseError, 'flatten(): Requires array to work with')
26     end
27
28     result = array.flatten
29
30     return result
31   end
32 end
33
34 # vim: set ts=2 sw=2 et :