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 / unique.rb
1 #
2 # unique.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:unique, :type => :rvalue, :doc => <<-DOC
6     This function will remove duplicates from strings and arrays.
7
8     *Examples:*
9
10         unique("aabbcc")
11
12     Will return:
13
14         abc
15
16     You can also use this with arrays:
17
18         unique(["a","a","b","b","c","c"])
19
20     This returns:
21
22         ["a","b","c"]
23     DOC
24              ) do |arguments|
25
26     if Puppet::Util::Package.versioncmp(Puppet.version, '5.0.0') >= 0
27       function_deprecation([:unique, 'This method is deprecated, please use the core puppet unique function. There is further documentation for the function in the release notes of Puppet 5.0.'])
28     end
29
30     raise(Puppet::ParseError, "unique(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
31
32     value = arguments[0]
33
34     unless value.is_a?(Array) || value.is_a?(String)
35       raise(Puppet::ParseError, 'unique(): Requires either array or string to work with')
36     end
37
38     result = value.clone
39
40     string = value.is_a?(String) ? true : false
41
42     # We turn any string value into an array to be able to shuffle ...
43     result = string ? result.split('') : result
44     result = result.uniq # Remove duplicates ...
45     result = string ? result.join : result
46
47     return result
48   end
49 end
50
51 # vim: set ts=2 sw=2 et :