Update puppetlabs/stdlib module
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / sort.rb
1 #
2 #  sort.rb
3 #  Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
4 #
5 module Puppet::Parser::Functions
6   newfunction(:sort, :type => :rvalue, :doc => <<-DOC
7     Sorts strings and arrays lexically.
8
9     Note that from Puppet 6.0.0 the same function in Puppet will be used instead of this.
10   DOC
11              ) do |arguments|
12
13     if arguments.size != 1
14       raise(Puppet::ParseError, "sort(): Wrong number of arguments given #{arguments.size} for 1")
15     end
16
17     value = arguments[0]
18
19     if value.is_a?(Array)
20       value.sort
21     elsif value.is_a?(String)
22       value.split('').sort.join('')
23     end
24   end
25 end
26
27 # vim: set ts=2 sw=2 et :