Class: ReeNumber::NumberToDelimited

Inherits:
Object
  • Object
show all
Includes:
Ree::FnDSL
Defined in:
lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb

Constant Summary collapse

DEFAULTS =
{
  separator: ".",
  delimiter: ",",
  pattern: /(\d)(?=(\d\d\d)+(?!\d))/
}.freeze

Instance Method Summary collapse

Instance Method Details

#call(number, **opts) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/ree_lib/packages/ree_number/package/ree_number/functions/number_to_delimited.rb', line 35

def call(number, **opts)
  options = DEFAULTS.merge(opts)
  left, right = number.to_s.split(".")
  
  delimited_left = left.gsub(options[:pattern] || options[:pattern]) do |digit_to_delimit|
    "#{digit_to_delimit}#{options[:delimiter]}"
  end

  [delimited_left, right].compact.join(options[:separator])
end