16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/chupakabra_tools/strings.rb', line 16
def self.trim(str, chars = nil, options = {})
options = {force_trim_whitespaces: false}.merge!(options || {})
return str if str.nil?
return nil unless str.is_a?(String)
return "" if str.blank?
return str.strip if chars.nil? || !chars.is_a?(String) || chars.blank? || chars.length > 1
result_str = str
result_str.strip! if options[:force_trim_whitespaces]
result_str = result_str.slice(1..result_str.length-1) while result_str.length > 0 && result_str.chr == chars
result_str.reverse!
result_str = result_str.slice!((1..result_str.length-1)) while result_str.length > 0 && result_str.chr == chars
result_str.reverse!
end
|