]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/unique.rb
upgrade to stdlib 4.6.1
[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     raise(Puppet::ParseError, "unique(): Wrong number of arguments " +
28       "given (#{arguments.size} for 1)") if arguments.size < 1
29
30     value = arguments[0]
31
32     unless value.is_a?(Array) || value.is_a?(String)
33       raise(Puppet::ParseError, 'unique(): Requires either ' +
34         'array or string to work with')
35     end
36
37     result = value.clone
38
39     string = value.is_a?(String) ? true : false
40
41     # We turn any string value into an array to be able to shuffle ...
42     result = string ? result.split('') : result
43     result = result.uniq # Remove duplicates ...
44     result = string ? result.join : result
45
46     return result
47   end
48 end
49
50 # vim: set ts=2 sw=2 et :