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