5 module Puppet::Parser::Functions
6 newfunction(:shuffle, :type => :rvalue, :doc => <<-EOS
7 Randomizes the order of a string or array elements.
11 raise(Puppet::ParseError, "shuffle(): Wrong number of arguments " +
12 "given (#{arguments.size} for 1)") if arguments.size < 1
17 unless [Array, String].include?(klass)
18 raise(Puppet::ParseError, 'shuffle(): Requires either ' +
19 'array or string to work with')
24 string = value.is_a?(String) ? true : false
26 # Check whether it makes sense to shuffle ...
27 return result if result.size <= 1
29 # We turn any string value into an array to be able to shuffle ...
30 result = string ? result.split('') : result
32 elements = result.size
34 # Simple implementation of Fisher–Yates in-place shuffle ...
36 j = rand(elements - i) + i
37 result[j], result[i] = result[i], result[j]
40 result = string ? result.join : result
46 # vim: set ts=2 sw=2 et :