Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / chop.rb
1 #
2 #  chop.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:chop, :type => :rvalue, :doc => <<-DOC
6     @summary
7       **Deprecated** Returns a new string with the last character removed.
8
9     If the string ends with `\r\n`, both characters are removed. Applying
10     chop to an empty string returns an empty string. If you wish to merely
11     remove record separators then you should use the `chomp` function.
12     Requires a string or array of strings as input.
13
14     > *Note:* **Deprecated** from Puppet 6.0.0, this function has been replaced with a
15     built-in [`chop`](https://puppet.com/docs/puppet/latest/function.html#chop) function.
16
17     @return [String] The given String, sans the last character.
18     DOC
19              ) do |arguments|
20
21     raise(Puppet::ParseError, "chop(): Wrong number of arguments given (#{arguments.size} for 1)") if arguments.empty?
22
23     value = arguments[0]
24
25     unless value.is_a?(Array) || value.is_a?(String)
26       raise(Puppet::ParseError, 'chop(): Requires either an array or string to work with')
27     end
28
29     result = if value.is_a?(Array)
30                # Numbers in Puppet are often string-encoded which is troublesome ...
31                value.map { |i| i.is_a?(String) ? i.chop : i }
32              else
33                value.chop
34              end
35
36     return result
37   end
38 end
39
40 # vim: set ts=2 sw=2 et :