Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / abs.rb
1 #
2 # abs.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:abs, :type => :rvalue, :doc => <<-DOC
6     @summary
7       **Deprecated:** Returns the absolute value of a number
8
9     For example -34.56 becomes 34.56.
10     Takes a single integer or float value as an argument.
11
12     > *Note:*
13       **Deprected** from Puppet 6.0.0, the built-in
14       ['abs'](https://puppet.com/docs/puppet/6.4/function.html#abs)function will be used instead.
15
16     @return The absolute value of the given number if it was an Integer
17
18     DOC
19              ) do |arguments|
20
21     raise(Puppet::ParseError, "abs(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
22
23     value = arguments[0]
24
25     # Numbers in Puppet are often string-encoded which is troublesome ...
26     if value.is_a?(String)
27       if value =~ %r{^-?(?:\d+)(?:\.\d+){1}$}
28         value = value.to_f
29       elsif value =~ %r{^-?\d+$}
30         value = value.to_i
31       else
32         raise(Puppet::ParseError, 'abs(): Requires float or integer to work with')
33       end
34     end
35
36     # We have numeric value to handle ...
37     result = value.abs
38
39     return result
40   end
41 end
42
43 # vim: set ts=2 sw=2 et :